Skip to content

Commit

Permalink
add residual and jacobain trainers and transfer options
Browse files Browse the repository at this point in the history
update variable names

fix issue with parallel implementation

need to close jacobian for nonlinear

update clears for other containers

remove duplicate snapshot

change clone to collect and add error checking

update snapshot method

update method names

add option to variable mapping base

save before refactor

Fix snapshot container system. (idaholab#27101)
  • Loading branch information
maxnezdyur committed Mar 15, 2024
1 parent f5ce2c7 commit ec5882c
Show file tree
Hide file tree
Showing 48 changed files with 5,887 additions and 30 deletions.
2 changes: 1 addition & 1 deletion framework/contrib/wasp
Submodule wasp updated from f16be3 to 28fe0d
2 changes: 1 addition & 1 deletion framework/src/reporters/AccumulateReporter.C
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ AccumulateReporter::validParams()
"over time into a vector reporter value of the same type.");
params.addRequiredParam<std::vector<ReporterName>>("reporters", "The reporters to accumulate.");

params.set<ExecFlagEnum>("execute_on") = {EXEC_INITIAL, EXEC_TIMESTEP_END};
params.set<ExecFlagEnum>("execute_on") = {EXEC_MULTIAPP_FIXED_POINT_END};
params.suppressParameter<ExecFlagEnum>("execute_on");
return params;
}
Expand Down
2 changes: 1 addition & 1 deletion large_media
Submodule large_media updated 72 files
+ framework/meshgenerators/hex_split.png
+ framework/meshgenerators/prism_split.png
+ framework/meshgenerators/pyramid_split.png
+ framework/meshgenerators/tet_cut.png
+ gallery/twist_white.mp4
+ gallery/twist_white.webm
+ gallery/weld.mp4
+ optimization/diffusion_reaction/forward_exact.png
+ optimization/diffusion_reaction/optimize_out_forward0.png
+ optimization/diffusion_reaction/solution_compare.png
+ optimization/diffusion_reaction/solution_mesh.png
+ organization_logos/23-50052_webbanner_R1-03.jpg
+ organization_logos/23-50052_webbanner_R1-03_dark.jpg
+ solid_mechanics/disp_cross_section.png
+ solid_mechanics/flow-velocity.png
+0 −1 tensor_mechanics
+ tensor_mechanics/3D_bed_separation.ogv
+ tensor_mechanics/3D_destressed.ogv
+ tensor_mechanics/3D_shear_failure.ogv
+ tensor_mechanics/3D_shear_failure_screen_shot.png
+ tensor_mechanics/cframe_iga.png
+ tensor_mechanics/cooksetup.png
+ tensor_mechanics/crystal_plasticity/HCP_basal_plane_diagram.png
+ tensor_mechanics/crystal_plasticity/Inverse_Pole_Fig.png
+ tensor_mechanics/crystal_plasticity/crystal_plasticity_stress_update_algorithm.png
+ tensor_mechanics/crystal_plasticity/multiple_crystal_plasticity_stress_update_constitutive_convergence.png
+0 −0 tensor_mechanics/crystal_plasticity/tikz_files/HCP_basal_plane_diagram.tex
+0 −0 tensor_mechanics/crystal_plasticity/tikz_files/crystal_plasticity_stress_update_algorithm.tex
+0 −0 ...hanics/crystal_plasticity/tikz_files/multiple_crystal_plasticity_stress_update_constitutive_convergence.tex
+ tensor_mechanics/flowchart_ComputeMultipleInelasticStress-SingleModel.png
+ tensor_mechanics/flowchart_ComputeMultipleInelasticStress.png
+ tensor_mechanics/homogenization-setup.png
+ tensor_mechanics/homogenization-strain.png
+ tensor_mechanics/homogenization-stress.png
+ tensor_mechanics/largecook.png
+ tensor_mechanics/lwr_3quarter_demo.png
+ tensor_mechanics/mechanics_potatoes.png
+ tensor_mechanics/neohookean.png
+ tensor_mechanics/perfectplastic.png
+ tensor_mechanics/polyxtal27_temp.gif
+ tensor_mechanics/rotatecube.gif
+ tensor_mechanics/rotation.png
+ tensor_mechanics/shearcompare.png
+ tensor_mechanics/slip_weakening/2dl2errornorm.png
+ tensor_mechanics/slip_weakening/2dmesh.png
+ tensor_mechanics/slip_weakening/2dslipratetimehist.png
+ tensor_mechanics/slip_weakening/2dsliptimehist.png
+ tensor_mechanics/slip_weakening/3derrornorm.png
+ tensor_mechanics/slip_weakening/3dl2errornorm.png
+ tensor_mechanics/slip_weakening/3dmesh.png
+ tensor_mechanics/slip_weakening/3dshearstressdistribution.png
+ tensor_mechanics/slip_weakening/3dslipratetimehist.png
+ tensor_mechanics/slip_weakening/3dsliptimehist.png
+ tensor_mechanics/slip_weakening/flowchart.png
+ tensor_mechanics/slip_weakening/swlaw.png
+ tensor_mechanics/smallcook.png
+ tensor_mechanics/stvenant.png
+0 −0 tensor_mechanics/tikz_files/flowchart_ComputeMultipleInelasticStress-SingleModel.tex
+0 −0 tensor_mechanics/tikz_files/flowchart_ComputeMultipleInelasticStress.tex
+ thermal_hydraulics/misc/TH_scales.png
+ thermal_hydraulics/misc/TH_scales_new.png
+ thermal_hydraulics/misc/example_corrosion.png
+ thermal_hydraulics/misc/example_httf.png
+ thermal_hydraulics/misc/example_httr.png
+ thermal_hydraulics/misc/example_laser_weld.png
+ thermal_hydraulics/misc/example_msr.png
+ thermal_hydraulics/misc/example_msre.png
+ thermal_hydraulics/misc/example_rayleigh_benard.png
+ thermal_hydraulics/misc/example_subchannel.png
+ thermal_hydraulics/misc/example_subchannel_lf.png
+ thermal_hydraulics/misc/example_two_phase_channel.png
+ thermal_hydraulics/tikz_diagrams/volume_junction.png
2 changes: 1 addition & 1 deletion libmesh
Submodule libmesh updated 1086 files
36 changes: 36 additions & 0 deletions modules/stochastic_tools/include/reporters/JacobianContainer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//* 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 "SnapshotContainerBase.h"
#include "libmesh/id_types.h"
#include "libmesh/petsc_vector.h"
#include "libmesh/vectormap.h"

/**
* This class is responsible for collecting jacobian row concatenated vectors in one place. The
* vectors are kept distributed with respect to the communicator of the application.
* The whole jacobian row concatenated vector is stored.
* The saving frequency can be defined using the `execute_on` parameter.
*/
class JacobianContainer : public SnapshotContainerBase
{
public:
static InputParameters validParams();
JacobianContainer(const InputParameters & parameters);

protected:
virtual std::unique_ptr<NumericVector<Number>> collectSnapshot() override;

std::vector<std::pair<dof_id_type, dof_id_type>> & _sparse_ind;

NonlinearSystem & _nl_sys;
const TagID _tag_id;
};
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ class MappingReporter : public StochasticReporter, public MappingInterface
std::vector<std::vector<std::vector<Real>> *> _vector_real_values_parallel_storage;
std::vector<std::vector<Real> *> _vector_real_values;
///@}
const bool _build_all_mappings_only;
};
35 changes: 35 additions & 0 deletions modules/stochastic_tools/include/reporters/ResidualContainer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//* 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 "MooseTypes.h"
#include "NonlinearSystemBase.h"
#include "SnapshotContainerBase.h"
#include "SystemBase.h"
#include "libmesh/petsc_vector.h"

/**
* This class is responsible for collecting residual vectors in one place. The
* vectors are kept distributed with respect to the communicator of the application.
* The whole residual vector is stored.
* The saving frequency can be defined using the `execute_on` parameter.
*/
class ResidualContainer : public SnapshotContainerBase
{
public:
static InputParameters validParams();
ResidualContainer(const InputParameters & parameters);

protected:
virtual std::unique_ptr<NumericVector<Number>> collectSnapshot() override;

const NonlinearSystem & _nl_sys;
const TagID _tag_id;
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma once

#include "GeneralReporter.h"
#include "TaggingInterface.h"
#include "libmesh/petsc_vector.h"
#include "libmesh/petsc_matrix.h"
#include "NonlinearSystemBase.h"
Expand Down Expand Up @@ -59,4 +60,7 @@ class SnapshotContainerBase : public GeneralReporter

/// The nonlinear system's number whose solution shall be collected
const unsigned int _nonlinear_system_number;
/// Tolerance for comparing two snapshots. If comparison is below tolerance
/// snapshot is not saved.
const Real _save_tolerance;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//* 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

// MOOSE includes
#include "ParallelSolutionStorage.h"
#include "StochasticToolsTransfer.h"
#include "SnapshotContainerBase.h"
#include "UserObjectInterface.h"

// Forward declarations
class ParallelSolutionStorage;

/**
* This class is responsible for serializing solutions coming from subapps on
* specific processors. It is designed to serve as an interface between
* SolutionContainer and ParallelSolutionStorage objects.
*/
class SerializedSnapshotTransfer : public StochasticToolsTransfer
{
public:
static InputParameters validParams();

SerializedSnapshotTransfer(const InputParameters & parameters);

virtual void initialSetup() override;
virtual void execute() override;

///@{
/**
* Methods used when running in batch mode (see SamplerFullSolveMultiApp)
*/
void initializeFromMultiapp() override {}
void executeFromMultiapp() override;
void finalizeFromMultiapp() override {}

void initializeToMultiapp() override {}
void executeToMultiapp() override {}
void finalizeToMultiapp() override {}
///@}

protected:
/// The storage on the main application where the serialized solutions should be
/// transferred
ParallelSolutionStorage * _parallel_storage;

/// Link to the storage spaces on the subapplications (will only hold one in batch mode)
std::vector<SnapshotContainerBase *> _solution_container;
/// Link to the storage spaces on the subapplications (will only hold one in batch mode)
std::vector<SnapshotContainerBase *> _residual_container;
/// Link to the storage spaces on the subapplications (will only hold one in batch mode)
std::vector<SnapshotContainerBase *> _jacobian_container;

private:
/// Serialize on the root processor of the subapplication and transfer the result to the main application
void transferToSubAppRoot(FEProblemBase & app_problem,
SnapshotContainerBase & snap_container,
const dof_id_type global_i,
const std::string snapshot_type);

/**
* Serialize on methodically determined rank of the subapp and transfer to the main application.
* Example: Let's say we have 5 samples and 3 processors on a sub-application.
* In this case, we will serialize the first two on rank 1, the second two on rank
* 2 and the last one on rank 3.
*/
void transferInParallel(FEProblemBase & app_problem,
SnapshotContainerBase & snap_container,
const dof_id_type global_i,
const std::string snapshot_type);

/**
* Initializes the solution container if the multiapp is run in normal mode. We need this because
* in normal mode we don't have a function for initialization besides `initialSetup()`, which
* is execute every time regardless of the multiapp settings.
*/
void initializeInNormalMode();

/// This routine queries the solution container addresses from the subapps. We need to redo this
/// every time initialSetup() (batch-reset) is called on the subapp because the address
/// of SolutionContainer changes. Considering that the transfer doesn't know the multiapp
/// setting, we use the same approach for batch-restore as well, which might be a little
/// wasteful if the execution of the subapps is very fast (usually not the case).
void initializeInBatchMode();

/// Return the system which contains the given variable. It can either be a flavor of
/// a nonlinear system or the auxiliary system.
/// @param vname The name of the variable whose system is queried
SystemBase & getSystem(FEProblemBase & app_problem, const VariableName & vname);

/// User-selected switch that determines if we want to serialize on the root of the subapp
/// only or distribute the solutions between all the ranks of the subapp.
const bool _serialize_on_root;
};
165 changes: 165 additions & 0 deletions modules/stochastic_tools/include/userobjects/InverseRB.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
///* 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 "DEIMRBMapping.h"
#include "GeneralUserObject.h"
#include "MappingInterface.h"
#include "NonlinearSystemBase.h"
#include "SystemBase.h"
#include "libmesh/elem.h"
#include "libmesh/elem_range.h"
#include "libmesh/id_types.h"
#include <memory>

/**
* InverseRB is a user object that performs inverse reduced basis mapping.
*/
class InverseRB : public GeneralUserObject, public MappingInterface
{
public:
static InputParameters validParams();

/**
* Constructor for InverseRB.
* @param parameters Input parameters
*/
InverseRB(const InputParameters & parameters);

virtual void initialize() override;

virtual void execute() override;

virtual void finalize() override;

virtual void initialSetup() override;

// only needed for ElementUserObjects and NodalUseroObjects
virtual void threadJoin(const UserObject & /*y*/) override{};

protected:
/// Link to the mapping object which provides the inverse mapping function
DEIMRBMapping * _mapping;

std::vector<dof_id_type> _residual_inds;

std::vector<std::pair<dof_id_type, dof_id_type>> _jacobian_matrix_inds;

const DofMap & _dof_map;

const dof_id_type _max_iter;

const Real _tolerance;

const Real _relax_factor;

private:
/**
* Finds the reduced element range for the given DOFs.
* @param dofs Vector of DOF indices
* @return Vector of pointers to the reduced elements
*/
std::vector<const Elem *> findReducedElemRange(const std::vector<dof_id_type> & dofs);

/**
* Finds the reduced node range for the given DOFs.
* @param dofs Vector of DOF indices
* @return Vector of pointers to the reduced nodes
*/
std::vector<const Node *> findReducedNodeRange(const std::vector<dof_id_type> & dofs);

/**
* Creates a range object from a vector of items.
* @tparam RangeType Type of the range object
* @tparam ItemType Type of the items in the vector
* @tparam IteratorType Type of the iterator for the range object
* @param items Vector of pointers to the items
* @return Unique pointer to the created range object
*/
template <typename RangeType, typename ItemType, typename IteratorType>
std::unique_ptr<RangeType> createRangeFromVector(const std::vector<const ItemType *> & items);

/**
* Retrieves the reduced Jacobian values from the sparse matrix.
* @param jac Sparse Jacobian matrix
* @return Vector of reduced Jacobian values
*/
std::vector<Real> getReducedJacValues(const SparseMatrix<Number> & jac);

/**
* Retrieves the reduced residual values from the numeric vector.
* @param res Numeric residual vector
* @return Reduced residual values
*/
std::vector<Real> getReducedResValues(const NumericVector<Number> & res);

/**
* Computes the reduced Jacobian matrix.
* @return Reduced Jacobian
*/
DenseMatrix<Real> computeReducedJacobian();

/**
* Computes the reduced residual vector.
* @return Reduced residual
*/
DenseVector<Real> computeReducedResidual();

/**
* Updates the solution vector with the reduced solution.
* @param reduced_sol Reduced solution vector
*/
void updateSolution(const DenseVector<Real> & reduced_sol);

/**
* Computes the residual norm.
* @return Residual norm value
*/
Real computeResidual();

/// Vector of reduced Jacobian elements
std::vector<const Elem *> _red_jac_elem;

/// Vector of reduced residual elements
std::vector<const Elem *> _red_res_elem;

/// Vector of reduced Jacobian nodes
std::vector<const Node *> _red_jac_node;

/// Vector of reduced residual nodes
std::vector<const Node *> _red_res_node;

/// Local range of reduced Jacobian elements
std::unique_ptr<ConstElemRange> _red_jac_elem_local_range;

/// Local range of reduced residual elements
std::unique_ptr<ConstElemRange> _red_res_elem_local_range;

/// Local range of reduced Jacobian nodes
std::unique_ptr<ConstNodeRange> _red_jac_node_local_range;

/// Local range of reduced residual nodes
std::unique_ptr<ConstNodeRange> _red_res_node_local_range;

/// Nonlinear system
NonlinearSystemBase & _nl_sys;

/// Jacobian matrix
SparseMatrix<Number> * _jac_matrix;

/// Residual vector
NumericVector<Number> * _residual;

/// Current solution
const NumericVector<Number> * _curr_sol;

/// Linear solver
LinearSolver<Number> * _solver;
};
Loading

0 comments on commit ec5882c

Please sign in to comment.