Skip to content

Commit

Permalink
Fix error of missing () in residual integration loop
Browse files Browse the repository at this point in the history
Add test cases and gold files for the 2d homogenization
  • Loading branch information
ttruster committed Jan 11, 2023
1 parent 33ae53e commit 9528cb5
Show file tree
Hide file tree
Showing 11 changed files with 497 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class HomogenizedTotalLagrangianStressDivergenceS : public TotalLagrangianStress
/// Type of each constraint (stress or strain) for each component
HomogenizationS::ConstraintMap _cmap;

/// The constraint type
/// The constraint type; initialize with 'none'
HomogenizationS::ConstraintType _ctype = HomogenizationS::ConstraintType::None;

/// Used internally to iterate over each scalar component
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#pragma once

#include "Material.h"

// Helpers common to the whole homogenization system
namespace HomogenizationM
{
/// Moose constraint type, for input
const MultiMooseEnum constraintType("strain stress none");
/// Constraint type: stress/PK stress or strain/deformation gradient
enum class ConstraintType
{
Strain,
Stress,
None
};
typedef std::map<std::pair<unsigned int, unsigned int>, std::pair<ConstraintType, const Function *>>
ConstraintMap;
}

/// Calculate the tensor corresponding to homogenization gradient
///
/// This class takes a scalar field of the right size representing a
/// constant deformation gradient over the domain and casts it into
/// a RankTwo material property
///
class ComputeHomogenizedLagrangianStrainS : public Material
{
public:
static InputParameters validParams();
ComputeHomogenizedLagrangianStrainS(const InputParameters & parameters);

protected:
virtual void computeQpProperties() override;

protected:
/// The base name for material properties
const std::string _base_name;

/// Constraint map
HomogenizationM::ConstraintMap _cmap;

/// ScalarVariable with the field
const VariableValue & _macro_gradient;

/// Unwrapped into a tensor
MaterialProperty<RankTwoTensor> & _homogenization_contribution;
};
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,20 @@ HomogenizedTotalLagrangianStressDivergenceS::computeScalarResidual()
if (_large_kinematics)
{
if (ctype == HomogenizationS::ConstraintType::Stress)
scalar_residuals[_h++] += dV * _pk1[_qp](i, j) - ctarget->value(_t, _q_point[_qp]);
scalar_residuals[_h++] += dV * (_pk1[_qp](i, j) - ctarget->value(_t, _q_point[_qp]));
else if (ctype == HomogenizationS::ConstraintType::Strain)
scalar_residuals[_h++] +=
dV * _F[_qp](i, j) - (Real(i == j) + ctarget->value(_t, _q_point[_qp]));
dV * (_F[_qp](i, j) - (Real(i == j) + ctarget->value(_t, _q_point[_qp])));
else
mooseError("Unknown constraint type in the integral!");
}
else
{
if (ctype == HomogenizationS::ConstraintType::Stress)
scalar_residuals[_h++] += dV * _pk1[_qp](i, j) - ctarget->value(_t, _q_point[_qp]);
scalar_residuals[_h++] += dV * (_pk1[_qp](i, j) - ctarget->value(_t, _q_point[_qp]));
else if (ctype == HomogenizationS::ConstraintType::Strain)
scalar_residuals[_h++] += dV * 0.5 * (_F[_qp](i, j) + _F[_qp](j, i)) -
(Real(i == j) + ctarget->value(_t, _q_point[_qp]));
scalar_residuals[_h++] += dV * (0.5 * (_F[_qp](i, j) + _F[_qp](j, i)) -
(Real(i == j) + ctarget->value(_t, _q_point[_qp])));
else
mooseError("Unknown constraint type in the integral!");
}
Expand Down Expand Up @@ -148,14 +148,14 @@ HomogenizedTotalLagrangianStressDivergenceS::computeScalarJacobian()
{
auto && [a, b] = indices2.first;
if (ctype == HomogenizationS::ConstraintType::Stress)
_local_ke(_h, _l++) += dV * _dpk1[_qp](i, j, a, b);
_local_ke(_h, _l++) += dV * (_dpk1[_qp](i, j, a, b));
else if (ctype == HomogenizationS::ConstraintType::Strain)
{
if (_large_kinematics)
_local_ke(_h, _l++) += dV * Real(i == a && j == b);
_local_ke(_h, _l++) += dV * (Real(i == a && j == b));
else
_local_ke(_h, _l++) +=
dV * 0.5 * Real(i == a && j == b) + 0.5 * Real(i == b && j == a);
dV * (0.5 * Real(i == a && j == b) + 0.5 * Real(i == b && j == a));
}
else
mooseError("Unknown constraint type in Jacobian calculator!");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "ComputeHomogenizedLagrangianStrainS.h"

registerMooseObject("TensorMechanicsApp", ComputeHomogenizedLagrangianStrainS);

InputParameters
ComputeHomogenizedLagrangianStrainS::validParams()
{
InputParameters params = Material::validParams();
params.addParam<std::string>("base_name", "Material property base name");
params.addParam<MaterialPropertyName>("homogenization_gradient_name",
"homogenization_gradient",
"Name of the constant gradient field");
params.addRequiredParam<MultiMooseEnum>(
"constraint_types",
HomogenizationM::constraintType,
"Type of each constraint: strain, stress, or none. The types are specified in the "
"column-major order, and there must be 9 entries in total.");
params.addRequiredParam<std::vector<FunctionName>>(
"targets", "Functions giving the targets to hit for constraint types that are not none.");
params.addRequiredCoupledVar("macro_gradient",
"Scalar field defining the "
"macro gradient");
return params;
}

ComputeHomogenizedLagrangianStrainS::ComputeHomogenizedLagrangianStrainS(
const InputParameters & parameters)
: Material(parameters),
_base_name(isParamValid("base_name") ? getParam<std::string>("base_name") + "_" : ""),
_macro_gradient(coupledScalarValue("macro_gradient")),
_homogenization_contribution(
declareProperty<RankTwoTensor>(_base_name + "homogenization_gradient_name"))
{
// Constraint types
auto types = getParam<MultiMooseEnum>("constraint_types");
if (types.size() != Moose::dim * Moose::dim)
mooseError("Number of constraint types must equal dim * dim. ", types.size(), " are provided.");

// Targets to hit
const std::vector<FunctionName> & fnames = getParam<std::vector<FunctionName>>("targets");

// Prepare the constraint map
unsigned int fcount = 0;
for (const auto j : make_range(Moose::dim))
for (const auto i : make_range(Moose::dim))
{
const auto idx = i + Moose::dim * j;
const auto ctype = static_cast<HomogenizationM::ConstraintType>(types.get(idx));
if (ctype != HomogenizationM::ConstraintType::None)
{
const Function * const f = &getFunctionByName(fnames[fcount++]);
_cmap[{i, j}] = {ctype, f};
}
}
}

void
ComputeHomogenizedLagrangianStrainS::computeQpProperties()
{
_homogenization_contribution[_qp].zero();
unsigned int count = 0;
for (auto && indices : _cmap)
{
auto && [i, j] = indices.first;
_homogenization_contribution[_qp](i, j) = _macro_gradient[count++];
}
}
Binary file not shown.
Loading

0 comments on commit 9528cb5

Please sign in to comment.