Skip to content

Commit

Permalink
💻🔧 Return nodal property with Eigen::Map and change the type of its m…
Browse files Browse the repository at this point in the history
…ethods from boolean to void
  • Loading branch information
thiagordonho committed Mar 3, 2020
1 parent 6f2f14a commit c2e1c70
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
9 changes: 7 additions & 2 deletions include/nodal_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
#include <map>

namespace mpm {

// Define Eigen map type
typedef Eigen::Matrix<double, Eigen::Dynamic, 1> MatrixProperty;
typedef Eigen::Map<MatrixProperty> MapProperty;

// \brief Multimaterial parameters on each node
struct NodalProperties {

Expand All @@ -13,7 +18,7 @@ struct NodalProperties {
//! \param[in] rows Number of nodes times the number of the dimension of the
//! property (1 if scalar, Tdim if vector)
//! \param[in] columns Number of materials
bool create_property(const std::string& property, unsigned rows,
void create_property(const std::string& property, unsigned rows,
unsigned columns);

// Return data in the nodal properties map at a specific index
Expand All @@ -30,7 +35,7 @@ struct NodalProperties {
// \param[in] mat_id Id of the material within the property data
// \param[in] nprops Dimension of property (1 if scalar, Tdim if vector)
// \param[in] property Property name
bool assign_property(const std::string& property, unsigned node_id,
void assign_property(const std::string& property, unsigned node_id,
unsigned mat_id, Eigen::MatrixXd property_value,
unsigned nprops = 1);

Expand Down
14 changes: 7 additions & 7 deletions src/nodal_properties.cc
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#include "nodal_properties.h"

// Function to create new property with given name and size (rows x cols)
bool mpm::NodalProperties::create_property(const std::string& property,
void mpm::NodalProperties::create_property(const std::string& property,
unsigned rows, unsigned columns) {
// Initialize a matrix with size of rows times columns and insert it to the
// property database map
Eigen::MatrixXd property_data = Eigen::MatrixXd::Zero(rows, columns);
properties_.insert(
std::pair<std::string, Eigen::MatrixXd>(property, property_data));
return true;
}

// Return data in the nodal properties map at a specific index
Eigen::MatrixXd mpm::NodalProperties::property(const std::string& property,
unsigned node_id,
unsigned mat_id,
unsigned nprops = 1) const {
return properties_.at(property).block(node_id * nprops, mat_id, nprops, 1);
unsigned nprops) const {
double* position = &properties_.at(property)(node_id * nprops, mat_id);
mpm::MapProperty property_map(position, nprops);
return property_map;
}

// Assign property value to a pair of node and material
bool mpm::NodalProperties::assign_property(const std::string& property,
void mpm::NodalProperties::assign_property(const std::string& property,
unsigned node_id, unsigned mat_id,
Eigen::MatrixXd property_value,
unsigned nprops = 1) {
unsigned nprops) {
properties_.at(property).block(node_id * nprops, mat_id, nprops, 1) =
property_value;
return true;
}
18 changes: 10 additions & 8 deletions tests/nodal_properties_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ TEST_CASE("NodalProperties is checked", "[nodal_properties]") {
const unsigned dim = 1;

// Check property creation
REQUIRE(nodal_properties.create_property(property1, nnodes, nmaterials));
REQUIRE(nodal_properties.create_property(property2, nnodes, nmaterials));
REQUIRE_NOTHROW(
nodal_properties.create_property(property1, nnodes, nmaterials));
REQUIRE_NOTHROW(
nodal_properties.create_property(property2, nnodes, nmaterials));

// Check size of matrix of property data
REQUIRE(nodal_properties.properties_.at(property1).rows() == 3);
Expand Down Expand Up @@ -64,9 +66,9 @@ TEST_CASE("NodalProperties is checked", "[nodal_properties]") {
// Update values in the property data matrix and check update
for (int i = 0; i < nnodes; ++i) {
for (int j = 0; j < nmaterials; ++j) {
REQUIRE(nodal_properties.assign_property(
REQUIRE_NOTHROW(nodal_properties.assign_property(
property1, i, j, data1.block<dim, 1>(i * dim, j), dim));
REQUIRE(nodal_properties.assign_property(
REQUIRE_NOTHROW(nodal_properties.assign_property(
property2, i, j, data2.block<dim, 1>(i * dim, j), dim));
}
}
Expand Down Expand Up @@ -97,9 +99,9 @@ TEST_CASE("NodalProperties is checked", "[nodal_properties]") {
const unsigned dim = 2;

// Check property creation
REQUIRE(
REQUIRE_NOTHROW(
nodal_properties.create_property(property1, nnodes * dim, nmaterials));
REQUIRE(
REQUIRE_NOTHROW(
nodal_properties.create_property(property2, nnodes * dim, nmaterials));

// Check size of matrix of property data
Expand Down Expand Up @@ -142,9 +144,9 @@ TEST_CASE("NodalProperties is checked", "[nodal_properties]") {
// Update values in the property data matrix and check update
for (int i = 0; i < nnodes; ++i) {
for (int j = 0; j < nmaterials; ++j) {
REQUIRE(nodal_properties.assign_property(
REQUIRE_NOTHROW(nodal_properties.assign_property(
property1, i, j, data1.block<dim, 1>(i * dim, j), dim));
REQUIRE(nodal_properties.assign_property(
REQUIRE_NOTHROW(nodal_properties.assign_property(
property2, i, j, data2.block<dim, 1>(i * dim, j), dim));
}
}
Expand Down

0 comments on commit c2e1c70

Please sign in to comment.