Skip to content
Permalink
Browse files

Merge pull request #13098 from tophmatthews/ad_action_13097

AD option for TM action
  • Loading branch information...
permcody committed Apr 2, 2019
2 parents 4e13dc3 + 15a1d94 commit 5b9aba41e13601bd8ff042096abceec941f30ebb
@@ -31,6 +31,9 @@ class TensorMechanicsActionBase : public Action
static const std::map<std::string, std::pair<std::string, std::vector<std::string>>>
_ranktwoscalaraux_table;
///@}

protected:
const bool _use_ad;
};

#endif // TENSORMECHANICSACTIONBASE_H
@@ -44,6 +44,8 @@ validParams<DynamicTensorMechanicsAction>()
DynamicTensorMechanicsAction::DynamicTensorMechanicsAction(const InputParameters & params)
: TensorMechanicsAction(params)
{
if (_use_ad)
paramError("use_ad", "AD not setup for use with DynamicTensorMechanicsAction");
}

std::string
@@ -32,6 +32,8 @@ validParams<PoroMechanicsAction>()
PoroMechanicsAction::PoroMechanicsAction(const InputParameters & params)
: TensorMechanicsAction(params)
{
if (_use_ad)
paramError("use_ad", "AD not setup for use with PoroMechanicsAction");
}

void
@@ -149,6 +149,14 @@ TensorMechanicsAction::TensorMechanicsAction(const InputParameters & params)
void
TensorMechanicsAction::act()
{
std::string ad_prepend = "";
std::string ad_append = "";
if (_use_ad)
{
ad_prepend = "AD";
ad_append = "<RESIDUAL>";
}

//
// Consistency check for the coordinate system
//
@@ -171,6 +179,8 @@ TensorMechanicsAction::act()
{
if (_planar_formulation == PlanarFormulation::GeneralizedPlaneStrain)
{
if (_use_ad)
paramError("use_ad", "AD not setup for use with PlaneStrain");
// Set the action parameters
const std::string type = "GeneralizedPlaneStrainAction";
auto action_params = _action_factory.getValidParams(type);
@@ -245,6 +255,9 @@ TensorMechanicsAction::act()
else if (_planar_formulation == PlanarFormulation::PlaneStrain ||
_planar_formulation == PlanarFormulation::GeneralizedPlaneStrain)
{
if (_use_ad)
paramError("use_ad", "AD not setup for use with PlaneStrain");

std::map<StrainAndIncrement, std::string> type_map = {
{StrainAndIncrement::SmallTotal, "ComputePlaneSmallStrain"},
{StrainAndIncrement::SmallIncremental, "ComputePlaneIncrementalStrain"},
@@ -261,7 +274,7 @@ TensorMechanicsAction::act()
mooseError("Unsupported planar formulation");

// set material parameters
auto params = _factory.getValidParams(type);
auto params = _factory.getValidParams(ad_prepend + type + ad_append);
params.applyParameters(parameters(),
{"displacements", "use_displaced_mesh", "scalar_out_of_plane_strain"});

@@ -275,7 +288,16 @@ TensorMechanicsAction::act()
params.set<std::vector<VariableName>>("scalar_out_of_plane_strain") = {
getParam<VariableName>("scalar_out_of_plane_strain")};

_problem->addMaterial(type, name() + "_strain", params);
if (_use_ad)
{
_problem->addADResidualMaterial(
ad_prepend + type + "<RESIDUAL>", name() + "_strain" + "_residual", params);
_problem->addADJacobianMaterial(
ad_prepend + type + "<JACOBIAN>", name() + "_strain" + "_jacobian", params);
_problem->haveADObjects(true);
}
else
_problem->addMaterial(type, name() + "_strain", params);
}

//
@@ -284,7 +306,7 @@ TensorMechanicsAction::act()
else if (_current_task == "add_kernel")
{
auto tensor_kernel_type = getKernelType();
auto params = getKernelParameters(tensor_kernel_type);
auto params = getKernelParameters(ad_prepend + tensor_kernel_type + ad_append);

for (unsigned int i = 0; i < _ndisp; ++i)
{
@@ -306,7 +328,16 @@ TensorMechanicsAction::act()
if (_diag_save_in.size() == _ndisp)
params.set<std::vector<AuxVariableName>>("diag_save_in") = {_diag_save_in[i]};

_problem->addKernel(tensor_kernel_type, kernel_name, params);
if (_use_ad)
{
_problem->addKernel(
ad_prepend + tensor_kernel_type + "<RESIDUAL>", kernel_name + "_residual", params);
_problem->addKernel(
ad_prepend + tensor_kernel_type + "<JACOBIAN>", kernel_name + "_jacobian", params);
_problem->haveADObjects(true);
}
else
_problem->addKernel(tensor_kernel_type, kernel_name, params);
}
}
}
@@ -57,6 +57,9 @@ validParams<TensorMechanicsActionBase>()
params.addParam<bool>("add_variables", false, "Add the displacement variables");
params.addParam<std::vector<MaterialPropertyName>>(
"eigenstrain_names", "List of eigenstrains to be applied in this strain calculation");
params.addParam<bool>("use_automatic_differentiation",
false,
"Flag to use automatic differentiation (AD) objects when possible");
// Global Strain
params.addParam<MaterialPropertyName>(
"global_strain",
@@ -104,7 +107,7 @@ validParams<TensorMechanicsActionBase>()
}

TensorMechanicsActionBase::TensorMechanicsActionBase(const InputParameters & parameters)
: Action(parameters)
: Action(parameters), _use_ad(getParam<bool>("use_automatic_differentiation"))
{
// check if a container block with common parameters is found
auto action = _awh.getActions<CommonTensorMechanicsAction>();
@@ -0,0 +1,99 @@
[Tests]
issues = '#7555'
[./two_block_new]
type = Exodiff
input = 'two_block_new.i'
exodiff = 'two_block_new_out.e'
design = 'syntax/Modules/TensorMechanics/Master/index.md'
requirement = 'The TensorMechanics MasterAction shall create a consistent strain calculator material and stress divergence kernel and shall generate different sets of outputs for different mesh subblocks.'
[../]

[./two_block]
type = Exodiff
input = 'two_block.i'
exodiff = 'two_block_out.e'
cli_args = 'Modules/TensorMechanics/Master/block1/block=1'
design = 'syntax/Modules/TensorMechanics/Master/index.md'
requirement = 'The TensorMechanics MasterAction shall create different sets of consistent strain calculator material and stress divergence kernel pairs for different mesh subblocks requesting different strain formulations.'
[../]

[./error_unrestricted]
type = RunException
input = 'two_block.i'
expect_err = 'No TensorMechanics/Master action can be block unrestricted if more than one TensorMechanics/Master action is specified.'
prereq = 'two_block'
design = 'syntax/Modules/TensorMechanics/Master/index.md'
requirement = 'The TensorMechanics MasterAction shall error if an input file does not specify block restrictions for the MasterAction in input files with more than one instance of the MasterAction block.'
[../]

[./error_overlapping]
type = RunException
input = 'two_block.i'
cli_args = 'Modules/TensorMechanics/Master/block1/block=2'
expect_err = 'The block restrictions in the TensorMechanics/Master actions must be non-overlapping.'
prereq = 'error_unrestricted'
design = 'syntax/Modules/TensorMechanics/Master/index.md'
requirement = 'The TensorMechanics MasterAction shall error if an input file specifies overlapping block restrictions for the MasterAction in input files with more than one instance of the MasterAction block.'
[../]

[./two_coord]
type = Exodiff
input = 'two_coord.i'
exodiff = 'two_coord_out.e'
rel_err = 1e-5
design = 'syntax/Modules/TensorMechanics/Master/index.md'
requirement = 'The TensorMechanics MasterAction shall create different sets of consistent strain calculator material and stress divergence kernel pairs for different mesh subblocks using different coordinate systems.'
[../]

[./error_coord]
type = RunException
input = 'two_coord.i'
cli_args = 'Modules/TensorMechanics/Master/active=error'
expect_err = 'The TensorMechanics action requires all subdomains to have the same coordinate system.'
prereq = 'two_coord'
design = 'syntax/Modules/TensorMechanics/Master/index.md'
requirement = 'The TensorMechanics MasterAction shall error if an input file assigns the same TensorMechanics MasterAction block to mesh blocks with different coordinate systems.'
[../]

[./two_block-jac]
type = 'PetscJacobianTester'
input = 'two_block.i'
run_sim = 'True'
ratio_tol = 1e-7
difference_tol = 1e4
cli_args = 'Modules/TensorMechanics/Master/block1/block=1'
petsc_version = '>=3.9.0'
requirement = 'The Jacobian for the automatic differentiation in the two_block testproblem shall be perfect'
design = 'jacobian_definitions.md'
[../]
[./two_block_no_action-jac]
type = 'PetscJacobianTester'
input = 'two_block_no_action.i'
run_sim = 'True'
ratio_tol = 1e-7
difference_tol = 1e4
petsc_version = '>=3.9.0'
requirement = 'The Jacobian for the automatic differentiation in the two_block testproblem shall be perfect (non action test case)'
design = 'jacobian_definitions.md'
[../]
[./two_block_new-jac]
type = 'PetscJacobianTester'
input = 'two_block_new.i'
run_sim = 'True'
ratio_tol = 1e-7
difference_tol = 1e4
petsc_version = '>=3.9.0'
requirement = 'The Jacobian for the automatic differentiation in the two_block_new problem shall be perfect'
design = 'jacobian_definitions.md'
[../]
[./two_coord-jac]
type = 'PetscJacobianTester'
input = 'two_coord.i'
run_sim = 'True'
ratio_tol = 1e-7
difference_tol = 1e4
petsc_version = '>=3.9.0'
requirement = 'The Jacobian for the automatic differentiation two_coord problem shall be perfect'
design = 'jacobian_definitions.md'
[../]
[]
@@ -0,0 +1,144 @@
[Mesh]
type = GeneratedMesh
dim = 2
nx = 4
ny = 4
[]

[MeshModifiers]
[./block1]
type = SubdomainBoundingBox
block_id = 1
bottom_left = '0 0 0'
top_right = '0.5 1 0'
[../]
[./block2]
type = SubdomainBoundingBox
block_id = 2
bottom_left = '0.5 0 0'
top_right = '1 1 0'
[../]
[]

[GlobalParams]
displacements = 'disp_x disp_y'
[]

[Modules/TensorMechanics/Master]
[./block1]
strain = FINITE
add_variables = true
#block = 1
use_automatic_differentiation = true
[../]
[./block2]
strain = SMALL
add_variables = true
block = 2
use_automatic_differentiation = true
[../]
[]

[AuxVariables]
[./stress_theta]
order = CONSTANT
family = MONOMIAL
[../]
[./strain_theta]
order = CONSTANT
family = MONOMIAL
[../]
[]

[AuxKernels]
[./stress_theta]
type = RankTwoAux
rank_two_tensor = stress
index_i = 2
index_j = 2
variable = stress_theta
execute_on = timestep_end
[../]
[./strain_theta]
type = RankTwoAux
rank_two_tensor = total_strain
index_i = 2
index_j = 2
variable = strain_theta
execute_on = timestep_end
[../]
[]

[Materials]
[./elasticity_tensor]
type = ComputeIsotropicElasticityTensor
youngs_modulus = 1e10
poissons_ratio = 0.345
[../]
[./_elastic_stress1]
type = ADComputeFiniteStrainElasticStress
block = 1
[../]
[./_elastic_stress2]
type = ADComputeLinearElasticStress
block = 2
[../]
[]

[BCs]
[./left]
type = PresetBC
boundary = 'left'
variable = disp_x
value = 0.0
[../]
[./top]
type = PresetBC
boundary = 'top'
variable = disp_y
value = 0.0
[../]
[./right]
type = PresetBC
boundary = 'right'
variable = disp_x
value = 0.01
[../]
[./bottom]
type = PresetBC
boundary = 'bottom'
variable = disp_y
value = 0.01
[../]
[]

[Debug]
show_var_residual_norms = true
[]

[Preconditioning]
[./full]
type = SMP
full = true
[../]
[]

[Executioner]
type = Steady

petsc_options_iname = '-ksp_gmres_restart -pc_type -pc_hypre_type -pc_hypre_boomeramg_max_iter'
petsc_options_value = ' 201 hypre boomeramg 10'

line_search = 'none'

nl_rel_tol = 5e-9
nl_abs_tol = 1e-10
nl_max_its = 15

l_tol = 1e-3
l_max_its = 50
[]

[Outputs]
exodus = true
[]
Oops, something went wrong.

0 comments on commit 5b9aba4

Please sign in to comment.
You can’t perform that action at this time.