v0.16.3
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Attributes | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
Example Struct Reference

[Example] More...

Collaboration diagram for Example:
[legend]

Classes

struct  BoundaryOp
 
struct  CommonData
 [Example] More...
 
struct  DynamicFirstOrderConsConstantTimeScale
 
struct  DynamicFirstOrderConsSinusTimeScale
 
struct  OpCalcSurfaceAverageTemperature
 
struct  OpError
 
struct  OpError< 1 >
 [OpError def] More...
 
struct  OpFirst
 
struct  OpFluxRhs
 
struct  OpRadiationLhs
 
struct  OpRadiationRhs
 
struct  OpRhs
 
struct  OpSecond
 [Operator] More...
 
struct  OpZero
 [Common data] More...
 
struct  ScaledTimeScale
 

Public Types

enum  { VOL , COUNT }
 
enum  { VOL , COUNT }
 

Public Member Functions

 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 [Run problem]
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 Main driver function for the optimization process.
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 Main driver function for the optimization process.
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 
 Example (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 

Static Public Attributes

static std::array< double, 2 > meshVolumeAndCount = {0, 0}
 

Private Types

enum  BoundingBox {
  CENTER_X = 0 , CENTER_Y , MAX_X , MAX_Y ,
  MIN_X , MIN_Y , LAST_BB
}
 

Private Member Functions

MoFEMErrorCode setupProblem ()
 [Run problem]
 
MoFEMErrorCode createCommonData ()
 [Set up problem]
 
MoFEMErrorCode bC ()
 [Create common data]
 
MoFEMErrorCode OPs ()
 [Boundary condition]
 
MoFEMErrorCode tsSolve ()
 
MoFEMErrorCode testOperators ()
 [Solve]
 
MoFEMErrorCode readMesh ()
 [Run problem]
 
MoFEMErrorCode setupProblem ()
 
MoFEMErrorCode boundaryCondition ()
 [Set up problem]
 
MoFEMErrorCode assembleSystem ()
 [Push operators to pipeline]
 
MoFEMErrorCode solveSystem ()
 [Solve]
 
MoFEMErrorCode outputResults ()
 [Solve]
 
MoFEMErrorCode checkResults ()
 [Postprocess results]
 
MoFEMErrorCode readMesh ()
 
MoFEMErrorCode setupProblem ()
 
MoFEMErrorCode assembleSystem ()
 
MoFEMErrorCode solveSystem ()
 
MoFEMErrorCode projectResults (BitRefLevel parent_bit, BitRefLevel child_bit, BitRefLevel refine_bit)
 [Solve]
 
MoFEMErrorCode outputResults (std::string FIELD_NAME_U)
 [Project results]
 
MoFEMErrorCode edgeFlips (BitRefLevel parent_bit, BitRefLevel child_bit)
 [Output results]
 
MoFEMErrorCode refineSkin (BitRefLevel parent_bit, BitRefLevel refine_bit)
 [Edge flips]
 
MoFEMErrorCode reSetupProblem (BitRefLevel child_bit)
 [Refine skin]
 
MoFEMErrorCode readMesh ()
 
MoFEMErrorCode setupProblem ()
 
MoFEMErrorCode boundaryCondition ()
 
MoFEMErrorCode assembleSystem ()
 
MoFEMErrorCode solveSystem ()
 
MoFEMErrorCode outputResults ()
 
MoFEMErrorCode checkResults ()
 
MoFEMErrorCode setUp ()
 [Run all]
 
MoFEMErrorCode createCommonData ()
 
MoFEMErrorCode setFieldValues ()
 [Create common data]
 
MoFEMErrorCode pushOperators ()
 [Set density distribution]
 
MoFEMErrorCode integrateElements ()
 [Push operators to pipeline]
 
MoFEMErrorCode postProcess ()
 [Integrate]
 
MoFEMErrorCode checkResults ()
 
MoFEMErrorCode readMesh ()
 
MoFEMErrorCode setupProblem ()
 
MoFEMErrorCode setIntegrationRules ()
 [Set up problem]
 
MoFEMErrorCode createCommonData ()
 
MoFEMErrorCode boundaryCondition ()
 
MoFEMErrorCode assembleSystem ()
 
MoFEMErrorCode solveSystem ()
 
MoFEMErrorCode outputResults ()
 
MoFEMErrorCode checkResults ()
 
MoFEMErrorCode readMesh ()
 
MoFEMErrorCode setupProblem ()
 
MoFEMErrorCode assembleSystemIntensity ()
 [Calculate flux on boundary]
 
MoFEMErrorCode assembleSystemFlux ()
 
MoFEMErrorCode solveSystem ()
 
MoFEMErrorCode calculateFlux (double &calc_flux)
 [Set up problem]
 
MoFEMErrorCode outputResults (const int i)
 [Solve]
 
MoFEMErrorCode readMesh ()
 
MoFEMErrorCode setupProblem ()
 
MoFEMErrorCode setIntegrationRules ()
 
MoFEMErrorCode createCommonData ()
 
MoFEMErrorCode boundaryCondition ()
 
MoFEMErrorCode assembleSystem ()
 
MoFEMErrorCode solveSystem ()
 
MoFEMErrorCode outputResults ()
 
MoFEMErrorCode checkResults ()
 
MoFEMErrorCode readMesh ()
 
MoFEMErrorCode setupProblem ()
 
MoFEMErrorCode boundaryCondition ()
 
MoFEMErrorCode assembleSystem ()
 
MoFEMErrorCode solveSystem ()
 
MoFEMErrorCode outputResults ()
 
MoFEMErrorCode checkResults ()
 
MoFEMErrorCode setupProblem ()
 
MoFEMErrorCode createCommonData ()
 
MoFEMErrorCode bC ()
 
MoFEMErrorCode OPs ()
 
MoFEMErrorCode kspSolve ()
 [Push operators to pipeline]
 
MoFEMErrorCode postProcess ()
 
MoFEMErrorCode checkResults ()
 
MoFEMErrorCode readMesh ()
 
MoFEMErrorCode setupProblem ()
 
MoFEMErrorCode setIntegrationRules ()
 
MoFEMErrorCode createCommonData ()
 
MoFEMErrorCode boundaryCondition ()
 
MoFEMErrorCode assembleSystem ()
 
MoFEMErrorCode solveSystem ()
 
MoFEMErrorCode outputResults ()
 
MoFEMErrorCode checkResults ()
 
MoFEMErrorCode readMesh ()
 
MoFEMErrorCode setupProblem ()
 
MoFEMErrorCode createCommonData ()
 
MoFEMErrorCode boundaryCondition ()
 
MoFEMErrorCode assembleSystem ()
 
MoFEMErrorCode solveSystem ()
 
MoFEMErrorCode outputResults ()
 
MoFEMErrorCode checkResults ()
 
MoFEMErrorCode readMesh ()
 
MoFEMErrorCode setupProblem ()
 
MoFEMErrorCode boundaryCondition ()
 
MoFEMErrorCode assembleSystem ()
 
MoFEMErrorCode solveSystem ()
 
MoFEMErrorCode outputResults ()
 
MoFEMErrorCode checkResults ()
 
MoFEMErrorCode readMesh ()
 
MoFEMErrorCode setupProblem ()
 
MoFEMErrorCode createCommonData ()
 
MoFEMErrorCode boundaryCondition ()
 
MoFEMErrorCode assembleSystem ()
 
MoFEMErrorCode solveSystem ()
 
MoFEMErrorCode outputResults ()
 
MoFEMErrorCode checkResults ()
 
MoFEMErrorCode readMesh ()
 Read mesh from file and setup meshsets.
 
MoFEMErrorCode setupProblem ()
 Setup fields, approximation spaces and DOFs.
 
MoFEMErrorCode setupAdJoint ()
 Setup adjoint fields and finite elements.
 
MoFEMErrorCode boundaryCondition ()
 Apply essential boundary conditions.
 
MoFEMErrorCode topologyModes ()
 Compute topology optimization modes.
 
MoFEMErrorCode assembleSystem ()
 Setup operators in finite element pipeline.
 
MoFEMErrorCode solveElastic ()
 Solve forward elastic problem.
 
MoFEMErrorCode postprocessElastic (int iter, SmartPetscObj< Vec > adjoint_vector=nullptr)
 Post-process and output results.
 
MoFEMErrorCode calculateGradient (PetscReal *objective_function_value, Vec objective_function_gradient, Vec adjoint_vector)
 Calculate objective function gradient using adjoint method.
 
MoFEMErrorCode readMesh ()
 
MoFEMErrorCode setupProblem ()
 
MoFEMErrorCode setupAdJoint ()
 
MoFEMErrorCode boundaryCondition ()
 
MoFEMErrorCode assembleSystem ()
 
MoFEMErrorCode solveElastic ()
 Solve forward elastic problem.
 
MoFEMErrorCode postprocessElastic (int iter, SmartPetscObj< Vec > gradient_vector=nullptr, SmartPetscObj< Vec > adjoint_vector=nullptr, SmartPetscObj< Vec > dJ_du=nullptr)
 Post-process and output results.
 
MoFEMErrorCode calculateGradient (PetscReal *objective_function_value, Vec objective_function_gradient, Vec adjoint_vector, Vec dJ_du)
 Calculate objective function gradient using adjoint method.
 
MoFEMErrorCode testGradient (Vec gradient_vector)
 [calculateGradient]
 
friend PetscErrorCode::MyTSResizeTransfer (TS, PetscInt, Vec[], Vec[], void *)
 
MoFEMErrorCode setupProblem ()
 
MoFEMErrorCode createCommonData ()
 
MoFEMErrorCode initialConditions ()
 [Create common data]
 
MoFEMErrorCode mechanicalBC (BitRefLevel bit, BitRefLevel mask)
 [Initial conditions]
 
MoFEMErrorCode thermalBC (BitRefLevel bit, BitRefLevel mask)
 [Mechanical boundary conditions]
 
MoFEMErrorCode getElementQuality (std::multimap< double, EntityHandle > &el_q_map, Range &flipped_els, std::vector< EntityHandle > &new_connectivity, bool &do_refine, Tag &th_spatial_coords)
 [Get element quality]
 
MoFEMErrorCode edgeFlips (BitRefLevel parent_bit, BitRefLevel child_bit)
 
MoFEMErrorCode doEdgeFlips (std::multimap< double, EntityHandle > &el_q_map, Range &flipped_els, Tag &th_spatial_coords, std::vector< EntityHandle > &new_connectivity)
 [Edge flips]
 
MoFEMErrorCode doEdgeSplits (bool &refine, bool add_ents)
 [Do Edge Flips]
 
MoFEMErrorCode OPs ()
 
MoFEMErrorCode tsSolve ()
 
template<int DIM, AssemblyType A, IntegrationType I, typename DomainEleOp >
MoFEMErrorCode opThermoPlasticFactoryDomainRhs (MoFEM::Interface &m_field, std::string block_name, std::string thermal_block_name, std::string thermoelastic_block_name, std::string thermoplastic_block_name, Pip &pip, std::string u, std::string ep, std::string tau, std::string temperature)
 
template<int DIM, AssemblyType A, IntegrationType I, typename DomainEleOp >
MoFEMErrorCode opThermoPlasticFactoryDomainLhs (MoFEM::Interface &m_field, std::string block_name, std::string thermal_block_name, std::string thermoelastic_block_name, std::string thermoplastic_block_name, Pip &pip, std::string u, std::string ep, std::string tau, std::string temperature)
 
template<int DIM, IntegrationType I, typename DomainEleOp >
auto createCommonThermoPlasticOps (MoFEM::Interface &m_field, std::string plastic_block_name, std::string thermal_block_name, std::string thermoelastic_block_name, std::string thermoplastic_block_name, boost::ptr_deque< ForcesAndSourcesCore::UserDataOperator > &pip, std::string u, std::string ep, std::string tau, std::string temperature, double scale, ScalerFunTwoArgs thermal_conductivity_scaling, ScalerFunTwoArgs heat_capacity_scaling, ScalerFunThreeArgs inelastic_heat_fraction_scaling, Sev sev, bool with_rates=true)
 
MoFEMErrorCode setupProblem ()
 
MoFEMErrorCode createCommonData ()
 
MoFEMErrorCode bC ()
 
MoFEMErrorCode OPs ()
 
MoFEMErrorCode tsSolve ()
 
MoFEMErrorCode testOperators ()
 
MoFEMErrorCode readMesh ()
 
MoFEMErrorCode setupProblem ()
 
MoFEMErrorCode boundaryCondition ()
 
MoFEMErrorCode assembleSystem ()
 
MoFEMErrorCode solveSystem ()
 
MoFEMErrorCode outputResults ()
 
MoFEMErrorCode checkResults ()
 
MoFEMErrorCode readMesh ()
 
MoFEMErrorCode setupProblem ()
 
MoFEMErrorCode boundaryCondition ()
 
MoFEMErrorCode assembleSystem ()
 
MoFEMErrorCode solveSystem ()
 
MoFEMErrorCode gettingNorms ()
 [Solve]
 
MoFEMErrorCode outputResults ()
 
MoFEMErrorCode checkResults ()
 

Static Private Member Functions

static std::pair< int, int > getCoordsInImage (double x, double y)
 
static double rhsSource (const double x, const double y, const double)
 
static double lhsFlux (const double x, const double y, const double)
 
static int integrationRule (int, int, int p_data)
 

Private Attributes

MoFEM::Interface & mField
 Reference to MoFEM interface.
 
std::tuple< SmartPetscObj< Vec >, SmartPetscObj< VecScatter > > uXScatter
 
std::tuple< SmartPetscObj< Vec >, SmartPetscObj< VecScatter > > uYScatter
 
std::tuple< SmartPetscObj< Vec >, SmartPetscObj< VecScatter > > uZScatter
 
Simple * simpleInterface
 
boost::shared_ptr< std::vector< unsigned char > > boundaryMarker
 
boost::shared_ptr< CommonData > commonDataPtr
 
FieldApproximationBase base
 Choice of finite element basis functions.
 
FieldSpace space
 
Simple * simple
 
SmartPetscObj< Mat > M
 
SmartPetscObj< Mat > H
 
SmartPetscObj< EPS > eps
 
boost::shared_ptr< VectorDouble > approxVals
 
boost::shared_ptr< MatrixDouble > approxGradVals
 
Range pinchNodes
 
boost::shared_ptr< MatrixDouble > matDPtr
 
SmartPetscObj< Mat > K
 
SmartPetscObj< EPS > ePS
 
std::array< SmartPetscObj< Vec >, 6 > rigidBodyMotion
 
boost::shared_ptr< FEMethod > domianLhsFEPtr
 
boost::shared_ptr< FEMethod > domianRhsFEPtr
 
boost::shared_ptr< MatrixDouble > vectorFieldPtr
 Field values at evaluation points.
 
int fieldOrder = 2
 Polynomial order for approximation.
 
SmartPetscObj< KSP > kspElastic
 Linear solver for elastic problem.
 
SmartPetscObj< DM > adjointDM
 Data manager for adjoint problem.
 
boost::shared_ptr< ObjectiveFunctionData > pythonPtr
 Interface to Python objective function.
 
std::vector< SmartPetscObj< Vec > > modeVecs
 Topology mode vectors (design variables)
 
std::vector< std::array< double, 3 > > modeCentroids
 Centroids of optimization blocks.
 
std::vector< std::array< double, 6 > > modeBBoxes
 Bounding boxes of optimization blocks.
 
SmartPetscObj< Vec > initialGeometry
 Initial geometry field.
 
boost::shared_ptr< DomainEle > reactionFe
 
boost::shared_ptr< VectorDouble > tempFieldPtr
 
boost::shared_ptr< MatrixDouble > fluxFieldPtr
 
boost::shared_ptr< MatrixDouble > dispFieldPtr
 
boost::shared_ptr< MatrixDouble > dispGradPtr
 
boost::shared_ptr< MatrixDouble > strainFieldPtr
 
boost::shared_ptr< MatrixDouble > stressFieldPtr
 
boost::shared_ptr< VectorDouble > plasticMultiplierFieldPtr
 
boost::shared_ptr< MatrixDouble > plasticStrainFieldPtr
 

Static Private Attributes

static std::vector< double > rZ
 
static std::vector< MatrixInt > iI
 
static std::array< double, LAST_BB > aveMaxMin
 
static int focalIndex
 
static int savitzkyGolayNormalisation
 
static const int * savitzkyGolayWeights
 
static ApproxFieldFunction< FIELD_DIM > approxFunction
 

Friends

struct TSPrePostProc
 

Detailed Description

[Example]

[Create Example_struct]

Examples
mofem/tutorials/adv-0_plasticity/plastic.cpp, mofem/tutorials/adv-4_dynamic_first_order_con_law/dynamic_first_order_con_law.cpp, mofem/tutorials/adv-6_dg_projection/between_meshes_dg_projection.cpp, mofem/tutorials/clx-0_acoustics/helmholtz.cpp, mofem/tutorials/fun-1_integration/integration.cpp, mofem/tutorials/fun-2_plot_base/plot_base.cpp, mofem/tutorials/mix-1_light_intensity_equation/phase.cpp, mofem/tutorials/scl-0_least_squares/approximation.cpp, mofem/tutorials/scl-8_heat_radiation/radiation.cpp, mofem/tutorials/scl-9_heat_method/heat_method.cpp, mofem/tutorials/vec-1_eigen_elasticity/eigen_elastic.cpp, mofem/tutorials/vec-3_nonlinear_dynamics/nonlinear_dynamic_elastic.cpp, mofem/tutorials/vec-4_shallow_wave/shallow_wave.cpp, mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp, nonlinear_elastic.cpp, plastic.cpp, and thermoplastic.cpp.

Definition at line 216 of file plastic.cpp.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
VOL 
COUNT 

Definition at line 222 of file plastic.cpp.

222{ VOL, COUNT };

◆ anonymous enum

anonymous enum
Enumerator
VOL 
COUNT 

Definition at line 222 of file plastic.cpp.

222{ VOL, COUNT };

◆ BoundingBox

enum Example::BoundingBox
private
Enumerator
CENTER_X 
CENTER_Y 
MAX_X 
MAX_Y 
MIN_X 
MIN_Y 
LAST_BB 
Examples
mofem/tutorials/mix-1_light_intensity_equation/phase.cpp.

Definition at line 98 of file phase.cpp.

98 {
99 CENTER_X = 0,
100 CENTER_Y,
101 MAX_X,
102 MAX_Y,
103 MIN_X,
104 MIN_Y,
105 LAST_BB
106 };
@ MAX_X
Definition phase.cpp:101
@ MIN_X
Definition phase.cpp:103
@ MIN_Y
Definition phase.cpp:104
@ CENTER_X
Definition phase.cpp:99
@ MAX_Y
Definition phase.cpp:102
@ CENTER_Y
Definition phase.cpp:100
@ LAST_BB
Definition phase.cpp:105

Constructor & Destructor Documentation

◆ Example() [1/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 218 of file plastic.cpp.

218: mField(m_field) {}
MoFEM::Interface & mField
Reference to MoFEM interface.
Definition plastic.cpp:226

◆ Example() [2/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 421 of file dynamic_first_order_con_law.cpp.

421: mField(m_field) {}

◆ Example() [3/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 42 of file between_meshes_dg_projection.cpp.

42: mField(m_field) {}

◆ Example() [4/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 35 of file helmholtz.cpp.

35: mField(m_field) {}

◆ Example() [5/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 25 of file integration.cpp.

25: mField(m_field) {}

◆ Example() [6/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 50 of file plot_base.cpp.

50: mField(m_field) {}

◆ Example() [7/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 81 of file phase.cpp.

81: mField(m_field) {}

◆ Example() [8/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 51 of file approximation.cpp.

51: mField(m_field) {}

◆ Example() [9/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 57 of file schrod_eig.cpp.

57: mField(m_field) {}

◆ Example() [10/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 45 of file radiation.cpp.

45: mField(m_field) {}

◆ Example() [11/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 47 of file heat_method.cpp.

47: mField(m_field) {}

◆ Example() [12/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 54 of file eigen_elastic.cpp.

54: mField(m_field) {}

◆ Example() [13/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 52 of file nonlinear_dynamic_elastic.cpp.

52: mField(m_field) {}

◆ Example() [14/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 339 of file shallow_wave.cpp.

339: mField(m_field) {}

◆ Example() [15/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 129 of file adjoint.cpp.

129: mField(m_field) {}

◆ Example() [16/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 116 of file gradient.cpp.

116: mField(m_field) {}

◆ Example() [17/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 896 of file thermoplastic.cpp.

896: mField(m_field) {}

◆ Example() [18/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 218 of file plastic.cpp.

218: mField(m_field) {}

◆ Example() [19/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 80 of file elastic.cpp.

80: mField(m_field) {}

◆ Example() [20/20]

Example::Example ( MoFEM::Interface &  m_field)
inline

Definition at line 58 of file nonlinear_elastic.cpp.

58: mField(m_field) {}

Member Function Documentation

◆ assembleSystem() [1/14]

MoFEMErrorCode Example::assembleSystem ( )
private

[Push operators to pipeline]

[Adjoint modes]

[Boundary condition]

[Applying essential BC]

[Set up problem]

[Push operators to pipeline]

[Assemble system]

[Integration rule]

[Integration rule]

[Push domain stiffness matrix to pipeline]

[Push domain stiffness matrix to pipeline]

[Push Body forces]

[Push Body forces]

[Push natural boundary conditions]

[Push natural boundary conditions]

Examples
mofem/tutorials/adv-4_dynamic_first_order_con_law/dynamic_first_order_con_law.cpp, mofem/tutorials/adv-6_dg_projection/between_meshes_dg_projection.cpp, mofem/tutorials/clx-0_acoustics/helmholtz.cpp, mofem/tutorials/fun-2_plot_base/plot_base.cpp, mofem/tutorials/scl-0_least_squares/approximation.cpp, mofem/tutorials/scl-9_heat_method/heat_method.cpp, mofem/tutorials/vec-1_eigen_elasticity/eigen_elastic.cpp, mofem/tutorials/vec-3_nonlinear_dynamics/nonlinear_dynamic_elastic.cpp, mofem/tutorials/vec-4_shallow_wave/shallow_wave.cpp, mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp, and nonlinear_elastic.cpp.

Definition at line 639 of file dynamic_first_order_con_law.cpp.

639 {
641 auto get_body_force = [this](const double, const double, const double) {
644 t_source(i) = 0.;
645 t_source(0) = 0.1;
646 t_source(1) = 1.;
647 return t_source;
648 };
649
650 // specific time scaling
651 auto get_time_scale = [this](const double time) {
652 return sin(time * omega * M_PI);
653 };
654
655 auto apply_rhs = [&](auto &pip) {
657
659 "GEOMETRY");
660
661 // Calculate Gradient of velocity
662 auto mat_v_grad_ptr = boost::make_shared<MatrixDouble>();
664 "V", mat_v_grad_ptr));
665
666 auto gravity_vector_ptr = boost::make_shared<MatrixDouble>();
667 gravity_vector_ptr->resize(1, SPACE_DIM);
668 auto set_body_force = [&]() {
671 auto t_force = getFTensor1FromMat<SPACE_DIM, 0>(*gravity_vector_ptr);
672 double unit_weight = 0.;
673 CHKERR PetscOptionsGetReal(PETSC_NULLPTR, "", "-unit_weight", &unit_weight,
674 PETSC_NULLPTR);
675 t_force(i) = 0;
676 if (SPACE_DIM == 2) {
677 t_force(1) = -unit_weight;
678 } else if (SPACE_DIM == 3) {
679 t_force(2) = unit_weight;
680 }
682 };
683
684 CHKERR set_body_force();
685 pip.push_back(new OpBodyForce("V", gravity_vector_ptr,
686 [](double, double, double) { return 1.; }));
687
688 // Calculate unknown F
689 auto mat_H_tensor_ptr = boost::make_shared<MatrixDouble>();
691 "F", mat_H_tensor_ptr));
692
693 // // Calculate F
694 double tau = 0.2;
695 CHKERR PetscOptionsGetReal(PETSC_NULLPTR, "", "-tau", &tau, PETSC_NULLPTR);
696
697 double xi = 0.;
698 CHKERR PetscOptionsGetReal(PETSC_NULLPTR, "", "-xi", &xi, PETSC_NULLPTR);
699
700 // Calculate P stab
701 auto one = [&](const double, const double, const double) {
702 return 3. * bulk_modulus_K;
703 };
704 auto minus_one = [](const double, const double, const double) {
705 return -1.;
706 };
707
708 auto mat_dot_F_tensor_ptr = boost::make_shared<MatrixDouble>();
710 "F_dot", mat_dot_F_tensor_ptr));
711
712 // Calculate Gradient of Spatial Positions
713 auto mat_x_grad_ptr = boost::make_shared<MatrixDouble>();
715 "x_2", mat_x_grad_ptr));
716
717 auto mat_F_tensor_ptr = boost::make_shared<MatrixDouble>();
719 mat_F_tensor_ptr, mat_H_tensor_ptr));
720
721 auto mat_F_stab_ptr = boost::make_shared<MatrixDouble>();
723 mat_F_tensor_ptr, mat_F_stab_ptr, mat_dot_F_tensor_ptr, tau, xi,
724 mat_x_grad_ptr, mat_v_grad_ptr));
725
726 PetscBool is_linear_elasticity = PETSC_TRUE;
727 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-is_linear_elasticity",
728 &is_linear_elasticity, PETSC_NULLPTR);
729
730 auto mat_P_stab_ptr = boost::make_shared<MatrixDouble>();
731 if (is_linear_elasticity) {
734 mat_F_stab_ptr));
735 } else {
736 auto inv_F = boost::make_shared<MatrixDouble>();
737 auto det_ptr = boost::make_shared<VectorDouble>();
738
739 pip.push_back(
740 new OpInvertMatrix<SPACE_DIM>(mat_F_stab_ptr, det_ptr, inv_F));
741
742 // OpCalculatePiolaIncompressibleNH
745 mat_F_stab_ptr, inv_F, det_ptr));
746 }
747
748 pip.push_back(new OpGradTimesTensor2("V", mat_P_stab_ptr, minus_one));
749 pip.push_back(new OpRhsTestPiola("F", mat_v_grad_ptr, one));
750
752 };
753
754 auto *pipeline_mng = mField.getInterface<PipelineManager>();
755 CHKERR apply_rhs(pipeline_mng->getOpDomainExplicitRhsPipeline());
756
757 auto integration_rule = [](int, int, int approx_order) {
758 return 2 * approx_order;
759 };
760 CHKERR pipeline_mng->setDomainExplicitRhsIntegrationRule(integration_rule);
761
763}
@ H1
continuous field
Definition definitions.h:85
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
constexpr double omega
Save field DOFS on vertices/tags.
double bulk_modulus_K
FormsIntegrators< DomainEleOp >::Assembly< AssemblyType::PETSC >::LinearForm< IntegrationType::GAUSS >::OpBaseTimesVector< 1, SPACE_DIM *SPACE_DIM, SPACE_DIM *SPACE_DIM > OpRhsTestPiola
FormsIntegrators< DomainEleOp >::Assembly< AssemblyType::PETSC >::LinearForm< IntegrationType::GAUSS >::OpGradTimesTensor< 1, SPACE_DIM, SPACE_DIM > OpGradTimesTensor2
double shear_modulus_G
auto integration_rule
FTensor::Index< 'i', SPACE_DIM > i
PetscErrorCode PetscOptionsGetReal(PetscOptions *, const char pre[], const char name[], PetscReal *dval, PetscBool *set)
PetscErrorCode PetscOptionsGetBool(PetscOptions *, const char pre[], const char name[], PetscBool *bval, PetscBool *set)
static constexpr int approx_order
Add operators pushing bases from local to physical configuration.
Get values at integration pts for tensor field rank 2, i.e. matrix field.
Get field gradients at integration pts for scalar field rank 0, i.e. vector field.
Operator for inverting matrices at integration points.
PipelineManager interface.
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.
constexpr int SPACE_DIM
DomainNaturalBC::OpFlux< NaturalMeshsetType< BLOCKSET >, 1, SPACE_DIM > OpBodyForce

◆ assembleSystem() [2/14]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [3/14]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [4/14]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [5/14]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [6/14]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [7/14]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [8/14]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [9/14]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [10/14]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [11/14]

MoFEMErrorCode Example::assembleSystem ( )
private

Setup operators in finite element pipeline.

◆ assembleSystem() [12/14]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [13/14]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [14/14]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystemFlux()

MoFEMErrorCode Example::assembleSystemFlux ( )
private

◆ assembleSystemIntensity()

MoFEMErrorCode Example::assembleSystemIntensity ( )
private

[Calculate flux on boundary]

[Push operators to pipeline]

Examples
mofem/tutorials/mix-1_light_intensity_equation/phase.cpp.

Definition at line 434 of file phase.cpp.

434 {
436
437 auto *pipeline_mng = mField.getInterface<PipelineManager>();
438
439 pipeline_mng->getDomainLhsFE().reset();
440 pipeline_mng->getDomainRhsFE().reset();
441 pipeline_mng->getBoundaryRhsFE().reset();
442
443 auto rule_vol = [](int, int, int order) { return 2 * (order + 1); };
444 pipeline_mng->setDomainLhsIntegrationRule(rule_vol);
445 pipeline_mng->setDomainRhsIntegrationRule(rule_vol);
446
447 CHKERR AddHOOps<2, 2, 2>::add(pipeline_mng->getOpDomainLhsPipeline(), {HDIV, L2});
448 CHKERR AddHOOps<2, 2, 2>::add(pipeline_mng->getOpDomainRhsPipeline(), {HDIV, L2});
449
450 pipeline_mng->getOpDomainLhsPipeline().push_back(
451 new OpHdivHdiv("S", "S", lhsFlux));
452 auto unity = []() { return 1; };
453 pipeline_mng->getOpDomainLhsPipeline().push_back(
454 new OpHdivU("S", "PHI", unity, true));
455 pipeline_mng->getOpDomainRhsPipeline().push_back(
456 new OpDomainSource("PHI", rhsSource));
457
459}
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::LinearForm< GAUSS >::OpSource< 1, FIELD_DIM > OpDomainSource
constexpr int order
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::BiLinearForm< GAUSS >::OpMixDivTimesScalar< 2 > OpHdivU
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::BiLinearForm< GAUSS >::OpMass< 3, 3 > OpHdivHdiv
Integrate Lhs base of flux (1/k) base of flux (FLUX x FLUX)
Definition seepage.cpp:86
static double rhsSource(const double x, const double y, const double)
Definition phase.cpp:150
static double lhsFlux(const double x, const double y, const double)
Definition phase.cpp:165
boost::shared_ptr< FEMethod > & getDomainLhsFE()
Get domain left-hand side finite element.

◆ bC() [1/3]

MoFEMErrorCode Example::bC ( )
private

[Create common data]

[Boundary condition]

Examples
mofem/tutorials/adv-0_plasticity/plastic.cpp, mofem/tutorials/scl-8_heat_radiation/radiation.cpp, and plastic.cpp.

Definition at line 604 of file plastic.cpp.

604 {
606
608 auto bc_mng = mField.getInterface<BcManager>();
609
610 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), "REMOVE_X",
611 "U", 0, 0);
612 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), "REMOVE_Y",
613 "U", 1, 1);
614 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), "REMOVE_Z",
615 "U", 2, 2);
616 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(),
617 "REMOVE_ALL", "U", 0, 3);
618
619#ifdef ADD_CONTACT
620 for (auto b : {"FIX_X", "REMOVE_X"})
621 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), b,
622 "SIGMA", 0, 0, false, true);
623 for (auto b : {"FIX_Y", "REMOVE_Y"})
624 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), b,
625 "SIGMA", 1, 1, false, true);
626 for (auto b : {"FIX_Z", "REMOVE_Z"})
627 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), b,
628 "SIGMA", 2, 2, false, true);
629 for (auto b : {"FIX_ALL", "REMOVE_ALL"})
630 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), b,
631 "SIGMA", 0, 3, false, true);
632 CHKERR bc_mng->removeBlockDOFsOnEntities(
633 simple->getProblemName(), "NO_CONTACT", "SIGMA", 0, 3, false, true);
634#endif
635
636 CHKERR bc_mng->pushMarkDOFsOnEntities<DisplacementCubitBcData>(
637 simple->getProblemName(), "U");
638
639 auto &bc_map = bc_mng->getBcMapByBlockName();
640 for (auto bc : bc_map)
641 MOFEM_LOG("PLASTICITY", Sev::verbose) << "Marker " << bc.first;
642
644}
#define MOFEM_LOG(channel, severity)
Log.
SeverityLevel
Severity levels.
Simple * simple
Boundary condition manager for finite element problem setup.
Definition of the displacement bc data structure.
Definition BCData.hpp:72
Simple interface for fast problem set-up.
Definition Simple.hpp:27
const std::string getProblemName() const
Get the Problem Name.
Definition Simple.hpp:450

◆ bC() [2/3]

MoFEMErrorCode Example::bC ( )
private

◆ bC() [3/3]

MoFEMErrorCode Example::bC ( )
private

◆ boundaryCondition() [1/13]

MoFEMErrorCode Example::boundaryCondition ( )
private

[Set up problem]

[Setup adjoint]

[Create common data]

[Boundary condition]

[Applying essential BC]

[Define gravity vector]

Examples
mofem/tutorials/adv-4_dynamic_first_order_con_law/dynamic_first_order_con_law.cpp, mofem/tutorials/clx-0_acoustics/helmholtz.cpp, mofem/tutorials/fun-2_plot_base/plot_base.cpp, mofem/tutorials/scl-0_least_squares/approximation.cpp, mofem/tutorials/scl-9_heat_method/heat_method.cpp, mofem/tutorials/vec-1_eigen_elasticity/eigen_elastic.cpp, mofem/tutorials/vec-3_nonlinear_dynamics/nonlinear_dynamic_elastic.cpp, mofem/tutorials/vec-4_shallow_wave/shallow_wave.cpp, mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp, and nonlinear_elastic.cpp.

Definition at line 536 of file dynamic_first_order_con_law.cpp.

536 {
538
540 auto bc_mng = mField.getInterface<BcManager>();
541 auto *pipeline_mng = mField.getInterface<PipelineManager>();
542 auto time_scale = boost::make_shared<TimeScale>();
543
544 PetscBool sin_time_function = PETSC_FALSE;
545 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-sin_time_function",
546 &sin_time_function, PETSC_NULLPTR);
547
548 if (sin_time_function)
549 time_scale = boost::make_shared<DynamicFirstOrderConsSinusTimeScale>();
550 else
551 time_scale = boost::make_shared<DynamicFirstOrderConsConstantTimeScale>();
552
553 pipeline_mng->getBoundaryExplicitRhsFE().reset();
555 pipeline_mng->getOpBoundaryExplicitRhsPipeline(), {NOSPACE}, "GEOMETRY");
556
558 pipeline_mng->getOpBoundaryExplicitRhsPipeline(), mField, "V",
559 {time_scale}, "FORCE", "PRESSURE", Sev::inform);
560
561 auto integration_rule = [](int, int, int approx_order) {
562 return 2 * approx_order;
563 };
564
565 CHKERR pipeline_mng->setBoundaryExplicitRhsIntegrationRule(integration_rule);
566 CHKERR pipeline_mng->setDomainExplicitRhsIntegrationRule(integration_rule);
567
568 CHKERR bc_mng->removeBlockDOFsOnEntities<DisplacementCubitBcData>(
569 simple->getProblemName(), "V");
570
571 auto get_pre_proc_hook = [&]() {
573 mField, pipeline_mng->getDomainExplicitRhsFE(), {time_scale});
574 };
575 pipeline_mng->getDomainExplicitRhsFE()->preProcessHook = get_pre_proc_hook();
576
578}
Class (Function) to enforce essential constrains.
Definition Essential.hpp:25

◆ boundaryCondition() [2/13]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ boundaryCondition() [3/13]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ boundaryCondition() [4/13]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ boundaryCondition() [5/13]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ boundaryCondition() [6/13]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ boundaryCondition() [7/13]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ boundaryCondition() [8/13]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ boundaryCondition() [9/13]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ boundaryCondition() [10/13]

MoFEMErrorCode Example::boundaryCondition ( )
private

Apply essential boundary conditions.

◆ boundaryCondition() [11/13]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ boundaryCondition() [12/13]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ boundaryCondition() [13/13]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ calculateFlux()

MoFEMErrorCode Example::calculateFlux ( double &  calc_flux)
private

[Set up problem]

[Calculate flux on boundary]

Examples
mofem/tutorials/mix-1_light_intensity_equation/phase.cpp.

Definition at line 402 of file phase.cpp.

402 {
404 auto pipeline_mng = mField.getInterface<PipelineManager>();
405
406 auto rule = [](int, int, int order) { return 2 * (order + 1); };
407 CHKERR pipeline_mng->setEvaluationIntegrationRule(rule);
408
409 auto op_loop_boundary = new OpLoopSide<EdgeEle>(
410 mField, simpleInterface->getBoundaryFEName(), 1, Sev::verbose);
411 op_loop_boundary->getSideFEPtr()->getRuleHook = rule;
412 auto &boundary_evaluation_pipeline = op_loop_boundary->getOpPtrVector();
413
414 CHKERR AddHOOps<1, 2, 2>::add(boundary_evaluation_pipeline, {HDIV});
415
416 auto flux_ptr = boost::make_shared<MatrixDouble>();
417 boundary_evaluation_pipeline.push_back(
418 new OpCalculateHVecVectorField<3>("S", flux_ptr));
419 boundary_evaluation_pipeline.push_back(new BoundaryOp(flux_ptr, calc_flux));
420 pipeline_mng->getOpEvaluationPipeline().push_back(op_loop_boundary);
421
422 calc_flux = 0;
423 CHKERR pipeline_mng->loopFiniteElementsEvaluation();
424 double global_flux_assembeld = 0;
425 MPI_Allreduce(&calc_flux, &global_flux_assembeld, 1, MPI_DOUBLE, MPI_SUM,
426 mField.get_comm());
427 calc_flux = global_flux_assembeld;
428
430}
@ HDIV
field with continuous normal traction
Definition definitions.h:87
virtual MPI_Comm & get_comm() const =0
Get vector field for H-div approximation.
Element used to execute operators on side of the element.
MoFEMErrorCode setEvaluationIntegrationRule(RuleHookFun rule)
Set integration rule for domain evaluation finite element.
const std::string getBoundaryFEName() const
Get the Boundary FE Name.
Definition Simple.hpp:436

◆ calculateGradient() [1/2]

MoFEMErrorCode Example::calculateGradient ( PetscReal *  objective_function_value,
Vec  objective_function_gradient,
Vec  adjoint_vector 
)
private

Calculate objective function gradient using adjoint method.

Examples
mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 1763 of file adjoint.cpp.

1765 {
1766 MOFEM_LOG_CHANNEL("WORLD");
1768 auto simple = mField.getInterface<Simple>();
1769
1770 auto ents = get_range_from_block(mField, "OPTIMISE", SPACE_DIM - 1);
1771
1772 auto get_essential_fe = [this]() {
1773 auto post_proc_rhs = boost::make_shared<FEMethod>();
1774 auto get_post_proc_hook_rhs = [this, post_proc_rhs]() {
1776
1778 post_proc_rhs, 0)();
1780 };
1781 post_proc_rhs->postProcessHook = get_post_proc_hook_rhs;
1782 return post_proc_rhs;
1783 };
1784
1785 auto get_fd_direvative_fe = [&]() {
1786 auto fe = boost::make_shared<DomainEle>(mField);
1787 fe->getRuleHook = [](int, int, int p_data) {
1788 return 2 * p_data + p_data - 1;
1789 };
1790 auto &pip = fe->getOpPtrVector();
1792 // Add RHS operators for internal forces
1793 HookeOps::opFactoryDomainRhs<SPACE_DIM, A, I, DomainEleOp>(
1794 mField, pip, "U", "MAT_ELASTIC", Sev::noisy);
1795
1796 return fe;
1797 };
1798
1799 auto calulate_fd_residual = [&](auto eps, auto diff_vec, auto fd_vec) {
1801
1802 constexpr bool debug = false;
1803
1804 auto geom_norm = [](MoFEM::Interface &mField) {
1806 auto field_blas = mField.getInterface<FieldBlas>();
1807 double nrm2 = 0.0;
1808 auto norm2_field = [&](const double val) {
1809 nrm2 += val * val;
1810 return val;
1811 };
1812 CHKERR field_blas->fieldLambdaOnValues(norm2_field, "GEOMETRY");
1813 MPI_Allreduce(MPI_IN_PLACE, &nrm2, 1, MPI_DOUBLE, MPI_SUM,
1814 mField.get_comm());
1815 MOFEM_LOG("WORLD", Sev::inform) << "Geometry norm: " << sqrt(nrm2);
1817 };
1818
1819 if constexpr (debug)
1820 CHKERR geom_norm(mField);
1821
1822 auto initial_current_geometry = createDMVector(adjointDM);
1823 CHKERR mField.getInterface<VecManager>()->setOtherLocalGhostVector(
1824 "ADJOINT", "ADJOINT_FIELD", "GEOMETRY", RowColData::ROW,
1825 initial_current_geometry, INSERT_VALUES, SCATTER_FORWARD);
1826 CHKERR VecAssemblyBegin(initial_current_geometry);
1827 CHKERR VecAssemblyEnd(initial_current_geometry);
1828
1829 if constexpr (debug)
1830 CHKERR geom_norm(mField);
1831
1832 auto perturb_geometry = [&](auto eps, auto diff_vec) {
1834 auto current_geometry = vectorDuplicate(initial_current_geometry);
1835 CHKERR VecCopy(initial_current_geometry, current_geometry);
1836 CHKERR VecAXPY(current_geometry, eps, diff_vec);
1837 CHKERR mField.getInterface<VecManager>()->setOtherLocalGhostVector(
1838 "ADJOINT", "ADJOINT_FIELD", "GEOMETRY", RowColData::ROW,
1839 current_geometry, INSERT_VALUES, SCATTER_REVERSE);
1841 };
1842
1843 auto fe = get_fd_direvative_fe();
1844 auto fp = vectorDuplicate(diff_vec);
1845 auto fm = vectorDuplicate(diff_vec);
1846 auto calc_impl = [&](auto f, auto eps) { // is this finite difference!
1848 CHKERR VecZeroEntries(f);
1849 fe->f = f;
1850 CHKERR perturb_geometry(eps, diff_vec);
1852 simple->getDomainFEName(), fe);
1853 CHKERR VecAssemblyBegin(f);
1854 CHKERR VecAssemblyEnd(f);
1855 CHKERR VecGhostUpdateBegin(f, ADD_VALUES, SCATTER_REVERSE);
1856 CHKERR VecGhostUpdateEnd(f, ADD_VALUES, SCATTER_REVERSE);
1857 auto post_proc_rhs = get_essential_fe();
1858 post_proc_rhs->f = f;
1860 post_proc_rhs.get());
1862 };
1863 CHKERR calc_impl(fp, eps);
1864 CHKERR calc_impl(fm, -eps);
1865 CHKERR VecWAXPY(fd_vec, -1.0, fm, fp);
1866 CHKERR VecScale(fd_vec, 1.0 / (2.0 * eps));
1867
1868 CHKERR mField.getInterface<VecManager>()->setOtherLocalGhostVector(
1869 "ADJOINT", "ADJOINT_FIELD", "GEOMETRY", RowColData::ROW,
1870 initial_current_geometry, INSERT_VALUES, SCATTER_REVERSE);
1871
1872 if constexpr (debug)
1873 CHKERR geom_norm(mField);
1874
1876 };
1877 // here starts the preperation for the derivative
1878 auto get_direvative_fe = [&](auto diff_vec) {
1879 auto fe_adjoint = boost::make_shared<DomainEle>(mField);
1880 fe_adjoint->getRuleHook = [](int, int, int p_data) {
1881 return 2 * p_data + p_data - 1;
1882 };
1883 auto &pip = fe_adjoint->getOpPtrVector();
1884
1885 auto jac_ptr = boost::make_shared<MatrixDouble>();
1886 auto det_ptr = boost::make_shared<VectorDouble>();
1887 auto inv_jac_ptr = boost::make_shared<MatrixDouble>();
1888 auto diff_jac_ptr = boost::make_shared<MatrixDouble>();
1889 auto cof_ptr = boost::make_shared<VectorDouble>();
1890
1891 using OpCoFactor =
1892 AdJoint<DomainEleOp>::Integration<GAUSS>::OpGetCoFactor<SPACE_DIM>;
1893
1895
1897 "GEOMETRY", jac_ptr));
1899 "U", diff_jac_ptr, diff_vec));
1900
1901 pip.push_back(new OpCoFactor(jac_ptr, diff_jac_ptr, cof_ptr));
1902
1903 // Add RHS operators for internal forces
1904 auto common_ptr = HookeOps::commonDataFactory<SPACE_DIM, I, DomainEleOp>(
1905 mField, pip, "U", "MAT_ELASTIC", Sev::noisy);
1907 "U", common_ptr, jac_ptr, diff_jac_ptr, cof_ptr));
1908
1909 return fe_adjoint;
1910 };
1911
1912 auto get_objective_fe = [&](auto diff_vec, auto grad_vec,
1913 auto glob_objective_ptr,
1914 auto glob_objective_grad_ptr) {
1915 auto fe_adjoint = boost::make_shared<DomainEle>(mField);
1916 fe_adjoint->getRuleHook = [](int, int, int p_data) {
1917 return 2 * p_data + p_data - 1;
1918 };
1919 auto &pip = fe_adjoint->getOpPtrVector();
1921
1922 auto jac_ptr = boost::make_shared<MatrixDouble>();
1923 auto det_ptr = boost::make_shared<VectorDouble>();
1924 auto inv_jac_ptr = boost::make_shared<MatrixDouble>();
1925 auto diff_jac_ptr = boost::make_shared<MatrixDouble>();
1926 auto cof_ptr = boost::make_shared<VectorDouble>();
1927 auto d_grad_ptr = boost::make_shared<MatrixDouble>();
1928 auto d_u_ptr = boost::make_shared<MatrixDouble>();
1929 auto u_ptr = boost::make_shared<MatrixDouble>();
1930
1931 using OpCoFactor =
1932 AdJoint<DomainEleOp>::Integration<GAUSS>::OpGetCoFactor<SPACE_DIM>;
1934 "GEOMETRY", jac_ptr));
1936 "U", diff_jac_ptr,
1937 diff_vec)); // Note: that vector is stored on displacemnt vector, that
1938 // why is used here
1939 pip.push_back(new OpCoFactor(jac_ptr, diff_jac_ptr, cof_ptr));
1941 "U", d_grad_ptr, grad_vec));
1942 pip.push_back(
1943 new OpCalculateVectorFieldValues<SPACE_DIM>("U", d_u_ptr, grad_vec));
1944 pip.push_back(new OpCalculateVectorFieldValues<SPACE_DIM>("U", u_ptr));
1945
1946 auto common_ptr = HookeOps::commonDataFactory<SPACE_DIM, I, DomainEleOp>(
1947 mField, pip, "U", "MAT_ELASTIC", Sev::noisy);
1948 pip.push_back(new OpAdJointObjective(
1949 pythonPtr, common_ptr, jac_ptr, diff_jac_ptr, cof_ptr, d_grad_ptr,
1950 d_u_ptr, u_ptr, glob_objective_ptr, glob_objective_grad_ptr));
1951
1952 return fe_adjoint;
1953 };
1954
1955 auto dm = simple->getDM();
1956 auto f = createDMVector(dm);
1957 auto d = vectorDuplicate(f);
1958 auto dm_diff_vec = vectorDuplicate(d);
1959 auto zero_diff_vec = vectorDuplicate(dm_diff_vec);
1960 auto zero_state_sensitivity = vectorDuplicate(d);
1961 CHKERR VecZeroEntries(zero_diff_vec);
1962 CHKERR VecZeroEntries(zero_state_sensitivity);
1963 CHKERR VecGhostUpdateBegin(zero_diff_vec, INSERT_VALUES, SCATTER_FORWARD);
1964 CHKERR VecGhostUpdateEnd(zero_diff_vec, INSERT_VALUES, SCATTER_FORWARD);
1965 CHKERR VecGhostUpdateBegin(zero_state_sensitivity, INSERT_VALUES,
1966 SCATTER_FORWARD);
1967 CHKERR VecGhostUpdateEnd(zero_state_sensitivity, INSERT_VALUES,
1968 SCATTER_FORWARD);
1969
1970 auto adjoint_fe = get_direvative_fe(dm_diff_vec);
1971 auto objective_ptr_direct = boost::make_shared<double>(0.0);
1972 auto objective_grad_ptr_direct = boost::make_shared<double>(0.0);
1973 auto objective_fe_direct = get_objective_fe(
1974 dm_diff_vec, d, objective_ptr_direct, objective_grad_ptr_direct);
1975 auto objective_ptr_explicit = boost::make_shared<double>(0.0);
1976 auto objective_grad_ptr_explicit = boost::make_shared<double>(0.0);
1977 auto objective_fe_explicit =
1978 get_objective_fe(dm_diff_vec, zero_state_sensitivity,
1979 objective_ptr_explicit, objective_grad_ptr_explicit);
1980 auto objective_ptr_value = boost::make_shared<double>(0.0);
1981 auto objective_grad_ptr_value = boost::make_shared<double>(0.0);
1982 auto objective_fe_value =
1983 get_objective_fe(zero_diff_vec, zero_state_sensitivity,
1984 objective_ptr_value, objective_grad_ptr_value);
1985
1986 auto set_variance_of_geometry =
1987 [&](auto mode, auto mod_vec) { // think of mod_vec as X(tau) = X + tau*v_h
1988 // take the mod_vec and write it into the adjoint DM then copy that
1989 // field to dm_diff_vec using the same layout as U
1991 CHKERR DMoFEMMeshToLocalVector(adjointDM, mod_vec, INSERT_VALUES,
1992 SCATTER_REVERSE);
1993 CHKERR mField.getInterface<VecManager>()->setOtherLocalGhostVector(
1994 simple->getProblemName(), "U", "ADJOINT_FIELD", RowColData::ROW,
1995 dm_diff_vec, INSERT_VALUES, SCATTER_FORWARD);
1996 CHKERR VecGhostUpdateBegin(dm_diff_vec, INSERT_VALUES, SCATTER_FORWARD);
1997 CHKERR VecGhostUpdateEnd(dm_diff_vec, INSERT_VALUES, SCATTER_FORWARD);
1999 };
2000
2001 auto calculate_variance_internal_forces = [&](auto mode, auto mod_vec) {
2003 CHKERR VecZeroEntries(f);
2004 CHKERR VecGhostUpdateBegin(f, INSERT_VALUES, SCATTER_FORWARD);
2005 CHKERR VecGhostUpdateEnd(f, INSERT_VALUES, SCATTER_FORWARD);
2006 adjoint_fe->f = f;
2008 CHKERR VecAssemblyBegin(f);
2009 CHKERR VecAssemblyEnd(f);
2010 CHKERR VecGhostUpdateBegin(f, ADD_VALUES, SCATTER_REVERSE);
2011 CHKERR VecGhostUpdateEnd(f, ADD_VALUES, SCATTER_REVERSE);
2012 auto post_proc_rhs = get_essential_fe();
2013 post_proc_rhs->f = f;
2015 post_proc_rhs.get());
2016 CHKERR VecScale(f, -1.0);
2017
2018#ifndef NDEBUG
2019 constexpr bool debug = true;
2020 if constexpr (debug) {
2021 double norm0;
2022 CHKERR VecNorm(f, NORM_2, &norm0);
2023 auto fd_check = vectorDuplicate(f);
2024 double eps = 1e-5;
2025 CHKERR calulate_fd_residual(eps, dm_diff_vec, fd_check);
2026 double nrm;
2027 CHKERR VecAXPY(fd_check, -1.0, f);
2028 CHKERR VecNorm(fd_check, NORM_2, &nrm);
2029 MOFEM_LOG("WORLD", Sev::inform)
2030 << " FD check for internal forces [ " << mode << " ]: " << nrm
2031 << " / " << norm0 << " ( " << (nrm / norm0) << " )";
2032 }
2033#endif
2034
2036 };
2037
2038 auto calculate_variance_of_displacement = [&](auto mode, auto mod_vec) {
2040 CHKERR KSPSolve(kspElastic, f, d);
2041
2042 CHKERR VecGhostUpdateBegin(d, INSERT_VALUES, SCATTER_FORWARD);
2043 CHKERR VecGhostUpdateEnd(d, INSERT_VALUES, SCATTER_FORWARD);
2045 };
2046
2047 auto evaluate_objective_terms =
2048 [&](auto objective_fe, auto objective_ptr, auto objective_grad_ptr,
2049 double &objective_value, double &objective_gradient) {
2051 *objective_ptr = 0.0;
2052 *objective_grad_ptr = 0.0;
2054 objective_fe);
2055 std::array<double, 2> array = {*objective_ptr, *objective_grad_ptr};
2056 MPI_Allreduce(MPI_IN_PLACE, array.data(), 2, MPI_DOUBLE, MPI_SUM,
2057 mField.get_comm());
2058 objective_value = array[0];
2059 objective_gradient = array[1];
2061 };
2062
2063 auto calculate_objective_value = [&]() {
2065 double objective_value = 0;
2066 double objective_gradient = 0;
2067 CHKERR evaluate_objective_terms(objective_fe_value, objective_ptr_value,
2068 objective_grad_ptr_value, objective_value,
2069 objective_gradient);
2070 *objective_function_value = objective_value;
2072 };
2073
2074 auto calculate_variance_of_objective_function_dJ_du = [&](Vec dJ_du) {
2076
2077 auto fe = boost::make_shared<DomainEle>(mField);
2078 fe->getRuleHook = [](int, int, int p_data) {
2079 return 2 * p_data + p_data - 1;
2080 };
2081 auto &pip = fe->getOpPtrVector();
2083
2084 auto u_ptr = boost::make_shared<MatrixDouble>();
2085 pip.push_back(new OpCalculateVectorFieldValues<SPACE_DIM>("U", u_ptr));
2086
2087 auto common_ptr = HookeOps::commonDataFactory<SPACE_DIM, I, DomainEleOp>(
2088 mField, pip, "U", "MAT_ELASTIC", Sev::noisy);
2089 pip.push_back(new OpStateSensitivity("U", pythonPtr, common_ptr, u_ptr));
2090
2091 CHKERR VecZeroEntries(dJ_du);
2092 fe->f = dJ_du;
2094 CHKERR VecAssemblyBegin(dJ_du);
2095 CHKERR VecAssemblyEnd(dJ_du);
2096
2097 auto post_proc_rhs = get_essential_fe();
2098 post_proc_rhs->f = dJ_du;
2100 post_proc_rhs.get());
2101
2103 };
2104
2105 auto calculate_adjoint_lambda = [&](auto lambda, auto dJ_du) {
2107
2108 MOFEM_LOG("WORLD", Sev::inform) << "Solving for adjoint variable lambda";
2109 CHKERR VecZeroEntries(lambda);
2110 CHKERR KSPSolveTranspose(kspElastic, dJ_du, lambda);
2111 CHKERR VecGhostUpdateBegin(lambda, INSERT_VALUES, SCATTER_FORWARD);
2112 CHKERR VecGhostUpdateEnd(lambda, INSERT_VALUES, SCATTER_FORWARD);
2114 };
2115
2116 CHKERR VecZeroEntries(objective_function_gradient);
2117 CHKERR VecZeroEntries(adjoint_vector);
2118
2119 CHKERR calculate_objective_value();
2120 MOFEM_LOG("WORLD", Sev::verbose)
2121 << "Objective function: " << *objective_function_value;
2122
2123 auto direct = [&]() {
2125 int mode = 0;
2126 for (auto mod_vec : modeVecs) {
2127 CHKERR set_variance_of_geometry(mode, mod_vec);
2128 CHKERR calculate_variance_internal_forces(mode, mod_vec);
2129 CHKERR calculate_variance_of_displacement(mode, mod_vec);
2130 double objective_value = 0;
2131 double objective_gradient = 0;
2132 CHKERR evaluate_objective_terms(objective_fe_direct, objective_ptr_direct,
2133 objective_grad_ptr_direct,
2134 objective_value, objective_gradient);
2135 CHKERR VecSetValue(objective_function_gradient, mode, objective_gradient,
2136 INSERT_VALUES);
2137 CHKERR VecAXPY(adjoint_vector, objective_gradient, dm_diff_vec);
2138 ++mode;
2139 }
2141 };
2142
2143 auto lambda = vectorDuplicate(f);
2144 auto dJ_du = vectorDuplicate(f);
2145
2146 auto adjoint = [&]() {
2148
2149 CHKERR calculate_variance_of_objective_function_dJ_du(dJ_du);
2150 CHKERR calculate_adjoint_lambda(lambda, dJ_du);
2151
2152 int mode = 0;
2153
2154 for (auto mod_vec : modeVecs) {
2155
2156 CHKERR set_variance_of_geometry(mode, mod_vec);
2157 CHKERR calculate_variance_internal_forces(mode, mod_vec);
2158 double objective_value = 0;
2159 double objective_gradient_explicit = 0;
2160 CHKERR evaluate_objective_terms(
2161 objective_fe_explicit, objective_ptr_explicit,
2162 objective_grad_ptr_explicit, objective_value,
2163 objective_gradient_explicit);
2164 double lambda_dot_residual_variation = 0;
2165 CHKERR VecDot(lambda, f, &lambda_dot_residual_variation);
2166 const double dJ_dp =
2167 objective_gradient_explicit + lambda_dot_residual_variation;
2168 CHKERR VecSetValue(objective_function_gradient, mode, dJ_dp,
2169 INSERT_VALUES);
2170 CHKERR VecAXPY(adjoint_vector, dJ_dp, dm_diff_vec);
2171 ++mode;
2172 }
2173 CHKERR VecAssemblyBegin(objective_function_gradient);
2174 CHKERR VecAssemblyEnd(objective_function_gradient);
2176 };
2177
2178 switch (derivative_type) {
2179 case DIRECT:
2180 MOFEM_LOG("WORLD", Sev::inform) << "Running Direct Sensitivity...";
2181 CHKERR direct();
2182 break;
2183
2184 case ADJOINT:
2185 MOFEM_LOG("WORLD", Sev::inform) << "Running Adjoint Sensitivity...";
2186 CHKERR adjoint();
2187 break;
2188
2189 default:
2190 SETERRQ(PETSC_COMM_WORLD, MOFEM_DATA_INCONSISTENCY,
2191 "Wrong sensitivity type selected");
2192 }
2193
2194 CHKERR VecAssemblyBegin(objective_function_gradient);
2195 CHKERR VecAssemblyEnd(objective_function_gradient);
2196
2197 CHKERR VecAssemblyBegin(adjoint_vector);
2198 CHKERR VecAssemblyEnd(adjoint_vector);
2199 CHKERR VecGhostUpdateBegin(adjoint_vector, INSERT_VALUES, SCATTER_FORWARD);
2200 CHKERR VecGhostUpdateEnd(adjoint_vector, INSERT_VALUES, SCATTER_FORWARD);
2201
2203}
SensitivityMethod derivative_type
Definition adjoint.cpp:109
@ DIRECT
Definition adjoint.cpp:107
@ ADJOINT
Definition adjoint.cpp:107
Range get_range_from_block(MoFEM::Interface &m_field, const std::string block_name, int dim)
Definition adjoint.cpp:2291
@ ROW
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
@ MOFEM_DATA_INCONSISTENCY
Definition definitions.h:31
#define MoFEMFunctionBeginHot
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
PetscErrorCode DMoFEMPostProcessFiniteElements(DM dm, MoFEM::FEMethod *method)
execute finite element method for each element in dm (problem)
Definition DMMoFEM.cpp:546
PetscErrorCode DMoFEMMeshToLocalVector(DM dm, Vec l, InsertMode mode, ScatterMode scatter_mode, RowColData rc=RowColData::COL)
set local (or ghosted) vector values on mesh for partition only
Definition DMMoFEM.cpp:514
PetscErrorCode DMoFEMLoopFiniteElements(DM dm, const char fe_name[], MoFEM::FEMethod *method, CacheTupleWeakPtr cache_ptr=CacheTupleSharedPtr())
Executes FEMethod for finite elements in DM.
Definition DMMoFEM.cpp:576
auto createDMVector(DM dm, RowColData rc=RowColData::COL)
Get smart vector from DM.
Definition DMMoFEM.hpp:1237
@ GAUSS
Gaussian quadrature integration.
#define MOFEM_LOG_CHANNEL(channel)
Set and reset channel.
static double lambda
const FTensor::Tensor2< T, Dim, Dim > Vec
static const bool debug
SmartPetscObj< Vec > vectorDuplicate(Vec vec)
Create duplicate vector of smart vector.
boost::shared_ptr< ObjectiveFunctionData > pythonPtr
Interface to Python objective function.
Definition adjoint.cpp:170
std::vector< SmartPetscObj< Vec > > modeVecs
Topology mode vectors (design variables)
Definition adjoint.cpp:174
SmartPetscObj< DM > adjointDM
Data manager for adjoint problem.
Definition adjoint.cpp:168
SmartPetscObj< KSP > kspElastic
Linear solver for elastic problem.
Definition adjoint.cpp:167
SmartPetscObj< EPS > eps
Deprecated interface functions.
Class (Function) to enforce essential constrains on the right hand side diagonal.
Definition Essential.hpp:41
Basic algebra on fields.
Definition FieldBlas.hpp:21
MoFEMErrorCode fieldLambdaOnValues(OneFieldFunctionOnValues lambda, const std::string field_name, Range *ents_ptr=nullptr)
field lambda
Definition FieldBlas.cpp:21
Specialization for MatrixDouble vector field values calculation.
MoFEMErrorCode getDM(DM *dm)
Get DM.
Definition Simple.cpp:799
const std::string getDomainFEName() const
Get the Domain FE Name.
Definition Simple.hpp:429
Vector manager is used to create vectors \mofem_vectors.
Forward declaration of operator for gradient times symmetric tensor operations.
Definition adjoint.cpp:105

◆ calculateGradient() [2/2]

MoFEMErrorCode Example::calculateGradient ( PetscReal *  objective_function_value,
Vec  objective_function_gradient,
Vec  adjoint_vector,
Vec  dJ_du 
)
private

Calculate objective function gradient using adjoint method.

Definition at line 1057 of file gradient.cpp.

1059 {
1060 MOFEM_LOG_CHANNEL("WORLD");
1062 auto simple = mField.getInterface<Simple>();
1063 auto dm = simple->getDM();
1064
1065 auto get_essential_fe = [this]() {
1066 auto post_proc_rhs = boost::make_shared<FEMethod>();
1067 auto get_post_proc_hook_rhs = [this, post_proc_rhs]() {
1070 post_proc_rhs, 0)();
1072 };
1073 post_proc_rhs->postProcessHook = get_post_proc_hook_rhs;
1074 return post_proc_rhs;
1075 };
1076
1077 auto get_objective_fe = [&](auto lambda_vec, auto glob_objective_ptr,
1078 auto fe_rule) {
1079 auto fe_obj = boost::make_shared<DomainEle>(mField);
1080 fe_obj->getRuleHook = fe_rule;
1081 auto &pip = fe_obj->getOpPtrVector();
1083
1084 auto jac_ptr = boost::make_shared<MatrixDouble>();
1085 auto u_ptr = boost::make_shared<MatrixDouble>();
1086 auto grad_lambda_ptr = boost::make_shared<MatrixDouble>();
1087
1089 "GEOMETRY", jac_ptr));
1090 pip.push_back(new OpCalculateVectorFieldValues<SPACE_DIM>("U", u_ptr));
1091 auto lambda_smart_vec = SmartPetscObj<Vec>(lambda_vec, true);
1093 "U", grad_lambda_ptr, lambda_smart_vec));
1094
1095 auto common_ptr = HookeOps::commonDataFactory<SPACE_DIM, I, DomainEleOp>(
1096 mField, pip, "U", "MAT_ELASTIC", Sev::noisy);
1097 pip.push_back(new OpAdJointObjective(pythonPtr, common_ptr, jac_ptr, u_ptr,
1098 grad_lambda_ptr,
1099 glob_objective_ptr));
1100 return fe_obj;
1101 };
1102
1103 auto evaluate_objective_terms =
1104 [&](auto objective_fe, auto objective_ptr) {
1106 *objective_ptr = 0.0;
1107 CHKERR VecZeroEntries(objective_function_gradient);
1108 CHKERR VecGhostUpdateBegin(lambda, INSERT_VALUES, SCATTER_FORWARD);
1109 CHKERR VecGhostUpdateEnd(lambda, INSERT_VALUES, SCATTER_FORWARD);
1110 objective_fe->f = objective_function_gradient;
1111 CHKERR DMoFEMLoopFiniteElements(adjointDM, "ADJOINT_DOMAIN_FE",
1112 objective_fe);
1113 MPI_Allreduce(MPI_IN_PLACE, objective_ptr.get(), 1, MPI_DOUBLE, MPI_SUM,
1114 mField.get_comm());
1115 CHKERR VecAssemblyBegin(objective_function_gradient);
1116 CHKERR VecAssemblyEnd(objective_function_gradient);
1117 CHKERR VecGhostUpdateBegin(objective_function_gradient, ADD_VALUES,
1118 SCATTER_REVERSE);
1119 CHKERR VecGhostUpdateEnd(objective_function_gradient, ADD_VALUES,
1120 SCATTER_REVERSE);
1122 };
1123
1124 auto calculate_variance_of_objective_function_dJ_du = [&]() {
1126 auto fe = boost::make_shared<DomainEle>(mField);
1127 fe->getRuleHook = [](int, int, int p_data) {
1128 return 2 * p_data + p_data - 1;
1129 };
1130 auto &pip = fe->getOpPtrVector();
1132 auto u_ptr = boost::make_shared<MatrixDouble>();
1133 pip.push_back(new OpCalculateVectorFieldValues<SPACE_DIM>("U", u_ptr));
1134 auto common_ptr = HookeOps::commonDataFactory<SPACE_DIM, I, DomainEleOp>(
1135 mField, pip, "U", "MAT_ELASTIC", Sev::noisy);
1136 pip.push_back(new OpStateSensitivity("U", pythonPtr, common_ptr, u_ptr));
1137 CHKERR VecZeroEntries(dJ_du);
1138 fe->f = dJ_du;
1140 CHKERR VecAssemblyBegin(dJ_du);
1141 CHKERR VecAssemblyEnd(dJ_du);
1142 auto post_proc_rhs = get_essential_fe();
1143 post_proc_rhs->f = dJ_du;
1145 post_proc_rhs.get());
1147 };
1148
1149 auto calculate_adjoint_lambda = [&]() {
1151
1152 MOFEM_LOG("WORLD", Sev::inform) << "Solving for adjoint variable lambda";
1153 CHKERR VecZeroEntries(lambda);
1154 CHKERR KSPSolveTranspose(kspElastic, dJ_du, lambda);
1155 CHKERR VecGhostUpdateBegin(lambda, INSERT_VALUES, SCATTER_FORWARD);
1156 CHKERR VecGhostUpdateEnd(lambda, INSERT_VALUES, SCATTER_FORWARD);
1158 };
1159
1160 auto fe_rule = [](int, int, int p_data) { return 2 * p_data + p_data - 1; };
1161 auto objective_ptr_value = boost::make_shared<double>(0.0);
1162 auto objective_fe = get_objective_fe(lambda, objective_ptr_value, fe_rule);
1163
1164 auto adjoint = [&]() {
1166
1167 CHKERR calculate_variance_of_objective_function_dJ_du();
1168 CHKERR calculate_adjoint_lambda();
1169 CHKERR evaluate_objective_terms(objective_fe, objective_ptr_value);
1170 *objective_function_value = *objective_ptr_value;
1171
1172 MOFEM_LOG("WORLD", Sev::verbose)
1173 << "Objective function: " << *objective_function_value;
1174
1175 CHKERR VecAssemblyBegin(objective_function_gradient);
1176 CHKERR VecAssemblyEnd(objective_function_gradient);
1177 CHKERR VecGhostUpdateBegin(lambda, INSERT_VALUES, SCATTER_FORWARD);
1178 CHKERR VecGhostUpdateEnd(lambda, INSERT_VALUES, SCATTER_FORWARD);
1179
1181 };
1182
1183 switch (derivative_type) {
1184
1185 case ADJOINT:
1186 MOFEM_LOG("WORLD", Sev::inform) << "Running Adjoint Sensitivity...";
1187 CHKERR adjoint();
1188 break;
1189
1190 default:
1191 SETERRQ(PETSC_COMM_WORLD, MOFEM_DATA_INCONSISTENCY,
1192 "Wrong sensitivity type selected");
1193 }
1194
1195 CHKERR VecAssemblyBegin(objective_function_gradient);
1196 CHKERR VecAssemblyEnd(objective_function_gradient);
1197
1199}
intrusive_ptr for managing petsc objects

◆ checkResults() [1/13]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [2/13]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [3/13]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [4/13]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [5/13]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [6/13]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [7/13]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [8/13]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [9/13]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [10/13]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [11/13]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [12/13]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [13/13]

MoFEMErrorCode Example::checkResults ( )
private

◆ createCommonData() [1/10]

MoFEMErrorCode Example::createCommonData ( )
private

[Set up problem]

[Create Example_struct]

[Set integration rule]

[Create common data]

< true if tau order is set

< true if tau order is set

Examples
mofem/tutorials/adv-0_plasticity/plastic.cpp, mofem/tutorials/fun-1_integration/integration.cpp, mofem/tutorials/fun-2_plot_base/plot_base.cpp, mofem/tutorials/scl-0_least_squares/approximation.cpp, mofem/tutorials/scl-8_heat_radiation/radiation.cpp, mofem/tutorials/scl-9_heat_method/heat_method.cpp, mofem/tutorials/vec-1_eigen_elasticity/eigen_elastic.cpp, mofem/tutorials/vec-4_shallow_wave/shallow_wave.cpp, plastic.cpp, and thermoplastic.cpp.

Definition at line 478 of file plastic.cpp.

478 {
480
481 auto get_command_line_parameters = [&]() {
483
484 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-scale", &scale, PETSC_NULLPTR);
485 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-young_modulus",
486 &young_modulus, PETSC_NULLPTR);
487 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-poisson_ratio",
488 &poisson_ratio, PETSC_NULLPTR);
489 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-hardening", &H, PETSC_NULLPTR);
490 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-hardening_viscous", &visH,
491 PETSC_NULLPTR);
492 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-yield_stress", &sigmaY,
493 PETSC_NULLPTR);
494 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-cn0", &cn0, PETSC_NULLPTR);
495 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-cn1", &cn1, PETSC_NULLPTR);
496 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-zeta", &zeta, PETSC_NULLPTR);
497 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-Qinf", &Qinf, PETSC_NULLPTR);
498 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-b_iso", &b_iso, PETSC_NULLPTR);
499 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-C1_k", &C1_k, PETSC_NULLPTR);
500 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-large_strains",
501 &is_large_strains, PETSC_NULLPTR);
502 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-set_timer", &set_timer,
503 PETSC_NULLPTR);
504 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-atom_test", &atom_test,
505 PETSC_NULLPTR);
506
507 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-order", &order, PETSC_NULLPTR);
508 PetscBool tau_order_is_set; ///< true if tau order is set
509 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-tau_order", &tau_order,
510 &tau_order_is_set);
511 PetscBool ep_order_is_set; ///< true if tau order is set
512 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-ep_order", &ep_order,
513 &ep_order_is_set);
514 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-geom_order", &geom_order,
515 PETSC_NULLPTR);
516
517 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-rho", &rho, PETSC_NULLPTR);
518 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-alpha_damping",
519 &alpha_damping, PETSC_NULLPTR);
520
521 MOFEM_LOG("PLASTICITY", Sev::inform) << "Young modulus " << young_modulus;
522 MOFEM_LOG("PLASTICITY", Sev::inform) << "Poisson ratio " << poisson_ratio;
523 MOFEM_LOG("PLASTICITY", Sev::inform) << "Yield stress " << sigmaY;
524 MOFEM_LOG("PLASTICITY", Sev::inform) << "Hardening " << H;
525 MOFEM_LOG("PLASTICITY", Sev::inform) << "Viscous hardening " << visH;
526 MOFEM_LOG("PLASTICITY", Sev::inform) << "Saturation yield stress " << Qinf;
527 MOFEM_LOG("PLASTICITY", Sev::inform) << "Saturation exponent " << b_iso;
528 MOFEM_LOG("PLASTICITY", Sev::inform) << "Kinematic hardening " << C1_k;
529 MOFEM_LOG("PLASTICITY", Sev::inform) << "cn0 " << cn0;
530 MOFEM_LOG("PLASTICITY", Sev::inform) << "cn1 " << cn1;
531 MOFEM_LOG("PLASTICITY", Sev::inform) << "zeta " << zeta;
532
533 if (tau_order_is_set == PETSC_FALSE)
534 tau_order = order - 2;
535 if (ep_order_is_set == PETSC_FALSE)
536 ep_order = order - 1;
537
538 MOFEM_LOG("PLASTICITY", Sev::inform) << "Approximation order " << order;
539 MOFEM_LOG("PLASTICITY", Sev::inform)
540 << "Ep approximation order " << ep_order;
541 MOFEM_LOG("PLASTICITY", Sev::inform)
542 << "Tau approximation order " << tau_order;
543 MOFEM_LOG("PLASTICITY", Sev::inform)
544 << "Geometry approximation order " << geom_order;
545
546 MOFEM_LOG("PLASTICITY", Sev::inform) << "Density " << rho;
547 MOFEM_LOG("PLASTICITY", Sev::inform) << "alpha_damping " << alpha_damping;
548
549 PetscBool is_scale = PETSC_TRUE;
550 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-is_scale", &is_scale,
551 PETSC_NULLPTR);
552 if (is_scale) {
554 }
555
556 MOFEM_LOG("PLASTICITY", Sev::inform) << "Scale " << scale;
557
558#ifdef ADD_CONTACT
559 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-cn_contact",
560 &ContactOps::cn_contact, PETSC_NULLPTR);
561 MOFEM_LOG("CONTACT", Sev::inform)
562 << "cn_contact " << ContactOps::cn_contact;
563#endif // ADD_CONTACT
564
565 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-quasi_static",
566 &is_quasi_static, PETSC_NULLPTR);
567 MOFEM_LOG("PLASTICITY", Sev::inform)
568 << "Is quasi static: " << (is_quasi_static ? "true" : "false");
569
571 };
572
573 CHKERR get_command_line_parameters();
574
575#ifdef ADD_CONTACT
576 #ifdef ENABLE_PYTHON_BINDING
577 auto file_exists = [](std::string myfile) {
578 std::ifstream file(myfile.c_str());
579 if (file) {
580 return true;
581 }
582 return false;
583 };
584 char sdf_file_name[255] = "sdf.py";
585 CHKERR PetscOptionsGetString(PETSC_NULLPTR, PETSC_NULLPTR, "-sdf_file",
586 sdf_file_name, 255, PETSC_NULLPTR);
587
588 if (file_exists(sdf_file_name)) {
589 MOFEM_LOG("CONTACT", Sev::inform) << sdf_file_name << " file found";
590 sdfPythonPtr = boost::make_shared<ContactOps::SDFPython>();
591 CHKERR sdfPythonPtr->sdfInit(sdf_file_name);
592 ContactOps::sdfPythonWeakPtr = sdfPythonPtr;
593 } else {
594 MOFEM_LOG("CONTACT", Sev::warning) << sdf_file_name << " file NOT found";
595 }
596 #endif
597#endif // ADD_CONTACT
598
600}
double cn_contact
Definition contact.cpp:97
PetscErrorCode PetscOptionsGetInt(PetscOptions *, const char pre[], const char name[], PetscInt *ivalue, PetscBool *set)
PetscErrorCode PetscOptionsGetScalar(PetscOptions *, const char pre[], const char name[], PetscScalar *dval, PetscBool *set)
PetscErrorCode PetscOptionsGetString(PetscOptions *, const char pre[], const char name[], char str[], size_t size, PetscBool *set)
SmartPetscObj< Mat > H
double young_modulus
Young modulus.
Definition plastic.cpp:125
double C1_k
Kinematic hardening.
Definition plastic.cpp:133
double Qinf
Saturation yield stress.
Definition plastic.cpp:131
double rho
Definition plastic.cpp:144
int atom_test
Atom test.
Definition plastic.cpp:121
PetscBool is_quasi_static
Definition plastic.cpp:143
double alpha_damping
Definition plastic.cpp:145
double visH
Viscous hardening.
Definition plastic.cpp:129
double poisson_ratio
Poisson ratio.
Definition plastic.cpp:126
PetscBool set_timer
Set timer.
Definition plastic.cpp:118
double scale
Definition plastic.cpp:123
double zeta
Viscous hardening.
Definition plastic.cpp:130
int tau_order
Order of tau files.
Definition plastic.cpp:139
double cn0
Definition plastic.cpp:135
double b_iso
Saturation exponent.
Definition plastic.cpp:132
PetscBool is_large_strains
Large strains.
Definition plastic.cpp:117
int geom_order
Order if fixed.
Definition plastic.cpp:141
double sigmaY
Yield stress.
Definition plastic.cpp:127
int ep_order
Order of ep files.
Definition plastic.cpp:140
double cn1
Definition plastic.cpp:136

◆ createCommonData() [2/10]

MoFEMErrorCode Example::createCommonData ( )
private

◆ createCommonData() [3/10]

MoFEMErrorCode Example::createCommonData ( )
private

◆ createCommonData() [4/10]

MoFEMErrorCode Example::createCommonData ( )
private

◆ createCommonData() [5/10]

MoFEMErrorCode Example::createCommonData ( )
private

◆ createCommonData() [6/10]

MoFEMErrorCode Example::createCommonData ( )
private

◆ createCommonData() [7/10]

MoFEMErrorCode Example::createCommonData ( )
private

◆ createCommonData() [8/10]

MoFEMErrorCode Example::createCommonData ( )
private

◆ createCommonData() [9/10]

MoFEMErrorCode Example::createCommonData ( )
private

◆ createCommonData() [10/10]

MoFEMErrorCode Example::createCommonData ( )
private

◆ createCommonThermoPlasticOps()

template<int DIM, IntegrationType I, typename DomainEleOp >
auto Example::createCommonThermoPlasticOps ( MoFEM::Interface &  m_field,
std::string  plastic_block_name,
std::string  thermal_block_name,
std::string  thermoelastic_block_name,
std::string  thermoplastic_block_name,
boost::ptr_deque< ForcesAndSourcesCore::UserDataOperator > &  pip,
std::string  u,
std::string  ep,
std::string  tau,
std::string  temperature,
double  scale,
ScalerFunTwoArgs  thermal_conductivity_scaling,
ScalerFunTwoArgs  heat_capacity_scaling,
ScalerFunThreeArgs  inelastic_heat_fraction_scaling,
Sev  sev,
bool  with_rates = true 
)
inlineprivate
Examples
thermoplastic.cpp.

Definition at line 1164 of file thermoplastic.cpp.

1173 {
1174
1176
1177 auto common_plastic_ptr = boost::make_shared<PlasticOps::CommonData>();
1178 auto common_thermal_ptr =
1179 boost::make_shared<ThermoElasticOps::BlockedThermalParameters>();
1180 auto common_thermoelastic_ptr =
1181 boost::make_shared<ThermoElasticOps::BlockedThermoElasticParameters>();
1182 auto common_thermoplastic_ptr =
1183 boost::make_shared<ThermoPlasticOps::ThermoPlasticBlockedParameters>();
1184
1185 constexpr auto size_symm = (DIM * (DIM + 1)) / 2;
1186 auto make_d_mat = []() {
1187 return boost::make_shared<MatrixDouble>(size_symm * size_symm, 1);
1188 };
1189
1190 common_plastic_ptr->mDPtr = boost::make_shared<MatrixDouble>();
1191 common_plastic_ptr->mGradPtr = boost::make_shared<MatrixDouble>();
1192 common_plastic_ptr->mStrainPtr = boost::make_shared<MatrixDouble>();
1193 common_plastic_ptr->mStressPtr = boost::make_shared<MatrixDouble>();
1194
1195 auto m_D_ptr = common_plastic_ptr->mDPtr;
1196
1197 CHK_THROW_MESSAGE(PlasticOps::addMatBlockOps<DIM>(
1198 m_field, plastic_block_name, pip, m_D_ptr,
1199 common_plastic_ptr->getParamsPtr(), scale, sev),
1200 "add mat block plastic ops");
1202 m_field, pip, thermal_block_name, common_thermal_ptr,
1207 "add mat block thermal ops");
1209 m_field, pip, thermoelastic_block_name,
1210 common_thermoelastic_ptr, default_coeff_expansion,
1211 default_ref_temp, sev),
1212 "add mat block thermal ops");
1214 m_field, pip, thermoplastic_block_name,
1215 common_thermoplastic_ptr, common_thermal_ptr, sev,
1217 "add mat block thermoplastic ops");
1218 auto common_hencky_ptr =
1219 HenckyOps::commonDataFactory<SPACE_DIM, IT, DomainEleOp>(
1220 mField, pip, "U", "MAT_ELASTIC", Sev::inform, scale);
1221
1222 common_plastic_ptr->mDPtr = common_hencky_ptr->matDPtr;
1223
1224 pip.push_back(new OpCalculateScalarFieldValues(
1225 tau, common_plastic_ptr->getPlasticTauPtr()));
1227 ep, common_plastic_ptr->getPlasticStrainPtr()));
1229 u, common_plastic_ptr->mGradPtr));
1230
1231 pip.push_back(new OpCalculateScalarFieldValues(
1232 temperature, common_thermoplastic_ptr->getTempPtr()));
1234 "FLUX", common_thermoplastic_ptr->getHeatFluxPtr()));
1235
1236 common_plastic_ptr->mGradPtr = common_hencky_ptr->matGradPtr;
1237 common_plastic_ptr->mDPtr = common_hencky_ptr->matDPtr;
1238 common_hencky_ptr->matLogCPlastic =
1239 common_plastic_ptr->getPlasticStrainPtr();
1240 common_plastic_ptr->mStrainPtr = common_hencky_ptr->getMatLogC();
1241 common_plastic_ptr->mStressPtr = common_hencky_ptr->getMatHenckyStress();
1242
1244
1245 pip.push_back(new typename H::template OpCalculateEigenVals<DIM, I>(
1246 u, common_hencky_ptr));
1247 pip.push_back(
1248 new typename H::template OpCalculateLogC<DIM, I>(u, common_hencky_ptr));
1249 pip.push_back(new typename H::template OpCalculateLogC_dC<DIM, I>(
1250 u, common_hencky_ptr));
1251
1252 pip.push_back(
1253 new
1254 typename H::template OpCalculateHenckyThermoPlasticStress<DIM, I, 0>(
1255 u, common_thermoplastic_ptr->getTempPtr(), common_hencky_ptr,
1256 common_thermoelastic_ptr->getCoeffExpansionPtr(),
1257 common_thermoelastic_ptr->getRefTempPtr()));
1258 pip.push_back(new typename H::template OpCalculatePiolaStress<DIM, I, 0>(
1259 u, common_hencky_ptr));
1260
1261 pip.push_back(new typename P::template OpCalculatePlasticSurface<DIM, I>(
1262 u, common_plastic_ptr));
1263
1264 pip.push_back(new typename P::template OpCalculatePlasticHardening<DIM, I>(
1265 u, common_plastic_ptr, common_thermoplastic_ptr));
1266
1267 return std::make_tuple(common_plastic_ptr, common_hencky_ptr,
1268 common_thermal_ptr, common_thermoelastic_ptr,
1269 common_thermoplastic_ptr);
1270 }
#define CHK_THROW_MESSAGE(err, msg)
Check and throw MoFEM exception.
MoFEMErrorCode addMatThermalBlockOps(MoFEM::Interface &m_field, boost::ptr_deque< ForcesAndSourcesCore::UserDataOperator > &pipeline, std::string block_name, boost::shared_ptr< BlockedThermalParameters > blockedParamsPtr, double default_heat_conductivity, double default_heat_capacity, double default_thermal_conductivity_scale, double default_thermal_capacity_scale, Sev sev, ScalerFunTwoArgs thermal_conductivity_scaling_func, ScalerFunTwoArgs heat_capacity_scaling_func)
MoFEMErrorCode addMatThermoElasticBlockOps(MoFEM::Interface &m_field, boost::ptr_deque< ForcesAndSourcesCore::UserDataOperator > &pipeline, std::string block_name, boost::shared_ptr< BlockedThermoElasticParameters > blockedParamsPtr, double default_coeff_expansion, double default_ref_temp, Sev sev)
MoFEMErrorCode addMatThermoPlasticBlockOps(MoFEM::Interface &m_field, boost::ptr_deque< ForcesAndSourcesCore::UserDataOperator > &pipeline, std::string thermoplastic_block_name, boost::shared_ptr< ThermoPlasticBlockedParameters > blockedParamsPtr, boost::shared_ptr< ThermoElasticOps::BlockedThermalParameters > &blockedThermalParamsPtr, Sev sev, ScalerFunThreeArgs inelastic_heat_fraction_scaling)
[Hencky integrators]
Specialization for double precision scalar field values calculation.
Calculate symmetric tensor field values at integration pts.
double default_heat_capacity_scale
double default_thermal_conductivity_scale
ScalerFunTwoArgs heat_capacity_scaling
ScalerFunThreeArgs inelastic_heat_fraction_scaling
ScalerFunTwoArgs thermal_conductivity_scaling
constexpr auto size_symm
Definition plastic.cpp:42
double default_ref_temp
double default_heat_capacity
double default_coeff_expansion
double default_heat_conductivity

◆ doEdgeFlips()

MoFEMErrorCode Example::doEdgeFlips ( std::multimap< double, EntityHandle > &  el_q_map,
Range &  flipped_els,
Tag &  th_spatial_coords,
std::vector< EntityHandle > &  new_connectivity 
)
private

[Edge flips]

[Do Edge Flips]

Examples
thermoplastic.cpp.

Definition at line 2513 of file thermoplastic.cpp.

2515 {
2517
2518 ReadUtilIface *read_util;
2519 CHKERR mField.get_moab().query_interface(read_util);
2520
2521 const int num_ele = el_q_map.size() * 2; // each flip creates 2 elements
2522 int num_nod_per_ele;
2523 EntityType ent_type;
2524
2525 if (SPACE_DIM == 2) {
2526 num_nod_per_ele = 3;
2527 ent_type = MBTRI;
2528 } else {
2529 num_nod_per_ele = 4;
2530 ent_type = MBTET;
2531 }
2532
2533 Range new_elements;
2534 auto new_conn = new_connectivity.begin();
2535 for (auto e = 0; e != num_ele; ++e) {
2536 EntityHandle conn[num_nod_per_ele];
2537 for (int n = 0; n < num_nod_per_ele; ++n) {
2538 conn[n] = *new_conn;
2539 ++new_conn;
2540 }
2541 EntityHandle new_ele;
2542 Range adj_ele;
2543 CHKERR mField.get_moab().get_adjacencies(conn, num_nod_per_ele, SPACE_DIM,
2544 false, adj_ele);
2545 if (adj_ele.size()) {
2546 if (adj_ele.size() != 1) {
2547 SETERRQ(PETSC_COMM_SELF, MOFEM_STD_EXCEPTION_THROW,
2548 "Element duplication");
2549 } else {
2550 new_ele = adj_ele.front();
2551 }
2552 } else {
2553 CHKERR mField.get_moab().create_element(ent_type, conn, num_nod_per_ele,
2554 new_ele);
2555 }
2556 new_elements.insert(new_ele);
2557 }
2558
2559 MOFEM_LOG("REMESHING", Sev::verbose)
2560 << "New elements from edge flipping: " << new_elements;
2561
2562 auto reset_flip_bit = [](EntityHandle ent, BitRefLevel &bit) {
2563 bit.set(STORAGE_BIT, true);
2564 bit.set(FLIPPED_BIT, false);
2565 };
2566 CHKERR mField.getInterface<BitRefManager>()->lambdaBitRefLevel(
2567 reset_flip_bit);
2568
2569 auto get_adj = [&](auto ents) {
2570 Range adj;
2571 for (auto d = 0; d != SPACE_DIM; ++d) {
2573 mField.get_moab().get_adjacencies(ents, d, true, adj,
2574 moab::Interface::UNION),
2575 "Getting adjacencies of dimension " + std::to_string(d) + " failed");
2576 }
2577 return adj;
2578 };
2579
2580 Range non_flipped_range;
2581 CHKERR mField.getInterface<BitRefManager>()->getEntitiesByDimAndRefLevel(
2582 virgin_mesh_bit, BitRefLevel().set(), SPACE_DIM, non_flipped_range);
2583
2584 non_flipped_range = subtract(non_flipped_range, flipped_els);
2585 auto flip_bit_ents = new_elements;
2586 flip_bit_ents.merge(non_flipped_range);
2587 auto adj = get_adj(new_elements);
2588 // flip_bit_ents.merge(get_adj(new_elements));
2589
2590 CHKERR mField.getInterface<BitRefManager>()->setBitRefLevel(
2591 flip_bit_ents, flipped_bit, false);
2592
2593 CHKERR mField.getInterface<BitRefManager>()->writeBitLevelByDim(
2594 BitRefLevel().set(FLIPPED_BIT), BitRefLevel().set(), 2,
2595 "new_elements_after_edge_flips.vtk", "VTK", "");
2596
2598};
@ MOFEM_STD_EXCEPTION_THROW
Definition definitions.h:39
auto bit
set bit
const double n
refractive index of diffusive medium
std::bitset< BITREFLEVEL_SIZE > BitRefLevel
Bit structure attached to each entity identifying to what mesh entity is attached.
Definition Types.hpp:40
Managing BitRefLevels.
virtual moab::Interface & get_moab()=0
BitRefLevel flipped_bit
@ FLIPPED_BIT
@ STORAGE_BIT
BitRefLevel virgin_mesh_bit

◆ doEdgeSplits()

MoFEMErrorCode Example::doEdgeSplits ( bool &  refined,
bool  add_ents 
)
private

[Do Edge Flips]

[Refine edges]

Examples
thermoplastic.cpp.

Definition at line 2602 of file thermoplastic.cpp.

2602 {
2604
2605 Tag th_spatial_coords;
2606 double def_coord[3] = {0.0, 0.0, 0.0};
2607
2608 auto refined_mesh = [&](auto level) {
2610 auto bit = BitRefLevel().set(FLIPPED_BIT + level);
2611 Range edges;
2612 CHKERR mField.getInterface<BitRefManager>()->getEntitiesByDimAndRefLevel(
2613 bit, BitRefLevel().set(), SPACE_DIM - 1, edges);
2614 CHKERR mField.get_moab().tag_get_handle(
2615 "SpatialCoord", 3, MB_TYPE_DOUBLE, th_spatial_coords,
2616 MB_TAG_DENSE | MB_TAG_CREAT, def_coord);
2617
2618 Range refine_edges;
2619 if (add_ents)
2620 for (auto e : edges) {
2621 const EntityHandle *conn;
2622 int num_nodes;
2623 CHKERR mField.get_moab().get_connectivity(e, conn, num_nodes, true);
2624 std::array<double, 6> ref_coords, spatial_coords;
2625 CHKERR mField.get_moab().get_coords(conn, num_nodes, ref_coords.data());
2626 CHKERR mField.get_moab().tag_get_data(th_spatial_coords, conn,
2627 num_nodes, spatial_coords.data());
2628 auto get_length = [](auto &a) {
2630 FTensor::Tensor1<double, 3> p0{a[0], a[1], a[2]};
2631 FTensor::Tensor1<double, 3> p1{a[3], a[4], a[5]};
2632 p1(i) = p1(i) - p0(i);
2633 return p1.l2();
2634 };
2635 auto ref_edge_length = get_length(ref_coords);
2636 auto spatial_edge_length = get_length(spatial_coords);
2637 auto change = spatial_edge_length / ref_edge_length;
2638 if (change >= 1. + edge_growth_thresh) {
2639 refine_edges.insert(e);
2640 }
2641 }
2642
2643 if (refine_edges.size())
2644 refined = true;
2645
2646 Range prev_level_ents;
2647 CHKERR mField.getInterface<BitRefManager>()->getEntitiesByTypeAndRefLevel(
2648 bit, BitRefLevel().set(), MBEDGE, prev_level_ents);
2649
2650 Range prev_ref_ents;
2651 CHKERR mField.getInterface<BitRefManager>()->getEntitiesByTypeAndRefLevel(
2652 BitRefLevel().set(REFINED_EDGES_BIT), BitRefLevel().set(), MBEDGE,
2653 prev_ref_ents);
2654
2655 refine_edges.merge(intersect(prev_ref_ents, prev_level_ents));
2656 CHKERR mField.getInterface<BitRefManager>()->setNthBitRefLevel(
2657 refine_edges, REFINED_EDGES_BIT, true);
2658
2659 auto refine = mField.getInterface<MeshRefinement>();
2660 auto refined_bit = BitRefLevel().set(FIRST_REF_BIT + level);
2661 CHKERR refine->addVerticesInTheMiddleOfEdges(refine_edges, refined_bit);
2662 Range tris_to_refine;
2663 CHKERR mField.getInterface<BitRefManager>()->getEntitiesByDimAndRefLevel(
2664 bit, BitRefLevel().set(), SPACE_DIM, tris_to_refine);
2665 CHKERR refine->refineTris(tris_to_refine, refined_bit, QUIET, false);
2666
2667 auto set_spatial_coords = [&]() {
2669 Range new_vertices;
2670 CHKERR mField.getInterface<BitRefManager>()->getEntitiesByTypeAndRefLevel(
2671 refined_bit, refined_bit, MBVERTEX, new_vertices);
2672 auto th_parent_handle =
2673 mField.getInterface<BitRefManager>()->get_th_RefParentHandle();
2674 for (auto v : new_vertices) {
2675 EntityHandle parent;
2676 CHKERR mField.get_moab().tag_get_data(th_parent_handle, &v, 1, &parent);
2677 const EntityHandle *conn;
2678 int num_nodes;
2679 CHKERR mField.get_moab().get_connectivity(parent, conn, num_nodes,
2680 true);
2681 std::array<double, 6> spatial_coords;
2682 CHKERR mField.get_moab().tag_get_data(th_spatial_coords, conn,
2683 num_nodes, spatial_coords.data());
2684 for (auto d = 0; d < 3; ++d) {
2685 spatial_coords[d] += spatial_coords[3 + d];
2686 }
2687 for (auto d = 0; d < 3; ++d) {
2688 spatial_coords[d] /= 2.0;
2689 }
2690 CHKERR mField.get_moab().tag_set_data(th_spatial_coords, &v, 1,
2691 spatial_coords.data());
2692 }
2694 };
2695
2696 CHKERR set_spatial_coords();
2697
2698 CHKERR mField.getInterface<BitRefManager>()->writeBitLevelByDim(
2699 BitRefLevel().set(FIRST_REF_BIT + level), BitRefLevel().set(), 2,
2700 ("A_refined_" + boost::lexical_cast<std::string>(level) + ".vtk")
2701 .c_str(),
2702 "VTK", "");
2703
2705 };
2706
2707 auto reset_ref_bits = [](EntityHandle ent, BitRefLevel &bit) {
2708 bit.set(STORAGE_BIT, true);
2709 for (int l = 0; l < num_refinement_levels; ++l) {
2710 bit.set(FIRST_REF_BIT + l, false);
2711 }
2712 };
2713 CHKERR mField.getInterface<BitRefManager>()->lambdaBitRefLevel(
2714 reset_ref_bits);
2715
2716 for (auto l = 0; l < num_refinement_levels; ++l) {
2717 CHKERR refined_mesh(l);
2718 };
2719
2720 CHKERR mField.getInterface<BitRefManager>()->writeBitLevelByDim(
2722 BitRefLevel().set(), 2, "new_elements_after_edge_splits.vtk", "VTK", "");
2723
2725};
#define FTENSOR_INDEX(DIM, I)
constexpr double a
@ QUIET
const double v
phase velocity of light in medium (cm/ns)
FTensor::Index< 'l', 3 > l
Mesh refinement interface.
MoFEMErrorCode addVerticesInTheMiddleOfEdges(const EntityHandle meshset, const BitRefLevel &bit, const bool recursive=false, int verb=QUIET, EntityHandle start_v=0)
make vertices in the middle of edges in meshset and add them to refinement levels defined by bit
@ FIRST_REF_BIT
@ REFINED_EDGES_BIT
double edge_growth_thresh
BitRefLevel refined_bit
int num_refinement_levels

◆ edgeFlips() [1/2]

MoFEMErrorCode Example::edgeFlips ( BitRefLevel  parent_bit,
BitRefLevel  child_bit 
)
private

[Output results]

[Get element quality]

[Edge flips]

Examples
mofem/tutorials/adv-6_dg_projection/between_meshes_dg_projection.cpp, and thermoplastic.cpp.

Definition at line 577 of file between_meshes_dg_projection.cpp.

578 {
580
581 moab::Interface &moab = mField.get_moab();
582
583 auto make_edge_flip = [&](auto edge, auto adj_faces, Range &new_tris) {
585
586 auto get_conn = [&](EntityHandle e, EntityHandle *conn_cpy) {
588 const EntityHandle *conn;
589 int num_nodes;
590 CHKERR moab.get_connectivity(e, conn, num_nodes, true);
591 std::copy(conn, conn + num_nodes, conn_cpy);
593 };
594
595 auto get_tri_normals = [&](auto &conn) {
596 std::array<double, 18> coords;
597 CHKERR moab.get_coords(conn.data(), 6, coords.data());
598 std::array<FTensor::Tensor1<double, 3>, 2> tri_normals;
599 for (int t = 0; t != 2; ++t) {
600 CHKERR Tools::getTriNormal(&coords[9 * t], &tri_normals[t](0));
601 }
602 return tri_normals;
603 };
604
605 auto test_flip = [&](auto &&t_normals) {
606 FTENSOR_INDEX(3, i);
607 if (t_normals[0](i) * t_normals[1](i) <
608 std::numeric_limits<float>::epsilon())
609 return false;
610 return true;
611 };
612
613 std::array<EntityHandle, 6> adj_conn;
614 CHKERR get_conn(adj_faces[0], &adj_conn[0]);
615 CHKERR get_conn(adj_faces[1], &adj_conn[3]);
616 std::array<EntityHandle, 2> edge_conn;
617 CHKERR get_conn(edge, edge_conn.data());
618 std::array<EntityHandle, 2> new_edge_conn;
619
620 int j = 1;
621 for (int i = 0; i != 6; ++i) {
622 if (adj_conn[i] != edge_conn[0] && adj_conn[i] != edge_conn[1]) {
623 new_edge_conn[j] = adj_conn[i];
624 --j;
625 }
626 }
627
628 auto &new_conn = adj_conn; //< just alias this
629 for (int t = 0; t != 2; ++t) {
630 for (int i = 0; i != 3; ++i) {
631 if (
632
633 (adj_conn[3 * t + i % 3] == edge_conn[0] &&
634 adj_conn[3 * t + (i + 1) % 3] == edge_conn[1])
635
636 ||
637
638 (adj_conn[3 * t + i % 3] == edge_conn[1] &&
639 adj_conn[3 * t + (i + 1) % 3] == edge_conn[0])
640
641 ) {
642 new_conn[3 * t + (i + 1) % 3] = new_edge_conn[t];
643 break;
644 }
645 }
646 }
647
648 if (test_flip(get_tri_normals(new_conn))) {
649 for (int t = 0; t != 2; ++t) {
650 Range rtri;
651 CHKERR moab.get_adjacencies(&new_conn[3 * t], SPACE_DIM + 1, SPACE_DIM,
652 false, rtri);
653 if (!rtri.size()) {
654 EntityHandle tri;
655 CHKERR moab.create_element(MBTRI, &new_conn[3 * t], SPACE_DIM + 1,
656 tri);
657 new_tris.insert(tri);
658 } else {
659#ifndef NDEBUG
660 if (rtri.size() != 1) {
661 MOFEM_LOG("SELF", Sev::error)
662 << "Multiple tries created during edge flip for edge " << edge
663 << " adjacent faces " << std::endl
664 << rtri;
665 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
666 "Multiple tries created during edge flip");
667 }
668#endif // NDEBUG
669 new_tris.merge(rtri);
670 }
671 }
672
673 Range new_edges;
674 CHKERR moab.get_adjacencies(new_tris, SPACE_DIM - 1, true, new_edges,
675 moab::Interface::UNION);
676 } else {
677
678 MOFEM_LOG_CHANNEL("SELF");
679 MOFEM_LOG("SELF", Sev::warning)
680 << "Edge flip rejected for edge " << edge << " adjacent faces "
681 << adj_faces;
682 }
683
685 };
686
687 Range tris;
688 CHKERR moab.get_entities_by_dimension(0, SPACE_DIM, tris);
689 CHKERR mField.getInterface<BitRefManager>()->filterEntitiesByRefLevel(
690 parent_bit, BitRefLevel().set(), tris);
691 Skinner skin(&moab);
692 Range skin_edges;
693 CHKERR skin.find_skin(0, tris, false, skin_edges);
694
695 Range edges;
696 CHKERR moab.get_entities_by_dimension(0, SPACE_DIM - 1, edges);
697 edges = subtract(edges, skin_edges);
698 CHKERR mField.getInterface<BitRefManager>()->filterEntitiesByRefLevel(
699 parent_bit, BitRefLevel().set(), edges);
700
701 Range new_tris, flipped_tris, forbidden_tris;
702 int flip_count = 0;
703 for (auto edge : edges) {
704 Range adjacent_tris;
705 CHKERR moab.get_adjacencies(&edge, 1, SPACE_DIM, true, adjacent_tris);
706
707 adjacent_tris = intersect(adjacent_tris, tris);
708 adjacent_tris = subtract(adjacent_tris, forbidden_tris);
709 if (adjacent_tris.size() == 2) {
710
711#ifndef NDEBUG
712 int side_number0, sense0, offset0;
713 CHKERR mField.get_moab().side_number(adjacent_tris[0], edge, side_number0,
714 sense0, offset0);
715 int side_number1, sense1, offset1;
716 CHKERR mField.get_moab().side_number(adjacent_tris[1], edge, side_number1,
717 sense1, offset1);
718 if (sense0 * sense1 > 0)
719 SETERRQ(
720 PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
721 "Cannot flip edge with same orientation in both adjacent faces");
722#endif // NDEBUG
723
724 Range new_flipped_tris;
725 CHKERR make_edge_flip(edge, adjacent_tris, new_flipped_tris);
726 if (new_flipped_tris.size()) {
727 flipped_tris.merge(adjacent_tris);
728 forbidden_tris.merge(adjacent_tris);
729 new_tris.merge(new_flipped_tris);
730
731#ifndef NDEBUG
732 CHKERR save_range(moab,
733 "flipped_tris_" + std::to_string(flip_count) + ".vtk",
734 adjacent_tris);
736 moab, "new_flipped_tris_" + std::to_string(flip_count) + ".vtk",
737 new_flipped_tris);
738
739#endif // NDEBUG
740
741 ++flip_count;
742 }
743 }
744 }
745
746 Range all_tris;
747 CHKERR moab.get_entities_by_dimension(0, SPACE_DIM, all_tris);
748 Range not_flipped_tris = subtract(all_tris, flipped_tris);
749
750 MOFEM_LOG("SELF", Sev::noisy)
751 << "Flipped " << flip_count << " edges with two adjacent faces.";
752 CHKERR mField.getInterface<BitRefManager>()->setBitRefLevel(not_flipped_tris,
753 child_bit);
754 CHKERR mField.getInterface<BitRefManager>()->setBitRefLevel(new_tris,
755 child_bit);
756 CHKERR mField.getInterface<BitRefManager>()->writeBitLevel(
757 child_bit, BitRefLevel().set(), "edge_flips_before_refinement.vtk", "VTK",
758 "");
759
761}
@ MOFEM_ATOM_TEST_INVALID
Definition definitions.h:40
FTensor::Index< 'j', 3 > j
constexpr double t
plate stiffness
Definition plate.cpp:58
static MoFEMErrorCode getTriNormal(const double *coords, double *normal, double *d_normal=nullptr)
Get the Tri Normal objectGet triangle normal.
Definition Tools.cpp:353
auto save_range

◆ edgeFlips() [2/2]

MoFEMErrorCode Example::edgeFlips ( BitRefLevel  parent_bit,
BitRefLevel  child_bit 
)
private

◆ getCoordsInImage()

std::pair< int, int > Example::getCoordsInImage ( double  x,
double  y 
)
staticprivate
Examples
mofem/tutorials/mix-1_light_intensity_equation/phase.cpp.

Definition at line 131 of file phase.cpp.

131 {
132
133 auto &m = iI[focalIndex];
134 x -= aveMaxMin[MIN_X];
135 y -= aveMaxMin[MIN_Y];
136 x *= (m.size1() - 1) / (aveMaxMin[MAX_X] - aveMaxMin[MIN_X]);
137 y *= (m.size2() - 1) / (aveMaxMin[MAX_Y] - aveMaxMin[MIN_Y]);
138 const auto p = std::make_pair<int, int>(std::round(x), std::round(y));
139
140#ifndef NDEBUG
141 if (p.first < 0 && p.first >= m.size1())
142 THROW_MESSAGE("Wrong index");
143 if (p.second < 0 && p.second >= m.size2())
144 THROW_MESSAGE("Wrong index");
145#endif
146
147 return p;
148}
#define THROW_MESSAGE(msg)
Throw MoFEM exception.
FTensor::Index< 'm', 3 > m
static std::array< double, LAST_BB > aveMaxMin
Definition phase.cpp:110
static int focalIndex
Definition phase.cpp:112
static std::vector< MatrixInt > iI
Definition phase.cpp:109

◆ getElementQuality()

MoFEMErrorCode Example::getElementQuality ( std::multimap< double, EntityHandle > &  el_q_map,
Range &  flipped_els,
std::vector< EntityHandle > &  new_connectivity,
bool &  do_refine,
Tag &  th_spatial_coords 
)
private

[Get element quality]

Examples
thermoplastic.cpp.

Definition at line 1882 of file thermoplastic.cpp.

1885 {
1887
1888 Range verts;
1889 CHKERR mField.get_moab().get_entities_by_type(0, MBVERTEX, verts);
1890 std::vector<double> coords(verts.size() * 3);
1891 CHKERR mField.get_moab().get_coords(verts, coords.data());
1892 auto t_x = getFTensor1FromPtr<3>(coords.data());
1893
1894 auto save_tag = [&](boost::shared_ptr<FieldEntity> ent_ptr) {
1896 FTENSOR_INDEX(3, i)
1897 auto field_data = ent_ptr->getEntFieldData();
1899 if (SPACE_DIM == 2) {
1900 t_u = {field_data[0], field_data[1], 0.0};
1901 } else {
1902 t_u = {field_data[0], field_data[1], field_data[2]};
1903 }
1904
1905 t_x(i) += t_u(i);
1906 ++t_x;
1908 };
1909
1910 mField.getInterface<FieldBlas>()->fieldLambdaOnEntities(save_tag, "U",
1911 &verts);
1912 double def_coord[3] = {0.0, 0.0, 0.0};
1913 CHKERR mField.get_moab().tag_get_handle(
1914 "SpatialCoord", 3, MB_TYPE_DOUBLE, th_spatial_coords,
1915 MB_TAG_DENSE | MB_TAG_CREAT, def_coord);
1916 CHKERR mField.get_moab().tag_set_data(th_spatial_coords, verts,
1917 coords.data());
1918
1919 // get a map which has the element quality for each tag
1920
1921 Range all_els;
1922 CHKERR mField.get_moab().get_entities_by_dimension(0, SPACE_DIM, all_els,
1923 true);
1924 CHKERR mField.getInterface<BitRefManager>()->filterEntitiesByRefLevel(
1925 virgin_mesh_bit, BitRefLevel().set(), all_els);
1926
1927 std::multimap<double, EntityHandle> candidate_el_q_map;
1928 double spatial_coords[9], material_coords[9];
1929 const EntityHandle *conn;
1930 int num_nodes;
1931 Range::iterator nit = all_els.begin();
1932 for (int gg = 0; nit != all_els.end(); nit++, gg++) {
1933 CHKERR mField.get_moab().get_connectivity(*nit, conn, num_nodes, true);
1934
1935 CHKERR mField.get_moab().get_coords(conn, num_nodes, material_coords);
1936 CHKERR mField.get_moab().tag_get_data(th_spatial_coords, conn, num_nodes,
1937 spatial_coords);
1938
1939 double q = triangleAreaLengthQuality(spatial_coords);
1940 if (!std::isnormal(q))
1941 SETERRQ(PETSC_COMM_WORLD, MOFEM_DATA_INCONSISTENCY,
1942 "Calculated quality of element is not "
1943 "as expected: %f",
1944 q);
1945
1946 if (q < qual_tol && q > 0.)
1947 candidate_el_q_map.insert(std::pair<double, EntityHandle>(q, *nit));
1948 }
1949
1950 double min_q = 1;
1951 CHKERR minTriangleQuality(mField, all_els, min_q, th_spatial_coords);
1952 MOFEM_LOG("REMESHING", Sev::inform)
1953 << "Old minimum element quality: " << min_q;
1954 // Get the first element using begin()
1955 auto pair = candidate_el_q_map.begin();
1956 MOFEM_LOG("REMESHING", Sev::inform)
1957 << "New minimum element quality: " << pair->first;
1958
1959 for (auto pair = candidate_el_q_map.begin(); pair != candidate_el_q_map.end();
1960 ++pair) {
1961 double quality = pair->first;
1962 Range element;
1963 element.insert(pair->second);
1964 if (!flipped_els.contains(element)) {
1965 // Get edges of element
1966 Range edges;
1967 CHKERR mField.get_moab().get_adjacencies(element, 1, false, edges);
1968
1969 // Get the longest edge
1970 EntityHandle longest_edge;
1971 double longest_edge_length = 0;
1972 std::vector<std::pair<double, EntityHandle>> edge_lengths;
1973 edge_lengths.reserve(edges.size());
1974 for (auto edge : edges) {
1975 edge_lengths.emplace_back(
1976 mField.getInterface<Tools>()->getEdgeLength(edge), edge);
1977 }
1978 if (!edge_lengths.empty()) {
1979 const auto it = std::max_element(
1980 edge_lengths.begin(), edge_lengths.end(),
1981 [](const auto &a, const auto &b) { return a.first < b.first; });
1982 longest_edge_length = it->first;
1983 longest_edge = it->second;
1984 } else {
1985 SETERRQ(PETSC_COMM_WORLD, MOFEM_DATA_INCONSISTENCY,
1986 "Unable to calculate edge lengths to find longest edge.");
1987 }
1988
1989 MOFEM_LOG("REMESHING", Sev::verbose)
1990 << "Edge flip longest edge length: " << longest_edge_length
1991 << " for edge: " << longest_edge;
1992
1993 auto get_skin = [&]() {
1994 Range body_ents;
1995 CHKERR mField.get_moab().get_entities_by_dimension(0, SPACE_DIM,
1996 body_ents);
1997 Skinner skin(&mField.get_moab());
1998 Range skin_ents;
1999 CHKERR skin.find_skin(0, body_ents, false, skin_ents);
2000 return skin_ents;
2001 };
2002
2003 auto filter_true_skin = [&](auto skin) {
2004 Range boundary_ents;
2005 ParallelComm *pcomm =
2006 ParallelComm::get_pcomm(&mField.get_moab(), MYPCOMM_INDEX);
2007 CHKERR pcomm->filter_pstatus(skin, PSTATUS_SHARED | PSTATUS_MULTISHARED,
2008 PSTATUS_NOT, -1, &boundary_ents);
2009 return boundary_ents;
2010 };
2011
2012 Range boundary_ents = get_skin();
2013
2014 MOFEM_LOG("REMESHING", Sev::verbose)
2015 << "Boundary entities: " << boundary_ents;
2016
2017 // Get neighbouring element with the longest edge
2018 Range flip_candidate_els;
2019 CHKERR mField.get_moab().get_adjacencies(&longest_edge, 1, SPACE_DIM,
2020 false, flip_candidate_els);
2021 CHKERR mField.getInterface<BitRefManager>()->filterEntitiesByRefLevel(
2022 virgin_mesh_bit, BitRefLevel().set(), flip_candidate_els);
2023
2024 Range neighbouring_el = subtract(flip_candidate_els, element);
2025 CHKERR mField.getInterface<BitRefManager>()->filterEntitiesByRefLevel(
2026 virgin_mesh_bit, BitRefLevel().set(), neighbouring_el);
2027
2028 if (boundary_ents.contains(Range(longest_edge, longest_edge))) {
2029 continue;
2030 }
2031
2032 if (flipped_els.contains(neighbouring_el))
2033 continue; // Already flipped
2034
2035#ifndef NDEBUG
2036 if (neighbouring_el.size() != 1) {
2037 SETERRQ(PETSC_COMM_WORLD, MOFEM_DATA_INCONSISTENCY,
2038 "Should be 1 neighbouring element to bad element for edge "
2039 "flip. Instead, there are %zu",
2040 neighbouring_el.size());
2041 }
2042#endif
2043
2044 MOFEM_LOG("REMESHING", Sev::verbose)
2045 << "flip_candidate_els: " << flip_candidate_els;
2046 MOFEM_LOG("REMESHING", Sev::verbose)
2047 << "Neighbouring element: " << neighbouring_el;
2048
2049 // Check the quality of the neighbouring element
2050 std::vector<EntityHandle> neighbouring_nodes;
2051 CHKERR mField.get_moab().get_connectivity(&neighbouring_el.front(), 1,
2052 neighbouring_nodes, true);
2053
2054 std::vector<EntityHandle> element_nodes;
2055 CHKERR mField.get_moab().get_connectivity(&element.front(), 1,
2056 element_nodes, true);
2057
2058 CHKERR mField.get_moab().tag_get_data(
2059 th_spatial_coords, &neighbouring_nodes.front(), 3, spatial_coords);
2060
2061 double neighbour_qual = triangleAreaLengthQuality(spatial_coords);
2062 if (!std::isnormal(neighbour_qual))
2063 SETERRQ(PETSC_COMM_WORLD, MOFEM_DATA_INCONSISTENCY,
2064 "Calculated quality of neighbouring element is not as "
2065 "expected: %f",
2066 neighbour_qual);
2067
2068 // Get the nodes of flip_candidate_els as well as the nodes of
2069 // longest_edge
2070
2071 MOFEM_LOG("REMESHING", Sev::verbose)
2072 << "Element nodes before swap: "
2073 << get_string_from_vector(element_nodes);
2074 MOFEM_LOG("REMESHING", Sev::verbose)
2075 << "Neighbouring nodes before swap: "
2076 << get_string_from_vector(neighbouring_nodes);
2077
2078 CHKERR save_range(mField.get_moab(), "bad_element.vtk", element);
2079 CHKERR save_range(mField.get_moab(), "bad_element_neighbour.vtk",
2080 neighbouring_el);
2081
2082 // Get new canonical ordering of new nodes and new neighbouring
2083 // nodes
2084 std::vector<EntityHandle> reversed_neighbouring_nodes =
2085 neighbouring_nodes;
2086 std::reverse(reversed_neighbouring_nodes.begin(),
2087 reversed_neighbouring_nodes.end());
2088
2089 int num_matches = 0;
2090 std::vector<bool> mismatch_mask(element_nodes.size());
2091 int loop_counter = 0; // To prevent infinite loop
2092 while (num_matches != 2) {
2093 // Permute first element to the end
2094 std::rotate(reversed_neighbouring_nodes.begin(),
2095 reversed_neighbouring_nodes.begin() + 1,
2096 reversed_neighbouring_nodes.end());
2097 // Create a boolean mask
2098 std::transform(element_nodes.begin(), element_nodes.end(),
2099 reversed_neighbouring_nodes.begin(),
2100 mismatch_mask.begin(), std::equal_to<EntityHandle>());
2101 // Check if common edge is found
2102 num_matches =
2103 std::count(mismatch_mask.begin(), mismatch_mask.end(), true);
2104
2105 ++loop_counter;
2106 if (loop_counter > 3) {
2107 SETERRQ(PETSC_COMM_SELF, MOFEM_STD_EXCEPTION_THROW,
2108 "Not found matching nodes for edge flipping");
2109 }
2110 }
2111
2112 // Get matching nodes
2113 std::vector<EntityHandle> matched_elements(element_nodes.size());
2114 std::transform(element_nodes.begin(), element_nodes.end(),
2115 mismatch_mask.begin(), matched_elements.begin(),
2116 [](EntityHandle el, bool match) {
2117 return match ? el : -1; // Or some other "null" value
2118 });
2119
2120 // Remove zero (or "null") elements
2121 matched_elements.erase(
2122 std::remove(matched_elements.begin(), matched_elements.end(), -1),
2123 matched_elements.end());
2124
2125 // Get mismatching nodes
2126 std::vector<EntityHandle> mismatched_elements(element_nodes.size()),
2127 neighbouring_mismatched_elements(neighbouring_nodes.size());
2128 std::transform(element_nodes.begin(), element_nodes.end(),
2129 mismatch_mask.begin(), mismatched_elements.begin(),
2130 [](EntityHandle el, bool match) {
2131 return match ? -1 : el; // Or some other "null" value
2132 });
2133 std::transform(reversed_neighbouring_nodes.begin(),
2134 reversed_neighbouring_nodes.end(), mismatch_mask.begin(),
2135 neighbouring_mismatched_elements.begin(),
2136 [](EntityHandle el, bool match) {
2137 return match ? -1 : el; // Or some other "null" value
2138 });
2139
2140 // Remove zero (or "null") elements
2141 mismatched_elements.erase(std::remove(mismatched_elements.begin(),
2142 mismatched_elements.end(), -1),
2143 mismatched_elements.end());
2144 neighbouring_mismatched_elements.erase(
2145 std::remove(neighbouring_mismatched_elements.begin(),
2146 neighbouring_mismatched_elements.end(), -1),
2147 neighbouring_mismatched_elements.end());
2148
2149 mismatched_elements.insert(mismatched_elements.end(),
2150 neighbouring_mismatched_elements.begin(),
2151 neighbouring_mismatched_elements.end());
2152
2153 MOFEM_LOG("REMESHING", Sev::verbose)
2154 << "Reversed neighbouring nodes: "
2155 << get_string_from_vector(reversed_neighbouring_nodes);
2156
2157 MOFEM_LOG("REMESHING", Sev::verbose)
2158 << "mismatch mask: " << get_string_from_vector(mismatch_mask);
2159
2160 MOFEM_LOG("REMESHING", Sev::verbose)
2161 << "Old nodes are: " << get_string_from_vector(matched_elements);
2162
2163 MOFEM_LOG("REMESHING", Sev::verbose)
2164 << "New nodes are: " << get_string_from_vector(mismatched_elements);
2165
2166 auto replace_correct_nodes = [](std::vector<EntityHandle> &ABC,
2167 std::vector<EntityHandle> &DBA,
2168 const std::vector<EntityHandle> &AB,
2169 const std::vector<EntityHandle> &CD) {
2171 std::vector<std::vector<EntityHandle>> results;
2172 // Assume AB.size() == 2, CD.size() == 2 for tris
2173 for (int i = 0; i < 2; ++i) { // i: 0 for A, 1 for B
2174 for (int j = 0; j < 2; ++j) { // j: 0 for C, 1 for D
2175 // Only try to replace if CD[j] is not already in ABC
2176 if (std::find(ABC.begin(), ABC.end(), CD[j]) == ABC.end()) {
2177 std::vector<EntityHandle> tmp = ABC;
2178 // Find AB[i] in ABC and replace with CD[j]
2179 auto it = std::find(tmp.begin(), tmp.end(), AB[i]);
2180 if (it != tmp.end()) {
2181 *it = CD[j];
2182 results.push_back(tmp);
2183 }
2184 }
2185 }
2186 }
2187
2188 if (results.size() != 2) {
2189 SETERRQ(PETSC_COMM_SELF, MOFEM_STD_EXCEPTION_THROW,
2190 "Failed to find two valid vertex replacements for edge "
2191 "flip");
2192 }
2193
2194 ABC = results[0];
2195 DBA = results[1];
2196
2198 };
2199
2200 CHKERR replace_correct_nodes(element_nodes, neighbouring_nodes,
2201 matched_elements, mismatched_elements);
2202
2203 MOFEM_LOG("REMESHING", Sev::verbose)
2204 << "Element nodes after swap: "
2205 << get_string_from_vector(element_nodes);
2206 MOFEM_LOG("REMESHING", Sev::verbose)
2207 << "Neighbouring nodes after swap: "
2208 << get_string_from_vector(neighbouring_nodes);
2209
2210 // Calculate the quality of the new elements
2211 CHKERR mField.get_moab().tag_get_data(
2212 th_spatial_coords, &element_nodes.front(), 3, spatial_coords);
2213
2214 double new_qual = triangleAreaLengthQuality(spatial_coords);
2215 if (new_qual < 0.0 || !std::isfinite(new_qual))
2216 SETERRQ(PETSC_COMM_WORLD, MOFEM_DATA_INCONSISTENCY,
2217 "Calculated quality of new element is not as expected: %f",
2218 new_qual);
2219
2220#ifndef NDEBUG
2221 auto check_normal_direction = [&](double qual_val) {
2223 FTensor::Index<'i', 3> i;
2224 auto t_correct_normal = FTensor::Tensor1<double, 3>(0.0, 0.0, 1.0);
2225 double new_area = 0;
2226 std::array<double, 3> new_normal;
2227 CHKERR getTriangleAreaAndNormal(spatial_coords, new_area, new_normal);
2228 auto t_new_normal = FTensor::Tensor1<double, 3>(
2229 new_normal[0], new_normal[1], new_normal[2]);
2230 auto t_diff = FTensor::Tensor1<double, 3>();
2231 t_diff(i) = t_new_normal(i) - t_correct_normal(i);
2232 if (qual_val > 1e-6 && t_diff(i) * t_diff(i) > 1e-6) {
2233 SETERRQ(PETSC_COMM_SELF, MOFEM_STD_EXCEPTION_THROW,
2234 "Direction of element to be created is wrong orientation");
2235 }
2237 };
2238
2239 CHKERR check_normal_direction(new_qual);
2240#endif
2241
2242 CHKERR mField.get_moab().tag_get_data(
2243 th_spatial_coords, &neighbouring_nodes.front(), 3, spatial_coords);
2244
2245 double new_neighbour_qual = triangleAreaLengthQuality(spatial_coords);
2246 if (new_neighbour_qual < 0.0 || !std::isfinite(new_neighbour_qual))
2247 SETERRQ(PETSC_COMM_WORLD, MOFEM_DATA_INCONSISTENCY,
2248 "Calculated quality of new neighbouring element is not "
2249 "as expected: %f",
2250 new_neighbour_qual);
2251
2252#ifndef NDEBUG
2253 CHKERR check_normal_direction(new_neighbour_qual);
2254#endif
2255
2256 // If the minimum element quality has improved, do the flip
2257 if (std::min(new_qual, new_neighbour_qual) >
2258 (1. + qual_thresh) * std::min(quality, neighbour_qual)) {
2259 MOFEM_LOG("REMESHING", Sev::inform)
2260 << "Element quality improved from " << quality << " and "
2261 << neighbour_qual << " to " << new_qual << " and "
2262 << new_neighbour_qual << " for elements" << element << " and "
2263 << neighbouring_el;
2264
2265 // then push to creation "pipeline".
2266 flipped_els.merge(flip_candidate_els);
2267 el_q_map.insert(
2268 std::pair<double, EntityHandle>(pair->first, pair->second));
2269 new_connectivity.insert(new_connectivity.end(), element_nodes.begin(),
2270 element_nodes.end());
2271 new_connectivity.insert(new_connectivity.end(),
2272 neighbouring_nodes.begin(),
2273 neighbouring_nodes.end());
2274 }
2275 }
2276 }
2277
2278 if (el_q_map.size() > 0) {
2279 MOFEM_LOG("REMESHING", Sev::verbose) << "Flipped elements: " << flipped_els;
2280 MOFEM_LOG("REMESHING", Sev::verbose)
2281 << "New connectivity: " << get_string_from_vector(new_connectivity);
2282 }
2283
2284 Range edges;
2285 CHKERR mField.getInterface<BitRefManager>()->getEntitiesByDimAndRefLevel(
2286 virgin_mesh_bit, BitRefLevel().set(), SPACE_DIM - 1, edges);
2287 CHKERR mField.get_moab().tag_get_handle(
2288 "SpatialCoord", 3, MB_TYPE_DOUBLE, th_spatial_coords,
2289 MB_TAG_DENSE | MB_TAG_CREAT, def_coord);
2290
2291 Range prev_ref_ents;
2292 CHKERR mField.getInterface<BitRefManager>()->getEntitiesByTypeAndRefLevel(
2293 BitRefLevel().set(REFINED_EDGES_BIT), BitRefLevel().set(), MBEDGE,
2294 prev_ref_ents);
2295
2296 for (auto e : edges) {
2297 const EntityHandle *conn;
2298 int num_nodes;
2299 CHKERR mField.get_moab().get_connectivity(e, conn, num_nodes, true);
2300 std::array<double, 6> ref_coords, spatial_coords;
2301 CHKERR mField.get_moab().get_coords(conn, num_nodes, ref_coords.data());
2302 CHKERR mField.get_moab().tag_get_data(th_spatial_coords, conn, num_nodes,
2303 spatial_coords.data());
2304 auto get_length = [](auto &a) {
2306 FTensor::Tensor1<double, 3> p0{a[0], a[1], a[2]};
2307 FTensor::Tensor1<double, 3> p1{a[3], a[4], a[5]};
2308 p1(i) = p1(i) - p0(i);
2309 return p1.l2();
2310 };
2311 auto ref_edge_length = get_length(ref_coords);
2312 auto spatial_edge_length = get_length(spatial_coords);
2313 auto change = spatial_edge_length / ref_edge_length;
2314 if ((change >= 1. + edge_growth_thresh) && (num_refinement_levels > 0) &&
2315 !prev_ref_ents.contains(Range(e, e))) {
2316 do_refine = true;
2317 }
2318 }
2319
2321};
static auto filter_true_skin(MoFEM::Interface &m_field, Range &&skin)
#define MYPCOMM_INDEX
default communicator number PCOMM
double q
Auxiliary tools.
Definition Tools.hpp:19
static double getEdgeLength(const double *edge_coords)
Get edge length.
Definition Tools.cpp:418
double qual_thresh
auto get_string_from_vector

◆ gettingNorms()

MoFEMErrorCode Example::gettingNorms ( )
private

[Solve]

[Getting norms]

Examples
nonlinear_elastic.cpp.

Definition at line 382 of file nonlinear_elastic.cpp.

382 {
384
386 auto dm = simple->getDM();
387
388 auto T = createDMVector(simple->getDM());
389 CHKERR DMoFEMMeshToLocalVector(simple->getDM(), T, INSERT_VALUES,
390 SCATTER_FORWARD);
391 double nrm2;
392 CHKERR VecNorm(T, NORM_2, &nrm2);
393 MOFEM_LOG("EXAMPLE", Sev::inform) << "Solution norm " << nrm2;
394
395 auto post_proc_norm_fe = boost::make_shared<DomainEle>(mField);
396
397 auto post_proc_norm_rule_hook = [](int, int, int p) -> int { return 2 * p; };
398 post_proc_norm_fe->getRuleHook = post_proc_norm_rule_hook;
399
401 post_proc_norm_fe->getOpPtrVector(), {H1});
402
403 enum NORMS { U_NORM_L2 = 0, PIOLA_NORM, LAST_NORM };
404 auto norms_vec =
406 (mField.get_comm_rank() == 0) ? LAST_NORM : 0, LAST_NORM);
407 CHKERR VecZeroEntries(norms_vec);
408
409 auto u_ptr = boost::make_shared<MatrixDouble>();
410 post_proc_norm_fe->getOpPtrVector().push_back(
412
413 post_proc_norm_fe->getOpPtrVector().push_back(
414 new OpCalcNormL2Tensor1<SPACE_DIM>(u_ptr, norms_vec, U_NORM_L2));
415
416 auto common_ptr = commonDataFactory<SPACE_DIM, GAUSS, DomainEleOp>(
417 mField, post_proc_norm_fe->getOpPtrVector(), "U", "MAT_ELASTIC",
418 Sev::inform);
419
420 post_proc_norm_fe->getOpPtrVector().push_back(
422 common_ptr->getMatFirstPiolaStress(), norms_vec, PIOLA_NORM));
423
425 post_proc_norm_fe);
426
427 CHKERR VecAssemblyBegin(norms_vec);
428 CHKERR VecAssemblyEnd(norms_vec);
429
430 MOFEM_LOG_CHANNEL("SELF"); // Clear channel from old tags
431 if (mField.get_comm_rank() == 0) {
432 const double *norms;
433 CHKERR VecGetArrayRead(norms_vec, &norms);
434 MOFEM_TAG_AND_LOG("SELF", Sev::inform, "example")
435 << "norm_u: " << std::scientific << std::sqrt(norms[U_NORM_L2]);
436 MOFEM_TAG_AND_LOG("SELF", Sev::inform, "example")
437 << "norm_piola: " << std::scientific << std::sqrt(norms[PIOLA_NORM]);
438 CHKERR VecRestoreArrayRead(norms_vec, &norms);
439 }
440
442}
#define MOFEM_TAG_AND_LOG(channel, severity, tag)
Tag and log in channel.
auto createVectorMPI(MPI_Comm comm, PetscInt n, PetscInt N)
Create MPI Vector.
virtual int get_comm_rank() const =0
Get norm of input MatrixDouble for Tensor1.
Get norm of input MatrixDouble for Tensor2.

◆ initialConditions()

MoFEMErrorCode Example::initialConditions ( )
private

[Create common data]

[Initial conditions]

Examples
thermoplastic.cpp.

Definition at line 3158 of file thermoplastic.cpp.

3158 {
3160
3161 auto vol_rule = [](int, int, int ao) { return 2 * ao + geom_order; };
3162
3163 // #ifdef PYTHON_INIT_SURFACE
3164 // auto get_py_surface_init = []() {
3165 // auto py_surf_init = boost::make_shared<SurfacePython>();
3166 // CHKERR py_surf_init->surfaceInit("surface.py");
3167 // surfacePythonWeakPtr = py_surf_init;
3168 // return py_surf_init;
3169 // };
3170 // auto py_surf_init = get_py_surface_init();
3171 // #endif
3172
3173 auto simple = mField.getInterface<Simple>();
3174
3175 // CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(),
3176 // "REMOVE_X",
3177 // "U", 0, 0);
3178 // CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(),
3179 // "REMOVE_Y",
3180 // "U", 1, 1);
3181 // CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(),
3182 // "REMOVE_Z",
3183 // "U", 2, 2);
3184 // CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(),
3185 // "REMOVE_ALL", "U", 0, 3);
3186
3187 // #ifdef ADD_CONTACT
3188 // for (auto b : {"FIX_X", "REMOVE_X"})
3189 // CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), b,
3190 // "SIGMA", 0, 0, false, true);
3191 // for (auto b : {"FIX_Y", "REMOVE_Y"})
3192 // CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), b,
3193 // "SIGMA", 1, 1, false, true);
3194 // for (auto b : {"FIX_Z", "REMOVE_Z"})
3195 // CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), b,
3196 // "SIGMA", 2, 2, false, true);
3197 // for (auto b : {"FIX_ALL", "REMOVE_ALL"})
3198 // CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), b,
3199 // "SIGMA", 0, 3, false, true);
3200 // CHKERR bc_mng->removeBlockDOFsOnEntities(
3201 // simple->getProblemName(), "NO_CONTACT", "SIGMA", 0, 3, false, true);
3202 // #endif
3203
3204 // CHKERR bc_mng->pushMarkDOFsOnEntities<DisplacementCubitBcData>(
3205 // simple->getProblemName(), "U");
3206
3207 using UDO = ForcesAndSourcesCore::UserDataOperator;
3208
3209 auto T_ptr = boost::make_shared<VectorDouble>();
3210
3211 auto post_proc = [&](auto dm) {
3213
3214 auto post_proc_fe = boost::make_shared<PostProcEle>(mField);
3215
3217
3218 post_proc_fe->getOpPtrVector().push_back(
3219 new OpCalculateScalarFieldValues("T", T_ptr));
3220 // post_proc_fe->getOpPtrVector().push_back(
3221 // new OpCalculateVectorFieldValues<SPACE_DIM>("U", u_ptr));
3222 if (atom_test == 8) {
3223
3224 auto TAU_ptr = boost::make_shared<VectorDouble>();
3225 auto EP_ptr = boost::make_shared<MatrixDouble>();
3226
3227 post_proc_fe->getOpPtrVector().push_back(
3228 new OpCalculateScalarFieldValues("TAU", TAU_ptr));
3229 post_proc_fe->getOpPtrVector().push_back(
3231
3232 post_proc_fe->getOpPtrVector().push_back(
3233
3234 new OpPPMap(post_proc_fe->getPostProcMesh(),
3235 post_proc_fe->getMapGaussPts(),
3236
3237 {{"T", T_ptr}, {"TAU", TAU_ptr}},
3238
3239 {},
3240
3241 {},
3242
3243 {{"EP", EP_ptr}}
3244
3245 )
3246
3247 );
3248 } else {
3249 post_proc_fe->getOpPtrVector().push_back(
3250
3251 new OpPPMap(post_proc_fe->getPostProcMesh(),
3252 post_proc_fe->getMapGaussPts(),
3253
3254 {{"T", T_ptr}},
3255
3256 {},
3257
3258 {},
3259
3260 {}
3261
3262 )
3263
3264 );
3265 }
3266
3267 CHKERR DMoFEMLoopFiniteElements(dm, "dFE", post_proc_fe);
3268 CHKERR post_proc_fe->writeFile("out_init.h5m");
3269
3271 };
3272
3273 auto solve_init = [&]() {
3275
3276 auto set_domain_rhs = [&](auto &pip) {
3278
3280
3281 pip.push_back(new OpCalculateScalarFieldValues("T", T_ptr));
3282 pip.push_back(new OpRhsSetInitT<AT, IT>(
3283 "T", nullptr, T_ptr, nullptr, boost::make_shared<double>(init_temp),
3284 boost::make_shared<double>(peak_temp)));
3285
3286 if (atom_test == 8) {
3287 auto TAU_ptr = boost::make_shared<VectorDouble>();
3288 auto EP_ptr = boost::make_shared<MatrixDouble>();
3289
3290 pip.push_back(new OpCalculateScalarFieldValues("TAU", TAU_ptr));
3291 auto min_tau = boost::make_shared<double>(1.0);
3292 auto max_tau = boost::make_shared<double>(2.0);
3293 pip.push_back(new OpRhsSetInitT<AT, IT>("TAU", nullptr, TAU_ptr,
3294 nullptr, min_tau, max_tau));
3295
3297 "EP", EP_ptr));
3298 auto min_EP = boost::make_shared<double>(0.0);
3299 auto max_EP = boost::make_shared<double>(0.01);
3301 "EP", nullptr, EP_ptr, nullptr, min_EP, max_EP));
3302 }
3303
3304 // using B = typename FormsIntegrators<DomainEleOp>::template Assembly<
3305 // AT>::template LinearForm<IT>;
3306 // using OpInternalForceCauchy =
3307 // typename B::template OpGradTimesSymTensor<1, SPACE_DIM, SPACE_DIM>;
3308 // using OpInternalForcePiola =
3309 // typename B::template OpGradTimesTensor<1, SPACE_DIM, SPACE_DIM>;
3310
3311 // using P = PlasticityIntegrators<DomainEleOp>;
3312
3313 // auto [common_plastic_ptr, common_hencky_ptr, common_thermal_ptr,
3314 // common_thermoelastic_ptr, common_thermoplastic_ptr] =
3315 // createCommonThermoPlasticOps<SPACE_DIM, IT, DomainEleOp>(
3316 // mField, "MAT_ELASTIC", "MAT_THERMAL", "MAT_THERMOPLASTIC", pip,
3317 // "U", "EP", "TAU", "T", scale, thermal_conductivity_scale,
3318 // heat_capacity_scale, inelastic_heat_fraction_scale,
3319 // Sev::inform);
3320 // auto m_D_ptr = common_hencky_ptr->matDPtr;
3321
3322 // using B = typename FormsIntegrators<DomainEleOp>::template Assembly<
3323 // AT>::template LinearForm<IT>;
3324 // using H = HenckyOps::HenckyIntegrators<DomainEleOp>;
3325 // auto coeff_expansion_ptr = common_thermal_ptr->getCoeffExpansionPtr();
3326 // auto ref_temp_ptr = common_thermal_ptr->getRefTempPtr();
3327 // pip.push_back(new
3328 // typename H::template
3329 // OpCalculateHenckyThermalStress<SPACE_DIM, IT>(
3330 // "U", T_ptr, common_hencky_ptr,
3331 // coeff_expansion_ptr, ref_temp_ptr));
3332 // pip.push_back(new typename H::template
3333 // OpCalculatePiolaStress<SPACE_DIM, IT>(
3334 // "U", common_hencky_ptr));
3335 // using OpInternalForcePiola =
3336 // typename B::template OpGradTimesTensor<1, SPACE_DIM, SPACE_DIM>;
3337 // pip.push_back(new OpInternalForcePiola(
3338 // "U", common_hencky_ptr->getMatFirstPiolaStress()));
3339
3341 };
3342
3343 auto set_domain_lhs = [&](auto &pip) {
3345
3347
3348 using OpLhsScalarLeastSquaresProj = FormsIntegrators<
3349 DomainEleOp>::Assembly<AT>::BiLinearForm<IT>::OpMass<1, 1>;
3350 pip.push_back(new OpLhsScalarLeastSquaresProj("T", "T"));
3351 if (atom_test == 8) {
3352 pip.push_back(new OpLhsScalarLeastSquaresProj("TAU", "TAU"));
3353 pip.push_back(
3355 "EP", "EP"));
3356 }
3357
3358 // auto [common_plastic_ptr, common_hencky_ptr, common_thermal_ptr,
3359 // common_thermoelastic_ptr, common_thermoplastic_ptr] =
3360 // createCommonThermoPlasticOps<SPACE_DIM, IT, DomainEleOp>(
3361 // mField, "MAT_ELASTIC", "MAT_THERMAL", "MAT_THERMOPLASTIC", pip,
3362 // "U", "EP", "TAU", "T", scale, thermal_conductivity_scale,
3363 // heat_capacity_scale, inelastic_heat_fraction_scale,
3364 // Sev::inform);
3365
3366 // auto m_D_ptr = common_hencky_ptr->matDPtr;
3367
3368 // using B = typename FormsIntegrators<DomainEleOp>::template Assembly<
3369 // AT>::template BiLinearForm<IT>;
3370 // using OpKPiola = typename B::template OpGradTensorGrad<1, SPACE_DIM,
3371 // SPACE_DIM, 1>;
3372
3373 // using H = HenckyIntegrators<DomainEleOp>;
3374 // pip.push_back(new OpCalculateScalarFieldValues("T", T_ptr));
3375 // auto coeff_expansion_ptr = common_thermal_ptr->getCoeffExpansionPtr();
3376 // auto ref_temp_ptr = common_thermal_ptr->getRefTempPtr();
3377 // pip.push_back(new
3378 // typename H::template
3379 // OpCalculateHenckyThermalStress<SPACE_DIM, IT>(
3380 // "U", T_ptr, common_hencky_ptr,
3381 // coeff_expansion_ptr, ref_temp_ptr));
3382 // pip.push_back(new typename H::template
3383 // OpCalculatePiolaStress<SPACE_DIM, IT>(
3384 // "U", common_hencky_ptr));
3385 // pip.push_back(new typename H::template OpHenckyTangent<SPACE_DIM, IT>(
3386 // "U", common_hencky_ptr));
3387 // pip.push_back(new OpKPiola("U", "U",
3388 // common_hencky_ptr->getMatTangent()));
3389 // pip.push_back(new typename HenckyOps::OpCalculateHenckyThermalStressdT<
3390 // SPACE_DIM, IT, AssemblyDomainEleOp>("U", "T",
3391 // common_hencky_ptr,
3392 // coeff_expansion_ptr));
3393
3395 };
3396
3397 auto create_sub_dm = [&](SmartPetscObj<DM> base_dm) {
3398 auto dm_sub = createDM(mField.get_comm(), "DMMOFEM");
3399 CHKERR DMMoFEMCreateSubDM(dm_sub, base_dm, "INIT_DM");
3400 CHKERR DMMoFEMSetSquareProblem(dm_sub, PETSC_TRUE);
3402 for (auto f : {"T"}) {
3405 }
3406 if (atom_test == 8) {
3407 for (auto f : {"TAU", "EP"}) {
3410 }
3411 }
3412 CHKERR DMSetUp(dm_sub);
3413 return dm_sub;
3414 };
3415
3416 auto fe_rhs = boost::make_shared<DomainEle>(mField);
3417 auto fe_lhs = boost::make_shared<DomainEle>(mField);
3418 fe_rhs->getRuleHook = vol_rule;
3419 fe_lhs->getRuleHook = vol_rule;
3420 CHKERR set_domain_rhs(fe_rhs->getOpPtrVector());
3421 CHKERR set_domain_lhs(fe_lhs->getOpPtrVector());
3422
3423 auto sub_dm = create_sub_dm(simple->getDM());
3424
3425 auto null_fe = boost::shared_ptr<FEMethod>();
3427 fe_lhs, null_fe, null_fe);
3429 null_fe, null_fe);
3430
3431 auto ksp = MoFEM::createKSP(mField.get_comm());
3432 CHKERR KSPSetDM(ksp, sub_dm);
3433 CHKERR KSPSetFromOptions(ksp);
3434
3435 auto D = createDMVector(sub_dm);
3436
3437 CHKERR KSPSolve(ksp, PETSC_NULLPTR, D);
3438
3439 CHKERR VecGhostUpdateBegin(D, INSERT_VALUES, SCATTER_FORWARD);
3440 CHKERR VecGhostUpdateEnd(D, INSERT_VALUES, SCATTER_FORWARD);
3441 CHKERR DMoFEMMeshToGlobalVector(sub_dm, D, INSERT_VALUES, SCATTER_REVERSE);
3442
3444 };
3445
3446 CHKERR solve_init();
3447 CHKERR post_proc(simple->getDM());
3448
3449 MOFEM_LOG("THERMAL", Sev::inform) << "Set thermoelastic initial conditions";
3450
3452}
PetscErrorCode DMoFEMMeshToGlobalVector(DM dm, Vec g, InsertMode mode, ScatterMode scatter_mode, RowColData rc=RowColData::COL)
set ghosted vector values on all existing mesh entities
Definition DMMoFEM.cpp:525
PetscErrorCode DMMoFEMCreateSubDM(DM subdm, DM dm, const char problem_name[])
Must be called by user to set Sub DM MoFEM data structures.
Definition DMMoFEM.cpp:215
PetscErrorCode DMMoFEMAddElement(DM dm, std::string fe_name)
add element to dm
Definition DMMoFEM.cpp:488
PetscErrorCode DMMoFEMSetSquareProblem(DM dm, PetscBool square_problem)
set squared problem
Definition DMMoFEM.cpp:450
PetscErrorCode DMMoFEMAddSubFieldRow(DM dm, const char field_name[])
Definition DMMoFEM.cpp:238
PetscErrorCode DMMoFEMKSPSetComputeRHS(DM dm, const char fe_name[], MoFEM::FEMethod *method, MoFEM::BasicMethod *pre_only, MoFEM::BasicMethod *post_only)
set KSP right hand side evaluation function
Definition DMMoFEM.cpp:627
PetscErrorCode DMMoFEMAddSubFieldCol(DM dm, const char field_name[])
Definition DMMoFEM.cpp:280
PetscErrorCode DMMoFEMKSPSetComputeOperators(DM dm, const char fe_name[], MoFEM::FEMethod *method, MoFEM::BasicMethod *pre_only, MoFEM::BasicMethod *post_only)
Set KSP operators and push mofem finite element methods.
Definition DMMoFEM.cpp:668
double D
auto createKSP(MPI_Comm comm)
auto createDM(MPI_Comm comm, const std::string dm_type_name)
Creates smart DM object.
OpPostProcMapInMoab< SPACE_DIM, SPACE_DIM > OpPPMap
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::BiLinearForm< GAUSS >::OpMass< 1, SPACE_DIM > OpMass
[Only used with Hooke equation (linear material model)]
Definition seepage.cpp:56
Rhs for testing EP mapping with initial conditions.
Post post-proc data at points from hash maps.
[Target temperature]
constexpr IntegrationType IT
double peak_temp
double init_temp

◆ integrateElements()

MoFEMErrorCode Example::integrateElements ( )
private

[Push operators to pipeline]

[Integrate]

Examples
mofem/tutorials/fun-1_integration/integration.cpp.

Definition at line 218 of file integration.cpp.

218 {
220 // Zero global vector
221 CHKERR VecZeroEntries(commonDataPtr->petscVec);
222
223 // Integrate elements by executing operators in the pipeline
225 CHKERR pipeline_mng->loopFiniteElements();
226
227 // Assemble MPI vector
228 CHKERR VecAssemblyBegin(commonDataPtr->petscVec);
229 CHKERR VecAssemblyEnd(commonDataPtr->petscVec);
231}
MoFEMErrorCode loopFiniteElements(SmartPetscObj< DM > dm=nullptr)
Iterate finite elements.
boost::shared_ptr< CommonData > commonDataPtr

◆ integrationRule()

static int Example::integrationRule ( int  ,
int  ,
int  p_data 
)
inlinestaticprivate
Examples
mofem/tutorials/scl-8_heat_radiation/radiation.cpp.

Definition at line 52 of file radiation.cpp.

52{ return 2 * p_data; };

◆ kspSolve()

MoFEMErrorCode Example::kspSolve ( )
private

[Push operators to pipeline]

[Solve]

Examples
mofem/tutorials/scl-8_heat_radiation/radiation.cpp.

Definition at line 230 of file radiation.cpp.

230 {
234 auto ts = pipeline_mng->createTSIM();
235
236 double ftime = 1;
237 CHKERR TSSetMaxTime(ts, ftime);
238 CHKERR TSSetFromOptions(ts);
239 CHKERR TSSetExactFinalTime(ts, TS_EXACTFINALTIME_MATCHSTEP);
240
241 auto T = createDMVector(simple->getDM());
242 CHKERR DMoFEMMeshToLocalVector(simple->getDM(), T, INSERT_VALUES,
243 SCATTER_FORWARD);
244
245 CHKERR TSSolve(ts, T);
246 CHKERR TSGetTime(ts, &ftime);
247
248 PetscInt steps, snesfails, rejects, nonlinits, linits;
249 CHKERR TSGetStepNumber(ts, &steps);
250 CHKERR TSGetSNESFailures(ts, &snesfails);
251 CHKERR TSGetStepRejections(ts, &rejects);
252 CHKERR TSGetSNESIterations(ts, &nonlinits);
253 CHKERR TSGetKSPIterations(ts, &linits);
254 MOFEM_LOG_C("EXAMPLE", Sev::inform,
255 "steps %d (%d rejected, %d SNES fails), ftime %g, nonlinits "
256 "%d, linits %d",
257 steps, rejects, snesfails, ftime, nonlinits, linits);
258
260}
#define MOFEM_LOG_C(channel, severity, format,...)
SmartPetscObj< TS > createTSIM(SmartPetscObj< DM > dm=nullptr)
Create TS (time) implicit solver.

◆ lhsFlux()

double Example::lhsFlux ( const double  x,
const double  y,
const double   
)
staticprivate
Examples
mofem/tutorials/mix-1_light_intensity_equation/phase.cpp.

Definition at line 165 of file phase.cpp.

165 {
166 const auto idx = getCoordsInImage(x, y);
167 const auto &m = iI[focalIndex];
168 return 1. / m(idx.first, idx.second);
169}
static std::pair< int, int > getCoordsInImage(double x, double y)
Definition phase.cpp:131

◆ mechanicalBC()

MoFEMErrorCode Example::mechanicalBC ( BitRefLevel  bit,
BitRefLevel  mask 
)
private

[Initial conditions]

[Mechanical boundary conditions]

Examples
thermoplastic.cpp.

Definition at line 3456 of file thermoplastic.cpp.

3456 {
3458
3459 auto simple = mField.getInterface<Simple>();
3460 auto bc_mng = mField.getInterface<BcManager>();
3461
3462 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), "REMOVE_X",
3463 "U", 0, 0, true,
3465 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), "REMOVE_Y",
3466 "U", 1, 1, true,
3468 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), "REMOVE_Z",
3469 "U", 2, 2, true,
3471 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(),
3472 "REMOVE_ALL", "U", 0, 3, true,
3474
3475#ifdef ADD_CONTACT
3476 for (auto b : {"FIX_X", "REMOVE_X"})
3477 CHKERR bc_mng->removeBlockDOFsOnEntities(
3478 simple->getProblemName(), b, "SIGMA", 0, 0, false, is_distributed_mesh);
3479 for (auto b : {"FIX_Y", "REMOVE_Y"})
3480 CHKERR bc_mng->removeBlockDOFsOnEntities(
3481 simple->getProblemName(), b, "SIGMA", 1, 1, false, is_distributed_mesh);
3482 for (auto b : {"FIX_Z", "REMOVE_Z"})
3483 CHKERR bc_mng->removeBlockDOFsOnEntities(
3484 simple->getProblemName(), b, "SIGMA", 2, 2, false, is_distributed_mesh);
3485 for (auto b : {"FIX_ALL", "REMOVE_ALL"})
3486 CHKERR bc_mng->removeBlockDOFsOnEntities(
3487 simple->getProblemName(), b, "SIGMA", 0, 3, false, is_distributed_mesh);
3488 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(),
3489 "NO_CONTACT", "SIGMA", 0, 3, false,
3491#endif
3492
3493 CHKERR bc_mng->pushMarkDOFsOnEntities<DisplacementCubitBcData>(
3494 simple->getProblemName(), "U");
3495
3496 auto &bc_map = bc_mng->getBcMapByBlockName();
3497 for (auto bc : bc_map)
3498 MOFEM_LOG("PLASTICITY", Sev::verbose) << "Marker " << bc.first;
3499
3501}
PetscBool is_distributed_mesh

◆ OPs() [1/4]

MoFEMErrorCode Example::OPs ( )
private

[Boundary condition]

[Thermal Boundary conditions]

[Push operators to pipeline]

[Only used for dynamics]

[Only used for dynamics]

[Only used for dynamics]

[Only used for dynamics]

Examples
mofem/tutorials/adv-0_plasticity/plastic.cpp, mofem/tutorials/scl-8_heat_radiation/radiation.cpp, plastic.cpp, and thermoplastic.cpp.

Definition at line 648 of file plastic.cpp.

648 {
650 auto pip_mng = mField.getInterface<PipelineManager>();
651
652 auto integration_rule_bc = [](int, int, int ao) { return 2 * ao; };
653
654 auto vol_rule = [](int, int, int ao) { return 2 * ao + geom_order - 1; };
655
656 auto add_boundary_ops_lhs_mechanical = [&](auto &pip) {
658
660 pip, {HDIV}, "GEOMETRY");
661 pip.push_back(new OpSetHOWeightsOnSubDim<SPACE_DIM>());
662
663 // Add Natural BCs to LHS
665 pip, mField, "U", Sev::inform);
666
667#ifdef ADD_CONTACT
669 CHKERR
670 ContactOps::opFactoryBoundaryLhs<SPACE_DIM, AT, GAUSS, BoundaryEleOp>(
671 pip, "SIGMA", "U");
672 CHKERR
673 ContactOps::opFactoryBoundaryToDomainLhs<SPACE_DIM, AT, IT, DomainEle>(
674 mField, pip, simple->getDomainFEName(), "SIGMA", "U", "GEOMETRY",
675 vol_rule);
676#endif // ADD_CONTACT
677
679 };
680
681 auto add_boundary_ops_rhs_mechanical = [&](auto &pip) {
683
685 pip, {HDIV}, "GEOMETRY");
686 pip.push_back(new OpSetHOWeightsOnSubDim<SPACE_DIM>());
687
688 // Add Natural BCs to RHS
690 pip, mField, "U", {boost::make_shared<ScaledTimeScale>()}, Sev::inform);
691
692#ifdef ADD_CONTACT
693 CHKERR ContactOps::opFactoryBoundaryRhs<SPACE_DIM, AT, IT, BoundaryEleOp>(
694 pip, "SIGMA", "U");
695#endif // ADD_CONTACT
696
698 };
699
700 auto add_domain_ops_lhs = [this](auto &pip) {
703 pip, {H1, HDIV}, "GEOMETRY");
704
705 if (is_quasi_static == PETSC_FALSE) {
706
707 //! [Only used for dynamics]
710 //! [Only used for dynamics]
711
712 auto get_inertia_and_mass_damping = [this](const double, const double,
713 const double) {
714 auto *pip = mField.getInterface<PipelineManager>();
715 auto &fe_domain_lhs = pip->getDomainLhsFE();
716 return (rho / scale) * fe_domain_lhs->ts_aa +
717 (alpha_damping / scale) * fe_domain_lhs->ts_a;
718 };
719 pip.push_back(new OpMass("U", "U", get_inertia_and_mass_damping));
720 }
721
722 CHKERR PlasticOps::opFactoryDomainLhs<SPACE_DIM, AT, IT, DomainEleOp>(
723 mField, "MAT_PLASTIC", pip, "U", "EP", "TAU");
724
726 };
727
728 auto add_domain_ops_rhs = [this](auto &pip) {
730
732 pip, {H1, HDIV}, "GEOMETRY");
733
735 pip, mField, "U",
736 {boost::make_shared<ScaledTimeScale>("body_force_hist.txt")},
737 Sev::inform);
738
739 // only in case of dynamics
740 if (is_quasi_static == PETSC_FALSE) {
741
742 //! [Only used for dynamics]
745 //! [Only used for dynamics]
746
747 auto mat_acceleration = boost::make_shared<MatrixDouble>();
749 "U", mat_acceleration));
750 pip.push_back(
751 new OpInertiaForce("U", mat_acceleration, [](double, double, double) {
752 return rho / scale;
753 }));
754 if (alpha_damping > 0) {
755 auto mat_velocity = boost::make_shared<MatrixDouble>();
756 pip.push_back(
757 new OpCalculateVectorFieldValuesDot<SPACE_DIM>("U", mat_velocity));
758 pip.push_back(
759 new OpInertiaForce("U", mat_velocity, [](double, double, double) {
760 return alpha_damping / scale;
761 }));
762 }
763 }
764
765 CHKERR PlasticOps::opFactoryDomainRhs<SPACE_DIM, AT, IT, DomainEleOp>(
766 mField, "MAT_PLASTIC", pip, "U", "EP", "TAU");
767
768#ifdef ADD_CONTACT
769 CHKERR ContactOps::opFactoryDomainRhs<SPACE_DIM, AT, IT, DomainEleOp>(
770 pip, "SIGMA", "U");
771#endif // ADD_CONTACT
772
774 };
775
776 CHKERR add_domain_ops_lhs(pip_mng->getOpDomainLhsPipeline());
777 CHKERR add_domain_ops_rhs(pip_mng->getOpDomainRhsPipeline());
778
779 // Boundary
780 CHKERR add_boundary_ops_lhs_mechanical(pip_mng->getOpBoundaryLhsPipeline());
781 CHKERR add_boundary_ops_rhs_mechanical(pip_mng->getOpBoundaryRhsPipeline());
782
783 CHKERR pip_mng->setDomainRhsIntegrationRule(vol_rule);
784 CHKERR pip_mng->setDomainLhsIntegrationRule(vol_rule);
785
786 CHKERR pip_mng->setBoundaryLhsIntegrationRule(integration_rule_bc);
787 CHKERR pip_mng->setBoundaryRhsIntegrationRule(integration_rule_bc);
788
789 auto create_reaction_pipeline = [&](auto &pip) {
792 pip, {H1}, "GEOMETRY");
793 CHKERR PlasticOps::opFactoryDomainReactions<SPACE_DIM, AT, IT, DomainEleOp>(
794 mField, "MAT_PLASTIC", pip, "U", "EP", "TAU");
796 };
797
798 CHKERR pip_mng->setEvaluationIntegrationRule(vol_rule);
799 CHKERR create_reaction_pipeline(pip_mng->getOpEvaluationPipeline());
800 auto &reaction_fe = pip_mng->getEvaluationFE();
801 reaction_fe->postProcessHook =
803
805}
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::LinearForm< GAUSS >::OpBaseTimesVector< 1, SPACE_DIM, SPACE_DIM > OpInertiaForce
Class (Function) to calculate residual side diagonal.
Definition Essential.hpp:49
Approximate field values for given petsc vector.
constexpr AssemblyType AT

◆ OPs() [2/4]

MoFEMErrorCode Example::OPs ( )
private

◆ OPs() [3/4]

MoFEMErrorCode Example::OPs ( )
private

◆ OPs() [4/4]

MoFEMErrorCode Example::OPs ( )
private

◆ opThermoPlasticFactoryDomainLhs()

template<int DIM, AssemblyType A, IntegrationType I, typename DomainEleOp >
MoFEMErrorCode Example::opThermoPlasticFactoryDomainLhs ( MoFEM::Interface &  m_field,
std::string  block_name,
std::string  thermal_block_name,
std::string  thermoelastic_block_name,
std::string  thermoplastic_block_name,
Pip &  pip,
std::string  u,
std::string  ep,
std::string  tau,
std::string  temperature 
)
inlineprivate
Examples
thermoplastic.cpp.

Definition at line 1041 of file thermoplastic.cpp.

1045 {
1047
1048 using namespace HenckyOps;
1050
1051 using B = typename FormsIntegrators<DomainEleOp>::template Assembly<
1052 A>::template BiLinearForm<I>;
1053 using OpKPiola = typename B::template OpGradTensorGrad<1, DIM, DIM, 1>;
1054 using OpKCauchy = typename B::template OpGradSymTensorGrad<1, DIM, DIM, 0>;
1055
1057
1058 auto [common_plastic_ptr, common_hencky_ptr, common_thermal_ptr,
1059 common_thermoelastic_ptr, common_thermoplastic_ptr] =
1060 createCommonThermoPlasticOps<DIM, I, DomainEleOp>(
1061 m_field, block_name, thermal_block_name, thermoelastic_block_name,
1062 thermoplastic_block_name, pip, u, ep, tau, temperature, scale,
1064 inelastic_heat_fraction_scaling, Sev::inform);
1065
1066 auto m_D_ptr = common_hencky_ptr->matDPtr;
1067
1069 ep, common_plastic_ptr->getPlasticStrainDotPtr()));
1070 pip.push_back(new OpCalculateScalarFieldValuesDot(
1071 tau, common_plastic_ptr->getPlasticTauDotPtr()));
1072 pip.push_back(new typename P::template OpCalculatePlasticity<DIM, I>(
1073 u, common_plastic_ptr, m_D_ptr, common_thermoplastic_ptr));
1074
1075 if (common_hencky_ptr) {
1076 pip.push_back(new typename H::template OpHenckyTangent<DIM, I, 0>(
1077 u, common_hencky_ptr, m_D_ptr));
1078 pip.push_back(new OpKPiola(u, u, common_hencky_ptr->getMatTangent()));
1079 pip.push_back(
1080 new typename P::template Assembly<A>::
1081 template OpCalculatePlasticInternalForceLhs_LogStrain_dEP<DIM, I>(
1082 u, ep, common_plastic_ptr, common_hencky_ptr, m_D_ptr));
1083 } else {
1084 pip.push_back(new OpKCauchy(u, u, m_D_ptr));
1085 pip.push_back(new typename P::template Assembly<A>::
1086 template OpCalculatePlasticInternalForceLhs_dEP<DIM, I>(
1087 u, ep, common_plastic_ptr, m_D_ptr));
1088 }
1089
1090 if (common_hencky_ptr) {
1091 pip.push_back(
1092 new typename P::template Assembly<A>::
1093 template OpCalculateConstraintsLhs_LogStrain_dU<DIM, I>(
1094 tau, u, common_plastic_ptr, common_hencky_ptr, m_D_ptr));
1095 pip.push_back(
1096 new typename P::template Assembly<A>::
1097 template OpCalculatePlasticFlowLhs_LogStrain_dU<DIM, I>(
1098 ep, u, common_plastic_ptr, common_hencky_ptr, m_D_ptr));
1099 } else {
1100 pip.push_back(new typename P::template Assembly<A>::
1101 template OpCalculateConstraintsLhs_dU<DIM, I>(
1102 tau, u, common_plastic_ptr, m_D_ptr));
1103 pip.push_back(new typename P::template Assembly<A>::
1104 template OpCalculatePlasticFlowLhs_dU<DIM, I>(
1105 ep, u, common_plastic_ptr, m_D_ptr));
1106 }
1107
1108 pip.push_back(new typename P::template Assembly<A>::
1109 template OpCalculatePlasticFlowLhs_dEP<DIM, I>(
1110 ep, ep, common_plastic_ptr, m_D_ptr));
1111 pip.push_back(new typename P::template Assembly<A>::
1112 template OpCalculatePlasticFlowLhs_dTAU<DIM, I>(
1113 ep, tau, common_plastic_ptr, m_D_ptr));
1114 pip.push_back(
1116 ep, temperature, common_thermoplastic_ptr));
1117 pip.push_back(new typename P::template Assembly<A>::
1118 template OpCalculateConstraintsLhs_dEP<DIM, I>(
1119 tau, ep, common_plastic_ptr, m_D_ptr));
1120 pip.push_back(
1121 new typename P::template Assembly<
1122 A>::template OpCalculateConstraintsLhs_dTAU<I>(tau, tau,
1123 common_plastic_ptr));
1124
1125 // TODO: add scenario for when not using Hencky material
1126 pip.push_back(new typename H::template OpCalculateHenckyThermalStressdT<
1127 DIM, I, AssemblyDomainEleOp, 0>(
1128 u, temperature, common_hencky_ptr,
1129 common_thermoelastic_ptr->getCoeffExpansionPtr()));
1130
1131 auto inelastic_heat_frac_ptr =
1132 common_thermoplastic_ptr->getInelasticHeatFractionPtr();
1134 [inelastic_heat_frac_ptr](const double, const double, const double) {
1135 return (*inelastic_heat_frac_ptr);
1136 };
1137
1138 // TODO: add scenario for when not using Hencky material
1141 temperature, temperature, common_hencky_ptr, common_plastic_ptr,
1143 common_thermoelastic_ptr->getCoeffExpansionPtr()));
1144
1145 // TODO: add scenario for when not using Hencky material
1148 temperature, ep, common_hencky_ptr, common_plastic_ptr,
1150
1151 // TODO: add scenario for when not using Hencky material
1154 temperature, u, common_hencky_ptr, common_plastic_ptr,
1156
1158 tau, temperature, common_thermoplastic_ptr));
1159
1161 }
[HenckyOps]
Definition HenckyOps.hpp:12
OpCalculateScalarFieldValuesFromPetscVecImpl< PetscData::CTX_SET_X_T > OpCalculateScalarFieldValuesDot
constexpr IntegrationType I
constexpr AssemblyType A
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::BiLinearForm< GAUSS >::OpGradTensorGrad< 1, SPACE_DIM, SPACE_DIM, -1 > OpKPiola
[Only used for dynamics]
Definition seepage.cpp:63
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::BiLinearForm< GAUSS >::OpGradSymTensorGrad< 1, SPACE_DIM, SPACE_DIM, 0 > OpKCauchy
[Only used with Hooke equation (linear material model)]
Definition seepage.cpp:49
Calculate symmetric tensor field rates ant integratio pts.
double inelastic_heat_fraction
fraction of plastic dissipation converted to heat
constexpr bool IS_LARGE_STRAINS

◆ opThermoPlasticFactoryDomainRhs()

template<int DIM, AssemblyType A, IntegrationType I, typename DomainEleOp >
MoFEMErrorCode Example::opThermoPlasticFactoryDomainRhs ( MoFEM::Interface &  m_field,
std::string  block_name,
std::string  thermal_block_name,
std::string  thermoelastic_block_name,
std::string  thermoplastic_block_name,
Pip &  pip,
std::string  u,
std::string  ep,
std::string  tau,
std::string  temperature 
)
inlineprivate
Examples
thermoplastic.cpp.

Definition at line 960 of file thermoplastic.cpp.

964 {
966
967 using B = typename FormsIntegrators<DomainEleOp>::template Assembly<
968 A>::template LinearForm<I>;
970 typename B::template OpGradTimesSymTensor<1, DIM, DIM>;
972 typename B::template OpGradTimesTensor<1, DIM, DIM>;
973
975
977
978 auto [common_plastic_ptr, common_hencky_ptr, common_thermal_ptr,
979 common_thermoelastic_ptr, common_thermoplastic_ptr] =
980 createCommonThermoPlasticOps<DIM, I, DomainEleOp>(
981 m_field, block_name, thermal_block_name, thermoelastic_block_name,
982 thermoplastic_block_name, pip, u, ep, tau, temperature, scale,
985
986 auto m_D_ptr = common_hencky_ptr->matDPtr;
987
989 ep, common_plastic_ptr->getPlasticStrainDotPtr()));
990 pip.push_back(new OpCalculateScalarFieldValuesDot(
991 tau, common_plastic_ptr->getPlasticTauDotPtr()));
992 pip.push_back(new OpCalculateScalarFieldValues(
993 temperature, common_thermoplastic_ptr->getTempPtr()));
995 "FLUX", common_thermoplastic_ptr->getHeatFluxPtr()));
996 pip.push_back(new typename P::template OpCalculatePlasticity<DIM, I>(
997 u, common_plastic_ptr, m_D_ptr, common_thermoplastic_ptr));
998
999 pip.push_back(
1000 new
1001 typename H::template OpCalculateHenckyThermoPlasticStress<DIM, I, 0>(
1002 u, common_thermoplastic_ptr->getTempPtr(), common_hencky_ptr,
1003 common_thermoelastic_ptr->getCoeffExpansionPtr(),
1004 common_thermoelastic_ptr->getRefTempPtr()));
1005
1006 // Calculate internal forces
1007 if (common_hencky_ptr) {
1008 pip.push_back(new OpInternalForcePiola(
1009 u, common_hencky_ptr->getMatFirstPiolaStress()));
1010 } else {
1011 pip.push_back(
1012 new OpInternalForceCauchy(u, common_plastic_ptr->mStressPtr));
1013 }
1014
1015 auto inelastic_heat_frac_ptr =
1016 common_thermoplastic_ptr->getInelasticHeatFractionPtr();
1018 [inelastic_heat_frac_ptr](const double, const double, const double) {
1019 return (*inelastic_heat_frac_ptr);
1020 };
1021
1022 // TODO: add scenario for when not using Hencky material
1025 temperature, common_hencky_ptr->getMatHenckyStress(),
1026 common_plastic_ptr->getPlasticStrainDotPtr(), inelastic_heat_fraction));
1027
1028 pip.push_back(
1029 new
1030 typename P::template Assembly<A>::template OpCalculateConstraintsRhs<I>(
1031 tau, common_plastic_ptr, m_D_ptr));
1032 pip.push_back(
1033 new
1034 typename P::template Assembly<A>::template OpCalculatePlasticFlowRhs<
1035 DIM, I>(ep, common_plastic_ptr, m_D_ptr));
1036
1038 }
FormsIntegrators< DomainEleOp >::Assembly< A >::LinearForm< I >::OpGradTimesTensor< 1, FIELD_DIM, SPACE_DIM > OpGradTimesTensor
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::LinearForm< GAUSS >::OpGradTimesTensor< 1, SPACE_DIM, SPACE_DIM > OpInternalForcePiola
Definition seepage.cpp:65
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::LinearForm< GAUSS >::OpGradTimesSymTensor< 1, SPACE_DIM, SPACE_DIM > OpInternalForceCauchy
Definition seepage.cpp:51

◆ outputResults() [1/13]

MoFEMErrorCode Example::outputResults ( )
private

[Solve]

[Getting norms]

[Postprocess results]

[Postprocessing results]

[Postprocess clean]

[Postprocess clean]

[Postprocess initialise]

[Postprocess initialise]

Examples
mofem/tutorials/adv-4_dynamic_first_order_con_law/dynamic_first_order_con_law.cpp, mofem/tutorials/adv-6_dg_projection/between_meshes_dg_projection.cpp, mofem/tutorials/clx-0_acoustics/helmholtz.cpp, mofem/tutorials/fun-2_plot_base/plot_base.cpp, mofem/tutorials/mix-1_light_intensity_equation/phase.cpp, mofem/tutorials/scl-0_least_squares/approximation.cpp, mofem/tutorials/scl-9_heat_method/heat_method.cpp, mofem/tutorials/vec-1_eigen_elasticity/eigen_elastic.cpp, mofem/tutorials/vec-3_nonlinear_dynamics/nonlinear_dynamic_elastic.cpp, mofem/tutorials/vec-4_shallow_wave/shallow_wave.cpp, and nonlinear_elastic.cpp.

Definition at line 1159 of file dynamic_first_order_con_law.cpp.

1159 {
1161 PetscBool test_flg = PETSC_FALSE;
1162 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-test", &test_flg, PETSC_NULLPTR);
1163 if (test_flg) {
1164 auto *simple = mField.getInterface<Simple>();
1165 auto T = createDMVector(simple->getDM());
1166 CHKERR DMoFEMMeshToLocalVector(simple->getDM(), T, INSERT_VALUES,
1167 SCATTER_FORWARD);
1168 double nrm2;
1169 CHKERR VecNorm(T, NORM_2, &nrm2);
1170 MOFEM_LOG("EXAMPLE", Sev::inform) << "Regression norm " << nrm2;
1171 constexpr double regression_value = 0.0194561;
1172 if (fabs(nrm2 - regression_value) > 1e-2)
1173 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
1174 "Regression test failed; wrong norm value.");
1175 }
1177}

◆ outputResults() [2/13]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [3/13]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [4/13]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [5/13]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [6/13]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [7/13]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [8/13]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [9/13]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [10/13]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [11/13]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [12/13]

MoFEMErrorCode Example::outputResults ( const int  i)
private

[Solve]

[Postprocess results]

Definition at line 491 of file phase.cpp.

491 {
494
496
497 auto post_proc_fe = boost::make_shared<PostProcEle>(mField);
498 CHKERR AddHOOps<2, 2, 2>::add(post_proc_fe->getOpPtrVector(), {HDIV, L2});
499
500 auto phi_ptr = boost::make_shared<VectorDouble>();
501 auto s_ptr = boost::make_shared<MatrixDouble>();
502 post_proc_fe->getOpPtrVector().push_back(
503 new OpCalculateScalarFieldValues("PHI", phi_ptr));
504 post_proc_fe->getOpPtrVector().push_back(
505 new OpCalculateHVecVectorField<3>("S", s_ptr));
506
508
509 post_proc_fe->getOpPtrVector().push_back(
510
511 new OpPPMap(post_proc_fe->getPostProcMesh(),
512 post_proc_fe->getMapGaussPts(),
513
514 OpPPMap::DataMapVec{{"PHI", phi_ptr}},
515
516 OpPPMap::DataMapMat{{"S", s_ptr}},
517
519
521
522 )
523
524 );
525
526 pipeline_mng->getDomainPostProcFE() = post_proc_fe;
527 CHKERR pipeline_mng->loopFiniteElementsPostProc();
528 CHKERR post_proc_fe->writeFile("out_" + boost::lexical_cast<std::string>(i) +
529 ".h5m");
531}
MoFEMErrorCode loopFiniteElementsPostProc(SmartPetscObj< DM > dm=nullptr)
Iterate postprocessing finite elements.
std::map< std::string, ScalarDataPtr > DataMapVec
std::map< std::string, boost::shared_ptr< MatrixDouble > > DataMapMat
boost::shared_ptr< FEMethod > & getDomainPostProcFE()
Get domain postprocessing finite element.

◆ outputResults() [13/13]

MoFEMErrorCode Example::outputResults ( std::string  file_name)
private

[Project results]

[Output results]

Definition at line 520 of file between_meshes_dg_projection.cpp.

520 {
522
523 auto pipeline_mng = mField.getInterface<PipelineManager>();
524
525 auto post_proc_fe = boost::make_shared<PostProcFaceEle>(mField);
527 post_proc_fe->getOpPtrVector(), {H1});
528
529 auto u_ptr = boost::make_shared<VectorDouble>();
530 post_proc_fe->getOpPtrVector().push_back(
532 auto s_ptr = boost::make_shared<VectorDouble>();
533 post_proc_fe->getOpPtrVector().push_back(
535
536 auto grad_u_ptr = boost::make_shared<MatrixDouble>();
537 post_proc_fe->getOpPtrVector().push_back(
539 auto grad_s_ptr = boost::make_shared<MatrixDouble>();
540 post_proc_fe->getOpPtrVector().push_back(
542
543
545
546 post_proc_fe->getOpPtrVector().push_back(
547
548 new OpPPMap(
549 post_proc_fe->getPostProcMesh(), post_proc_fe->getMapGaussPts(),
550
551 OpPPMap::DataMapVec{{FIELD_NAME_U, u_ptr}, {FIELD_NAME_S, s_ptr}},
552
554
555 {"GRAD_" + std::string(FIELD_NAME_U), grad_u_ptr},
556 {"GRAD_" + std::string(FIELD_NAME_S), grad_s_ptr}
557
558 },
559
561
563
564 )
565
566 );
567
568 pipeline_mng->getDomainPostProcFE() = post_proc_fe;
569 CHKERR pipeline_mng->loopFiniteElementsPostProc();
570 CHKERR post_proc_fe->writeFile(file_name);
571
573}
constexpr char FIELD_NAME_U[]
constexpr char FIELD_NAME_S[]
Get field gradients at integration pts for scalar field rank 0, i.e. vector field.

◆ PetscErrorCode::MyTSResizeTransfer()

Example::PetscErrorCode::MyTSResizeTransfer ( TS  ,
PetscInt  ,
Vec  [],
Vec  [],
void *   
)
private

◆ postProcess() [1/2]

MoFEMErrorCode Example::postProcess ( )
private

[Integrate]

[Solve]

[Print results]

[Postprocess results]

Examples
mofem/tutorials/fun-1_integration/integration.cpp, and mofem/tutorials/scl-8_heat_radiation/radiation.cpp.

Definition at line 235 of file integration.cpp.

235 {
237 const double *array;
238 CHKERR VecGetArrayRead(commonDataPtr->petscVec, &array);
239 if (mField.get_comm_rank() == 0) {
240 MOFEM_LOG_C("SELF", Sev::inform, "Mass %6.4e", array[CommonData::ZERO]);
241 MOFEM_LOG_C("SELF", Sev::inform,
242 "First moment of inertia [ %6.4e, %6.4e, %6.4e ]",
244 array[CommonData::FIRST_Z]);
245 MOFEM_LOG_C("SELF", Sev::inform,
246 "Second moment of inertia [ %6.4e, %6.4e, %6.4e; %6.4e %6.4e; "
247 "%6.4e ]",
251 }
252 CHKERR VecRestoreArrayRead(commonDataPtr->petscVec, &array);
254}

◆ postProcess() [2/2]

MoFEMErrorCode Example::postProcess ( )
private

◆ postprocessElastic() [1/2]

MoFEMErrorCode Example::postprocessElastic ( int  iter,
SmartPetscObj< Vec >  adjoint_vector = nullptr 
)
private

Post-process and output results.

[Solve]

[Postprocess results]

Examples
mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 1326 of file adjoint.cpp.

1327 {
1329 auto simple = mField.getInterface<Simple>();
1333 "out_elastic_" + std::to_string(iter) + ".h5m",
1334 {{"ADJOINT", adjoint_vector}}, {}, Sev::noisy);
1336}
MoFEMErrorCode postProcessElasticResults(MoFEM::Interface &mField, SmartPetscObj< DM > dm, const std::string &domain_fe_name, const std::string &out_file_name, std::vector< std::pair< std::string, SmartPetscObj< Vec > > > extra_vectors={}, const std::vector< std::string > &tags_to_transfer={}, const Sev hooke_ops_sev=Sev::verbose)
ElementsAndOps< SPACE_DIM >::SideEle SideEle
Definition plastic.cpp:61
PipelineManager::ElementsAndOpsByDim< SPACE_DIM >::BoundaryEle BoundaryEle
PipelineManager::ElementsAndOpsByDim< SPACE_DIM >::DomainEle DomainEle

◆ postprocessElastic() [2/2]

MoFEMErrorCode Example::postprocessElastic ( int  iter,
SmartPetscObj< Vec >  gradient_vector = nullptr,
SmartPetscObj< Vec >  adjoint_vector = nullptr,
SmartPetscObj< Vec >  dJ_du = nullptr 
)
private

Post-process and output results.

[Solve]

[Postprocess results]

Definition at line 677 of file gradient.cpp.

680 {
683 std::vector<std::pair<std::string, SmartPetscObj<Vec>>> additional_vecs;
684 if (gradient_vector) {
685 additional_vecs.emplace_back("GRADIENT", gradient_vector);
686 }
687 if (adjoint_vector) {
688 additional_vecs.emplace_back("ADJOINT", adjoint_vector);
689 }
690 if (dJ_du) {
691 additional_vecs.emplace_back("dJ_dU", dJ_du);
692 }
696 "out_elastic_" + std::to_string(iter) + ".h5m", additional_vecs,
697 {}, Sev::noisy);
699}

◆ projectResults()

MoFEMErrorCode Example::projectResults ( BitRefLevel  parent_bit,
BitRefLevel  child_bit,
BitRefLevel  refine_bit 
)
private

[Solve]

[Project results]

Examples
mofem/tutorials/adv-6_dg_projection/between_meshes_dg_projection.cpp.

Definition at line 255 of file between_meshes_dg_projection.cpp.

257 {
260 auto pipeline_mng = mField.getInterface<PipelineManager>();
261
262 pipeline_mng->getDomainLhsFE().reset();
263 pipeline_mng->getDomainRhsFE().reset();
264 pipeline_mng->getOpDomainRhsPipeline().clear();
265
266 auto rule = [](int, int, int p) -> int { return 2 * p; };
267 CHKERR pipeline_mng->setDomainRhsIntegrationRule(rule);
268
269 // OpLoopThis, is child operator, and is use to execute
270 // fe_child_ptr, only on bit ref level and mask
271 // for child elements
272 auto get_child_op = [&](auto &pip) {
273 auto op_this_child =
275 child_bit | refine_bit, Sev::noisy);
276 auto fe_child_ptr = op_this_child->getThisFEPtr();
277 fe_child_ptr->getRuleHook = [] (int, int, int p) { return -1; };
278 Range child_edges;
279 CHKERR mField.getInterface<BitRefManager>()->getEntitiesByTypeAndRefLevel(
280 refine_bit, child_bit | refine_bit, MBEDGE, child_edges);
281 // set integration rule, such that integration points are not on flipped edge
282 CHKERR setDGSetIntegrationPoints<SPACE_DIM>(
283 fe_child_ptr->setRuleHook, [](int, int, int p) { return 2 * p; },
284 boost::make_shared<Range>(child_edges));
285 pip.push_back(op_this_child);
286 return fe_child_ptr;
287 };
288
289 // Use field evaluator to calculate field values on parent bitref level,
290 // i.e. elements which were flipped.
291 auto get_field_eval_op = [&](auto fe_child_ptr) {
292 auto field_eval_ptr = mField.getInterface<FieldEvaluatorInterface>();
293
294 // Get pointer of FieldEvaluator data. Note finite element and method
295 // set integration points is destroyed when this pointer is releases
296 auto field_eval_data = field_eval_ptr->getData<DomainEle>();
297 // Build tree for particular element
298 CHKERR field_eval_ptr->buildTree<SPACE_DIM>(
299 field_eval_data, simpleInterface->getDomainFEName(), parent_bit,
300 parent_bit | child_bit);
301
302 // You can add more fields here
303 auto data_U_ptr = boost::make_shared<MatrixDouble>();
304 auto eval_data_U_ptr = boost::make_shared<MatrixDouble>();
305 auto data_S_ptr = boost::make_shared<MatrixDouble>();
306 auto eval_data_S_ptr = boost::make_shared<MatrixDouble>();
307
308
309 if (auto fe_eval_ptr = field_eval_data->feMethodPtr) {
310 fe_eval_ptr->getRuleHook = [] (int, int, int p) { return -1; };
311 fe_eval_ptr->getOpPtrVector().push_back(
313 eval_data_U_ptr));
314 fe_eval_ptr->getOpPtrVector().push_back(
316 eval_data_S_ptr));
317
318 auto op_test = new DomainEleOp(NOSPACE, DomainEleOp::OPSPACE);
319 op_test->doWorkRhsHook =
320 [](DataOperator *base_op_ptr, int side, EntityType type,
323
324 auto op_ptr = static_cast<DomainEleOp *>(base_op_ptr);
325 MOFEM_LOG_CHANNEL("SELF");
326 MOFEM_LOG("SELF", Sev::noisy)
327 << "Field evaluator method pointer is valid";
328 MOFEM_LOG("SELF", Sev::noisy)
329 << op_ptr->getGaussPts();
330 MOFEM_LOG("SELF", Sev::noisy)
331 << "Loop size " << op_ptr->getPtrFE()->getLoopSize();
332 MOFEM_LOG("SELF", Sev::noisy)
333 << "Coords at gauss pts: " << op_ptr->getCoordsAtGaussPts();
334
336 };
337
338 fe_eval_ptr->getOpPtrVector().push_back(op_test);
339
340 } else {
342 "Field evaluator method pointer is expired");
343 }
344
345 auto op_ptr = field_eval_ptr->getDataOperator<SPACE_DIM>(
346 {{eval_data_U_ptr, data_U_ptr}, {eval_data_S_ptr, data_S_ptr}},
347 simpleInterface->getDomainFEName(), field_eval_data, 0,
348 mField.get_comm_size(), parent_bit, parent_bit | child_bit, MF_EXIST,
349 QUIET);
350
351 fe_child_ptr->getOpPtrVector().push_back(op_ptr);
352 return std::make_pair(
353
354 std::vector<std::pair<std::string, boost::shared_ptr<MatrixDouble>>>{
355 {FIELD_NAME_U, data_U_ptr}},
356
357 std::vector<std::pair<std::string, boost::shared_ptr<MatrixDouble>>>{
358 {FIELD_NAME_S, data_S_ptr}}
359
360 );
361
362 };
363
364 // calculate coefficients on child (flipped) elements
365 auto dg_projection_base = [&](auto fe_child_ptr, auto vec_data_ptr, auto mat,
366 auto vec) {
368 constexpr int projection_order = order;
369 auto entity_data_l2 = boost::make_shared<EntitiesFieldData>(MBENTITYSET);
370 auto mass_ptr = boost::make_shared<MatrixDouble>();
371 auto coeffs_ptr = boost::make_shared<MatrixDouble>();
372
373 // project L2 (directly from coefficients)
374 for (auto &p : vec_data_ptr.first) {
375 auto field_name = p.first;
376 auto data_ptr = p.second;
377
378 fe_child_ptr->getOpPtrVector().push_back(new OpDGProjectionMassMatrix(
379 projection_order, mass_ptr, entity_data_l2, AINSWORTH_LEGENDRE_BASE,
380 L2));
381 fe_child_ptr->getOpPtrVector().push_back(new OpDGProjectionCoefficients(
382 data_ptr, coeffs_ptr, mass_ptr, entity_data_l2,
383 AINSWORTH_LEGENDRE_BASE, L2, Sev::noisy));
384
385 // next two lines are only for testing if projection is correct, they are not
386 // essential
387 fe_child_ptr->getOpPtrVector().push_back(new OpDGProjectionEvaluation(
388 data_ptr, coeffs_ptr, entity_data_l2, AINSWORTH_LEGENDRE_BASE, L2,
389 Sev::noisy));
390 fe_child_ptr->getOpPtrVector().push_back(new OpError(data_ptr));
391
392 // set coefficients to flipped element
393 auto op_set_coeffs = new DomainEleOp(field_name, DomainEleOp::OPROW);
394 op_set_coeffs->doWorkRhsHook =
395 [coeffs_ptr](DataOperator *base_op_ptr, int side, EntityType type,
398 auto field_ents = data.getFieldEntities();
399 auto nb_dofs = data.getIndices().size();
400 if (!field_ents.size())
402 if (auto e_ptr = field_ents[0]) {
403 auto field_ent_data = e_ptr->getEntFieldData();
404 std::copy(coeffs_ptr->data().data(),
405 coeffs_ptr->data().data() + nb_dofs,
406 field_ent_data.begin());
407 }
409 };
410 fe_child_ptr->getOpPtrVector().push_back(op_set_coeffs);
411 }
412
413 // project H1 (via coefficients)
414 for (auto &p : vec_data_ptr.second) {
415 auto field_name = p.first;
416 auto data_ptr = p.second;
417
418 fe_child_ptr->getOpPtrVector().push_back(new OpDGProjectionMassMatrix(
419 projection_order, mass_ptr, entity_data_l2, AINSWORTH_LEGENDRE_BASE,
420 L2));
421 fe_child_ptr->getOpPtrVector().push_back(new OpDGProjectionCoefficients(
422 data_ptr, coeffs_ptr, mass_ptr, entity_data_l2,
423 AINSWORTH_LEGENDRE_BASE, L2, Sev::noisy));
424
425 // next two lines are only for testing if projection is correct, they are not
426 // essential
427 fe_child_ptr->getOpPtrVector().push_back(new OpDGProjectionEvaluation(
428 data_ptr, coeffs_ptr, entity_data_l2, AINSWORTH_LEGENDRE_BASE, L2,
429 Sev::noisy));
430 fe_child_ptr->getOpPtrVector().push_back(new OpError(data_ptr));
431
432 // assemble to global matrix, since this is H1 (you will do the shame for Hcurl of Hdiv)
433 auto beta = [](const double, const double, const double) { return 1; };
434 fe_child_ptr->getOpPtrVector().push_back(
437 GAUSS>::OpBaseTimesVector<1, FIELD_DIM, FIELD_DIM>;
438 fe_child_ptr->getOpPtrVector().push_back(
439 new OpVec(FIELD_NAME_S, data_ptr, beta));
440 }
441
443 };
444
445 auto dm = simple->getDM();
446 auto sub_dm = createDM(mField.get_comm(), "DMMOFEM");
447 CHKERR DMMoFEMCreateSubDM(sub_dm, dm, "SUB");
448 CHKERR DMMoFEMSetSquareProblem(sub_dm, PETSC_TRUE);
450
451 // get only refinement bit DOFs
452 auto ref_entities_ptr = boost::make_shared<Range>();
453 CHKERR mField.getInterface<BitRefManager>()->getEntitiesByRefLevel(
454 refine_bit, child_bit | refine_bit, *ref_entities_ptr);
455 Range verts;
456 CHKERR mField.get_moab().get_connectivity(*ref_entities_ptr, verts, true);
457 ref_entities_ptr->merge(verts);
458
459 CHKERR DMMoFEMAddSubFieldRow(sub_dm, FIELD_NAME_S, ref_entities_ptr);
460 CHKERR DMMoFEMAddSubFieldCol(sub_dm, FIELD_NAME_S, ref_entities_ptr);
461 CHKERR DMSetUp(sub_dm);
462
463 auto mat = createDMMatrix(sub_dm);
464 auto vec = createDMVector(sub_dm);
465
466 // create child operator, and fe_child_ptr element in it
467 auto fe_child_ptr = get_child_op(pipeline_mng->getOpDomainRhsPipeline());
468 // run dg projection, note that get_field_eval_op,
469 // pass data_ptr values used to project and calculate coefficients
470 CHKERR dg_projection_base(fe_child_ptr, get_field_eval_op(fe_child_ptr), mat,
471 vec);
472
473 // That is to test, if projection works, and coefficients are set in correctly
474 // Note: FIELD_S is not tested, it is in H1, so we have to solve KSP problem first
475 auto test_U_data_ptr = boost::make_shared<MatrixDouble>();
476 pipeline_mng->getOpDomainRhsPipeline().push_back(
478 test_U_data_ptr));
479 pipeline_mng->getOpDomainRhsPipeline().push_back(
480 new OpError(test_U_data_ptr, refine_bit, BitRefLevel().set()));
481
482 auto fe_rhs = pipeline_mng->getCastDomainRhsFE<DomainEle>();
483 fe_rhs->ksp_A = mat;
484 fe_rhs->ksp_B = mat;
485 fe_rhs->ksp_f = vec;
486 fe_rhs->data_ctx =
488 CHKERR pipeline_mng->loopFiniteElements(sub_dm);
489
490 CHKERR VecAssemblyBegin(vec);
491 CHKERR VecAssemblyEnd(vec);
492 CHKERR VecGhostUpdateBegin(vec, ADD_VALUES, SCATTER_REVERSE);
493 CHKERR VecGhostUpdateEnd(vec, ADD_VALUES, SCATTER_REVERSE);
494 CHKERR MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);
495 CHKERR MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);
496
497 auto ksp = createKSP(mField.get_comm());
498 CHKERR KSPSetOperators(ksp, mat, mat);
499 CHKERR KSPSetFromOptions(ksp);
500
501 auto sol = createDMVector(sub_dm);
502 CHKERR KSPSolve(ksp, vec, sol);
503 CHKERR VecGhostUpdateBegin(sol, INSERT_VALUES, SCATTER_FORWARD);
504 CHKERR VecGhostUpdateEnd(sol, INSERT_VALUES, SCATTER_FORWARD);
505 CHKERR DMoFEMMeshToLocalVector(sub_dm, sol, INSERT_VALUES, SCATTER_REVERSE);
506
507 pipeline_mng->getOpDomainRhsPipeline().clear();
508 auto test_S_data_ptr = boost::make_shared<MatrixDouble>();
509 pipeline_mng->getOpDomainRhsPipeline().push_back(
511 test_S_data_ptr));
512 pipeline_mng->getOpDomainRhsPipeline().push_back(
513 new OpError(test_S_data_ptr, refine_bit, BitRefLevel().set()));
514
516}
std::string type
ElementsAndOps< SPACE_DIM >::DomainEle DomainEle
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::BiLinearForm< GAUSS >::OpMass< 1, FIELD_DIM > OpDomainMass
@ MF_EXIST
@ AINSWORTH_LEGENDRE_BASE
Ainsworth Cole (Legendre) approx. base .
Definition definitions.h:60
@ L2
field with C-1 continuity
Definition definitions.h:88
@ NOSPACE
Definition definitions.h:83
auto createDMMatrix(DM dm)
Get smart matrix from DM.
Definition DMMoFEM.hpp:1194
@ PETSC
Standard PETSc assembly.
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
constexpr auto field_name
virtual int get_comm_size() const =0
base operator to do operations at Gauss Pt. level
Data on single entity (This is passed as argument to DataOperator::doWork)
Field evaluator interface.
boost::shared_ptr< SPD > getData(const double *ptr=nullptr, const int nb_eval_points=0, const double eps=1e-12, VERBOSITY_LEVELS verb=QUIET)
Get the Data object.
Evaluate field for given DG projection coefficients.
Evaluate right hand side for given data coefficients.
Execute "this" element in the operator.
static constexpr Switches CtxSetA
Jacobian matrix switch.
static constexpr Switches CtxSetF
Residual vector switch.
static constexpr Switches CtxSetB
Preconditioner matrix switch.
DomainEle::UserDataOperator DomainEleOp

◆ pushOperators()

MoFEMErrorCode Example::pushOperators ( )
private

[Set density distribution]

[Push operators to pipeline]

Examples
mofem/tutorials/fun-1_integration/integration.cpp.

Definition at line 188 of file integration.cpp.

188 {
191
192 // Push an operator which calculates values of density at integration points
193 pipeline_mng->getOpDomainRhsPipeline().push_back(
195 "rho", commonDataPtr->getRhoAtIntegrationPtsPtr()));
196
197 // Push an operator to pipeline to calculate zero moment of inertia (mass)
198 pipeline_mng->getOpDomainRhsPipeline().push_back(new OpZero(commonDataPtr));
199
200 // Push an operator to the pipeline to calculate first moment of inertaia
201 pipeline_mng->getOpDomainRhsPipeline().push_back(new OpFirst(commonDataPtr));
202
203 // Push an operator to the pipeline to calculate second moment of inertaia
204 pipeline_mng->getOpDomainRhsPipeline().push_back(new OpSecond(commonDataPtr));
205
206 // Set integration rule. Integration rule is equal to the polynomial order of
207 // the density field plus 2, since under the integral of the second moment of
208 // inertia term x*x is present
209 auto integration_rule = [](int, int, int p_data) { return p_data + 2; };
210
211 // Add integration rule to the element
214}
boost::ptr_deque< UserDataOperator > & getOpDomainRhsPipeline()
Get the Op Domain Rhs Pipeline object.
MoFEMErrorCode setDomainRhsIntegrationRule(RuleHookFun rule)
Set integration rule for domain right-hand side finite element.

◆ readMesh() [1/15]

MoFEMErrorCode Example::readMesh ( )
private

[Run problem]

[run problem]

[Run programme]

[Read mesh]

[Read mesh]

Read mesh from file and setup material/boundary condition meshsets

This function loads the finite element mesh from file and processes associated meshsets that define material properties and boundary conditions. The mesh is typically generated using CUBIT and exported in .h5m format.

Meshsets are used to group elements/faces by:

  • Material properties (for different material blocks)
  • Boundary conditions (for applying loads and constraints)
  • Optimization regions (for topology optimization)
Returns
MoFEMErrorCode Success or error code
Examples
mofem/tutorials/adv-4_dynamic_first_order_con_law/dynamic_first_order_con_law.cpp, mofem/tutorials/adv-6_dg_projection/between_meshes_dg_projection.cpp, mofem/tutorials/clx-0_acoustics/helmholtz.cpp, mofem/tutorials/fun-2_plot_base/plot_base.cpp, mofem/tutorials/mix-1_light_intensity_equation/phase.cpp, mofem/tutorials/scl-0_least_squares/approximation.cpp, mofem/tutorials/scl-9_heat_method/heat_method.cpp, mofem/tutorials/vec-1_eigen_elasticity/eigen_elastic.cpp, mofem/tutorials/vec-3_nonlinear_dynamics/nonlinear_dynamic_elastic.cpp, mofem/tutorials/vec-4_shallow_wave/shallow_wave.cpp, mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp, and nonlinear_elastic.cpp.

Definition at line 463 of file dynamic_first_order_con_law.cpp.

463 {
466
470}
MoFEMErrorCode loadFile(const std::string options, const std::string mesh_file_name, LoadFileFunc loadFunc=defaultLoadFileFunc)
Load mesh file.
Definition Simple.cpp:191
MoFEMErrorCode getOptions()
get options
Definition Simple.cpp:180

◆ readMesh() [2/15]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [3/15]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [4/15]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [5/15]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [6/15]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [7/15]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [8/15]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [9/15]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [10/15]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [11/15]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [12/15]

MoFEMErrorCode Example::readMesh ( )
private

Read mesh from file and setup meshsets.

◆ readMesh() [13/15]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [14/15]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [15/15]

MoFEMErrorCode Example::readMesh ( )
private

◆ refineSkin()

MoFEMErrorCode Example::refineSkin ( BitRefLevel  parent_bit,
BitRefLevel  child_bit 
)
private

[Edge flips]

[Refine skin]

Examples
mofem/tutorials/adv-6_dg_projection/between_meshes_dg_projection.cpp.

Definition at line 765 of file between_meshes_dg_projection.cpp.

766 {
768
769 moab::Interface &moab = mField.get_moab();
770 Range tris;
771 CHKERR moab.get_entities_by_dimension(0, SPACE_DIM, tris);
772 CHKERR mField.getInterface<BitRefManager>()->filterEntitiesByRefLevel(
773 parent_bit, BitRefLevel().set(), tris);
774
775 Skinner skin(&moab);
776 Range skin_edges;
777 CHKERR skin.find_skin(0, tris, false, skin_edges);
778
779 auto refine = mField.getInterface<MeshRefinement>();
780 CHKERR refine->addVerticesInTheMiddleOfEdges(skin_edges, child_bit);
781#ifndef NDEBUG
782 auto debug = true;
783#else
784 auto debug = false;
785#endif
786 CHKERR refine->refineTris(tris, child_bit, QUIET, debug);
787
788 CHKERR mField.getInterface<BitRefManager>()->writeBitLevel(
789 child_bit, BitRefLevel().set(), "edge_flips_after_refinement.vtk", "VTK",
790 "");
791
793}

◆ reSetupProblem()

MoFEMErrorCode Example::reSetupProblem ( BitRefLevel  child_bit)
private

[Refine skin]

[Re-setup problem after mesh modification

Examples
mofem/tutorials/adv-6_dg_projection/between_meshes_dg_projection.cpp.

Definition at line 797 of file between_meshes_dg_projection.cpp.

797 {
799 simpleInterface->getBitRefLevel() = child_bit;
802}
MoFEMErrorCode reSetUp(bool only_dm=false)
Rebuild internal MoFEM data structures.
Definition Simple.cpp:761
BitRefLevel & getBitRefLevel()
Get the BitRefLevel.
Definition Simple.hpp:415

◆ rhsSource()

double Example::rhsSource ( const double  x,
const double  y,
const double   
)
staticprivate
Examples
mofem/tutorials/mix-1_light_intensity_equation/phase.cpp.

Definition at line 150 of file phase.cpp.

150 {
151 const auto idx = getCoordsInImage(x, y);
152
153 double v = 0;
154 for (auto w = 0; w != window_savitzky_golay; ++w) {
155 const auto i = focalIndex - (window_savitzky_golay - 1) / 2 + w;
156 const auto &intensity = iI[i];
157 v += intensity(idx.first, idx.second) * savitzkyGolayWeights[w];
158 }
159 v = static_cast<double>(v) / savitzkyGolayNormalisation;
160
161 const auto dz = rZ[focalIndex + 1] - rZ[focalIndex - 1];
162 return -k * v / dz;
163}
FTensor::Index< 'k', 3 > k
static int window_savitzky_golay
Definition phase.cpp:77
static int savitzkyGolayNormalisation
Definition phase.cpp:113
static std::vector< double > rZ
Definition phase.cpp:108
static const int * savitzkyGolayWeights
Definition phase.cpp:114

◆ runProblem() [1/20]

MoFEMErrorCode Example::runProblem ( )

[Run problem]

[Refine edges]

[Run topology optimization problem]

[Create common data]

[OpError def]

[Operator]

[run problem]

[Run programme]

[Run all]

[Run problem]

Main driver for topology optimization using adjoint sensitivity analysis

This function orchestrates the complete topology optimization workflow:

  1. Initialize Python objective function interface
  2. Setup finite element problems (forward and adjoint)
  3. Compute initial elastic solution
  4. Generate topology optimization modes
  5. Run TAO optimization loop with adjoint-based gradients

The optimization uses TAO (Toolkit for Advanced Optimization) with L-BFGS algorithm. At each iteration:

  • Update geometry based on current design variables
  • Solve forward elastic problem
  • Compute objective function and gradients using adjoint method
  • Post-process results
Returns
MoFEMErrorCode Success or error code

Setup TAO (Toolkit for Advanced Optimization) solver for topology optimization

TAO provides various optimization algorithms. Here we use TAOLMVM (Limited Memory Variable Metric) which is a quasi-Newton method suitable for unconstrained optimization with gradient information.

The optimization variables are coefficients for the topology modes, and gradients are computed using the adjoint method for efficiency.

Generate modes for topology optimization design parameterization

These modes represent perturbations to the geometry that can be used as design variables in topology optimization. The modes are defined through the Python interface and provide spatial basis functions for shape modifications.

Start optimization with zero initial guess for design variables

The TAO solver will iteratively:

  1. Evaluate objective function at current design point
  2. Compute gradients using adjoint sensitivity analysis
  3. Update design variables using L-BFGS algorithm
  4. Check convergence criteria

Each function evaluation involves solving the forward elastic problem and the adjoint problem to compute sensitivities efficiently.

Examples
mofem/tutorials/adv-0_plasticity/plastic.cpp, mofem/tutorials/adv-4_dynamic_first_order_con_law/dynamic_first_order_con_law.cpp, mofem/tutorials/adv-6_dg_projection/between_meshes_dg_projection.cpp, mofem/tutorials/clx-0_acoustics/helmholtz.cpp, mofem/tutorials/fun-1_integration/integration.cpp, mofem/tutorials/fun-2_plot_base/plot_base.cpp, mofem/tutorials/mix-1_light_intensity_equation/phase.cpp, mofem/tutorials/scl-0_least_squares/approximation.cpp, mofem/tutorials/scl-8_heat_radiation/radiation.cpp, mofem/tutorials/scl-9_heat_method/heat_method.cpp, mofem/tutorials/vec-1_eigen_elasticity/eigen_elastic.cpp, mofem/tutorials/vec-3_nonlinear_dynamics/nonlinear_dynamic_elastic.cpp, mofem/tutorials/vec-4_shallow_wave/shallow_wave.cpp, mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp, nonlinear_elastic.cpp, plastic.cpp, and thermoplastic.cpp.

Definition at line 254 of file plastic.cpp.

254 {
258 CHKERR bC();
259 CHKERR OPs();
260 PetscBool test_ops = PETSC_FALSE;
261 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-test_operators", &test_ops,
262 PETSC_NULLPTR);
263 if (test_ops == PETSC_FALSE) {
264 CHKERR tsSolve();
265 } else {
267 }
269}
MoFEMErrorCode testOperators()
[Solve]
Definition plastic.cpp:1484
MoFEMErrorCode tsSolve()
Definition plastic.cpp:832
MoFEMErrorCode createCommonData()
[Set up problem]
Definition plastic.cpp:478
MoFEMErrorCode OPs()
[Boundary condition]
Definition plastic.cpp:648
MoFEMErrorCode setupProblem()
[Run problem]
Definition plastic.cpp:273
MoFEMErrorCode bC()
[Create common data]
Definition plastic.cpp:604

◆ runProblem() [2/20]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [3/20]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [4/20]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [5/20]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [6/20]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [7/20]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [8/20]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [9/20]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [10/20]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [11/20]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [12/20]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [13/20]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [14/20]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [15/20]

MoFEMErrorCode Example::runProblem ( )

Main driver function for the optimization process.

◆ runProblem() [16/20]

MoFEMErrorCode Example::runProblem ( )

Main driver function for the optimization process.

◆ runProblem() [17/20]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [18/20]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [19/20]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [20/20]

MoFEMErrorCode Example::runProblem ( )

◆ setFieldValues()

MoFEMErrorCode Example::setFieldValues ( )
private

[Create common data]

[Set density distribution]

Examples
mofem/tutorials/fun-1_integration/integration.cpp.

Definition at line 172 of file integration.cpp.

172 {
174 auto set_density = [&](VectorAdaptor &&field_data, double *xcoord,
175 double *ycoord, double *zcoord) {
177 field_data[0] = 1;
179 };
180 FieldBlas *field_blas;
181 CHKERR mField.getInterface(field_blas);
182 CHKERR field_blas->setVertexDofs(set_density, "rho");
184}
VectorShallowArrayAdaptor< double > VectorAdaptor
Definition Types.hpp:115
MoFEMErrorCode setVertexDofs(VertexCoordsFunction lambda, const std::string field_name, Range *verts=nullptr)
Set DOFs on vertices using user function.

◆ setIntegrationRules() [1/3]

MoFEMErrorCode Example::setIntegrationRules ( )
private

◆ setIntegrationRules() [2/3]

MoFEMErrorCode Example::setIntegrationRules ( )
private

◆ setIntegrationRules() [3/3]

MoFEMErrorCode Example::setIntegrationRules ( )
private

◆ setUp()

MoFEMErrorCode Example::setUp ( )
private

[Run all]

[Set up problem]

Examples
mofem/tutorials/fun-1_integration/integration.cpp.

Definition at line 137 of file integration.cpp.

137 {
142 // Add field
144 constexpr int order = 1;
148}
MoFEMErrorCode addDomainField(const std::string name, const FieldSpace space, const FieldApproximationBase base, const FieldCoefficientsNumber nb_of_coefficients, const TagType tag_type=MB_TAG_SPARSE, const enum MoFEMTypes bh=MF_ZERO, int verb=-1)
Add field on domain.
Definition Simple.cpp:261
MoFEMErrorCode setFieldOrder(const std::string field_name, const int order, const Range *ents=NULL)
Set field order.
Definition Simple.cpp:575
MoFEMErrorCode setUp(const PetscBool is_partitioned=PETSC_TRUE)
Setup problem.
Definition Simple.cpp:735

◆ setupAdJoint() [1/2]

MoFEMErrorCode Example::setupAdJoint ( )
private

Setup adjoint fields and finite elements.

[Set up problem]

[Setup adjoint]

Setup adjoint fields and finite elements for sensitivity analysis

The adjoint method is used to efficiently compute gradients of the objective function with respect to design variables. This function sets up:

  1. ADJOINT_FIELD - stores adjoint variables (Lagrange multipliers)
  2. ADJOINT_DM - data manager for adjoint problem
  3. Adjoint finite elements for domain and boundary

The adjoint equation is: K^T * λ = ∂f/∂u where λ are adjoint variables, K is stiffness matrix, f is objective

Returns
MoFEMErrorCode Success or error code

[Setup adjoint]

Examples
mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 602 of file adjoint.cpp.

602 {
605
606 // Create adjoint data manager and field
607 auto create_adjoint_dm = [&]() {
608 auto adjoint_dm = createDM(mField.get_comm(), "DMMOFEM");
609
610 auto add_field = [&]() {
612 CHKERR mField.add_field("ADJOINT_FIELD", H1, base, SPACE_DIM);
614 "ADJOINT_FIELD");
615 for (auto tt = MBEDGE; tt <= moab::CN::TypeDimensionMap[SPACE_DIM].second;
616 ++tt)
617 CHKERR mField.set_field_order(simple->getMeshset(), tt, "ADJOINT_FIELD",
618 fieldOrder);
620 "ADJOINT_FIELD", 1);
623 };
624
625 auto add_adjoint_fe_impl = [&]() {
627 CHKERR mField.add_finite_element("ADJOINT_DOMAIN_FE");
629 "ADJOINT_FIELD");
631 "ADJOINT_FIELD");
633 "ADJOINT_FIELD");
635 "GEOMETRY");
637 simple->getMeshset(), SPACE_DIM, "ADJOINT_DOMAIN_FE");
638 CHKERR mField.build_finite_elements("ADJOINT_DOMAIN_FE");
639
640 CHKERR mField.add_finite_element("ADJOINT_BOUNDARY_FE");
642 "ADJOINT_FIELD");
644 "ADJOINT_FIELD");
646 "ADJOINT_FIELD");
648 "GEOMETRY");
649
650 auto block_name = "OPTIMISE";
651 auto mesh_mng = mField.getInterface<MeshsetsManager>();
652 auto bcs = mesh_mng->getCubitMeshsetPtr(
653
654 std::regex((boost::format("%s(.*)") % block_name).str())
655
656 );
657
658 for (auto bc : bcs) {
660 bc->getMeshset(), SPACE_DIM - 1, "ADJOINT_BOUNDARY_FE");
661 }
662
663 CHKERR mField.build_finite_elements("ADJOINT_BOUNDARY_FE");
664
667
669 };
670
671 auto set_adjoint_dm_imp = [&]() {
673 CHKERR DMMoFEMCreateMoFEM(adjoint_dm, &mField, "ADJOINT",
676 CHKERR DMMoFEMSetDestroyProblem(adjoint_dm, PETSC_TRUE);
677 CHKERR DMSetFromOptions(adjoint_dm);
678 CHKERR DMMoFEMAddElement(adjoint_dm, "ADJOINT_DOMAIN_FE");
679 CHKERR DMMoFEMAddElement(adjoint_dm, "ADJOINT_BOUNDARY_FE");
680 CHKERR DMMoFEMSetSquareProblem(adjoint_dm, PETSC_TRUE);
681 CHKERR DMMoFEMSetIsPartitioned(adjoint_dm, PETSC_TRUE);
682 mField.getInterface<ProblemsManager>()->buildProblemFromFields =
683 PETSC_TRUE;
684 CHKERR DMSetUp(adjoint_dm);
685 mField.getInterface<ProblemsManager>()->buildProblemFromFields =
686 PETSC_FALSE;
688 };
689
690 CHK_THROW_MESSAGE(add_field(), "add adjoint field");
691 CHK_THROW_MESSAGE(add_adjoint_fe_impl(), "add adjoint fe");
692 CHK_THROW_MESSAGE(set_adjoint_dm_imp(), "set adjoint dm");
693
694 return adjoint_dm;
695 };
696
697 adjointDM = create_adjoint_dm();
698
700}
PetscErrorCode DMMoFEMSetIsPartitioned(DM dm, PetscBool is_partitioned)
Definition DMMoFEM.cpp:1113
PetscErrorCode DMMoFEMCreateMoFEM(DM dm, MoFEM::Interface *m_field_ptr, const char problem_name[], const MoFEM::BitRefLevel bit_level, const MoFEM::BitRefLevel bit_mask=MoFEM::BitRefLevel().set())
Must be called by user to set MoFEM data structures.
Definition DMMoFEM.cpp:114
virtual MoFEMErrorCode add_ents_to_finite_element_by_dim(const EntityHandle entities, const int dim, const std::string name, const bool recursive=true)=0
add entities to finite element
virtual MoFEMErrorCode add_finite_element(const std::string &fe_name, enum MoFEMTypes bh=MF_EXCL, int verb=DEFAULT_VERBOSITY)=0
add finite element
virtual MoFEMErrorCode build_finite_elements(int verb=DEFAULT_VERBOSITY)=0
Build finite elements.
virtual MoFEMErrorCode modify_finite_element_add_field_col(const std::string &fe_name, const std::string name_row)=0
set field col which finite element use
virtual MoFEMErrorCode modify_finite_element_add_field_row(const std::string &fe_name, const std::string name_row)=0
set field row which finite element use
virtual MoFEMErrorCode modify_finite_element_add_field_data(const std::string &fe_name, const std::string name_field)=0
set finite element field data
virtual MoFEMErrorCode build_fields(int verb=DEFAULT_VERBOSITY)=0
virtual MoFEMErrorCode add_ents_to_field_by_dim(const Range &ents, const int dim, const std::string &name, int verb=DEFAULT_VERBOSITY)=0
Add entities to field meshset.
virtual MoFEMErrorCode set_field_order(const EntityHandle meshset, const EntityType type, const std::string &name, const ApproximationOrder order, int verb=DEFAULT_VERBOSITY)=0
Set order approximation of the entities in the field.
MoFEMErrorCode getCubitMeshsetPtr(const int ms_id, const CubitBCType cubit_bc_type, const CubitMeshSets **cubit_meshset_ptr) const
get cubit meshset
PetscErrorCode DMMoFEMSetDestroyProblem(DM dm, PetscBool destroy_problem)
Definition DMMoFEM.cpp:434
FieldApproximationBase base
Choice of finite element basis functions.
Definition plot_base.cpp:68
int fieldOrder
Polynomial order for approximation.
Definition adjoint.cpp:164
virtual MoFEMErrorCode build_adjacencies(const Range &ents, int verb=DEFAULT_VERBOSITY)=0
build adjacencies
virtual MoFEMErrorCode add_field(const std::string name, const FieldSpace space, const FieldApproximationBase base, const FieldCoefficientsNumber nb_of_coefficients, const TagType tag_type=MB_TAG_SPARSE, const enum MoFEMTypes bh=MF_EXCL, int verb=DEFAULT_VERBOSITY)=0
Add field.
Interface for managing meshsets containing materials and boundary conditions.
Problem manager is used to build and partition problems.
EntityHandle & getMeshset()
Get the MeshSet object.
Definition Simple.hpp:394
BitRefLevel & getBitRefLevelMask()
Get the BitRefLevelMask.
Definition Simple.hpp:422

◆ setupAdJoint() [2/2]

MoFEMErrorCode Example::setupAdJoint ( )
private

◆ setupProblem() [1/19]

MoFEMErrorCode Example::setupProblem ( )
private

[Run problem]

[Read mesh]

[Set up problem]

[Set up problem]

Setup finite element fields, approximation spaces and degrees of freedom

This function configures the finite element problem by:

  1. Setting up the displacement field "U" with vector approximation
  2. Setting up the geometry field "GEOMETRY" for mesh deformation
  3. Defining polynomial approximation order and basis functions
  4. Creating degrees of freedom on mesh entities

The displacement field uses H1 vector space for standard elasticity. The geometry field allows mesh modification during topology optimization. Different basis functions (Ainsworth-Legendre vs Demkowicz) can be selected.

Returns
MoFEMErrorCode Success or error code

Setup displacement field "U" - the primary unknown in elasticity This field represents displacement vector at each node/DOF

Setup geometry field "GEOMETRY" - used for mesh deformation in optimization This field stores current nodal coordinates and can be modified during topology optimization to represent design changes

For higher-order elements, this projects the exact geometry onto the geometry field to maintain curved boundaries accurately

Examples
mofem/tutorials/adv-0_plasticity/plastic.cpp, mofem/tutorials/adv-4_dynamic_first_order_con_law/dynamic_first_order_con_law.cpp, mofem/tutorials/adv-6_dg_projection/between_meshes_dg_projection.cpp, mofem/tutorials/clx-0_acoustics/helmholtz.cpp, mofem/tutorials/fun-2_plot_base/plot_base.cpp, mofem/tutorials/mix-1_light_intensity_equation/phase.cpp, mofem/tutorials/scl-0_least_squares/approximation.cpp, mofem/tutorials/scl-8_heat_radiation/radiation.cpp, mofem/tutorials/scl-9_heat_method/heat_method.cpp, mofem/tutorials/vec-1_eigen_elasticity/eigen_elastic.cpp, mofem/tutorials/vec-3_nonlinear_dynamics/nonlinear_dynamic_elastic.cpp, mofem/tutorials/vec-4_shallow_wave/shallow_wave.cpp, mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp, nonlinear_elastic.cpp, plastic.cpp, and thermoplastic.cpp.

Definition at line 273 of file plastic.cpp.

273 {
276
277 Range domain_ents;
278 CHKERR mField.get_moab().get_entities_by_dimension(0, SPACE_DIM, domain_ents,
279 true);
280 auto get_ents_by_dim = [&](const auto dim) {
281 if (dim == SPACE_DIM) {
282 return domain_ents;
283 } else {
284 Range ents;
285 if (dim == 0)
286 CHKERR mField.get_moab().get_connectivity(domain_ents, ents, true);
287 else
288 CHKERR mField.get_moab().get_entities_by_dimension(0, dim, ents, true);
289 return ents;
290 }
291 };
292
293 auto get_base = [&]() {
294 auto domain_ents = get_ents_by_dim(SPACE_DIM);
295 if (domain_ents.empty())
296 CHK_THROW_MESSAGE(MOFEM_NOT_FOUND, "Empty mesh");
297 const auto type = type_from_handle(domain_ents[0]);
298 switch (type) {
299 case MBQUAD:
301 case MBHEX:
303 case MBTRI:
305 case MBTET:
307 default:
308 CHK_THROW_MESSAGE(MOFEM_NOT_FOUND, "Element type not handled");
309 }
310 return NOBASE;
311 };
312
313 const auto base = get_base();
314 MOFEM_LOG("PLASTICITY", Sev::inform)
315 << "Base " << ApproximationBaseNames[base];
316
319 CHKERR simple->addDomainField("TAU", L2, base, 1);
321
322 CHKERR simple->addDataField("GEOMETRY", H1, base, SPACE_DIM);
323
324 PetscBool order_edge = PETSC_FALSE;
325 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-order_edge", &order_edge,
326 PETSC_NULLPTR);
327 PetscBool order_face = PETSC_FALSE;
328 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-order_face", &order_face,
329 PETSC_NULLPTR);
330 PetscBool order_volume = PETSC_FALSE;
331 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-order_volume", &order_volume,
332 PETSC_NULLPTR);
333
335
336 MOFEM_LOG("PLASTICITY", Sev::inform) << "Order edge " << order_edge
337 ? "true"
338 : "false";
339 MOFEM_LOG("PLASTICITY", Sev::inform) << "Order face " << order_face
340 ? "true"
341 : "false";
342 MOFEM_LOG("PLASTICITY", Sev::inform) << "Order volume " << order_volume
343 ? "true"
344 : "false";
345
346 auto ents = get_ents_by_dim(0);
347 if (order_edge)
348 ents.merge(get_ents_by_dim(1));
349 if (order_face)
350 ents.merge(get_ents_by_dim(2));
351 if (order_volume)
352 ents.merge(get_ents_by_dim(3));
353 CHKERR simple->setFieldOrder("U", order, &ents);
354 } else {
356 }
359
361
362#ifdef ADD_CONTACT
364 SPACE_DIM);
366 SPACE_DIM);
367
368 auto get_skin = [&]() {
369 Range body_ents;
370 CHKERR mField.get_moab().get_entities_by_dimension(0, SPACE_DIM, body_ents);
371 Skinner skin(&mField.get_moab());
372 Range skin_ents;
373 CHKERR skin.find_skin(0, body_ents, false, skin_ents);
374 return skin_ents;
375 };
376
377 auto filter_blocks = [&](auto skin) {
378 bool is_contact_block = true;
379 Range contact_range;
380 for (auto m :
381 mField.getInterface<MeshsetsManager>()->getCubitMeshsetPtr(std::regex(
382
383 (boost::format("%s(.*)") % "CONTACT").str()
384
385 ))
386
387 ) {
388 is_contact_block =
389 true; ///< blocs interation is collective, so that is set irrespective
390 ///< if there are entities in given rank or not in the block
391 MOFEM_LOG("CONTACT", Sev::inform)
392 << "Find contact block set: " << m->getName();
393 auto meshset = m->getMeshset();
394 Range contact_meshset_range;
395 CHKERR mField.get_moab().get_entities_by_dimension(
396 meshset, SPACE_DIM - 1, contact_meshset_range, true);
397
398 CHKERR mField.getInterface<CommInterface>()->synchroniseEntities(
399 contact_meshset_range);
400 contact_range.merge(contact_meshset_range);
401 }
402 if (is_contact_block) {
403 MOFEM_LOG("SYNC", Sev::inform)
404 << "Nb entities in contact surface: " << contact_range.size();
406 skin = intersect(skin, contact_range);
407 }
408 return skin;
409 };
410
411 auto filter_true_skin = [&](auto skin) {
412 Range boundary_ents;
413 ParallelComm *pcomm =
414 ParallelComm::get_pcomm(&mField.get_moab(), MYPCOMM_INDEX);
415 CHKERR pcomm->filter_pstatus(skin, PSTATUS_SHARED | PSTATUS_MULTISHARED,
416 PSTATUS_NOT, -1, &boundary_ents);
417 return boundary_ents;
418 };
419
420 auto boundary_ents = filter_true_skin(filter_blocks(get_skin()));
421 CHKERR simple->setFieldOrder("SIGMA", 0);
422 CHKERR simple->setFieldOrder("SIGMA", order - 1, &boundary_ents);
423#endif
424
427
428 auto project_ho_geometry = [&]() {
429 Projection10NodeCoordsOnField ent_method(mField, "GEOMETRY");
430 return mField.loop_dofs("GEOMETRY", ent_method);
431 };
432 PetscBool project_geometry = PETSC_TRUE;
433 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-project_geometry",
434 &project_geometry, PETSC_NULLPTR);
435 if (project_geometry) {
436 CHKERR project_ho_geometry();
437 }
438
439 auto get_volume = [&]() {
440 using VolOp = DomainEle::UserDataOperator;
441 auto *op_ptr = new VolOp(NOSPACE, VolOp::OPSPACE);
442 std::array<double, 2> volume_and_count;
443 op_ptr->doWorkRhsHook = [&](DataOperator *base_op_ptr, int side,
444 EntityType type,
447 auto op_ptr = static_cast<VolOp *>(base_op_ptr);
448 volume_and_count[VOL] += op_ptr->getMeasure();
449 volume_and_count[COUNT] += 1;
450 // in necessary at integration over Gauss points.
452 };
453 volume_and_count = {0, 0};
454 auto fe = boost::make_shared<DomainEle>(mField);
455 fe->getOpPtrVector().push_back(op_ptr);
456
457 auto dm = simple->getDM();
460 "cac volume");
461 std::array<double, 2> tot_volume_and_count;
462 MPI_Allreduce(volume_and_count.data(), tot_volume_and_count.data(),
463 volume_and_count.size(), MPI_DOUBLE, MPI_SUM,
464 mField.get_comm());
465 return tot_volume_and_count;
466 };
467
468 meshVolumeAndCount = get_volume();
469 MOFEM_LOG("PLASTICITY", Sev::inform)
470 << "Mesh volume " << meshVolumeAndCount[VOL] << " nb. of elements "
472
474}
#define MOFEM_LOG_SYNCHRONISE(comm)
Synchronise "SYNC" channel.
@ NOBASE
Definition definitions.h:59
@ DEMKOWICZ_JACOBI_BASE
Definition definitions.h:66
@ MOFEM_NOT_FOUND
Definition definitions.h:33
static const char *const ApproximationBaseNames[]
Definition definitions.h:72
virtual MoFEMErrorCode loop_dofs(const Problem *problem_ptr, const std::string &field_name, RowColData rc, DofMethod &method, int lower_rank, int upper_rank, int verb=DEFAULT_VERBOSITY)=0
Make a loop over dofs.
auto type_from_handle(const EntityHandle h)
get type from entity handle
static std::array< double, 2 > meshVolumeAndCount
Definition plastic.cpp:223
Managing BitRefLevels.
double getMeasure() const
get measure of element
@ OPSPACE
operator do Work is execute on space data
Projection of edge entities with one mid-node on hierarchical basis.
MoFEMErrorCode addBoundaryField(const std::string name, const FieldSpace space, const FieldApproximationBase base, const FieldCoefficientsNumber nb_of_coefficients, const TagType tag_type=MB_TAG_SPARSE, const enum MoFEMTypes bh=MF_ZERO, int verb=-1)
Add field on boundary.
Definition Simple.cpp:355
MoFEMErrorCode addFieldToEmptyFieldBlocks(const std::string row_field, const std::string col_field) const
Add empty block to problem.
Definition Simple.cpp:834
MoFEMErrorCode addDataField(const std::string name, const FieldSpace space, const FieldApproximationBase base, const FieldCoefficientsNumber nb_of_coefficients, const TagType tag_type=MB_TAG_SPARSE, const enum MoFEMTypes bh=MF_ZERO, int verb=-1)
Add data field.
Definition Simple.cpp:393
VolEle::UserDataOperator VolOp
PetscBool order_face
PetscBool order_edge
PetscBool order_volume
constexpr FieldSpace CONTACT_SPACE
Definition plastic.cpp:52

◆ setupProblem() [2/19]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [3/19]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [4/19]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [5/19]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [6/19]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [7/19]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [8/19]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [9/19]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [10/19]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [11/19]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [12/19]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [13/19]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [14/19]

MoFEMErrorCode Example::setupProblem ( )
private

Setup fields, approximation spaces and DOFs.

◆ setupProblem() [15/19]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [16/19]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [17/19]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [18/19]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [19/19]

MoFEMErrorCode Example::setupProblem ( )
private

◆ solveElastic() [1/2]

MoFEMErrorCode Example::solveElastic ( )
private

Solve forward elastic problem.

[Push operators to pipeline]

[Solve]

Examples
mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 1198 of file adjoint.cpp.

1198 {
1200 auto simple = mField.getInterface<Simple>();
1201 auto dm = simple->getDM();
1202 auto f = createDMVector(dm);
1203 auto d = vectorDuplicate(f);
1204 CHKERR VecZeroEntries(d);
1205 CHKERR DMoFEMMeshToLocalVector(dm, d, INSERT_VALUES, SCATTER_REVERSE);
1206
1207 auto set_essential_bc = [&]() {
1209 // This is low level pushing finite elements (pipelines) to solver
1210
1211 auto ksp_ctx_ptr = getDMKspCtx(dm);
1212 auto pre_proc_rhs = boost::make_shared<FEMethod>();
1213 auto post_proc_rhs = boost::make_shared<FEMethod>();
1214 auto post_proc_lhs = boost::make_shared<FEMethod>();
1215
1216 auto get_pre_proc_hook = [&]() {
1218 {});
1219 };
1220 pre_proc_rhs->preProcessHook = get_pre_proc_hook();
1221
1222 auto get_post_proc_hook_rhs = [this, post_proc_rhs]() {
1224
1226 post_proc_rhs, 1.)();
1228 };
1229
1230 auto get_post_proc_hook_lhs = [this, post_proc_lhs]() {
1232
1234 post_proc_lhs, 1.)();
1236 };
1237
1238 post_proc_rhs->postProcessHook = get_post_proc_hook_rhs;
1239 post_proc_lhs->postProcessHook = get_post_proc_hook_lhs;
1240
1241 ksp_ctx_ptr->getPreProcComputeRhs().push_front(pre_proc_rhs);
1242 ksp_ctx_ptr->getPostProcComputeRhs().push_back(post_proc_rhs);
1243 ksp_ctx_ptr->getPostProcSetOperators().push_back(post_proc_lhs);
1245 };
1246
1247 auto setup_and_solve = [&](auto solver) {
1249 BOOST_LOG_SCOPED_THREAD_ATTR("Timeline", attrs::timer());
1250 MOFEM_LOG("TIMER", Sev::noisy) << "KSPSetUp";
1251 CHKERR KSPSetUp(solver);
1252 MOFEM_LOG("TIMER", Sev::noisy) << "KSPSetUp <= Done";
1253 MOFEM_LOG("TIMER", Sev::noisy) << "KSPSolve";
1254 CHKERR KSPSolve(solver, f, d);
1255 MOFEM_LOG("TIMER", Sev::noisy) << "KSPSolve <= Done";
1257 };
1258
1259 MOFEM_LOG_CHANNEL("TIMER");
1260 MOFEM_LOG_TAG("TIMER", "timer");
1261
1262 CHKERR set_essential_bc();
1263 CHKERR setup_and_solve(kspElastic);
1264
1265 CHKERR VecGhostUpdateBegin(d, INSERT_VALUES, SCATTER_FORWARD);
1266 CHKERR VecGhostUpdateEnd(d, INSERT_VALUES, SCATTER_FORWARD);
1267 CHKERR DMoFEMMeshToLocalVector(dm, d, INSERT_VALUES, SCATTER_REVERSE);
1268
1269 auto evaluate_field_at_the_point = [&]() {
1271
1272 int coords_dim = 3;
1273 std::array<double, 3> field_eval_coords{0.0, 0.0, 0.0};
1274 PetscBool do_eval_field = PETSC_FALSE;
1275 CHKERR PetscOptionsGetRealArray(NULL, NULL, "-field_eval_coords",
1276 field_eval_coords.data(), &coords_dim,
1277 &do_eval_field);
1278
1279 if (do_eval_field) {
1280
1281 vectorFieldPtr = boost::make_shared<MatrixDouble>();
1282 auto field_eval_data =
1283 mField.getInterface<FieldEvaluatorInterface>()->getData<DomainEle>();
1284
1286 ->buildTree<SPACE_DIM>(field_eval_data, simple->getDomainFEName());
1287
1288 field_eval_data->setEvalPoints(field_eval_coords.data(), 1);
1289 auto no_rule = [](int, int, int) { return -1; };
1290 auto field_eval_fe_ptr = field_eval_data->feMethodPtr;
1291 field_eval_fe_ptr->getRuleHook = no_rule;
1292
1293 field_eval_fe_ptr->getOpPtrVector().push_back(
1295
1297 ->evalFEAtThePoint<SPACE_DIM>(
1298 field_eval_coords.data(), 1e-12, simple->getProblemName(),
1299 simple->getDomainFEName(), field_eval_data,
1301 QUIET);
1302
1303 if (vectorFieldPtr->size1()) {
1304 auto t_disp = getFTensor1FromMat<SPACE_DIM>(*vectorFieldPtr);
1305 if constexpr (SPACE_DIM == 2)
1306 MOFEM_LOG("FieldEvaluator", Sev::inform)
1307 << "U_X: " << t_disp(0) << " U_Y: " << t_disp(1);
1308 else
1309 MOFEM_LOG("FieldEvaluator", Sev::inform)
1310 << "U_X: " << t_disp(0) << " U_Y: " << t_disp(1)
1311 << " U_Z: " << t_disp(2);
1312 }
1313
1315 }
1317 };
1318
1319 CHKERR evaluate_field_at_the_point();
1320
1322}
#define MOFEM_LOG_TAG(channel, tag)
Tag channel.
PetscErrorCode PetscOptionsGetRealArray(PetscOptions *, const char pre[], const char name[], PetscReal dval[], PetscInt *nmax, PetscBool *set)
auto getDMKspCtx(DM dm)
Get KSP context data structure used by DM.
Definition DMMoFEM.hpp:1251
boost::shared_ptr< MatrixDouble > vectorFieldPtr
Field values at evaluation points.
Definition adjoint.cpp:137
Class (Function) to enforce essential constrains on the left hand side diagonal.
Definition Essential.hpp:33
PetscBool do_eval_field
Evaluate field.
Definition plastic.cpp:119

◆ solveElastic() [2/2]

MoFEMErrorCode Example::solveElastic ( )
private

Solve forward elastic problem.

◆ solveSystem() [1/13]

MoFEMErrorCode Example::solveSystem ( )
private

[Solve]

[Assemble system]

[Push operators to pipeline]

[Solve]

< Mass matrix

< Linear solver

Examples
mofem/tutorials/adv-4_dynamic_first_order_con_law/dynamic_first_order_con_law.cpp, mofem/tutorials/adv-6_dg_projection/between_meshes_dg_projection.cpp, mofem/tutorials/clx-0_acoustics/helmholtz.cpp, mofem/tutorials/fun-2_plot_base/plot_base.cpp, mofem/tutorials/mix-1_light_intensity_equation/phase.cpp, mofem/tutorials/scl-0_least_squares/approximation.cpp, mofem/tutorials/scl-9_heat_method/heat_method.cpp, mofem/tutorials/vec-1_eigen_elasticity/eigen_elastic.cpp, mofem/tutorials/vec-3_nonlinear_dynamics/nonlinear_dynamic_elastic.cpp, mofem/tutorials/vec-4_shallow_wave/shallow_wave.cpp, and nonlinear_elastic.cpp.

Definition at line 876 of file dynamic_first_order_con_law.cpp.

876 {
878 auto *simple = mField.getInterface<Simple>();
879 auto *pipeline_mng = mField.getInterface<PipelineManager>();
880
881 auto dm = simple->getDM();
882
883 auto calculate_stress_ops = [&](auto &pip) {
885
886 auto v_ptr = boost::make_shared<MatrixDouble>();
887 pip.push_back(new OpCalculateVectorFieldValues<SPACE_DIM>("V", v_ptr));
888 auto X_ptr = boost::make_shared<MatrixDouble>();
889 pip.push_back(
890 new OpCalculateVectorFieldValues<SPACE_DIM>("GEOMETRY", X_ptr));
891
892 auto x_ptr = boost::make_shared<MatrixDouble>();
893 pip.push_back(new OpCalculateVectorFieldValues<SPACE_DIM>("x_1", x_ptr));
894
895 // Calculate unknown F
896 auto mat_H_tensor_ptr = boost::make_shared<MatrixDouble>();
898 "F", mat_H_tensor_ptr));
899
900 auto u_ptr = boost::make_shared<MatrixDouble>();
901 pip.push_back(new OpCalculateDisplacement<SPACE_DIM>(x_ptr, X_ptr, u_ptr));
902 // Calculate P
903
904 auto mat_F_ptr = boost::make_shared<MatrixDouble>();
906 mat_F_ptr, mat_H_tensor_ptr));
907
908 PetscBool is_linear_elasticity = PETSC_TRUE;
909 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-is_linear_elasticity",
910 &is_linear_elasticity, PETSC_NULLPTR);
911
912 auto mat_P_ptr = boost::make_shared<MatrixDouble>();
913 if (is_linear_elasticity) {
916 mat_F_ptr));
917 } else {
918 auto inv_F = boost::make_shared<MatrixDouble>();
919 auto det_ptr = boost::make_shared<VectorDouble>();
920
921 pip.push_back(new OpInvertMatrix<SPACE_DIM>(mat_F_ptr, det_ptr, inv_F));
922
925 mat_F_ptr, inv_F, det_ptr));
926 }
927
928 auto mat_v_grad_ptr = boost::make_shared<MatrixDouble>();
930 "V", mat_v_grad_ptr));
931
932 return boost::make_tuple(v_ptr, X_ptr, x_ptr, mat_P_ptr, mat_F_ptr, u_ptr);
933 };
934
935 auto post_proc_boundary = [&]() {
936 auto boundary_post_proc_fe = boost::make_shared<PostProcFaceEle>(mField);
937
939 boundary_post_proc_fe->getOpPtrVector(), {}, "GEOMETRY");
940 auto op_loop_side =
942 // push ops to side element, through op_loop_side operator
943 auto [boundary_v_ptr, boundary_X_ptr, boundary_x_ptr, boundary_mat_P_ptr,
944 boundary_mat_F_ptr, boundary_u_ptr] =
945 calculate_stress_ops(op_loop_side->getOpPtrVector());
946 boundary_post_proc_fe->getOpPtrVector().push_back(op_loop_side);
947
949
950 boundary_post_proc_fe->getOpPtrVector().push_back(
951
952 new OpPPMap(
953
954 boundary_post_proc_fe->getPostProcMesh(),
955 boundary_post_proc_fe->getMapGaussPts(),
956
958
959 OpPPMap::DataMapMat{{"V", boundary_v_ptr},
960 {"GEOMETRY", boundary_X_ptr},
961 {"x", boundary_x_ptr},
962 {"U", boundary_u_ptr}},
963
964 OpPPMap::DataMapMat{{"FIRST_PIOLA", boundary_mat_P_ptr},
965 {"F", boundary_mat_F_ptr}},
966
968
969 )
970
971 );
972 return boundary_post_proc_fe;
973 };
974
975 // Add monitor to time solver
976
977 double rho = 1.;
978 CHKERR PetscOptionsGetReal(PETSC_NULLPTR, "", "-density", &rho, PETSC_NULLPTR);
979 auto get_rho = [rho](const double, const double, const double) {
980 return rho;
981 };
982
983 SmartPetscObj<Mat> M; ///< Mass matrix
984 SmartPetscObj<KSP> ksp; ///< Linear solver
985
986 auto ts_pre_post_proc = boost::make_shared<TSPrePostProc>();
987 tsPrePostProc = ts_pre_post_proc;
988
990 CHKERR MatZeroEntries(M);
991
992 boost::shared_ptr<DomainEle> vol_mass_ele(new DomainEle(mField));
993
994 vol_mass_ele->B = M;
995
996 auto integration_rule = [](int, int, int approx_order) {
997 return 2 * approx_order;
998 };
999
1000 vol_mass_ele->getRuleHook = integration_rule;
1001
1002 vol_mass_ele->getOpPtrVector().push_back(new OpMassV("V", "V", get_rho));
1003 vol_mass_ele->getOpPtrVector().push_back(new OpMassF("F", "F"));
1004
1006 CHKERR MatAssemblyBegin(M, MAT_FINAL_ASSEMBLY);
1007 CHKERR MatAssemblyEnd(M, MAT_FINAL_ASSEMBLY);
1008
1009 auto lumpVec = createDMVector(simple->getDM());
1010 CHKERR MatGetRowSum(M, lumpVec);
1011
1012 CHKERR MatZeroEntries(M);
1013 CHKERR MatDiagonalSet(M, lumpVec, INSERT_VALUES);
1014
1015 // Create and septup KSP (linear solver), we need this to calculate g(t,u) =
1016 // M^-1G(t,u)
1017 ksp = createKSP(mField.get_comm());
1018 CHKERR KSPSetOperators(ksp, M, M);
1019 CHKERR KSPSetFromOptions(ksp);
1020 CHKERR KSPSetUp(ksp);
1021
1022 auto solve_boundary_for_g = [&]() {
1024 if (*(pipeline_mng->getBoundaryExplicitRhsFE()->vecAssembleSwitch)) {
1025
1026 CHKERR VecGhostUpdateBegin(pipeline_mng->getBoundaryExplicitRhsFE()->ts_F,
1027 ADD_VALUES, SCATTER_REVERSE);
1028 CHKERR VecGhostUpdateEnd(pipeline_mng->getBoundaryExplicitRhsFE()->ts_F,
1029 ADD_VALUES, SCATTER_REVERSE);
1030 CHKERR VecAssemblyBegin(pipeline_mng->getBoundaryExplicitRhsFE()->ts_F);
1031 CHKERR VecAssemblyEnd(pipeline_mng->getBoundaryExplicitRhsFE()->ts_F);
1032 *(pipeline_mng->getBoundaryExplicitRhsFE()->vecAssembleSwitch) = false;
1033
1034 auto D =
1035 vectorDuplicate(pipeline_mng->getBoundaryExplicitRhsFE()->ts_F);
1036 CHKERR KSPSolve(ksp, pipeline_mng->getBoundaryExplicitRhsFE()->ts_F, D);
1037 CHKERR VecGhostUpdateBegin(D, INSERT_VALUES, SCATTER_FORWARD);
1038 CHKERR VecGhostUpdateEnd(D, INSERT_VALUES, SCATTER_FORWARD);
1039 CHKERR VecCopy(D, pipeline_mng->getBoundaryExplicitRhsFE()->ts_F);
1040 }
1041
1043 };
1044
1045 pipeline_mng->getBoundaryExplicitRhsFE()->postProcessHook =
1046 solve_boundary_for_g;
1047
1049 ts = pipeline_mng->createTSEX(dm);
1050
1051 // Field eval
1052 PetscBool field_eval_flag = PETSC_TRUE;
1053 boost::shared_ptr<MatrixDouble> velocity_field_ptr;
1054 boost::shared_ptr<MatrixDouble> geometry_field_ptr;
1055 boost::shared_ptr<MatrixDouble> spatial_position_field_ptr;
1056 boost::shared_ptr<SetPtsData> field_eval_data;
1057
1058 std::array<double, 3> field_eval_coords = {0.5, 0.5, 5.};
1059 int dim = 3;
1060 CHKERR PetscOptionsGetRealArray(NULL, NULL, "-field_eval_coords",
1061 field_eval_coords.data(), &dim,
1062 &field_eval_flag);
1063
1064 if (field_eval_flag) {
1065 field_eval_data =
1066 mField.getInterface<FieldEvaluatorInterface>()->getData<DomainEle>();
1067 CHKERR mField.getInterface<FieldEvaluatorInterface>()->buildTree<SPACE_DIM>(
1068 field_eval_data, simple->getDomainFEName());
1069
1070 field_eval_data->setEvalPoints(field_eval_coords.data(), 1);
1071
1072 auto no_rule = [](int, int, int) { return -1; };
1073
1074 auto fe_ptr = field_eval_data->feMethodPtr;
1075 fe_ptr->getRuleHook = no_rule;
1076 velocity_field_ptr = boost::make_shared<MatrixDouble>();
1077 geometry_field_ptr = boost::make_shared<MatrixDouble>();
1078 spatial_position_field_ptr = boost::make_shared<MatrixDouble>();
1079 fe_ptr->getOpPtrVector().push_back(
1080 new OpCalculateVectorFieldValues<SPACE_DIM>("V", velocity_field_ptr));
1081 fe_ptr->getOpPtrVector().push_back(
1083 geometry_field_ptr));
1084 fe_ptr->getOpPtrVector().push_back(
1086 "x_2", spatial_position_field_ptr));
1087 }
1088
1089 auto post_proc_domain = [&]() {
1090 auto post_proc_fe_vol = boost::make_shared<PostProcEle>(mField);
1091
1093
1094 auto [boundary_v_ptr, boundary_X_ptr, boundary_x_ptr, boundary_mat_P_ptr,
1095 boundary_mat_F_ptr, boundary_u_ptr] =
1096 calculate_stress_ops(post_proc_fe_vol->getOpPtrVector());
1097
1098 post_proc_fe_vol->getOpPtrVector().push_back(
1099
1100 new OpPPMap(
1101
1102 post_proc_fe_vol->getPostProcMesh(),
1103 post_proc_fe_vol->getMapGaussPts(),
1104
1105 {},
1106
1107 {{"V", boundary_v_ptr},
1108 {"GEOMETRY", boundary_X_ptr},
1109 {"x", boundary_x_ptr},
1110 {"U", boundary_u_ptr}},
1111
1112 {{"FIRST_PIOLA", boundary_mat_P_ptr}, {"F", boundary_mat_F_ptr}},
1113
1114 {}
1115
1116 )
1117
1118 );
1119 return post_proc_fe_vol;
1120 };
1121
1122 boost::shared_ptr<FEMethod> null_fe;
1123 auto monitor_ptr = boost::make_shared<Monitor>(
1124 SmartPetscObj<DM>(dm, true), mField, post_proc_domain(),
1125 post_proc_boundary(), velocity_field_ptr, spatial_position_field_ptr,
1126 geometry_field_ptr, field_eval_coords, field_eval_data);
1127
1129 null_fe, monitor_ptr);
1130
1131 double ftime = 1;
1132 // CHKERR TSSetMaxTime(ts, ftime);
1133 CHKERR TSSetExactFinalTime(ts, TS_EXACTFINALTIME_MATCHSTEP);
1134
1135 auto T = createDMVector(simple->getDM());
1136 CHKERR DMoFEMMeshToLocalVector(simple->getDM(), T, INSERT_VALUES,
1137 SCATTER_FORWARD);
1138 CHKERR TSSetSolution(ts, T);
1139 CHKERR TSSetFromOptions(ts);
1140
1141 CHKERR TSSetPostStage(ts, TSPrePostProc::tsPostStage);
1142 CHKERR TSSetPostStep(ts, TSPrePostProc::tsPostStep);
1143 CHKERR TSSetPreStep(ts, TSPrePostProc::tsPreStep);
1144
1145 boost::shared_ptr<ForcesAndSourcesCore> null;
1146
1147 if (auto ptr = tsPrePostProc.lock()) {
1148 ptr->fsRawPtr = this;
1149 CHKERR TSSetUp(ts);
1150 CHKERR TSSolve(ts, NULL);
1151 CHKERR TSGetTime(ts, &ftime);
1152 }
1153
1155}
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::BiLinearForm< GAUSS >::OpMass< 1, SPACE_DIM > OpMassV
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::BiLinearForm< GAUSS >::OpMass< 1, SPACE_DIM *SPACE_DIM > OpMassF
static boost::weak_ptr< TSPrePostProc > tsPrePostProc
PetscErrorCode DMCreateMatrix_MoFEM(DM dm, Mat *M)
Definition DMMoFEM.cpp:1188
PetscErrorCode DMMoFEMTSSetMonitor(DM dm, TS ts, const std::string fe_name, boost::shared_ptr< MoFEM::FEMethod > method, boost::shared_ptr< MoFEM::BasicMethod > pre_only, boost::shared_ptr< MoFEM::BasicMethod > post_only)
Set Monitor To TS solver.
Definition DMMoFEM.cpp:1046
SmartPetscObj< Mat > M
static MoFEMErrorCode tsPostStep(TS ts)
static MoFEMErrorCode tsPreStep(TS ts)
static MoFEMErrorCode tsPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
[Boundary condition]

◆ solveSystem() [2/13]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [3/13]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [4/13]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [5/13]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [6/13]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [7/13]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [8/13]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [9/13]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [10/13]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [11/13]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [12/13]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [13/13]

MoFEMErrorCode Example::solveSystem ( )
private

◆ testGradient()

MoFEMErrorCode Example::testGradient ( Vec  gradient_vector)
private

[calculateGradient]

[Finite difference check]

Definition at line 1203 of file gradient.cpp.

1203 {
1205
1206 PetscBool gradient_fd_check = PETSC_FALSE;
1207 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, PETSC_NULLPTR, "-gradient_fd_check",
1208 &gradient_fd_check, PETSC_NULLPTR);
1209 if (gradient_fd_check) {
1210
1211 PetscInt nb_modes = 5;
1212 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, PETSC_NULLPTR,
1213 "-gradient_nb_modes", &nb_modes, PETSC_NULLPTR);
1214
1215 auto simple = mField.getInterface<Simple>();
1216 auto dm = simple->getDM();
1217 auto *adj_problem_ptr = getProblemPtr(adjointDM);
1218
1219 auto fe_rule = [](int, int, int p_data) { return 2 * p_data + p_data - 1; };
1220 auto get_objective_fe = [&](auto glob_objective_ptr, auto fe_rule) {
1221 auto fe_obj_fe = boost::make_shared<DomainEle>(mField);
1222 fe_obj_fe->getRuleHook = fe_rule;
1223 auto &pip = fe_obj_fe->getOpPtrVector();
1225 auto jac_ptr = boost::make_shared<MatrixDouble>();
1226 auto u_ptr = boost::make_shared<MatrixDouble>();
1228 "GEOMETRY", jac_ptr));
1229 pip.push_back(new OpCalculateVectorFieldValues<SPACE_DIM>("U", u_ptr));
1230 auto common_ptr = HookeOps::commonDataFactory<SPACE_DIM, I, DomainEleOp>(
1231 mField, pip, "U", "MAT_ELASTIC", Sev::noisy);
1232 pip.push_back(new OpObjective(pythonPtr, common_ptr, jac_ptr, u_ptr,
1233 glob_objective_ptr));
1234 return fe_obj_fe;
1235 };
1236
1237 auto f = boost::make_shared<double>(0.);
1238 auto fe_obj = get_objective_fe(f, fe_rule);
1239 CacheTupleSharedPtr tmp_cache_ptr = boost::make_shared<CacheTuple>();
1241 tmp_cache_ptr);
1242
1243 auto evaluate_objective_terms = [&](auto objective_fe, auto objective_ptr,
1244 double &objective_value) {
1246 *objective_ptr = 0.0;
1248 objective_fe);
1249 MPI_Allreduce(MPI_IN_PLACE, objective_ptr.get(), 1, MPI_DOUBLE, MPI_SUM,
1250 mField.get_comm());
1251 objective_value = *objective_ptr;
1253 };
1254
1255 auto geometry_bit_number = mField.get_field_bit_number("GEOMETRY");
1256 for (PetscInt mode = 0; mode < nb_modes; ++mode) {
1257 MOFEM_LOG_CHANNEL("WORLD");
1258 MOFEM_LOG("WORLD", Sev::verbose) << "Processing mode: " << mode;
1259
1260 auto &adj_dofs =
1261 adj_problem_ptr->getNumeredRowDofsPtr()->get<PetscGlobalIdx_mi_tag>();
1262 auto &dofs = mField.get_dofs()->get<Unique_mi_tag>();
1263
1264 auto a_dof = adj_dofs.find(mode);
1265 if (a_dof != adj_dofs.end()) {
1266 auto ent = (*a_dof)->getEnt();
1267 auto dof_idx = (*a_dof)->getEntDofIdx();
1269 dof_idx,
1270 FieldEntity::getLocalUniqueIdCalculate(geometry_bit_number, ent));
1271 auto dof = dofs.find(uid);
1272 if (dof != dofs.end()) {
1273 auto org_geom_val = (*dof)->getFieldData();
1274 constexpr double eps = 1e-6;
1275
1276 (*dof)->getFieldData() = org_geom_val + eps;
1277 CHKERR KSPReset(kspElastic);
1279 double f_plus;
1280 CHKERR evaluate_objective_terms(fe_obj, f, f_plus);
1281
1282 (*dof)->getFieldData() = org_geom_val - eps;
1283 CHKERR KSPReset(kspElastic);
1285 double f_minus;
1286 CHKERR evaluate_objective_terms(fe_obj, f, f_minus);
1287
1288 double *g_array;
1289 CHKERR VecGetArray(gradient_vector, &g_array);
1290 if ((*a_dof)->getHasLocalIndex()) {
1291 auto adjoint_grad = g_array[(*a_dof)->getPetscLocalDofIdx()];
1292 auto finite_diff_grad = (f_plus - f_minus) / (2 * eps);
1293 auto err = std::abs(adjoint_grad - finite_diff_grad) /
1294 std::max(std::abs(adjoint_grad),
1295 std::abs(finite_diff_grad));
1296 MOFEM_LOG("WORLD", Sev::inform)
1297 << "Mode: " << mode << ", Adjoint gradient: " << adjoint_grad
1298 << ", Finite difference gradient: " << finite_diff_grad
1299 << ", Relative error: "
1300 << std::abs(adjoint_grad - finite_diff_grad) /
1301 std::max(std::abs(adjoint_grad),
1302 std::abs(finite_diff_grad));
1303 constexpr double tol = 1e-4;
1304 if (err > tol) {
1305 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID,
1306 "Gradient check failed for mode %d: relative error %e is "
1307 "greater than tolerance %e",
1308 mode, err, tol);
1309 }
1310 }
1311
1312 CHKERR VecRestoreArray(gradient_vector, &g_array);
1313
1314 constexpr bool restore_solution = false;
1315 if constexpr (restore_solution) {
1316 (*dof)->getFieldData() = org_geom_val;
1317 CHKERR KSPReset(kspElastic);
1319 }
1320 }
1321 }
1322 }
1323 }
1324
1326}
virtual const DofEntity_multiIndex * get_dofs() const =0
Get the dofs object.
double tol
boost::shared_ptr< CacheTuple > CacheTupleSharedPtr
auto getProblemPtr(DM dm)
get problem pointer from DM
Definition DMMoFEM.hpp:1182
MoFEMErrorCode solveElastic()
Solve forward elastic problem.
Definition adjoint.cpp:1198
virtual FieldBitNumber get_field_bit_number(const std::string name) const =0
get field bit number
virtual MoFEMErrorCode cache_problem_entities(const std::string prb_name, CacheTupleWeakPtr cache_ptr)=0
Cache variables.
static UId getUniqueIdCalculate(const DofIdx dof, UId ent_uid)
UId getLocalUniqueIdCalculate()
Get the Local Unique Id Calculate object.

◆ testOperators() [1/2]

MoFEMErrorCode Example::testOperators ( )
private

[Solve]

[TestOperators]

Examples
mofem/tutorials/adv-0_plasticity/plastic.cpp, and plastic.cpp.

Definition at line 1484 of file plastic.cpp.

1484 {
1486
1487 // get operators tester
1488 auto simple = mField.getInterface<Simple>();
1489 auto opt = mField.getInterface<OperatorsTester>(); // get interface to
1490 // OperatorsTester
1491 auto pip = mField.getInterface<PipelineManager>(); // get interface to
1492 // pipeline manager
1493
1494 constexpr double eps = 1e-9;
1495
1496 auto x = opt->setRandomFields(simple->getDM(), {
1497
1498 {"U", {-1e-4, 1e-4}},
1499
1500 {"EP", {-1e-4, 1e-4}},
1501
1502 {"TAU", {0, 1e-4}}
1503
1504 });
1505
1506 auto dot_x_plastic_active =
1507 opt->setRandomFields(simple->getDM(), {
1508
1509 {"U", {-1, 1}},
1510
1511 {"EP", {-1, 1}},
1512
1513 {"TAU", {0.1, 0.5}}
1514
1515 });
1516 auto diff_x_plastic_active =
1517 opt->setRandomFields(simple->getDM(), {
1518
1519 {"U", {-1, 1}},
1520
1521 {"EP", {-1, 1}},
1522
1523 {"TAU", {-1, 1}}
1524
1525 });
1526
1527 auto dot_x_elastic =
1528 opt->setRandomFields(simple->getDM(), {
1529
1530 {"U", {-1, 1}},
1531
1532 {"EP", {-1, 1}},
1533
1534 {"TAU", {-1, -0.1}}
1535
1536 });
1537 auto diff_x_elastic =
1538 opt->setRandomFields(simple->getDM(), {
1539
1540 {"U", {-1, 1}},
1541
1542 {"EP", {-1, 1}},
1543
1544 {"TAU", {-1, 1}}
1545
1546 });
1547
1548 auto test_domain_ops = [&](auto fe_name, auto lhs_pipeline, auto rhs_pipeline,
1549 auto dot_x, auto diff_x) {
1551
1552 auto diff_res = opt->checkCentralFiniteDifference(
1553 simple->getDM(), fe_name, rhs_pipeline, lhs_pipeline, x, dot_x,
1554 SmartPetscObj<Vec>(), diff_x, 0, 0.5, eps);
1555
1556 // Calculate norm of difference between directional derivative calculated
1557 // from finite difference, and tangent matrix.
1558 double fnorm;
1559 CHKERR VecNorm(diff_res, NORM_2, &fnorm);
1560 MOFEM_LOG_C("PLASTICITY", Sev::inform,
1561 "Test consistency of tangent matrix %3.4e", fnorm);
1562
1563 constexpr double err = 1e-5;
1564 if (fnorm > err)
1565 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
1566 "Norm of directional derivative too large err = %3.4e", fnorm);
1567
1569 };
1570
1571 MOFEM_LOG("PLASTICITY", Sev::inform) << "Elastic active";
1572 CHKERR test_domain_ops(simple->getDomainFEName(), pip->getDomainLhsFE(),
1573 pip->getDomainRhsFE(), dot_x_elastic, diff_x_elastic);
1574
1575 MOFEM_LOG("PLASTICITY", Sev::inform) << "Plastic active";
1576 CHKERR test_domain_ops(simple->getDomainFEName(), pip->getDomainLhsFE(),
1577 pip->getDomainRhsFE(), dot_x_plastic_active,
1578 diff_x_plastic_active);
1579
1581};
Calculate directional derivative of the right hand side and compare it with tangent matrix derivative...

◆ testOperators() [2/2]

MoFEMErrorCode Example::testOperators ( )
private

◆ thermalBC()

MoFEMErrorCode Example::thermalBC ( BitRefLevel  bit,
BitRefLevel  mask 
)
private

[Mechanical boundary conditions]

[Thermal boundary conditions]

Examples
thermoplastic.cpp.

Definition at line 3505 of file thermoplastic.cpp.

3505 {
3507
3508 MOFEM_LOG("SYNC", Sev::noisy) << "bC";
3510
3511 auto simple = mField.getInterface<Simple>();
3512 auto bc_mng = mField.getInterface<BcManager>();
3513
3514 auto get_skin = [&]() {
3515 Range body_ents;
3516 CHKERR mField.get_moab().get_entities_by_dimension(0, SPACE_DIM, body_ents);
3517 CHKERR mField.getInterface<BitRefManager>()->filterEntitiesByRefLevel(
3518 bit, BitRefLevel().set(), body_ents);
3519 Skinner skin(&mField.get_moab());
3520 Range skin_ents;
3521 CHKERR skin.find_skin(0, body_ents, false, skin_ents);
3522 return skin_ents;
3523 };
3524
3525 auto filter_flux_blocks = [&](auto skin, bool temp_bc = false) {
3526 auto remove_cubit_blocks = [&](auto c) {
3528 for (auto m :
3529
3530 mField.getInterface<MeshsetsManager>()->getCubitMeshsetPtr(c)
3531
3532 ) {
3533 Range ents;
3534 CHKERR mField.get_moab().get_entities_by_dimension(
3535 m->getMeshset(), SPACE_DIM - 1, ents, true);
3536 skin = subtract(skin, ents);
3537 }
3539 };
3540
3541 auto remove_named_blocks = [&](auto n) {
3543 for (auto m : mField.getInterface<MeshsetsManager>()->getCubitMeshsetPtr(
3544 std::regex(
3545
3546 (boost::format("%s(.*)") % n).str()
3547
3548 ))
3549
3550 ) {
3551 Range ents;
3552 CHKERR mField.get_moab().get_entities_by_dimension(
3553 m->getMeshset(), SPACE_DIM - 1, ents, true);
3554 skin = subtract(skin, ents);
3555 }
3557 };
3558 if (!temp_bc) {
3559 CHK_THROW_MESSAGE(remove_cubit_blocks(NODESET | TEMPERATURESET),
3560 "remove_cubit_blocks");
3561 CHK_THROW_MESSAGE(remove_named_blocks("TEMPERATURE"),
3562 "remove_named_blocks");
3563 }
3564 CHK_THROW_MESSAGE(remove_cubit_blocks(SIDESET | HEATFLUXSET),
3565 "remove_cubit_blocks");
3566 CHK_THROW_MESSAGE(remove_named_blocks("HEATFLUX"), "remove_named_blocks");
3567 CHK_THROW_MESSAGE(remove_named_blocks("CONVECTION"), "remove_named_blocks");
3568 CHK_THROW_MESSAGE(remove_named_blocks("RADIATION"), "remove_named_blocks");
3569 return skin;
3570 };
3571
3572 auto filter_true_skin = [&](auto skin) {
3573 Range boundary_ents;
3574 ParallelComm *pcomm =
3575 ParallelComm::get_pcomm(&mField.get_moab(), MYPCOMM_INDEX);
3576 CHKERR pcomm->filter_pstatus(skin, PSTATUS_SHARED | PSTATUS_MULTISHARED,
3577 PSTATUS_NOT, -1, &boundary_ents);
3578 return boundary_ents;
3579 };
3580
3581 auto remove_flux_ents = filter_true_skin(filter_flux_blocks(get_skin()));
3582 auto remove_temp_bc_ents =
3583 filter_true_skin(filter_flux_blocks(get_skin(), true));
3584
3585 // CHKERR mField.getInterface<CommInterface>()->synchroniseEntities(
3586 // remove_flux_ents);
3587 // CHKERR mField.getInterface<CommInterface>()->synchroniseEntities(
3588 // remove_temp_bc_ents);
3589
3590 MOFEM_LOG("SYNC", Sev::noisy) << remove_flux_ents << endl;
3592
3593 MOFEM_LOG("SYNC", Sev::noisy) << remove_temp_bc_ents << endl;
3595
3596#ifndef NDEBUG
3597
3599 mField.get_moab(),
3600 (boost::format("flux_remove_%d.vtk") % mField.get_comm_rank()).str(),
3601 remove_flux_ents);
3602
3603#endif
3604
3605 if (is_distributed_mesh == PETSC_TRUE) {
3606 CHKERR mField.getInterface<ProblemsManager>()->removeDofsOnEntities(
3607 simple->getProblemName(), "FLUX", remove_flux_ents);
3608 CHKERR mField.getInterface<ProblemsManager>()->removeDofsOnEntities(
3609 simple->getProblemName(), "TBC", remove_temp_bc_ents);
3610 } else {
3612 ->removeDofsOnEntitiesNotDistributed(simple->getProblemName(), "FLUX",
3613 remove_flux_ents);
3615 ->removeDofsOnEntitiesNotDistributed(simple->getProblemName(), "TBC",
3616 remove_temp_bc_ents);
3617 }
3618
3619 // auto set_init_temp = [](boost::shared_ptr<FieldEntity> field_entity_ptr) {
3620 // field_entity_ptr->getEntFieldData()[0] = init_temp;
3621 // return 0;
3622 // };
3623 // CHKERR
3624 // mField.getInterface<FieldBlas>()->fieldLambdaOnEntities(set_init_temp,
3625 // "T");
3626
3627 CHKERR bc_mng->pushMarkDOFsOnEntities<HeatFluxCubitBcData>(
3628 simple->getProblemName(), "FLUX", false);
3629
3631}
#define MOFEM_LOG_SEVERITY_SYNC(comm, severity)
Synchronise "SYNC" on curtain severity level.
@ TEMPERATURESET
@ HEATFLUXSET
@ NODESET
@ SIDESET
const double c
speed of light (cm/ns)
Definition of the heat flux bc data structure.
Definition BCData.hpp:423

◆ topologyModes()

MoFEMErrorCode Example::topologyModes ( )
private

Compute topology optimization modes.

[Boundary condition]

[Adjoint modes]

Examples
mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 725 of file adjoint.cpp.

725 {
727
728 auto opt_ents = get_range_from_block(mField, "OPTIMISE", SPACE_DIM - 1);
729 auto subset_dm_bdy = createDM(mField.get_comm(), "DMMOFEM");
730 CHKERR DMMoFEMSetSquareProblem(subset_dm_bdy, PETSC_TRUE);
731 CHKERR DMMoFEMCreateSubDM(subset_dm_bdy, adjointDM, "SUBSET_BDY");
732 CHKERR DMMoFEMAddElement(subset_dm_bdy, "ADJOINT_BOUNDARY_FE");
733 CHKERR DMMoFEMAddSubFieldRow(subset_dm_bdy, "ADJOINT_FIELD",
734 boost::make_shared<Range>(opt_ents));
735 CHKERR DMMoFEMAddSubFieldCol(subset_dm_bdy, "ADJOINT_FIELD",
736 boost::make_shared<Range>(opt_ents));
737 CHKERR DMSetUp(subset_dm_bdy);
738
739 auto subset_dm_domain = createDM(mField.get_comm(), "DMMOFEM");
740 CHKERR DMMoFEMSetSquareProblem(subset_dm_domain, PETSC_TRUE);
741 CHKERR DMMoFEMCreateSubDM(subset_dm_domain, adjointDM, "SUBSET_DOMAIN");
742 CHKERR DMMoFEMAddElement(subset_dm_domain, "ADJOINT_DOMAIN_FE");
743 CHKERR DMMoFEMAddSubFieldRow(subset_dm_domain, "ADJOINT_FIELD");
744 CHKERR DMMoFEMAddSubFieldCol(subset_dm_domain, "ADJOINT_FIELD");
745 CHKERR DMSetUp(subset_dm_domain);
746
747 // remove dofs on boundary of the domain
748 auto remove_dofs = [&]() {
750
751 std::array<Range, 3> remove_dim_ents;
752 remove_dim_ents[0] =
753 get_range_from_block(mField, "OPT_REMOVE_X", SPACE_DIM - 1);
754 remove_dim_ents[1] =
755 get_range_from_block(mField, "OPT_REMOVE_Y", SPACE_DIM - 1);
756 remove_dim_ents[2] =
757 get_range_from_block(mField, "OPT_REMOVE_Z", SPACE_DIM - 1);
758
759 for (int d = 0; d != 3; ++d) {
760 MOFEM_LOG("WORLD", Sev::inform)
761 << "Removing topology modes on block OPT_REMOVE_" << (char)('X' + d)
762 << " with " << remove_dim_ents[d].size() << " entities";
763 }
764
765 Range body_ents;
766 CHKERR mField.get_moab().get_entities_by_dimension(0, SPACE_DIM, body_ents,
767 true);
768 auto skin = moab::Skinner(&mField.get_moab());
769 Range boundary_ents;
770 CHKERR skin.find_skin(0, body_ents, false, boundary_ents);
771 for (int d = 0; d != 3; ++d) {
772 boundary_ents = subtract(boundary_ents, remove_dim_ents[d]);
773 }
774 ParallelComm *pcomm =
775 ParallelComm::get_pcomm(&mField.get_moab(), MYPCOMM_INDEX);
776 CHKERR pcomm->filter_pstatus(boundary_ents,
777 PSTATUS_SHARED | PSTATUS_MULTISHARED,
778 PSTATUS_NOT, -1, nullptr);
779 for (auto d = SPACE_DIM - 2; d >= 0; --d) {
780 if (d >= 0) {
781 Range ents;
782 CHKERR mField.get_moab().get_adjacencies(boundary_ents, d, false, ents,
783 moab::Interface::UNION);
784 boundary_ents.merge(ents);
785 } else {
786 Range verts;
787 CHKERR mField.get_moab().get_connectivity(boundary_ents, verts);
788 boundary_ents.merge(verts);
789 }
790 CHKERR mField.getInterface<CommInterface>()->synchroniseEntities(
791 boundary_ents);
792 }
793 boundary_ents.merge(opt_ents);
794 CHKERR mField.getInterface<ProblemsManager>()->removeDofsOnEntities(
795 "SUBSET_DOMAIN", "ADJOINT_FIELD", boundary_ents);
796 for (int d = 0; d != 3; ++d) {
797 CHKERR mField.getInterface<ProblemsManager>()->removeDofsOnEntities(
798 "SUBSET_DOMAIN", "ADJOINT_FIELD", remove_dim_ents[d], d, d);
799 }
800
801 // #ifndef NDEBUG
802 if (mField.get_comm_rank() == 0) {
803 CHKERR save_range(mField.get_moab(), "topoMode_boundary_ents.vtk",
804 boundary_ents);
805 }
806 // #endif
807
809 };
810
811 CHKERR remove_dofs();
812
813 auto get_lhs_fe = [&]() {
814 auto fe_lhs = boost::make_shared<BoundaryEle>(mField);
815 fe_lhs->getRuleHook = [](int, int, int p_data) {
816 return 2 * p_data + p_data - 1;
817 };
818 auto &pip = fe_lhs->getOpPtrVector();
820 "GEOMETRY");
823 pip.push_back(new OpMass("ADJOINT_FIELD", "ADJOINT_FIELD",
824 [](double, double, double) { return 1.; }));
825 return fe_lhs;
826 };
827
828 auto get_rhs_fe = [&]() {
829 auto fe_rhs = boost::make_shared<BoundaryEle>(mField);
830 fe_rhs->getRuleHook = [](int, int, int p_data) {
831 return 2 * p_data + p_data - 1;
832 };
833 auto &pip = fe_rhs->getOpPtrVector();
835 "GEOMETRY");
836
837 return fe_rhs;
838 };
839
840 auto block_name = "OPTIMISE";
841 auto mesh_mng = mField.getInterface<MeshsetsManager>();
842 auto bcs = mesh_mng->getCubitMeshsetPtr(
843
844 std::regex((boost::format("%s(.*)") % block_name).str())
845
846 );
847
848 for (auto &v : modeVecs) {
849 v = createDMVector(subset_dm_bdy);
850 }
851
853 struct OpMode : public OP {
854 OpMode(const std::string name,
855 boost::shared_ptr<ObjectiveFunctionData> python_ptr, int id,
856 std::vector<SmartPetscObj<Vec>> mode_vecs,
857 std::vector<std::array<double, 3>> mode_centroids,
858 std::vector<std::array<double, 6>> mode_bboxes, int block_counter,
859 int mode_counter, boost::shared_ptr<Range> range = nullptr)
860 : OP(name, name, OP::OPROW, range), pythonPtr(python_ptr), iD(id),
861 modeVecs(mode_vecs), modeCentroids(mode_centroids),
862 modeBboxes(mode_bboxes), blockCounter(block_counter),
863 modeCounter(mode_counter) {}
864
865 MoFEMErrorCode doWork(int side, EntityType type, EntData &data) {
867
868 if (OP::entsPtr) {
869 if (OP::entsPtr->find(this->getFEEntityHandle()) == OP::entsPtr->end())
871 }
872
873 auto nb_rows = data.getIndices().size();
874 if (!nb_rows) {
876 }
877 auto nb_base_functions = data.getN().size2();
878
880 CHKERR pythonPtr->blockModes(iD, OP::getCoordsAtGaussPts(),
881 modeCentroids[blockCounter],
882 modeBboxes[blockCounter], blockModes);
883
884 auto nb_integration_pts = getGaussPts().size2();
885 if (blockModes.size2() != 3 * nb_integration_pts) {
886 MOFEM_LOG("WORLD", Sev::error)
887 << "Number of modes does not match number of integration points: "
888 << blockModes.size2() << "!=" << 3 * nb_integration_pts;
889 CHK_THROW_MESSAGE(MOFEM_DATA_INCONSISTENCY, "modes/integration points");
890 }
891
892 VectorDouble nf(nb_rows);
893
894 int nb_modes = blockModes.size1();
895 for (auto mode = 0; mode != nb_modes; ++mode) {
896 nf.clear();
897 // get mode
898 auto t_mode = getFTensor1FromPtr<3>(&blockModes(mode, 0));
899 // get element volume
900 const double vol = OP::getMeasure();
901 // get integration weights
902 auto t_w = OP::getFTensor0IntegrationWeight();
903 // get base function gradient on rows
904 auto t_base = data.getFTensor0N();
905 // loop over integration points
906 for (int gg = 0; gg != nb_integration_pts; gg++) {
907
908 // take into account Jacobian
909 const double alpha = t_w * vol;
910 // loop over rows base functions
911 auto t_nf = getFTensor1FromPtr<SPACE_DIM>(nf.data().data());
912 int rr = 0;
913 for (; rr != nb_rows / SPACE_DIM; ++rr) {
914 t_nf(i) += alpha * t_base * t_mode(i);
915 ++t_base;
916 ++t_nf;
917 }
918 for (; rr < nb_base_functions; ++rr)
919 ++t_base;
920 ++t_w; // move to another integration weight
921 ++t_mode; // move to another mode
922 }
923 Vec vec = modeVecs[modeCounter + mode];
924 auto size = data.getIndices().size();
925 auto *indices = data.getIndices().data().data();
926 auto *nf_data = nf.data().data();
927 CHKERR VecSetValues(vec, size, indices, nf_data, ADD_VALUES);
928 }
929
931 }
932
933 private:
934 boost::shared_ptr<ObjectiveFunctionData> pythonPtr;
935 MatrixDouble blockModes;
936 std::vector<std::array<double, 3>> modeCentroids;
937 std::vector<std::array<double, 6>> modeBboxes;
938 int iD;
939 std::vector<SmartPetscObj<Vec>> modeVecs;
940 int blockCounter;
941 int modeCounter;
942 };
943
944 auto solve_bdy = [&]() {
946
947 auto fe_lhs = get_lhs_fe();
948 auto fe_rhs = get_rhs_fe();
949 int block_counter = 0;
950 int mode_counter = 0;
951 for (auto &bc : bcs) {
952 auto id = bc->getMeshsetId();
953 Range ents;
954 CHKERR mField.get_moab().get_entities_by_handle(bc->getMeshset(), ents,
955 true);
956 auto range = boost::make_shared<Range>(ents);
957 auto &pip_rhs = fe_rhs->getOpPtrVector();
958 pip_rhs.push_back(new OpMode("ADJOINT_FIELD", pythonPtr, id, modeVecs,
959 modeCentroids, modeBBoxes, block_counter,
960 mode_counter, range));
961 CHKERR DMoFEMLoopFiniteElements(subset_dm_bdy, "ADJOINT_BOUNDARY_FE",
962 fe_rhs);
963 pip_rhs.pop_back();
964 int nb_modes;
965 CHKERR pythonPtr->numberOfModes(id, nb_modes);
966 ++block_counter;
967 mode_counter += nb_modes;
968 MOFEM_LOG("WORLD", Sev::inform)
969 << "Setting mode block block: " << bc->getName()
970 << " with ID: " << bc->getMeshsetId()
971 << " total modes: " << mode_counter;
972 }
973
974 for (auto &v : modeVecs) {
975 CHKERR VecAssemblyBegin(v);
976 CHKERR VecAssemblyEnd(v);
977 CHKERR VecGhostUpdateBegin(v, ADD_VALUES, SCATTER_REVERSE);
978 CHKERR VecGhostUpdateEnd(v, ADD_VALUES, SCATTER_REVERSE);
979 }
980
981 auto M = createDMMatrix(subset_dm_bdy);
982 fe_lhs->B = M;
983 CHKERR DMoFEMLoopFiniteElements(subset_dm_bdy, "ADJOINT_BOUNDARY_FE",
984 fe_lhs);
985 CHKERR MatAssemblyBegin(M, MAT_FINAL_ASSEMBLY);
986 CHKERR MatAssemblyEnd(M, MAT_FINAL_ASSEMBLY);
987
988 auto solver = createKSP(mField.get_comm());
989 CHKERR KSPSetOperators(solver, M, M);
990 CHKERR KSPSetFromOptions(solver);
991 CHKERR KSPSetUp(solver);
992 auto v = createDMVector(subset_dm_bdy);
993 for (auto &f : modeVecs) {
994 CHKERR KSPSolve(solver, f, v);
995 CHKERR VecSwap(f, v);
996 }
997
998 for (auto &v : modeVecs) {
999 CHKERR VecGhostUpdateBegin(v, INSERT_VALUES, SCATTER_FORWARD);
1000 CHKERR VecGhostUpdateEnd(v, INSERT_VALUES, SCATTER_FORWARD);
1001 }
1002
1004 };
1005
1006 CHKERR solve_bdy();
1007
1008 auto get_elastic_fe_lhs = [&]() {
1009 auto fe = boost::make_shared<DomainEle>(mField);
1010 fe->getRuleHook = [](int, int, int p_data) {
1011 return 2 * p_data + p_data - 1;
1012 };
1013 auto &pip = fe->getOpPtrVector();
1015 "GEOMETRY");
1016 CHKERR HookeOps::opFactoryDomainLhs<SPACE_DIM, A, I, DomainEleOp>(
1017 mField, pip, "ADJOINT_FIELD", "MAT_ADJOINT", Sev::noisy);
1018 return fe;
1019 };
1020
1021 auto get_elastic_fe_rhs = [&]() {
1022 auto fe = boost::make_shared<DomainEle>(mField);
1023 fe->getRuleHook = [](int, int, int p_data) {
1024 return 2 * p_data + p_data - 1;
1025 };
1026 auto &pip = fe->getOpPtrVector();
1028 "GEOMETRY");
1029 CHKERR HookeOps::opFactoryDomainRhs<SPACE_DIM, A, I, DomainEleOp>(
1030 mField, pip, "ADJOINT_FIELD", "MAT_ADJOINT", Sev::noisy);
1031 return fe;
1032 };
1033
1034 auto adjoint_gradient_postprocess = [&](auto mode) {
1036 auto post_proc_mesh = boost::make_shared<moab::Core>();
1037 auto post_proc_begin =
1038 boost::make_shared<PostProcBrokenMeshInMoabBaseBegin>(mField,
1039 post_proc_mesh);
1040 auto post_proc_end = boost::make_shared<PostProcBrokenMeshInMoabBaseEnd>(
1041 mField, post_proc_mesh);
1042
1043 auto geom_vec = boost::make_shared<MatrixDouble>();
1044
1045 auto post_proc_fe =
1046 boost::make_shared<PostProcEleDomain>(mField, post_proc_mesh);
1048 post_proc_fe->getOpPtrVector(), {H1}, "GEOMETRY");
1049 post_proc_fe->getOpPtrVector().push_back(
1050 new OpCalculateVectorFieldValues<SPACE_DIM>("ADJOINT_FIELD", geom_vec,
1051 modeVecs[mode]));
1052
1054
1055 post_proc_fe->getOpPtrVector().push_back(
1056
1057 new OpPPMap(
1058
1059 post_proc_fe->getPostProcMesh(), post_proc_fe->getMapGaussPts(),
1060
1061 {},
1062
1063 {{"MODE", geom_vec}},
1064
1065 {},
1066
1067 {}
1068
1069 )
1070
1071 );
1072
1074 post_proc_begin->getFEMethod());
1075 CHKERR DMoFEMLoopFiniteElements(adjointDM, "ADJOINT_DOMAIN_FE",
1076 post_proc_fe);
1078 post_proc_begin->getFEMethod());
1079
1080 CHKERR post_proc_end->writeFile("mode_" + std::to_string(mode) + ".h5m");
1081
1083 };
1084
1085 auto solve_domain = [&]() {
1087 auto fe_lhs = get_elastic_fe_lhs();
1088 auto fe_rhs = get_elastic_fe_rhs();
1089 auto v = createDMVector(subset_dm_domain);
1090 auto F = vectorDuplicate(v);
1091 fe_rhs->f = F;
1092
1093 auto M = createDMMatrix(subset_dm_domain);
1094 fe_lhs->B = M;
1095 CHKERR DMoFEMLoopFiniteElements(subset_dm_domain, "ADJOINT_DOMAIN_FE",
1096 fe_lhs);
1097 CHKERR MatAssemblyBegin(M, MAT_FINAL_ASSEMBLY);
1098 CHKERR MatAssemblyEnd(M, MAT_FINAL_ASSEMBLY);
1099
1100 auto solver = createKSP(mField.get_comm());
1101 CHKERR KSPSetOperators(solver, M, M);
1102 CHKERR KSPSetFromOptions(solver);
1103 CHKERR KSPSetUp(solver);
1104
1105 int mode_counter = 0;
1106 for (auto &f : modeVecs) {
1107 CHKERR mField.getInterface<FieldBlas>()->setField(0, "ADJOINT_FIELD");
1108 CHKERR DMoFEMMeshToLocalVector(subset_dm_bdy, f, INSERT_VALUES,
1109 SCATTER_REVERSE);
1110 CHKERR VecZeroEntries(F);
1111 CHKERR DMoFEMLoopFiniteElements(subset_dm_domain, "ADJOINT_DOMAIN_FE",
1112 fe_rhs);
1113 CHKERR VecAssemblyBegin(F);
1114 CHKERR VecAssemblyEnd(F);
1115 CHKERR VecGhostUpdateBegin(F, ADD_VALUES, SCATTER_REVERSE);
1116 CHKERR VecGhostUpdateEnd(F, ADD_VALUES, SCATTER_REVERSE);
1117 CHKERR KSPSolve(solver, F, v);
1118 CHKERR VecGhostUpdateBegin(v, INSERT_VALUES, SCATTER_FORWARD);
1119 CHKERR VecGhostUpdateEnd(v, INSERT_VALUES, SCATTER_FORWARD);
1120 CHKERR DMoFEMMeshToLocalVector(subset_dm_domain, v, INSERT_VALUES,
1121 SCATTER_REVERSE);
1122 auto m = createDMVector(adjointDM);
1123 CHKERR DMoFEMMeshToLocalVector(adjointDM, m, INSERT_VALUES,
1124 SCATTER_FORWARD);
1125 f = m;
1126 ++mode_counter;
1127 }
1129 };
1130
1131 CHKERR solve_domain();
1132
1133 for (int i = 0; i < modeVecs.size(); ++i) {
1134 CHKERR adjoint_gradient_postprocess(i);
1135 }
1136
1138}
constexpr int SPACE_DIM
@ F
PetscErrorCode DMoFEMPreProcessFiniteElements(DM dm, MoFEM::FEMethod *method)
execute finite element method for each element in dm (problem)
Definition DMMoFEM.cpp:536
MoFEMErrorCode VecSetValues(Vec V, const EntitiesFieldData::EntData &data, const double *ptr, InsertMode iora)
Assemble PETSc vector.
std::vector< std::array< double, 3 > > modeCentroids
Centroids of optimization blocks.
Definition adjoint.cpp:176
std::vector< std::array< double, 6 > > modeBBoxes
Bounding boxes of optimization blocks.
Definition adjoint.cpp:178
FTensor::Tensor0< FTensor::PackPtr< double *, 1 > > getFTensor0N(const FieldApproximationBase base)
Get base function as Tensor0.
MatrixDouble & getN(const FieldApproximationBase base)
get base functions this return matrix (nb. of rows is equal to nb. of Gauss pts, nb....
const VectorInt & getIndices() const
Get global indices of degrees of freedom on entity.

◆ tsSolve() [1/3]

MoFEMErrorCode Example::tsSolve ( )
private
Examples
mofem/tutorials/adv-0_plasticity/plastic.cpp, plastic.cpp, and thermoplastic.cpp.

Definition at line 832 of file plastic.cpp.

832 {
834
837 ISManager *is_manager = mField.getInterface<ISManager>();
838
839 auto snes_ctx_ptr = getDMSnesCtx(simple->getDM());
840
841 auto set_section_monitor = [&](auto solver) {
843 SNES snes;
844 CHKERR TSGetSNES(solver, &snes);
845 CHKERR SNESMonitorSet(snes,
846 (MoFEMErrorCode(*)(SNES, PetscInt, PetscReal,
848 (void *)(snes_ctx_ptr.get()), nullptr);
850 };
851
852 auto create_post_process_elements = [&]() {
853 auto push_vol_ops = [this](auto &pip) {
855 pip, {H1, HDIV}, "GEOMETRY");
856
857 auto [common_plastic_ptr, common_hencky_ptr] =
858 PlasticOps::createCommonPlasticOps<SPACE_DIM, IT, DomainEleOp>(
859 mField, "MAT_PLASTIC", pip, "U", "EP", "TAU", 1., Sev::inform);
860
861 if (common_hencky_ptr) {
862 if (common_plastic_ptr->mGradPtr != common_hencky_ptr->matGradPtr)
863 CHK_THROW_MESSAGE(MOFEM_DATA_INCONSISTENCY, "Wrong pointer for grad");
864 }
865
866 return std::make_pair(common_plastic_ptr, common_hencky_ptr);
867 };
868
869 auto push_vol_post_proc_ops = [this](auto &pp_fe, auto &&p) {
871
872 auto &pip = pp_fe->getOpPtrVector();
873
874 auto [common_plastic_ptr, common_hencky_ptr] = p;
875
877
878 auto x_ptr = boost::make_shared<MatrixDouble>();
879 pip.push_back(
880 new OpCalculateVectorFieldValues<SPACE_DIM>("GEOMETRY", x_ptr));
881 auto u_ptr = boost::make_shared<MatrixDouble>();
882 pip.push_back(new OpCalculateVectorFieldValues<SPACE_DIM>("U", u_ptr));
883
884 if (is_large_strains) {
885
886 pip.push_back(
887
888 new OpPPMap(
889
890 pp_fe->getPostProcMesh(), pp_fe->getMapGaussPts(),
891
892 {{"PLASTIC_SURFACE",
893 common_plastic_ptr->getPlasticSurfacePtr()},
894 {"PLASTIC_MULTIPLIER",
895 common_plastic_ptr->getPlasticTauPtr()}},
896
897 {{"U", u_ptr}, {"GEOMETRY", x_ptr}},
898
899 {{"GRAD", common_hencky_ptr->matGradPtr},
900 {"FIRST_PIOLA", common_hencky_ptr->getMatFirstPiolaStress()}},
901
902 {{"HENCKY_STRAIN", common_hencky_ptr->getMatLogC()},
903 {"PLASTIC_STRAIN", common_plastic_ptr->getPlasticStrainPtr()},
904 {"PLASTIC_FLOW", common_plastic_ptr->getPlasticFlowPtr()}}
905
906 )
907
908 );
909
910 } else {
911
912 pip.push_back(
913
914 new OpPPMap(
915
916 pp_fe->getPostProcMesh(), pp_fe->getMapGaussPts(),
917
918 {{"PLASTIC_SURFACE",
919 common_plastic_ptr->getPlasticSurfacePtr()},
920 {"PLASTIC_MULTIPLIER",
921 common_plastic_ptr->getPlasticTauPtr()}},
922
923 {{"U", u_ptr}, {"GEOMETRY", x_ptr}},
924
925 {},
926
927 {{"STRAIN", common_plastic_ptr->mStrainPtr},
928 {"STRESS", common_plastic_ptr->mStressPtr},
929 {"PLASTIC_STRAIN", common_plastic_ptr->getPlasticStrainPtr()},
930 {"PLASTIC_FLOW", common_plastic_ptr->getPlasticFlowPtr()}}
931
932 )
933
934 );
935 }
936
938 };
939
940 PetscBool post_proc_vol;
941 PetscBool post_proc_skin;
942
943 if constexpr (SPACE_DIM == 2) {
944 post_proc_vol = PETSC_TRUE;
945 post_proc_skin = PETSC_FALSE;
946 } else {
947 post_proc_vol = PETSC_FALSE;
948 post_proc_skin = PETSC_TRUE;
949 }
950 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-post_proc_vol", &post_proc_vol,
951 PETSC_NULLPTR);
952 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-post_proc_skin",
953 &post_proc_skin, PETSC_NULLPTR);
954
955 auto vol_post_proc = [this, push_vol_post_proc_ops, push_vol_ops,
956 post_proc_vol]() {
957 if (post_proc_vol == PETSC_FALSE)
958 return boost::shared_ptr<PostProcEle>();
959 auto pp_fe = boost::make_shared<PostProcEle>(mField);
961 push_vol_post_proc_ops(pp_fe, push_vol_ops(pp_fe->getOpPtrVector())),
962 "push_vol_post_proc_ops");
963 return pp_fe;
964 };
965
966 auto skin_post_proc = [this, push_vol_post_proc_ops, push_vol_ops,
967 post_proc_skin]() {
968 if (post_proc_skin == PETSC_FALSE)
969 return boost::shared_ptr<SkinPostProcEle>();
970
972 auto pp_fe = boost::make_shared<SkinPostProcEle>(mField);
973 auto op_side = new OpLoopSide<SideEle>(mField, simple->getDomainFEName(),
974 SPACE_DIM, Sev::verbose);
975 pp_fe->getOpPtrVector().push_back(op_side);
976 CHK_MOAB_THROW(push_vol_post_proc_ops(
977 pp_fe, push_vol_ops(op_side->getOpPtrVector())),
978 "push_vol_post_proc_ops");
979 return pp_fe;
980 };
981
982 return std::make_pair(vol_post_proc(), skin_post_proc());
983 };
984
985 auto scatter_create = [&](auto D, auto coeff) {
987 CHKERR is_manager->isCreateProblemFieldAndRank(simple->getProblemName(),
988 ROW, "U", coeff, coeff, is);
989 int loc_size;
990 CHKERR ISGetLocalSize(is, &loc_size);
991 Vec v;
992 CHKERR VecCreateMPI(mField.get_comm(), loc_size, PETSC_DETERMINE, &v);
993 VecScatter scatter;
994 CHKERR VecScatterCreate(D, is, v, PETSC_NULLPTR, &scatter);
995 return std::make_tuple(SmartPetscObj<Vec>(v),
997 };
998
999 boost::shared_ptr<SetPtsData> field_eval_data;
1000 boost::shared_ptr<MatrixDouble> u_field_ptr;
1001
1002 std::array<double, 3> field_eval_coords{0.0, 0.0, 0.0};
1003 int coords_dim = 3;
1004 CHKERR PetscOptionsGetRealArray(NULL, NULL, "-field_eval_coords",
1005 field_eval_coords.data(), &coords_dim,
1006 &do_eval_field);
1007
1008 boost::shared_ptr<std::map<std::string, boost::shared_ptr<VectorDouble>>>
1009 scalar_field_ptrs = boost::make_shared<
1010 std::map<std::string, boost::shared_ptr<VectorDouble>>>();
1011 boost::shared_ptr<std::map<std::string, boost::shared_ptr<MatrixDouble>>>
1012 vector_field_ptrs = boost::make_shared<
1013 std::map<std::string, boost::shared_ptr<MatrixDouble>>>();
1014 boost::shared_ptr<std::map<std::string, boost::shared_ptr<MatrixDouble>>>
1015 sym_tensor_field_ptrs = boost::make_shared<
1016 std::map<std::string, boost::shared_ptr<MatrixDouble>>>();
1017 boost::shared_ptr<std::map<std::string, boost::shared_ptr<MatrixDouble>>>
1018 tensor_field_ptrs = boost::make_shared<
1019 std::map<std::string, boost::shared_ptr<MatrixDouble>>>();
1020
1021 if (do_eval_field) {
1022 auto u_field_ptr = boost::make_shared<MatrixDouble>();
1023 field_eval_data =
1024 mField.getInterface<FieldEvaluatorInterface>()->getData<DomainEle>();
1025
1026 CHKERR mField.getInterface<FieldEvaluatorInterface>()->buildTree<SPACE_DIM>(
1027 field_eval_data, simple->getDomainFEName());
1028
1029 field_eval_data->setEvalPoints(field_eval_coords.data(), 1);
1030 auto no_rule = [](int, int, int) { return -1; };
1031 auto field_eval_fe_ptr = field_eval_data->feMethodPtr;
1032 field_eval_fe_ptr->getRuleHook = no_rule;
1033
1035 field_eval_fe_ptr->getOpPtrVector(), {H1, HDIV}, "GEOMETRY");
1036
1037 auto [common_plastic_ptr, common_hencky_ptr] =
1038 PlasticOps::createCommonPlasticOps<SPACE_DIM, IT, DomainEleOp>(
1039 mField, "MAT_PLASTIC", field_eval_fe_ptr->getOpPtrVector(), "U",
1040 "EP", "TAU", 1., Sev::inform);
1041
1042 field_eval_fe_ptr->getOpPtrVector().push_back(
1043 new OpCalculateVectorFieldValues<SPACE_DIM>("U", u_field_ptr));
1044
1045 if ((common_plastic_ptr) && (common_hencky_ptr) && (scalar_field_ptrs)) {
1046 if (is_large_strains) {
1047 scalar_field_ptrs->insert(
1048 {"PLASTIC_SURFACE", common_plastic_ptr->getPlasticSurfacePtr()});
1049 scalar_field_ptrs->insert(
1050 {"PLASTIC_MULTIPLIER", common_plastic_ptr->getPlasticTauPtr()});
1051 vector_field_ptrs->insert({"U", u_field_ptr});
1052 sym_tensor_field_ptrs->insert(
1053 {"PLASTIC_STRAIN", common_plastic_ptr->getPlasticStrainPtr()});
1054 sym_tensor_field_ptrs->insert(
1055 {"PLASTIC_FLOW", common_plastic_ptr->getPlasticFlowPtr()});
1056 sym_tensor_field_ptrs->insert(
1057 {"HENCKY_STRAIN", common_hencky_ptr->getMatLogC()});
1058 tensor_field_ptrs->insert({"GRAD", common_hencky_ptr->matGradPtr});
1059 tensor_field_ptrs->insert(
1060 {"FIRST_PIOLA", common_hencky_ptr->getMatFirstPiolaStress()});
1061 } else {
1062 scalar_field_ptrs->insert(
1063 {"PLASTIC_SURFACE", common_plastic_ptr->getPlasticSurfacePtr()});
1064 scalar_field_ptrs->insert(
1065 {"PLASTIC_MULTIPLIER", common_plastic_ptr->getPlasticTauPtr()});
1066 vector_field_ptrs->insert({"U", u_field_ptr});
1067 sym_tensor_field_ptrs->insert(
1068 {"STRAIN", common_plastic_ptr->mStrainPtr});
1069 sym_tensor_field_ptrs->insert(
1070 {"STRESS", common_plastic_ptr->mStressPtr});
1071 sym_tensor_field_ptrs->insert(
1072 {"PLASTIC_STRAIN", common_plastic_ptr->getPlasticStrainPtr()});
1073 sym_tensor_field_ptrs->insert(
1074 {"PLASTIC_FLOW", common_plastic_ptr->getPlasticFlowPtr()});
1075 }
1076 }
1077 }
1078
1079 auto test_monitor_ptr = boost::make_shared<FEMethod>();
1080
1081 auto set_time_monitor = [&](auto dm, auto solver) {
1083 boost::shared_ptr<Monitor<SPACE_DIM>> monitor_ptr(new Monitor<SPACE_DIM>(
1084 dm, create_post_process_elements(), uXScatter, uYScatter, uZScatter,
1085 field_eval_coords, field_eval_data, scalar_field_ptrs,
1086 vector_field_ptrs, sym_tensor_field_ptrs, tensor_field_ptrs));
1087 boost::shared_ptr<ForcesAndSourcesCore> null;
1088
1089 test_monitor_ptr->postProcessHook = [&]() {
1091
1092 if (atom_test && fabs(test_monitor_ptr->ts_t - 0.5) < 1e-12 &&
1093 test_monitor_ptr->ts_step == 25) {
1094
1095 if (scalar_field_ptrs->at("PLASTIC_MULTIPLIER")->size()) {
1096 auto t_tau =
1097 getFTensor0FromVec(*scalar_field_ptrs->at("PLASTIC_MULTIPLIER"));
1098 MOFEM_LOG("PlasticSync", Sev::inform) << "Eval point tau: " << t_tau;
1099
1100 if (atom_test == 1 && fabs(t_tau - 0.688861) > 1e-5) {
1101 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
1102 "atom test %d failed: wrong plastic multiplier value",
1103 atom_test);
1104 }
1105 }
1106
1107 if (vector_field_ptrs->at("U")->size1()) {
1109 auto t_disp =
1110 getFTensor1FromMat<SPACE_DIM>(*vector_field_ptrs->at("U"));
1111 MOFEM_LOG("PlasticSync", Sev::inform) << "Eval point U: " << t_disp;
1112
1113 if (atom_test == 1 && fabs(t_disp(0) - 0.25 / 2.) > 1e-5 ||
1114 fabs(t_disp(1) + 0.0526736) > 1e-5) {
1115 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
1116 "atom test %d failed: wrong displacement value",
1117 atom_test);
1118 }
1119 }
1120
1121 if (sym_tensor_field_ptrs->at("PLASTIC_STRAIN")->size1()) {
1122 auto t_plastic_strain = getFTensor2SymmetricFromMat<SPACE_DIM>(
1123 *sym_tensor_field_ptrs->at("PLASTIC_STRAIN"));
1124 MOFEM_LOG("PlasticSync", Sev::inform)
1125 << "Eval point EP: " << t_plastic_strain;
1126
1127 if (atom_test == 1 &&
1128 fabs(t_plastic_strain(0, 0) - 0.221943) > 1e-5 ||
1129 fabs(t_plastic_strain(0, 1)) > 1e-5 ||
1130 fabs(t_plastic_strain(1, 1) + 0.110971) > 1e-5) {
1131 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
1132 "atom test %d failed: wrong plastic strain value",
1133 atom_test);
1134 }
1135 }
1136
1137 if (tensor_field_ptrs->at("FIRST_PIOLA")->size1()) {
1138 auto t_piola_stress = getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(
1139 *tensor_field_ptrs->at("FIRST_PIOLA"));
1140 MOFEM_LOG("PlasticSync", Sev::inform)
1141 << "Eval point Piola stress: " << t_piola_stress;
1142
1143 if (atom_test == 1 && fabs((t_piola_stress(0, 0) - 198.775) /
1144 t_piola_stress(0, 0)) > 1e-5 ||
1145 fabs(t_piola_stress(0, 1)) + fabs(t_piola_stress(1, 0)) +
1146 fabs(t_piola_stress(1, 1)) >
1147 1e-5) {
1148 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
1149 "atom test %d failed: wrong Piola stress value",
1150 atom_test);
1151 }
1152 }
1153 }
1154
1157 };
1158
1160 monitor_ptr, null, test_monitor_ptr);
1161
1163 };
1164
1165 auto set_schur_pc = [&](auto solver,
1166 boost::shared_ptr<SetUpSchur> &schur_ptr) {
1168
1169 auto name_prb = simple->getProblemName();
1170
1171 // create sub dm for Schur complement
1172 auto create_schur_dm = [&](SmartPetscObj<DM> base_dm,
1173 SmartPetscObj<DM> &dm_sub) {
1175 dm_sub = createDM(mField.get_comm(), "DMMOFEM");
1176 CHKERR DMMoFEMCreateSubDM(dm_sub, base_dm, "SCHUR");
1177 CHKERR DMMoFEMSetSquareProblem(dm_sub, PETSC_TRUE);
1180 for (auto f : {"U"}) {
1183 }
1184 CHKERR DMSetUp(dm_sub);
1185
1187 };
1188
1189 auto create_block_dm = [&](SmartPetscObj<DM> base_dm,
1190 SmartPetscObj<DM> &dm_sub) {
1192 dm_sub = createDM(mField.get_comm(), "DMMOFEM");
1193 CHKERR DMMoFEMCreateSubDM(dm_sub, base_dm, "BLOCK");
1194 CHKERR DMMoFEMSetSquareProblem(dm_sub, PETSC_TRUE);
1197#ifdef ADD_CONTACT
1198 for (auto f : {"SIGMA", "EP", "TAU"}) {
1201 }
1202#else
1203 for (auto f : {"EP", "TAU"}) {
1206 }
1207#endif
1208 CHKERR DMSetUp(dm_sub);
1210 };
1211
1212 // Create nested (sub BC) Schur DM
1213 if constexpr (AT == AssemblyType::BLOCK_SCHUR) {
1214
1215 SmartPetscObj<DM> dm_schur;
1216 CHKERR create_schur_dm(simple->getDM(), dm_schur);
1217 SmartPetscObj<DM> dm_block;
1218 CHKERR create_block_dm(simple->getDM(), dm_block);
1219
1220#ifdef ADD_CONTACT
1221
1222 auto get_nested_mat_data = [&](auto schur_dm, auto block_dm) {
1223 auto block_mat_data = createBlockMatStructure(
1224 simple->getDM(),
1225
1226 {
1227
1228 {simple->getDomainFEName(),
1229
1230 {{"U", "U"},
1231 {"SIGMA", "SIGMA"},
1232 {"U", "SIGMA"},
1233 {"SIGMA", "U"},
1234 {"EP", "EP"},
1235 {"TAU", "TAU"},
1236 {"U", "EP"},
1237 {"EP", "U"},
1238 {"EP", "TAU"},
1239 {"TAU", "EP"},
1240 {"TAU", "U"}
1241
1242 }},
1243
1244 {simple->getBoundaryFEName(),
1245
1246 {{"SIGMA", "SIGMA"}, {"U", "SIGMA"}, {"SIGMA", "U"}
1247
1248 }}
1249
1250 }
1251
1252 );
1253
1255
1256 {dm_schur, dm_block}, block_mat_data,
1257
1258 {"SIGMA", "EP", "TAU"}, {nullptr, nullptr, nullptr}, true
1259
1260 );
1261 };
1262
1263#else
1264
1265 auto get_nested_mat_data = [&](auto schur_dm, auto block_dm) {
1266 auto block_mat_data =
1268
1269 {{simple->getDomainFEName(),
1270
1271 {{"U", "U"},
1272 {"EP", "EP"},
1273 {"TAU", "TAU"},
1274 {"U", "EP"},
1275 {"EP", "U"},
1276 {"EP", "TAU"},
1277 {"TAU", "U"},
1278 {"TAU", "EP"}
1279
1280 }}}
1281
1282 );
1283
1285
1286 {dm_schur, dm_block}, block_mat_data,
1287
1288 {"EP", "TAU"}, {nullptr, nullptr}, false
1289
1290 );
1291 };
1292
1293#endif
1294
1295 auto nested_mat_data = get_nested_mat_data(dm_schur, dm_block);
1296 CHKERR DMMoFEMSetNestSchurData(simple->getDM(), nested_mat_data);
1297
1298 auto block_is = getDMSubData(dm_block)->getSmartRowIs();
1299 auto ao_schur = getDMSubData(dm_schur)->getSmartRowMap();
1300
1301 // Indices has to be map fro very to level, while assembling Schur
1302 // complement.
1303 schur_ptr =
1304 SetUpSchur::createSetUpSchur(mField, dm_schur, block_is, ao_schur);
1305 CHKERR schur_ptr->setUp(solver);
1306 }
1307
1309 };
1310
1311 auto dm = simple->getDM();
1312 auto D = createDMVector(dm);
1313 auto DD = vectorDuplicate(D);
1314 CHKERR VecSetDM(D, PETSC_NULLPTR);
1315 CHKERR VecSetDM(DD, PETSC_NULLPTR);
1316 uXScatter = scatter_create(D, 0);
1317 uYScatter = scatter_create(D, 1);
1318 if constexpr (SPACE_DIM == 3)
1319 uZScatter = scatter_create(D, 2);
1320
1321 auto create_solver = [pip_mng]() {
1322 if (is_quasi_static == PETSC_TRUE)
1323 return pip_mng->createTSIM();
1324 else
1325 return pip_mng->createTSIM2();
1326 };
1327
1328 auto solver = create_solver();
1329
1330 auto active_pre_lhs = []() {
1332 std::fill(PlasticOps::CommonData::activityData.begin(),
1335 };
1336
1337 auto active_post_lhs = [&]() {
1339 auto get_iter = [&]() {
1340 SNES snes;
1341 CHK_THROW_MESSAGE(TSGetSNES(solver, &snes), "Can not get SNES");
1342 int iter;
1343 CHK_THROW_MESSAGE(SNESGetIterationNumber(snes, &iter),
1344 "Can not get iter");
1345 return iter;
1346 };
1347
1348 auto iter = get_iter();
1349 if (iter >= 0) {
1350
1351 std::array<int, 5> activity_data;
1352 std::fill(activity_data.begin(), activity_data.end(), 0);
1353 MPI_Allreduce(PlasticOps::CommonData::activityData.data(),
1354 activity_data.data(), activity_data.size(), MPI_INT,
1355 MPI_SUM, mField.get_comm());
1356
1357 int &active_points = activity_data[0];
1358 int &avtive_full_elems = activity_data[1];
1359 int &avtive_elems = activity_data[2];
1360 int &nb_points = activity_data[3];
1361 int &nb_elements = activity_data[4];
1362
1363 if (nb_points) {
1364
1365 double proc_nb_points =
1366 100 * static_cast<double>(active_points) / nb_points;
1367 double proc_nb_active =
1368 100 * static_cast<double>(avtive_elems) / nb_elements;
1369 double proc_nb_full_active = 100;
1370 if (avtive_elems)
1371 proc_nb_full_active =
1372 100 * static_cast<double>(avtive_full_elems) / avtive_elems;
1373
1374 MOFEM_LOG_C("PLASTICITY", Sev::inform,
1375 "Iter %d nb pts %d nb active pts %d (%3.3f\%) nb active "
1376 "elements %d "
1377 "(%3.3f\%) nb full active elems %d (%3.3f\%)",
1378 iter, nb_points, active_points, proc_nb_points,
1379 avtive_elems, proc_nb_active, avtive_full_elems,
1380 proc_nb_full_active, iter);
1381 }
1382 }
1383
1385 };
1386
1387 auto add_active_dofs_elem = [&](auto dm) {
1389 auto fe_pre_proc = boost::make_shared<FEMethod>();
1390 fe_pre_proc->preProcessHook = active_pre_lhs;
1391 auto fe_post_proc = boost::make_shared<FEMethod>();
1392 fe_post_proc->postProcessHook = active_post_lhs;
1393 auto ts_ctx_ptr = getDMTsCtx(dm);
1394 ts_ctx_ptr->getPreProcessIJacobian().push_front(fe_pre_proc);
1395 ts_ctx_ptr->getPostProcessIJacobian().push_back(fe_post_proc);
1397 };
1398
1399 auto set_essential_bc = [&](auto dm, auto solver) {
1401 // This is low level pushing finite elements (pipelines) to solver
1402
1403 auto pre_proc_ptr = boost::make_shared<FEMethod>();
1404 auto post_proc_rhs_ptr = boost::make_shared<FEMethod>();
1405 auto post_proc_lhs_ptr = boost::make_shared<FEMethod>();
1406 auto ts_ctx_ptr = getDMTsCtx(dm);
1407 ts_ctx_ptr->getPreProcessIFunction().push_front(pre_proc_ptr);
1408 ts_ctx_ptr->getPreProcessIJacobian().push_front(pre_proc_ptr);
1409 ts_ctx_ptr->getPostProcessIFunction().push_back(post_proc_rhs_ptr);
1410 ts_ctx_ptr->getPostProcessIJacobian().push_back(post_proc_lhs_ptr);
1411
1412 // Add boundary condition scaling
1413 auto disp_time_scale = boost::make_shared<TimeScale>();
1414
1415 auto get_bc_hook_rhs = [&]() {
1417 mField, pre_proc_ptr, {disp_time_scale}, false);
1418 };
1419 pre_proc_ptr->preProcessHook = get_bc_hook_rhs();
1420
1421 auto waak_post_proc_rhs_ptr = boost::weak_ptr<FEMethod>(
1422 post_proc_rhs_ptr); // fe method passed to lambda, have to be weak ptr to avoid circular shared ptr reference
1423 auto get_post_proc_hook_rhs = [this, waak_post_proc_rhs_ptr]() {
1426 mField, waak_post_proc_rhs_ptr.lock(), nullptr, Sev::verbose)();
1428 mField, waak_post_proc_rhs_ptr.lock(), 1.)();
1430 };
1431 auto get_post_proc_hook_lhs = [&]() {
1433 mField, post_proc_lhs_ptr, 1.);
1434 };
1435
1436 post_proc_rhs_ptr->postProcessHook = get_post_proc_hook_rhs;
1437 post_proc_lhs_ptr->postProcessHook = get_post_proc_hook_lhs();
1438
1440 };
1441
1442 auto B = createDMMatrix(dm);
1443 if (is_quasi_static == PETSC_FALSE) {
1444 CHKERR TSSetIJacobian(solver, B, B, PETSC_NULLPTR, PETSC_NULLPTR);
1445 } else {
1446 CHKERR TSSetI2Jacobian(solver, B, B, PETSC_NULLPTR, PETSC_NULLPTR);
1447 }
1448 if (is_quasi_static == PETSC_TRUE) {
1449 CHKERR TSSetSolution(solver, D);
1450 } else {
1451 CHKERR TS2SetSolution(solver, D, DD);
1452 }
1453 CHKERR set_section_monitor(solver);
1454 CHKERR set_time_monitor(dm, solver);
1455 CHKERR TSSetFromOptions(solver);
1456
1457 CHKERR add_active_dofs_elem(dm);
1458 boost::shared_ptr<SetUpSchur> schur_ptr;
1459 CHKERR set_schur_pc(solver, schur_ptr);
1460 CHKERR set_essential_bc(dm, solver);
1461
1462 MOFEM_LOG_CHANNEL("TIMER");
1463 MOFEM_LOG_TAG("TIMER", "timer");
1464 if (set_timer)
1465 BOOST_LOG_SCOPED_THREAD_ATTR("Timeline", attrs::timer());
1466 MOFEM_LOG("TIMER", Sev::verbose) << "TSSetUp";
1467 CHKERR TSSetUp(solver);
1468 MOFEM_LOG("TIMER", Sev::verbose) << "TSSetUp <= done";
1469 MOFEM_LOG("TIMER", Sev::verbose) << "TSSolve";
1470 CHKERR TSSolve(solver, NULL);
1471 MOFEM_LOG("TIMER", Sev::verbose) << "TSSolve <= done";
1472
1473 if (mField.get_comm_rank() == 0) {
1474 auto ts_ctx_ptr = getDMTsCtx(dm);
1476 "ts_manager_graph.dot");
1477 }
1478
1480}
#define CHK_MOAB_THROW(err, msg)
Check error code of MoAB function and throw MoFEM exception.
auto getDMTsCtx(DM dm)
Get TS context data structure used by DM.
Definition DMMoFEM.hpp:1279
MoFEMErrorCode MoFEMSNESMonitorFields(SNES snes, PetscInt its, PetscReal fgnorm, SnesCtx *ctx)
Sens monitor printing residual field by field.
Definition SnesCtx.cpp:600
auto getDMSubData(DM dm)
Get sub problem data structure.
Definition DMMoFEM.hpp:1295
boost::shared_ptr< BlockStructure > createBlockMatStructure(DM dm, SchurFEOpsFEandFields schur_fe_op_vec)
Create a Mat Diag Blocks object.
Definition Schur.cpp:1082
boost::shared_ptr< NestSchurData > createSchurNestedMatrixStruture(std::pair< SmartPetscObj< DM >, SmartPetscObj< DM > > dms, boost::shared_ptr< BlockStructure > block_mat_data_ptr, std::vector< std::string > fields_names, std::vector< boost::shared_ptr< Range > > field_ents, bool add_preconditioner_block)
Get the Schur Nest Mat Array object.
Definition Schur.cpp:2421
MoFEMErrorCode DMMoFEMSetNestSchurData(DM dm, boost::shared_ptr< NestSchurData >)
Definition DMMoFEM.cpp:1555
static auto getFTensor0FromVec(V &data)
Get tensor rank 0 (scalar) form data vector.
auto getDMSnesCtx(DM dm)
Get SNES context data structure used by DM.
Definition DMMoFEM.hpp:1265
std::tuple< SmartPetscObj< Vec >, SmartPetscObj< VecScatter > > uYScatter
Definition plastic.cpp:236
std::tuple< SmartPetscObj< Vec >, SmartPetscObj< VecScatter > > uZScatter
Definition plastic.cpp:237
std::tuple< SmartPetscObj< Vec >, SmartPetscObj< VecScatter > > uXScatter
Definition plastic.cpp:235
Section manager is used to create indexes and sections.
Definition ISManager.hpp:23
static MoFEMErrorCode writeTSGraphGraphviz(TsCtx *ts_ctx, std::string file_name)
TS graph to Graphviz file.
static std::array< int, 5 > activityData
static boost::shared_ptr< SetUpSchur > createSetUpSchur(MoFEM::Interface &m_field)

◆ tsSolve() [2/3]

MoFEMErrorCode Example::tsSolve ( )
private

◆ tsSolve() [3/3]

MoFEMErrorCode Example::tsSolve ( )
private

Friends And Related Symbol Documentation

◆ TSPrePostProc

TSPrePostProc
friend

Definition at line 435 of file dynamic_first_order_con_law.cpp.

Member Data Documentation

◆ adjointDM

SmartPetscObj< DM > Example::adjointDM
private

Data manager for adjoint problem.

Examples
mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 168 of file adjoint.cpp.

◆ approxFunction

ApproxFieldFunction< FIELD_DIM > Example::approxFunction
staticprivate

◆ approxGradVals

boost::shared_ptr<MatrixDouble> Example::approxGradVals
private

◆ approxVals

boost::shared_ptr<VectorDouble> Example::approxVals
private

◆ aveMaxMin

std::array< double, Example::BoundingBox::LAST_BB > Example::aveMaxMin
staticprivate

◆ base

FieldApproximationBase Example::base
private

◆ boundaryMarker

boost::shared_ptr< std::vector< unsigned char > > Example::boundaryMarker
private

◆ commonDataPtr

boost::shared_ptr< CommonData > Example::commonDataPtr
private

◆ dispFieldPtr

boost::shared_ptr<MatrixDouble> Example::dispFieldPtr
private
Initial value:
=
boost::make_shared<MatrixDouble>()
Examples
thermoplastic.cpp.

Definition at line 933 of file thermoplastic.cpp.

◆ dispGradPtr

boost::shared_ptr<MatrixDouble> Example::dispGradPtr
private
Initial value:
=
boost::make_shared<MatrixDouble>()
Examples
thermoplastic.cpp.

Definition at line 935 of file thermoplastic.cpp.

◆ domianLhsFEPtr

boost::shared_ptr<FEMethod> Example::domianLhsFEPtr
private

◆ domianRhsFEPtr

boost::shared_ptr<FEMethod> Example::domianRhsFEPtr
private

◆ eps

SmartPetscObj<EPS> Example::eps
private

◆ ePS

SmartPetscObj<EPS> Example::ePS
private

◆ fieldOrder

int Example::fieldOrder = 2
private

Polynomial order for approximation.

Examples
mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 164 of file adjoint.cpp.

◆ fluxFieldPtr

boost::shared_ptr<MatrixDouble> Example::fluxFieldPtr
private
Initial value:
=
boost::make_shared<MatrixDouble>()
Examples
thermoplastic.cpp.

Definition at line 931 of file thermoplastic.cpp.

◆ focalIndex

int Example::focalIndex
staticprivate

◆ H

SmartPetscObj<Mat> Example::H
private

◆ iI

std::vector< MatrixInt > Example::iI
staticprivate

◆ initialGeometry

SmartPetscObj<Vec> Example::initialGeometry
private

Initial geometry field.

Examples
mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 179 of file adjoint.cpp.

◆ K

SmartPetscObj<Mat> Example::K
private

◆ kspElastic

SmartPetscObj< KSP > Example::kspElastic
private

Linear solver for elastic problem.

Examples
mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 167 of file adjoint.cpp.

◆ M

SmartPetscObj< Mat > Example::M
private

◆ matDPtr

boost::shared_ptr<MatrixDouble> Example::matDPtr
private

◆ meshVolumeAndCount

static std::array< double, 2 > Example::meshVolumeAndCount = {0, 0}
inlinestatic
Examples
mofem/tutorials/adv-0_plasticity/plastic.cpp, and plastic.cpp.

Definition at line 223 of file plastic.cpp.

223{0, 0};

◆ mField

MoFEM::Interface & Example::mField
private

◆ modeBBoxes

std::vector<std::array<double, 6> > Example::modeBBoxes
private

Bounding boxes of optimization blocks.

Examples
mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 178 of file adjoint.cpp.

◆ modeCentroids

std::vector<std::array<double, 3> > Example::modeCentroids
private

Centroids of optimization blocks.

Examples
mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 176 of file adjoint.cpp.

◆ modeVecs

std::vector<SmartPetscObj<Vec> > Example::modeVecs
private

Topology mode vectors (design variables)

Examples
mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 174 of file adjoint.cpp.

◆ pinchNodes

Range Example::pinchNodes
private

◆ plasticMultiplierFieldPtr

boost::shared_ptr<VectorDouble> Example::plasticMultiplierFieldPtr
private
Initial value:
=
boost::make_shared<VectorDouble>()
Examples
thermoplastic.cpp.

Definition at line 941 of file thermoplastic.cpp.

◆ plasticStrainFieldPtr

boost::shared_ptr<MatrixDouble> Example::plasticStrainFieldPtr
private
Initial value:
=
boost::make_shared<MatrixDouble>()
Examples
thermoplastic.cpp.

Definition at line 943 of file thermoplastic.cpp.

◆ pythonPtr

boost::shared_ptr< ObjectiveFunctionData > Example::pythonPtr
private

Interface to Python objective function.

Examples
mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 170 of file adjoint.cpp.

◆ reactionFe

boost::shared_ptr< DomainEle > Example::reactionFe
private
Examples
plastic.cpp, and thermoplastic.cpp.

Definition at line 923 of file thermoplastic.cpp.

◆ rigidBodyMotion

std::array<SmartPetscObj<Vec>, 6> Example::rigidBodyMotion
private

◆ rZ

std::vector< double > Example::rZ
staticprivate

◆ savitzkyGolayNormalisation

int Example::savitzkyGolayNormalisation
staticprivate

◆ savitzkyGolayWeights

const int * Example::savitzkyGolayWeights
staticprivate

◆ simple

Simple* Example::simple
private

◆ simpleInterface

Simple * Example::simpleInterface
private

◆ space

FieldSpace Example::space
private
Examples
mofem/tutorials/fun-2_plot_base/plot_base.cpp.

Definition at line 69 of file plot_base.cpp.

◆ strainFieldPtr

boost::shared_ptr<MatrixDouble> Example::strainFieldPtr
private
Initial value:
=
boost::make_shared<MatrixDouble>()
Examples
thermoplastic.cpp.

Definition at line 937 of file thermoplastic.cpp.

◆ stressFieldPtr

boost::shared_ptr<MatrixDouble> Example::stressFieldPtr
private
Initial value:
=
boost::make_shared<MatrixDouble>()
Examples
thermoplastic.cpp.

Definition at line 939 of file thermoplastic.cpp.

◆ tempFieldPtr

boost::shared_ptr<VectorDouble> Example::tempFieldPtr
private
Initial value:
=
boost::make_shared<VectorDouble>()
Examples
thermoplastic.cpp.

Definition at line 929 of file thermoplastic.cpp.

◆ uXScatter

std::tuple< SmartPetscObj< Vec >, SmartPetscObj< VecScatter > > Example::uXScatter
private

◆ uYScatter

std::tuple< SmartPetscObj< Vec >, SmartPetscObj< VecScatter > > Example::uYScatter
private

◆ uZScatter

std::tuple< SmartPetscObj< Vec >, SmartPetscObj< VecScatter > > Example::uZScatter
private

◆ vectorFieldPtr

boost::shared_ptr< MatrixDouble > Example::vectorFieldPtr
private
Initial value:
=
nullptr

Field values at evaluation points.

Examples
mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 137 of file adjoint.cpp.


The documentation for this struct was generated from the following files: