Skip to content

Commit

Permalink
Merge pull request #11476 from KratosMultiphysics/geo/umat_parameters…
Browse files Browse the repository at this point in the history
…_python

Geo/umat parameters python
  • Loading branch information
EleniSmyrniou authored Oct 12, 2023
2 parents b4da381 + d43753d commit 291c05f
Show file tree
Hide file tree
Showing 19 changed files with 1,338 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ void UPwSmallStrainElement<TDim,TNumNodes>::
//Defining necessary variables
const GeometryType& rGeom = this->GetGeometry();
const IndexType NumGPoints = rGeom.IntegrationPointsNumber( mThisIntegrationMethod );
const PropertiesType& rProp = this->GetProperties();

if ( rOutput.size() != NumGPoints )
rOutput.resize(NumGPoints);
Expand All @@ -736,7 +737,6 @@ void UPwSmallStrainElement<TDim,TNumNodes>::
}
} else if (rVariable == TOTAL_STRESS_VECTOR) {
//Defining necessary variables
const PropertiesType& rProp = this->GetProperties();

ConstitutiveLaw::Parameters ConstitutiveParameters(rGeom,rProp,rCurrentProcessInfo);
ConstitutiveParameters.Set(ConstitutiveLaw::COMPUTE_CONSTITUTIVE_TENSOR);
Expand Down Expand Up @@ -836,7 +836,15 @@ void UPwSmallStrainElement<TDim,TNumNodes>::

rOutput[GPoint] = Variables.StrainVector;
}
} else {
}
else if (rProp.Has(rVariable))
{
// map initial material property to gauss points, as required for the output
rOutput.clear();
std::fill_n(std::back_inserter(rOutput), mConstitutiveLawVector.size(), rProp.GetValue(rVariable));

}
else {
for ( unsigned int i = 0; i < mConstitutiveLawVector.size(); ++i )
rOutput[i] = mConstitutiveLawVector[i]->GetValue( rVariable , rOutput[i] );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,132 +28,132 @@ namespace Kratos
{

SetParameterFieldProcess::SetParameterFieldProcess(ModelPart& rModelPart,
const Parameters& rSettings)
: Process(), mrModelPart(rModelPart),
mParameters(rSettings)
{
// function type: python, cpp, input
const Parameters default_parameters(R"(
const Parameters& rSettings)
: Process(),
mrModelPart(rModelPart),
mParameters(rSettings)
{
// function type: python, cpp, input
const Parameters default_parameters(R"(
{
"help" : "This process sets a parameter field on a model part, where each element can have different material properties.",
"model_part_name" : "please_specify_model_part_name",
"variable_name" : "CUSTOM",
"func_type" : "input",
"function" : "0",
"dataset" : "dummy",
"dataset_file_name" : "dummy"
"dataset_file_name" : "dummy",
"vector_variable_indices" : []
} )"
);

mParameters.RecursivelyValidateAndAssignDefaults(default_parameters);

}


void SetParameterFieldProcess::SetValueAtElement(Element& rElement, const Variable<double>& rVar, const double Value)
{

Properties& r_prop = rElement.GetProperties();

// Copies properties
Properties::Pointer p_new_prop = Kratos::make_shared<Properties>(r_prop);

// Adds new properties to the element
p_new_prop->SetValue(rVar, Value);
rElement.SetProperties(p_new_prop);

}


void SetParameterFieldProcess::SetParameterFieldUsingInputFunction(const Variable<double>& rVar)
{
auto parameter_function = BasicGenericFunctionUtility(mParameters["function"].GetString());
const double current_time = this->mrModelPart.GetProcessInfo().GetValue(TIME);

for (Element& r_element : mrModelPart.Elements()) {

const auto& r_geom = r_element.GetGeometry();

// calculate parameter value at current element
const double val = parameter_function.CallFunction(r_geom.Center().X(), r_geom.Center().Y(), r_geom.Center().Z(), current_time, 0, 0, 0);

SetValueAtElement(r_element, rVar, val);
);

mParameters.RecursivelyValidateAndAssignDefaults(default_parameters);
}
}

void SetParameterFieldProcess::SetParameterFieldUsingInputFunction(const Variable<double>& rVar)
{
auto parameter_function = BasicGenericFunctionUtility(mParameters["function"].GetString());
const double current_time = this->mrModelPart.GetProcessInfo().GetValue(TIME);

void SetParameterFieldProcess::SetParameterFieldUsingParametersClass(const Variable<double>& rVar, Parameters& rParameters)
{
for (Element& r_element : mrModelPart.Elements()) {
const auto& r_geom = r_element.GetGeometry();

const Vector& r_data_vector = rParameters["values"].GetVector();
// calculate parameter value at current element
const double val = parameter_function.CallFunction(r_geom.Center().X(), r_geom.Center().Y(), r_geom.Center().Z(), current_time, 0, 0, 0);

KRATOS_ERROR_IF_NOT(r_data_vector.size() == mrModelPart.Elements().size()) << "The parameter field "
"does not have the same size as the amount of elements within the model part!" << std::endl;
SetValueAtElement(r_element, rVar, val);
}
}

// set new data on the elements
IndexType i = 0;
for (Element& r_element : mrModelPart.Elements())
void SetParameterFieldProcess::SetParameterFieldUsingInputFunction(const Variable<Vector>& rVar)
{
SetValueAtElement(r_element, rVar, r_data_vector[i]);
++i;
auto parameter_function = BasicGenericFunctionUtility(mParameters["function"].GetString());
const double current_time = this->mrModelPart.GetProcessInfo().GetValue(TIME);
const auto indices = GetVectorIndices();

for (Element& r_element : mrModelPart.Elements()) {
const auto& r_geom = r_element.GetGeometry();

// calculate parameter value at current element
const double val = parameter_function.CallFunction(r_geom.Center().X(), r_geom.Center().Y(), r_geom.Center().Z(), current_time, 0, 0, 0);
// get properties per element
Properties& r_prop = r_element.GetProperties();
Vector& vector = r_prop.GetValue(rVar);

// loop through the indexes that need to be set
for (auto index : indices) {
vector[index] = val;
}

SetValueAtElement(r_element, rVar, vector);
}
}
}

void SetParameterFieldProcess::SetParameterFieldUsingJsonString(const Variable<double>& rVar)
{
// get new data from the data set
const std::string& r_dataset = mParameters["dataset"].GetString();


Parameters new_data{ r_dataset };
this->SetParameterFieldUsingParametersClass(rVar, new_data);

}
void SetParameterFieldProcess::SetParameterFieldUsingJsonFile(const Variable<double>& rVar)
{
// Read json string in field parameters file, create Parameters
const std::string& field_file_name = mParameters["dataset_file_name"].GetString();
KRATOS_ERROR_IF_NOT(std::filesystem::exists(field_file_name)) << "The parameter field file specified with name \"" << field_file_name << "\" does not exist!" << std::endl;
void SetParameterFieldProcess::SetParameterFieldUsingParametersClass(const Variable<double>& rVar,
const Parameters& rParameters)
{
const Vector& r_data_vector = rParameters["values"].GetVector();

std::ifstream ifs(field_file_name);
Parameters new_data{ ifs };
KRATOS_ERROR_IF_NOT(r_data_vector.size() == mrModelPart.Elements().size()) << "The parameter field "
"does not have the same size as the amount of elements within the model part!" << std::endl;

this->SetParameterFieldUsingParametersClass(rVar, new_data);
// set new data on the elements
IndexType i = 0;
for (Element& r_element : mrModelPart.Elements()) {
SetValueAtElement(r_element, rVar, r_data_vector[i]);
++i;
}
}

}
void SetParameterFieldProcess::SetParameterFieldUsingParametersClass(const Variable<Vector>& rVar,
const Parameters& rParameters)
{
const Matrix& r_data_matrix = rParameters["values"].GetMatrix();

KRATOS_ERROR_IF_NOT(r_data_matrix.size1() == mrModelPart.Elements().size()) << "The parameter field "
"does not have the same size as the amount of elements within the model part!" << std::endl;

// set new data on the elements
const IndexType vector_size = r_data_matrix.size2();
IndexType i = 0;
for (Element& r_element : mrModelPart.Elements()) {
Vector sub_vector;
sub_vector.resize(vector_size);
for (IndexType j = 0; j < vector_size; j++) {
sub_vector[j] = r_data_matrix(i, j);
}

SetValueAtElement(r_element, rVar, sub_vector);
++i;
}
}

void SetParameterFieldProcess::ExecuteInitialize()
{
if (mrModelPart.GetProcessInfo()[IS_RESTARTED]) {
return;
}

KRATOS_TRY

const auto variable_name = mParameters["variable_name"].GetString();
if (KratosComponents<Variable<double>>::Has(variable_name)) {
SetParameterFieldForVariableType(KratosComponents<Variable<double>>::Get(variable_name));
}
else if (KratosComponents<Variable<Vector>>::Has(variable_name)) {
SetParameterFieldForVariableType(KratosComponents<Variable<Vector>>::Get(variable_name));
}

void SetParameterFieldProcess::ExecuteInitialize()
{
if (mrModelPart.GetProcessInfo()[IS_RESTARTED]) {
return;
KRATOS_CATCH("")
}

KRATOS_TRY

const auto& r_var = KratosComponents< Variable<double> >::Get(mParameters["variable_name"].GetString());

// set parameter field from input function
if (mParameters["func_type"].GetString() == "input")
{
this->SetParameterFieldUsingInputFunction(r_var);
}
// set parameter field with a json string
else if (mParameters["func_type"].GetString() == "json_string")
{
this->SetParameterFieldUsingJsonString(r_var);
}
// set parameter field from a json input file
else if (mParameters["func_type"].GetString() == "json_file")
std::vector<IndexType> SetParameterFieldProcess::GetVectorIndices() const
{
this->SetParameterFieldUsingJsonFile(r_var);
std::vector<IndexType> result;
for (auto index : mParameters["vector_variable_indices"].GetVector()) {
result.push_back(static_cast<IndexType>(index));
}
return result;
}

KRATOS_CATCH("")
}


} // namespace Kratos.
} // namespace Kratos.
Loading

0 comments on commit 291c05f

Please sign in to comment.