Skip to content

Commit

Permalink
Merge pull request #12886 from KratosMultiphysics/adding-minor-capabi…
Browse files Browse the repository at this point in the history
…lity-to-table-accessor

[CORE] Adding ProcessInfo kind of variable as independent variable for TableAccessor
  • Loading branch information
AlejandroCornejo authored Nov 30, 2024
2 parents f957ad1 + 2cf70ee commit c470932
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
2 changes: 2 additions & 0 deletions kratos/includes/table_accessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class KRATOS_API(KRATOS_CORE) TableAccessor : public Accessor
mInputVariableType = Globals::DataLocation::NodeNonHistorical;
} else if (rInputVariableType == "element") {
mInputVariableType = Globals::DataLocation::Element;
} else if (rInputVariableType == "process_info") {
mInputVariableType = Globals::DataLocation::ProcessInfo;
} else {
KRATOS_ERROR << "The table_input_variable_type is incorrect or not supported. Types available are : 'node_historical', 'node_non_historical' and 'element'" << std::endl;
}
Expand Down
7 changes: 5 additions & 2 deletions kratos/sources/table_accessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ double TableAccessor::GetValueFromTable(
} else if (mInputVariableType == Globals::DataLocation::Element) {
KRATOS_DEBUG_ERROR_IF_NOT(rGeometry.Has(rIndependentVariable)) << "The Variable " << rIndependentVariable.Name() << " is not available at the Geometry to retrieve Table values." << std::endl;
independent_at_gauss = rGeometry.GetValue(rIndependentVariable);
} else if (mInputVariableType == Globals::DataLocation::ProcessInfo) {
KRATOS_DEBUG_ERROR_IF_NOT(rProcessInfo.Has(rIndependentVariable)) << "The Variable " << rIndependentVariable.Name() << " is not available at the rProcessInfo to retrieve Table values." << std::endl;
independent_at_gauss = rProcessInfo.GetValue(rIndependentVariable);
} else {
KRATOS_ERROR << "The table_input_variable_type is incorrect or not supported. Types available are : nodal_historical, nodal_non_historical and elemental_non_historical" << std::endl;
}
Expand All @@ -80,7 +83,7 @@ double TableAccessor::GetValueFromTable(
void TableAccessor::save(Serializer& rSerializer) const
{
rSerializer.save("InputVariable", mpInputVariable->Name());
// // we must do the int cast to be able to compile
// we must do the int cast to be able to compile
rSerializer.save("InputVariableType", static_cast<int>(mInputVariableType));
}
void TableAccessor::load(Serializer& rSerializer)
Expand All @@ -89,7 +92,7 @@ void TableAccessor::load(Serializer& rSerializer)
rSerializer.load("InputVariable", variable_name);
mpInputVariable = static_cast<Variable<double> *>(KratosComponents<VariableData>::pGet(variable_name));

// // we must do the int cast to be able to compile
// we must do the int cast to be able to compile
int enum_value;
rSerializer.load("InputVariableType", enum_value);
mInputVariableType = static_cast<Globals::DataLocation>(enum_value);
Expand Down
53 changes: 53 additions & 0 deletions kratos/tests/cpp_tests/includes/test_property_accessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,59 @@ KRATOS_TEST_CASE_IN_SUITE(TableAccessorSimpleProperties, KratosCoreFastSuite)
KRATOS_EXPECT_EQ(0.34, (*p_elem_prop).GetValue(POISSON_RATIO, *p_geom, N, r_model_part.GetProcessInfo()));
}

/**
* Checks the correct work of the TableAccessor when using ProcessInfo variables (TIME)
*/
KRATOS_TEST_CASE_IN_SUITE(TableAccessorSimplePropertiesProcessInfo, KratosCoreFastSuite)
{
Model current_model;
auto &r_model_part = current_model.CreateModelPart("ModelPart",1);
r_model_part.GetProcessInfo().SetValue(DOMAIN_SIZE, 2);
r_model_part.GetProcessInfo().SetValue(TIME, 0.0);

// Set the element properties
auto p_elem_prop = r_model_part.CreateNewProperties(0);
p_elem_prop->SetValue(YOUNG_MODULUS, 2.0);

auto p_node_1 = r_model_part.CreateNewNode(1, 0.0 , 0.0 , 0.0);
auto p_node_2 = r_model_part.CreateNewNode(2, 1.0 , 0.0 , 0.0);
auto p_node_3 = r_model_part.CreateNewNode(3, 1.0 , 1.0 , 0.0);
auto p_node_4 = r_model_part.CreateNewNode(4, 0.0 , 1.0 , 0.0);

std::vector<Node::Pointer> geom(4);
geom[0] = p_node_1;
geom[1] = p_node_2;
geom[2] = p_node_3;
geom[3] = p_node_4;

auto p_geom = Kratos::make_shared<Quadrilateral2D4<Node>>(PointerVector<Node>{geom});
Vector N = ZeroVector(4);
N[0] = 0.1;
N[0] = 0.2;
N[0] = 0.3;
N[0] = 0.4;

KRATOS_EXPECT_EQ(2.0, (*p_elem_prop)[YOUNG_MODULUS]);
KRATOS_EXPECT_EQ(2.0, (*p_elem_prop).GetValue(YOUNG_MODULUS));
KRATOS_EXPECT_EQ(2.0, (*p_elem_prop).GetValue(YOUNG_MODULUS, *p_geom, N, r_model_part.GetProcessInfo()));
KRATOS_EXPECT_EQ(false, (*p_elem_prop).HasAccessor(YOUNG_MODULUS));

Table<double> Time_E_table;
Time_E_table.PushBack(0.0, 2.0);
Time_E_table.PushBack(1.0, 1.0);

p_elem_prop->SetTable(TIME, YOUNG_MODULUS, Time_E_table);
KRATOS_EXPECT_EQ(true, (*p_elem_prop).HasTable(TIME, YOUNG_MODULUS));

TableAccessor E_table_accessor = TableAccessor(TIME, "process_info");
p_elem_prop->SetAccessor(YOUNG_MODULUS, E_table_accessor.Clone());
KRATOS_EXPECT_EQ(true, (*p_elem_prop).HasAccessor(YOUNG_MODULUS));
KRATOS_EXPECT_EQ(2.0, (*p_elem_prop).GetValue(YOUNG_MODULUS, *p_geom, N, r_model_part.GetProcessInfo()));

r_model_part.GetProcessInfo().SetValue(TIME, 0.5);
KRATOS_EXPECT_EQ(1.5, (*p_elem_prop).GetValue(YOUNG_MODULUS, *p_geom, N, r_model_part.GetProcessInfo()));
}

KRATOS_TEST_CASE_IN_SUITE(TableTableAccessorSerialization, KratosCoreFastSuite)
{
StreamSerializer serializer;
Expand Down

0 comments on commit c470932

Please sign in to comment.