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...

Inheritance diagram for Example:
[legend]
Collaboration diagram for Example:
[legend]

Classes

struct  BoundaryOp
 
struct  CommonData
 [Example] More...
 
struct  DynamicFirstOrderConsConstantTimeScale
 
struct  DynamicFirstOrderConsSinusTimeScale
 
struct  Mesh
 
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 }
 

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 ()
 
MoFEMErrorCode run ()
 
 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.
 
 UmatExample (MoFEM::Interface &m_field)
 
- Public Member Functions inherited from UmatExample
 UmatExample (MoFEM::Interface &m_field)
 
MoFEMErrorCode runProblem ()
 [Run problem]
 

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 agree (MoFEMErrorCode local_error)
 
MoFEMErrorCode readOptions ()
 
MoFEMErrorCode generateSphere ()
 
MoFEMErrorCode partitionSphere ()
 
MoFEMErrorCode distributeSphere ()
 
MoFEMErrorCode deformMesh (PetscInt step)
 
MoFEMErrorCode reconstructOnRoot (PetscInt step)
 
MoFEMErrorCode reconstructMesh (PetscInt step)
 
MoFEMErrorCode resetMeshForImport ()
 
MoFEMErrorCode deleteGeometry (const Range &remove)
 
MoFEMErrorCode synchroniseMesh ()
 
MoFEMErrorCode prepareDistributed (Mesh &mesh, const Range &preserve={})
 
MoFEMErrorCode rebuildHybrid (Mesh &mesh, const std::string &tag)
 
MoFEMErrorCode saveStage (Mesh &mesh, const char *stage, bool distributed, PetscInt step=0)
 
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 checkResults (Vec gradient_vector)
 [calculateGradient]
 

Static Private Member Functions

static auto cellKeys (const TetMeshData &mesh)
 
static auto skinKeys (const TetMeshData &mesh)
 
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)
 
static int integrationRule (int, int, int order)
 

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
 
std::unique_ptr< Mesh > currentPtr
 
Range sourceLineage
 
PetscInt stretchSteps = 10
 
PetscInt hybridOrder = 2
 
double twistDegrees = 60
 
TetMeshData sphere
 
TetMeshData sourceMesh
 
TetMeshData reconstructed
 
CGALTools::ImplicitMeshSettings settings
 
std::array< double, 3 > scale {2, 1, .75}
 
std::string script
 
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
 
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.
 

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
 

Additional Inherited Members

- Protected Member Functions inherited from UmatExample
MoFEMErrorCode readMesh ()
 [Run problem]
 
MoFEMErrorCode tagTransfer ()
 [Read mesh]
 
MoFEMErrorCode setupProblem ()
 [Set up problem]
 
MoFEMErrorCode boundaryCondition ()
 [Set up problem]
 
MoFEMErrorCode assembleSystemAdolc ()
 
MoFEMErrorCode opTest ()
 [Boundary condition]
 
MoFEMErrorCode TsSolve ()
 [TS Solve]
 
MoFEMErrorCode gettingNorms ()
 [TS Solve]
 
MoFEMErrorCode select_statev_tags (MatOps::StateTags &statev_tags)
 
- Static Protected Member Functions inherited from UmatExample
static PetscInt get_statev_index (const std::string &name)
 
static auto get_skin (MoFEM::Interface &m_field, Range body_ents)
 
static auto filter_true_skin (MoFEM::Interface &m_field, Range &&skin)
 
- Protected Attributes inherited from UmatExample
MoFEM::Interface & mField
 
boost::shared_ptr< DomainEle > updateFe
 
boost::shared_ptr< PostProcEleDomain > postProcDomainFe
 
boost::shared_ptr< PostProcEleBdy > postProcBdyFe
 
FieldApproximationBase approximationBase = AINSWORTH_LEGENDRE_BASE
 
PetscReal acceptedTime = 0
 
PetscReal acceptedDt = 0
 
PetscInt acceptedStep = 0
 
std::vector< Tag > listTagsToTransfer
 list of tags to transfer to postprocessor
 
boost::shared_ptr< MatOps::PhysicalEquations > physicalEquationsPtr
 
MatOps::StateTags postProcStateTags
 
- Static Protected Attributes inherited from UmatExample
static constexpr int modelType
 

Detailed Description

[Example]

[Create Example_struct]

Examples
mesh_reconstruction.cpp, 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, and mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 209 of file plastic.cpp.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
VOL 
COUNT 

Definition at line 215 of file plastic.cpp.

215{ 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/16]

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

Definition at line 211 of file plastic.cpp.

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

◆ Example() [2/16]

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

Definition at line 417 of file dynamic_first_order_con_law.cpp.

417: mField(m_field) {}

◆ Example() [3/16]

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

Definition at line 42 of file between_meshes_dg_projection.cpp.

42: mField(m_field) {}

◆ Example() [4/16]

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

Definition at line 35 of file helmholtz.cpp.

35: mField(m_field) {}

◆ Example() [5/16]

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

Definition at line 25 of file integration.cpp.

25: mField(m_field) {}

◆ Example() [6/16]

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

Definition at line 50 of file plot_base.cpp.

50: mField(m_field) {}

◆ Example() [7/16]

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

Definition at line 81 of file phase.cpp.

81: mField(m_field) {}

◆ Example() [8/16]

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

Definition at line 51 of file approximation.cpp.

51: mField(m_field) {}

◆ Example() [9/16]

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

Definition at line 57 of file schrod_eig.cpp.

57: mField(m_field) {}

◆ Example() [10/16]

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

Definition at line 45 of file radiation.cpp.

45: mField(m_field) {}

◆ Example() [11/16]

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

Definition at line 47 of file heat_method.cpp.

47: mField(m_field) {}

◆ Example() [12/16]

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

Definition at line 54 of file eigen_elastic.cpp.

54: mField(m_field) {}

◆ Example() [13/16]

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

Definition at line 52 of file nonlinear_dynamic_elastic.cpp.

52: mField(m_field) {}

◆ Example() [14/16]

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

Definition at line 339 of file shallow_wave.cpp.

339: mField(m_field) {}

◆ Example() [15/16]

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

Definition at line 129 of file adjoint.cpp.

129: mField(m_field) {}

◆ Example() [16/16]

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

Definition at line 116 of file gradient.cpp.

116: mField(m_field) {}

Member Function Documentation

◆ agree()

MoFEMErrorCode Example::agree ( MoFEMErrorCode  local_error)
inlineprivate
Examples
mesh_reconstruction.cpp.

Definition at line 65 of file mesh_reconstruction.cpp.

65 {
67 int error;
68 CHKERR MPI_Allreduce(&local_error, &error, 1, MPI_INT, MPI_MAX,
69 currentPtr->field.get_comm());
70 if (error)
71 SETERRQ(currentPtr->field.get_comm(), error,
72 "Mesh stage failed on an MPI rank");
74 }
#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.
std::unique_ptr< Mesh > currentPtr

◆ assembleSystem() [1/12]

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, and mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 635 of file dynamic_first_order_con_law.cpp.

635 {
637 [[maybe_unused]] auto get_body_force = [this](const double, const double,
638 const double) {
641 t_source(i) = 0.;
642 t_source(0) = 0.1;
643 t_source(1) = 1.;
644 return t_source;
645 };
646
647 // specific time scaling
648 [[maybe_unused]] auto get_time_scale = [this](const double time) {
649 return sin(time * omega * M_PI);
650 };
651
652 auto apply_rhs = [&](auto &pip) {
654
656 "GEOMETRY");
657
658 // Calculate Gradient of velocity
659 auto mat_v_grad_ptr = boost::make_shared<MatrixDouble>();
661 "V", mat_v_grad_ptr));
662
663 auto gravity_vector_ptr = boost::make_shared<MatrixDouble>();
664 gravity_vector_ptr->resize(1, SPACE_DIM);
665 auto set_body_force = [&]() {
668 auto t_force = getFTensor1FromMat<SPACE_DIM, 0>(*gravity_vector_ptr);
669 double unit_weight = 0.;
670 CHKERR PetscOptionsGetReal(PETSC_NULLPTR, "", "-unit_weight", &unit_weight,
671 PETSC_NULLPTR);
672 t_force(i) = 0;
673 if (SPACE_DIM == 2) {
674 t_force(1) = -unit_weight;
675 } else if (SPACE_DIM == 3) {
676 t_force(2) = unit_weight;
677 }
679 };
680
681 CHKERR set_body_force();
682 pip.push_back(new OpBodyForce("V", gravity_vector_ptr,
683 [](double, double, double) { return 1.; }));
684
685 // Calculate unknown F
686 auto mat_H_tensor_ptr = boost::make_shared<MatrixDouble>();
688 "F", mat_H_tensor_ptr));
689
690 // // Calculate F
691 double tau = 0.2;
692 CHKERR PetscOptionsGetReal(PETSC_NULLPTR, "", "-tau", &tau, PETSC_NULLPTR);
693
694 double xi = 0.;
695 CHKERR PetscOptionsGetReal(PETSC_NULLPTR, "", "-xi", &xi, PETSC_NULLPTR);
696
697 // Calculate P stab
698 auto one = [&](const double, const double, const double) {
699 return 3. * bulk_modulus_K;
700 };
701 auto minus_one = [](const double, const double, const double) {
702 return -1.;
703 };
704
705 auto mat_dot_F_tensor_ptr = boost::make_shared<MatrixDouble>();
707 "F_dot", mat_dot_F_tensor_ptr));
708
709 // Calculate Gradient of Spatial Positions
710 auto mat_x_grad_ptr = boost::make_shared<MatrixDouble>();
712 "x_2", mat_x_grad_ptr));
713
714 auto mat_F_tensor_ptr = boost::make_shared<MatrixDouble>();
716 mat_F_tensor_ptr, mat_H_tensor_ptr));
717
718 auto mat_F_stab_ptr = boost::make_shared<MatrixDouble>();
720 mat_F_tensor_ptr, mat_F_stab_ptr, mat_dot_F_tensor_ptr, tau, xi,
721 mat_x_grad_ptr, mat_v_grad_ptr));
722
723 PetscBool is_linear_elasticity = PETSC_TRUE;
724 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-is_linear_elasticity",
725 &is_linear_elasticity, PETSC_NULLPTR);
726
727 auto mat_P_stab_ptr = boost::make_shared<MatrixDouble>();
728 if (is_linear_elasticity) {
731 mat_F_stab_ptr));
732 } else {
733 auto inv_F = boost::make_shared<MatrixDouble>();
734 auto det_ptr = boost::make_shared<VectorDouble>();
735
736 pip.push_back(
737 new OpInvertMatrix<SPACE_DIM>(mat_F_stab_ptr, det_ptr, inv_F));
738
739 // OpCalculatePiolaIncompressibleNH
742 mat_F_stab_ptr, inv_F, det_ptr));
743 }
744
745 pip.push_back(new OpGradTimesTensor2("V", mat_P_stab_ptr, minus_one));
746 pip.push_back(new OpRhsTestPiola("F", mat_v_grad_ptr, one));
747
749 };
750
751 auto *pipeline_mng = mField.getInterface<PipelineManager>();
752 CHKERR apply_rhs(pipeline_mng->getOpDomainExplicitRhsPipeline());
753
754 auto integration_rule = [](int, int, int approx_order) {
755 return 2 * approx_order;
756 };
757 CHKERR pipeline_mng->setDomainExplicitRhsIntegrationRule(integration_rule);
758
760}
@ H1
continuous field
Definition definitions.h:85
constexpr double omega
Save field DOFS on vertices/tags.
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
auto integration_rule
constexpr int SPACE_DIM
[Define dimension]
Definition gradient.cpp:19
constexpr double shear_modulus_G
Shear modulus G = E/(2(1+ν))
Definition gradient.cpp:34
constexpr double bulk_modulus_K
Bulk modulus K = E/(3(1-2ν))
Definition gradient.cpp:31
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.
DomainNaturalBCRhs::OpFlux< NaturalMeshsetType< BLOCKSET >, 1, SPACE_DIM > OpBodyForce

◆ assembleSystem() [2/12]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [3/12]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [4/12]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [5/12]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [6/12]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [7/12]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [8/12]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [9/12]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [10/12]

MoFEMErrorCode Example::assembleSystem ( )
private

◆ assembleSystem() [11/12]

MoFEMErrorCode Example::assembleSystem ( )
private

Setup operators in finite element pipeline.

◆ assembleSystem() [12/12]

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/2]

MoFEMErrorCode Example::bC ( )
private

[Create common data]

[Boundary condition]

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

Definition at line 598 of file plastic.cpp.

598 {
600
602 auto bc_mng = mField.getInterface<BcManager>();
603
604 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), "REMOVE_X",
605 "U", 0, 0);
606 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), "REMOVE_Y",
607 "U", 1, 1);
608 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), "REMOVE_Z",
609 "U", 2, 2);
610 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(),
611 "REMOVE_ALL", "U", 0, 3);
612
613#ifdef ADD_CONTACT
614 for (auto b : {"FIX_X", "REMOVE_X"})
615 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), b,
616 "SIGMA", 0, 0, false, true);
617 for (auto b : {"FIX_Y", "REMOVE_Y"})
618 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), b,
619 "SIGMA", 1, 1, false, true);
620 for (auto b : {"FIX_Z", "REMOVE_Z"})
621 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), b,
622 "SIGMA", 2, 2, false, true);
623 for (auto b : {"FIX_ALL", "REMOVE_ALL"})
624 CHKERR bc_mng->removeBlockDOFsOnEntities(simple->getProblemName(), b,
625 "SIGMA", 0, 3, false, true);
626 CHKERR bc_mng->removeBlockDOFsOnEntities(
627 simple->getProblemName(), "NO_CONTACT", "SIGMA", 0, 3, false, true);
628#endif
629
630 CHKERR bc_mng->pushMarkDOFsOnEntities<DisplacementCubitBcData>(
631 simple->getProblemName(), "U");
632
633 auto &bc_map = bc_mng->getBcMapByBlockName();
634 for (auto bc : bc_map)
635 MOFEM_LOG("PLASTICITY", Sev::verbose) << "Marker " << bc.first;
636
638}
#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/2]

MoFEMErrorCode Example::bC ( )
private

◆ boundaryCondition() [1/11]

MoFEMErrorCode Example::boundaryCondition ( )
private

[Set up problem]

[Setup adjoint]

[Create common data]

[Boundary condition]

[Applying essential BC]

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, and mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 532 of file dynamic_first_order_con_law.cpp.

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

◆ boundaryCondition() [2/11]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ boundaryCondition() [3/11]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ boundaryCondition() [4/11]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ boundaryCondition() [5/11]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ boundaryCondition() [6/11]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ boundaryCondition() [7/11]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ boundaryCondition() [8/11]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ boundaryCondition() [9/11]

MoFEMErrorCode Example::boundaryCondition ( )
private

◆ boundaryCondition() [10/11]

MoFEMErrorCode Example::boundaryCondition ( )
private

Apply essential boundary conditions.

◆ boundaryCondition() [11/11]

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 1768 of file adjoint.cpp.

1770 {
1771 MOFEM_LOG_CHANNEL("WORLD");
1773 auto simple = mField.getInterface<Simple>();
1774
1775 auto ents = get_range_from_block(mField, "OPTIMISE", SPACE_DIM - 1);
1776
1777 auto get_essential_fe = [this]() {
1778 auto post_proc_rhs = boost::make_shared<FEMethod>();
1779 auto get_post_proc_hook_rhs = [this, post_proc_rhs]() {
1781
1783 post_proc_rhs, 0)();
1785 };
1786 post_proc_rhs->postProcessHook = get_post_proc_hook_rhs;
1787 return post_proc_rhs;
1788 };
1789
1790 auto get_fd_direvative_fe = [&]() {
1791 auto fe = boost::make_shared<DomainEle>(mField);
1792 fe->getRuleHook = integrationRule;
1793 auto &pip = fe->getOpPtrVector();
1795 // Add RHS operators for internal forces
1796 HookeOps::opFactoryDomainRhs<SPACE_DIM, A, I, DomainEleOp>(
1797 mField, pip, "U", "MAT_ELASTIC", Sev::noisy);
1798
1799 return fe;
1800 };
1801
1802 auto calulate_fd_residual = [&](auto eps, auto diff_vec, auto fd_vec) {
1804
1805 constexpr bool debug = false;
1806
1807 auto geom_norm = [](MoFEM::Interface &mField) {
1809 auto field_blas = mField.getInterface<FieldBlas>();
1810 double nrm2 = 0.0;
1811 auto norm2_field = [&](const double val) {
1812 nrm2 += val * val;
1813 return val;
1814 };
1815 CHKERR field_blas->fieldLambdaOnValues(norm2_field, "GEOMETRY");
1816 MPI_Allreduce(MPI_IN_PLACE, &nrm2, 1, MPI_DOUBLE, MPI_SUM,
1817 mField.get_comm());
1818 MOFEM_LOG("WORLD", Sev::inform) << "Geometry norm: " << sqrt(nrm2);
1820 };
1821
1822 if constexpr (debug)
1823 CHKERR geom_norm(mField);
1824
1825 auto initial_current_geometry = createDMVector(adjointDM);
1826 CHKERR mField.getInterface<VecManager>()->setOtherLocalGhostVector(
1827 "ADJOINT", "ADJOINT_FIELD", "GEOMETRY", RowColData::ROW,
1828 initial_current_geometry, INSERT_VALUES, SCATTER_FORWARD);
1829 CHKERR VecAssemblyBegin(initial_current_geometry);
1830 CHKERR VecAssemblyEnd(initial_current_geometry);
1831
1832 if constexpr (debug)
1833 CHKERR geom_norm(mField);
1834
1835 auto perturb_geometry = [&](auto eps, auto diff_vec) {
1837 auto current_geometry = vectorDuplicate(initial_current_geometry);
1838 CHKERR VecCopy(initial_current_geometry, current_geometry);
1839 CHKERR VecAXPY(current_geometry, eps, diff_vec);
1840 CHKERR mField.getInterface<VecManager>()->setOtherLocalGhostVector(
1841 "ADJOINT", "ADJOINT_FIELD", "GEOMETRY", RowColData::ROW,
1842 current_geometry, INSERT_VALUES, SCATTER_REVERSE);
1844 };
1845
1846 auto fe = get_fd_direvative_fe();
1847 auto fp = vectorDuplicate(diff_vec);
1848 auto fm = vectorDuplicate(diff_vec);
1849 auto calc_impl = [&](auto f, auto eps) { // is this finite difference!
1851 CHKERR VecZeroEntries(f);
1852 fe->f = f;
1853 CHKERR perturb_geometry(eps, diff_vec);
1855 simple->getDomainFEName(), fe);
1856 CHKERR VecAssemblyBegin(f);
1857 CHKERR VecAssemblyEnd(f);
1858 CHKERR VecGhostUpdateBegin(f, ADD_VALUES, SCATTER_REVERSE);
1859 CHKERR VecGhostUpdateEnd(f, ADD_VALUES, SCATTER_REVERSE);
1860 auto post_proc_rhs = get_essential_fe();
1861 post_proc_rhs->f = f;
1863 post_proc_rhs.get());
1865 };
1866 CHKERR calc_impl(fp, eps);
1867 CHKERR calc_impl(fm, -eps);
1868 CHKERR VecWAXPY(fd_vec, -1.0, fm, fp);
1869 CHKERR VecScale(fd_vec, 1.0 / (2.0 * eps));
1870
1871 CHKERR mField.getInterface<VecManager>()->setOtherLocalGhostVector(
1872 "ADJOINT", "ADJOINT_FIELD", "GEOMETRY", RowColData::ROW,
1873 initial_current_geometry, INSERT_VALUES, SCATTER_REVERSE);
1874
1875 if constexpr (debug)
1876 CHKERR geom_norm(mField);
1877
1879 };
1880 // here starts the preperation for the derivative
1881 auto get_direvative_fe = [&](auto diff_vec) {
1882 auto fe_adjoint = boost::make_shared<DomainEle>(mField);
1883 fe_adjoint->getRuleHook = integrationRule;
1884 auto &pip = fe_adjoint->getOpPtrVector();
1885
1886 auto jac_ptr = boost::make_shared<MatrixDouble>();
1887 auto det_ptr = boost::make_shared<VectorDouble>();
1888 auto inv_jac_ptr = boost::make_shared<MatrixDouble>();
1889 auto diff_jac_ptr = boost::make_shared<MatrixDouble>();
1890 auto cof_ptr = boost::make_shared<VectorDouble>();
1891
1892 using OpCoFactor =
1893 AdJoint<DomainEleOp>::Integration<GAUSS>::OpGetCoFactor<SPACE_DIM>;
1894
1896
1898 "GEOMETRY", jac_ptr));
1900 "U", diff_jac_ptr, diff_vec));
1901
1902 pip.push_back(new OpCoFactor(jac_ptr, diff_jac_ptr, cof_ptr));
1903
1904 // Add RHS operators for internal forces
1905 auto common_ptr = HookeOps::commonDataFactory<SPACE_DIM, I, DomainEleOp>(
1906 mField, pip, "U", "MAT_ELASTIC", Sev::noisy);
1908 "U", common_ptr, jac_ptr, diff_jac_ptr, cof_ptr));
1909
1910 return fe_adjoint;
1911 };
1912
1913 auto get_objective_fe = [&](auto diff_vec, auto grad_vec,
1914 auto glob_objective_ptr,
1915 auto glob_objective_grad_ptr) {
1916 auto fe_adjoint = boost::make_shared<DomainEle>(mField);
1917 fe_adjoint->getRuleHook = integrationRule;
1918 auto &pip = fe_adjoint->getOpPtrVector();
1920
1921 auto jac_ptr = boost::make_shared<MatrixDouble>();
1922 auto det_ptr = boost::make_shared<VectorDouble>();
1923 auto inv_jac_ptr = boost::make_shared<MatrixDouble>();
1924 auto diff_jac_ptr = boost::make_shared<MatrixDouble>();
1925 auto cof_ptr = boost::make_shared<VectorDouble>();
1926 auto d_grad_ptr = boost::make_shared<MatrixDouble>();
1927 auto d_u_ptr = boost::make_shared<MatrixDouble>();
1928 auto u_ptr = boost::make_shared<MatrixDouble>();
1929
1930 using OpCoFactor =
1931 AdJoint<DomainEleOp>::Integration<GAUSS>::OpGetCoFactor<SPACE_DIM>;
1933 "GEOMETRY", jac_ptr));
1935 "U", diff_jac_ptr,
1936 diff_vec)); // Note: that vector is stored on displacemnt vector, that
1937 // why is used here
1938 pip.push_back(new OpCoFactor(jac_ptr, diff_jac_ptr, cof_ptr));
1940 "U", d_grad_ptr, grad_vec));
1941 pip.push_back(
1942 new OpCalculateVectorFieldValues<SPACE_DIM>("U", d_u_ptr, grad_vec));
1943 pip.push_back(new OpCalculateVectorFieldValues<SPACE_DIM>("U", u_ptr));
1944
1945 auto common_ptr = HookeOps::commonDataFactory<SPACE_DIM, I, DomainEleOp>(
1946 mField, pip, "U", "MAT_ELASTIC", Sev::noisy);
1947 pip.push_back(new OpAdJointObjective(
1948 pythonPtr, common_ptr, jac_ptr, diff_jac_ptr, cof_ptr, d_grad_ptr,
1949 d_u_ptr, u_ptr, glob_objective_ptr, glob_objective_grad_ptr));
1950
1951 return fe_adjoint;
1952 };
1953
1954 auto dm = simple->getDM();
1955 auto f = createDMVector(dm);
1956 auto d = vectorDuplicate(f);
1957 auto dm_diff_vec = vectorDuplicate(d);
1958 auto zero_diff_vec = vectorDuplicate(dm_diff_vec);
1959 auto zero_state_sensitivity = vectorDuplicate(d);
1960 CHKERR VecZeroEntries(zero_diff_vec);
1961 CHKERR VecZeroEntries(zero_state_sensitivity);
1962 CHKERR VecGhostUpdateBegin(zero_diff_vec, INSERT_VALUES, SCATTER_FORWARD);
1963 CHKERR VecGhostUpdateEnd(zero_diff_vec, INSERT_VALUES, SCATTER_FORWARD);
1964 CHKERR VecGhostUpdateBegin(zero_state_sensitivity, INSERT_VALUES,
1965 SCATTER_FORWARD);
1966 CHKERR VecGhostUpdateEnd(zero_state_sensitivity, INSERT_VALUES,
1967 SCATTER_FORWARD);
1968
1969 auto adjoint_fe = get_direvative_fe(dm_diff_vec);
1970 auto objective_ptr_direct = boost::make_shared<double>(0.0);
1971 auto objective_grad_ptr_direct = boost::make_shared<double>(0.0);
1972 auto objective_fe_direct = get_objective_fe(
1973 dm_diff_vec, d, objective_ptr_direct, objective_grad_ptr_direct);
1974 auto objective_ptr_explicit = boost::make_shared<double>(0.0);
1975 auto objective_grad_ptr_explicit = boost::make_shared<double>(0.0);
1976 auto objective_fe_explicit =
1977 get_objective_fe(dm_diff_vec, zero_state_sensitivity,
1978 objective_ptr_explicit, objective_grad_ptr_explicit);
1979 auto objective_ptr_value = boost::make_shared<double>(0.0);
1980 auto objective_grad_ptr_value = boost::make_shared<double>(0.0);
1981 auto objective_fe_value =
1982 get_objective_fe(zero_diff_vec, zero_state_sensitivity,
1983 objective_ptr_value, objective_grad_ptr_value);
1984
1985 auto set_variance_of_geometry =
1986 [&](auto mode, auto mod_vec) { // think of mod_vec as X(tau) = X + tau*v_h
1987 // take the mod_vec and write it into the adjoint DM then copy that
1988 // field to dm_diff_vec using the same layout as U
1990 CHKERR DMoFEMMeshToLocalVector(adjointDM, mod_vec, INSERT_VALUES,
1991 SCATTER_REVERSE);
1992 CHKERR mField.getInterface<VecManager>()->setOtherLocalGhostVector(
1993 simple->getProblemName(), "U", "ADJOINT_FIELD", RowColData::ROW,
1994 dm_diff_vec, INSERT_VALUES, SCATTER_FORWARD);
1995 CHKERR VecGhostUpdateBegin(dm_diff_vec, INSERT_VALUES, SCATTER_FORWARD);
1996 CHKERR VecGhostUpdateEnd(dm_diff_vec, INSERT_VALUES, SCATTER_FORWARD);
1998 };
1999
2000 auto calculate_variance_internal_forces = [&](auto mode, auto mod_vec) {
2002 CHKERR VecZeroEntries(f);
2003 CHKERR VecGhostUpdateBegin(f, INSERT_VALUES, SCATTER_FORWARD);
2004 CHKERR VecGhostUpdateEnd(f, INSERT_VALUES, SCATTER_FORWARD);
2005 adjoint_fe->f = f;
2007 CHKERR VecAssemblyBegin(f);
2008 CHKERR VecAssemblyEnd(f);
2009 CHKERR VecGhostUpdateBegin(f, ADD_VALUES, SCATTER_REVERSE);
2010 CHKERR VecGhostUpdateEnd(f, ADD_VALUES, SCATTER_REVERSE);
2011 auto post_proc_rhs = get_essential_fe();
2012 post_proc_rhs->f = f;
2014 post_proc_rhs.get());
2015 CHKERR VecScale(f, -1.0);
2016
2017#ifndef NDEBUG
2018 constexpr bool debug = true;
2019 if constexpr (debug) {
2020 double norm0;
2021 CHKERR VecNorm(f, NORM_2, &norm0);
2022 auto fd_check = vectorDuplicate(f);
2023 double eps = 1e-5;
2024 CHKERR calulate_fd_residual(eps, dm_diff_vec, fd_check);
2025 double nrm;
2026 CHKERR VecAXPY(fd_check, -1.0, f);
2027 CHKERR VecNorm(fd_check, NORM_2, &nrm);
2028 MOFEM_LOG("WORLD", Sev::inform)
2029 << " FD check for internal forces [ " << mode << " ]: " << nrm
2030 << " / " << norm0 << " ( " << (nrm / norm0) << " )";
2031 }
2032#endif
2033
2035 };
2036
2037 auto calculate_variance_of_displacement = [&](auto mode, auto mod_vec) {
2039 CHKERR KSPSolve(kspElastic, f, d);
2040
2041 CHKERR VecGhostUpdateBegin(d, INSERT_VALUES, SCATTER_FORWARD);
2042 CHKERR VecGhostUpdateEnd(d, INSERT_VALUES, SCATTER_FORWARD);
2044 };
2045
2046 auto evaluate_objective_terms =
2047 [&](auto objective_fe, auto objective_ptr, auto objective_grad_ptr,
2048 double &objective_value, double &objective_gradient) {
2050 *objective_ptr = 0.0;
2051 *objective_grad_ptr = 0.0;
2053 objective_fe);
2054 std::array<double, 2> array = {*objective_ptr, *objective_grad_ptr};
2055 MPI_Allreduce(MPI_IN_PLACE, array.data(), 2, MPI_DOUBLE, MPI_SUM,
2056 mField.get_comm());
2057 objective_value = array[0];
2058 objective_gradient = array[1];
2060 };
2061
2062 auto calculate_objective_value = [&]() {
2064 double objective_value = 0;
2065 double objective_gradient = 0;
2066 CHKERR evaluate_objective_terms(objective_fe_value, objective_ptr_value,
2067 objective_grad_ptr_value, objective_value,
2068 objective_gradient);
2069 *objective_function_value = objective_value;
2071 };
2072
2073 auto calculate_variance_of_objective_function_dJ_du = [&](Vec dJ_du) {
2075
2076 auto fe = boost::make_shared<DomainEle>(mField);
2077 fe->getRuleHook = integrationRule;
2078 auto &pip = fe->getOpPtrVector();
2080
2081 auto u_ptr = boost::make_shared<MatrixDouble>();
2082 pip.push_back(new OpCalculateVectorFieldValues<SPACE_DIM>("U", u_ptr));
2083
2084 auto common_ptr = HookeOps::commonDataFactory<SPACE_DIM, I, DomainEleOp>(
2085 mField, pip, "U", "MAT_ELASTIC", Sev::noisy);
2086 pip.push_back(new OpStateSensitivity("U", pythonPtr, common_ptr, u_ptr));
2087
2088 CHKERR VecZeroEntries(dJ_du);
2089 fe->f = dJ_du;
2091 CHKERR VecAssemblyBegin(dJ_du);
2092 CHKERR VecAssemblyEnd(dJ_du);
2093
2094 auto post_proc_rhs = get_essential_fe();
2095 post_proc_rhs->f = dJ_du;
2097 post_proc_rhs.get());
2098
2100 };
2101
2102 auto calculate_adjoint_lambda = [&](auto lambda, auto dJ_du) {
2104
2105 MOFEM_LOG("WORLD", Sev::inform) << "Solving for adjoint variable lambda";
2106 CHKERR VecZeroEntries(lambda);
2107 CHKERR KSPSolveTranspose(kspElastic, dJ_du, lambda);
2108 CHKERR VecGhostUpdateBegin(lambda, INSERT_VALUES, SCATTER_FORWARD);
2109 CHKERR VecGhostUpdateEnd(lambda, INSERT_VALUES, SCATTER_FORWARD);
2111 };
2112
2113 CHKERR VecZeroEntries(objective_function_gradient);
2114 CHKERR VecZeroEntries(adjoint_vector);
2115
2116 CHKERR calculate_objective_value();
2117 MOFEM_LOG("WORLD", Sev::verbose)
2118 << "Objective function: " << *objective_function_value;
2119
2120 auto direct = [&]() {
2122 int mode = 0;
2123 for (auto mod_vec : modeVecs) {
2124 CHKERR set_variance_of_geometry(mode, mod_vec);
2125 CHKERR calculate_variance_internal_forces(mode, mod_vec);
2126 CHKERR calculate_variance_of_displacement(mode, mod_vec);
2127 double objective_value = 0;
2128 double objective_gradient = 0;
2129 CHKERR evaluate_objective_terms(objective_fe_direct, objective_ptr_direct,
2130 objective_grad_ptr_direct,
2131 objective_value, objective_gradient);
2132 CHKERR VecSetValue(objective_function_gradient, mode, objective_gradient,
2133 INSERT_VALUES);
2134 CHKERR VecAXPY(adjoint_vector, objective_gradient, dm_diff_vec);
2135 ++mode;
2136 }
2138 };
2139
2140 auto lambda = vectorDuplicate(f);
2141 auto dJ_du = vectorDuplicate(f);
2142
2143 auto adjoint = [&]() {
2145
2146 CHKERR calculate_variance_of_objective_function_dJ_du(dJ_du);
2147 CHKERR calculate_adjoint_lambda(lambda, dJ_du);
2148
2149 int mode = 0;
2150
2151 for (auto mod_vec : modeVecs) {
2152
2153 CHKERR set_variance_of_geometry(mode, mod_vec);
2154 CHKERR calculate_variance_internal_forces(mode, mod_vec);
2155 double objective_value = 0;
2156 double objective_gradient_explicit = 0;
2157 CHKERR evaluate_objective_terms(
2158 objective_fe_explicit, objective_ptr_explicit,
2159 objective_grad_ptr_explicit, objective_value,
2160 objective_gradient_explicit);
2161 double lambda_dot_residual_variation = 0;
2162 CHKERR VecDot(lambda, f, &lambda_dot_residual_variation);
2163 const double dJ_dp =
2164 objective_gradient_explicit + lambda_dot_residual_variation;
2165 CHKERR VecSetValue(objective_function_gradient, mode, dJ_dp,
2166 INSERT_VALUES);
2167 CHKERR VecAXPY(adjoint_vector, dJ_dp, dm_diff_vec);
2168 ++mode;
2169 }
2170 CHKERR VecAssemblyBegin(objective_function_gradient);
2171 CHKERR VecAssemblyEnd(objective_function_gradient);
2173 };
2174
2175 switch (derivative_type) {
2176 case DIRECT:
2177 MOFEM_LOG("WORLD", Sev::inform) << "Running Direct Sensitivity...";
2178 CHKERR direct();
2179 break;
2180
2181 case ADJOINT:
2182 MOFEM_LOG("WORLD", Sev::inform) << "Running Adjoint Sensitivity...";
2183 CHKERR adjoint();
2184 break;
2185
2186 default:
2187 SETERRQ(PETSC_COMM_WORLD, MOFEM_DATA_INCONSISTENCY,
2188 "Wrong sensitivity type selected");
2189 }
2190
2191 CHKERR VecAssemblyBegin(objective_function_gradient);
2192 CHKERR VecAssemblyEnd(objective_function_gradient);
2193
2194 CHKERR VecAssemblyBegin(adjoint_vector);
2195 CHKERR VecAssemblyEnd(adjoint_vector);
2196 CHKERR VecGhostUpdateBegin(adjoint_vector, INSERT_VALUES, SCATTER_FORWARD);
2197 CHKERR VecGhostUpdateEnd(adjoint_vector, INSERT_VALUES, SCATTER_FORWARD);
2198
2200}
@ 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 ...
static const bool debug
SensitivityMethod derivative_type
Definition gradient.cpp:96
@ DIRECT
Definition gradient.cpp:94
@ ADJOINT
Definition gradient.cpp:94
Range get_range_from_block(MoFEM::Interface &m_field, const std::string block_name, int dim)
PetscErrorCode DMoFEMPostProcessFiniteElements(DM dm, MoFEM::FEMethod *method)
execute finite element method for each element in dm (problem)
Definition DMMoFEM.cpp:632
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:600
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:662
auto createDMVector(DM dm, RowColData rc=RowColData::COL)
Get smart vector from DM.
Definition DMMoFEM.hpp:1279
@ GAUSS
Gaussian quadrature integration.
#define MOFEM_LOG_CHANNEL(channel)
Set and reset channel.
static double lambda
const FTensor::Tensor2< T, Dim, Dim > Vec
SmartPetscObj< Vec > vectorDuplicate(Vec vec)
Create duplicate vector of smart vector.
static int integrationRule(int, int, int p_data)
Definition radiation.cpp:52
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:802
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 1016 of file gradient.cpp.

1018 {
1019 MOFEM_LOG_CHANNEL("WORLD");
1021 auto simple = mField.getInterface<Simple>();
1022 auto dm = simple->getDM();
1023
1024 auto get_essential_fe = [this]() {
1025 auto post_proc_rhs = boost::make_shared<FEMethod>();
1026 auto get_post_proc_hook_rhs = [this, post_proc_rhs]() {
1029 post_proc_rhs, 0)();
1031 };
1032 post_proc_rhs->postProcessHook = get_post_proc_hook_rhs;
1033 return post_proc_rhs;
1034 };
1035
1036 auto get_objective_fe = [&](auto lambda_vec, auto glob_objective_ptr,
1037 auto fe_rule) {
1038 auto fe_obj = boost::make_shared<DomainEle>(mField);
1039 fe_obj->getRuleHook = fe_rule;
1040 auto &pip = fe_obj->getOpPtrVector();
1042
1043 auto jac_ptr = boost::make_shared<MatrixDouble>();
1044 auto u_ptr = boost::make_shared<MatrixDouble>();
1045 auto grad_lambda_ptr = boost::make_shared<MatrixDouble>();
1046
1048 "GEOMETRY", jac_ptr));
1049 pip.push_back(new OpCalculateVectorFieldValues<SPACE_DIM>("U", u_ptr));
1050 auto lambda_smart_vec = SmartPetscObj<Vec>(lambda_vec, true);
1052 "U", grad_lambda_ptr, lambda_smart_vec));
1053
1054 auto common_ptr = HookeOps::commonDataFactory<SPACE_DIM, I, DomainEleOp>(
1055 mField, pip, "U", "MAT_ELASTIC", Sev::noisy);
1056 pip.push_back(new OpAdJointObjective(pythonPtr, common_ptr, jac_ptr, u_ptr,
1057 grad_lambda_ptr,
1058 glob_objective_ptr));
1059 return fe_obj;
1060 };
1061
1062 auto evaluate_objective_terms =
1063 [&](auto objective_fe, auto objective_ptr) {
1065 *objective_ptr = 0.0;
1066 CHKERR VecZeroEntries(objective_function_gradient);
1067 CHKERR VecGhostUpdateBegin(lambda, INSERT_VALUES, SCATTER_FORWARD);
1068 CHKERR VecGhostUpdateEnd(lambda, INSERT_VALUES, SCATTER_FORWARD);
1069 objective_fe->f = objective_function_gradient;
1070 CHKERR DMoFEMLoopFiniteElements(adjointDM, "ADJOINT_DOMAIN_FE",
1071 objective_fe);
1072 MPI_Allreduce(MPI_IN_PLACE, objective_ptr.get(), 1, MPI_DOUBLE, MPI_SUM,
1073 mField.get_comm());
1074 CHKERR VecAssemblyBegin(objective_function_gradient);
1075 CHKERR VecAssemblyEnd(objective_function_gradient);
1076 CHKERR VecGhostUpdateBegin(objective_function_gradient, ADD_VALUES,
1077 SCATTER_REVERSE);
1078 CHKERR VecGhostUpdateEnd(objective_function_gradient, ADD_VALUES,
1079 SCATTER_REVERSE);
1081 };
1082
1083 auto calculate_variance_of_objective_function_dJ_du = [&]() {
1085 auto fe = boost::make_shared<DomainEle>(mField);
1086 fe->getRuleHook = [](int, int, int p_data) {
1087 return 2 * p_data + p_data - 1;
1088 };
1089 auto &pip = fe->getOpPtrVector();
1091 auto u_ptr = boost::make_shared<MatrixDouble>();
1092 pip.push_back(new OpCalculateVectorFieldValues<SPACE_DIM>("U", u_ptr));
1093 auto common_ptr = HookeOps::commonDataFactory<SPACE_DIM, I, DomainEleOp>(
1094 mField, pip, "U", "MAT_ELASTIC", Sev::noisy);
1095 pip.push_back(new OpStateSensitivity("U", pythonPtr, common_ptr, u_ptr));
1096 CHKERR VecZeroEntries(dJ_du);
1097 fe->f = dJ_du;
1099 CHKERR VecAssemblyBegin(dJ_du);
1100 CHKERR VecAssemblyEnd(dJ_du);
1101 auto post_proc_rhs = get_essential_fe();
1102 post_proc_rhs->f = dJ_du;
1104 post_proc_rhs.get());
1106 };
1107
1108 auto calculate_adjoint_lambda = [&]() {
1110
1111 MOFEM_LOG("WORLD", Sev::inform) << "Solving for adjoint variable lambda";
1112 CHKERR VecZeroEntries(lambda);
1113 CHKERR KSPSolveTranspose(kspElastic, dJ_du, lambda);
1114 CHKERR VecGhostUpdateBegin(lambda, INSERT_VALUES, SCATTER_FORWARD);
1115 CHKERR VecGhostUpdateEnd(lambda, INSERT_VALUES, SCATTER_FORWARD);
1117 };
1118
1119 auto fe_rule = [](int, int, int p_data) { return 2 * p_data + p_data - 1; };
1120 auto objective_ptr_value = boost::make_shared<double>(0.0);
1121 auto objective_fe = get_objective_fe(lambda, objective_ptr_value, fe_rule);
1122
1123 auto adjoint = [&]() {
1125
1126 CHKERR calculate_variance_of_objective_function_dJ_du();
1127 CHKERR calculate_adjoint_lambda();
1128 CHKERR evaluate_objective_terms(objective_fe, objective_ptr_value);
1129 *objective_function_value = *objective_ptr_value;
1130
1131 MOFEM_LOG("WORLD", Sev::verbose)
1132 << "Objective function: " << *objective_function_value;
1133
1134 CHKERR VecAssemblyBegin(objective_function_gradient);
1135 CHKERR VecAssemblyEnd(objective_function_gradient);
1136 CHKERR VecGhostUpdateBegin(lambda, INSERT_VALUES, SCATTER_FORWARD);
1137 CHKERR VecGhostUpdateEnd(lambda, INSERT_VALUES, SCATTER_FORWARD);
1138
1140 };
1141
1142 switch (derivative_type) {
1143
1144 case ADJOINT:
1145 MOFEM_LOG("WORLD", Sev::inform) << "Running Adjoint Sensitivity...";
1146 CHKERR adjoint();
1147 break;
1148
1149 default:
1150 SETERRQ(PETSC_COMM_WORLD, MOFEM_DATA_INCONSISTENCY,
1151 "Wrong sensitivity type selected");
1152 }
1153
1154 CHKERR VecAssemblyBegin(objective_function_gradient);
1155 CHKERR VecAssemblyEnd(objective_function_gradient);
1156
1158}
intrusive_ptr for managing petsc objects

◆ cellKeys()

static auto Example::cellKeys ( const TetMeshData &  mesh)
inlinestaticprivate
Examples
mesh_reconstruction.cpp.

Definition at line 198 of file mesh_reconstruction.cpp.

198 {
199 std::set<std::array<int, 4>> keys;
200 for (const auto &cell : mesh.cells) {
201 std::array<int, 4> ids;
202 for (int d = 0; d != 4; ++d)
203 ids[d] = mesh.vertexIds[cell[d]];
204 std::sort(ids.begin(), ids.end());
205 keys.insert(ids);
206 }
207 return keys;
208 }
WorkMesh mesh
std::vector< Cell > cells

◆ checkResults() [1/12]

MoFEMErrorCode Example::checkResults ( )
private

[Postprocess results]

[Print results]

[Check]

[Check results]

[Test example]

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-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-3_nonlinear_dynamics/nonlinear_dynamic_elastic.cpp, and mofem/tutorials/vec-4_shallow_wave/shallow_wave.cpp.

Definition at line 1172 of file dynamic_first_order_con_law.cpp.

1172 {
1175 CHKERR testing->addTestCommandLine("", 1, "solution coefficient norm", [&]() {
1177 auto *simple = mField.getInterface<Simple>();
1178 auto T = createDMVector(simple->getDM());
1179 CHKERR DMoFEMMeshToLocalVector(simple->getDM(), T, INSERT_VALUES,
1180 SCATTER_FORWARD);
1181 double nrm2;
1182 CHKERR VecNorm(T, NORM_2, &nrm2);
1183 MOFEM_LOG("EXAMPLE", Sev::inform) << "Regression norm " << nrm2;
1184 constexpr double regression_value = 0.0194561;
1185 CHKERR testing->checkValue<TestType::SCALAR>(
1186 "solution coefficient norm", nrm2, regression_value, {1e-2, 0.});
1188 });
1189 CHKERR testing->runTests();
1191}
PetscBool testing
virtual MoFEMErrorCode addTestCommandLine(const std::string &prefix, int number, const std::string &name, TestHook hook)=0

◆ checkResults() [2/12]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [3/12]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [4/12]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [5/12]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [6/12]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [7/12]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [8/12]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [9/12]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [10/12]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [11/12]

MoFEMErrorCode Example::checkResults ( )
private

◆ checkResults() [12/12]

MoFEMErrorCode Example::checkResults ( Vec  gradient_vector)
private

[calculateGradient]

[Finite difference check]

Definition at line 1162 of file gradient.cpp.

1162 {
1165
1166 CHKERR testing->addTestCommandLine("", 1, "gradient_fd", [&]() {
1168
1169 PetscInt nb_modes = 5;
1170 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, PETSC_NULLPTR,
1171 "-gradient_nb_modes", &nb_modes, PETSC_NULLPTR);
1172 PetscReal eps = 1e-6;
1173 CHKERR PetscOptionsGetReal(PETSC_NULLPTR, PETSC_NULLPTR, "-gradient_fd_eps",
1174 &eps, PETSC_NULLPTR);
1175
1176 auto simple = mField.getInterface<Simple>();
1177 auto dm = simple->getDM();
1178 auto *adj_problem_ptr = getProblemPtr(adjointDM);
1179
1180 auto get_objective_fe = [&](auto glob_objective_ptr) {
1181 auto fe_obj_fe = boost::make_shared<DomainEle>(mField);
1182 fe_obj_fe->getRuleHook = [](int, int, int p_data) {
1183 return 2 * p_data + p_data - 1;
1184 };
1185 auto &pip = fe_obj_fe->getOpPtrVector();
1187 auto jac_ptr = boost::make_shared<MatrixDouble>();
1188 auto u_ptr = boost::make_shared<MatrixDouble>();
1190 "GEOMETRY", jac_ptr));
1191 pip.push_back(new OpCalculateVectorFieldValues<SPACE_DIM>("U", u_ptr));
1192 auto common_ptr = HookeOps::commonDataFactory<SPACE_DIM, I, DomainEleOp>(
1193 mField, pip, "U", "MAT_ELASTIC", Sev::noisy);
1194 pip.push_back(new OpObjective(pythonPtr, common_ptr, jac_ptr, u_ptr,
1195 glob_objective_ptr));
1196 return fe_obj_fe;
1197 };
1198
1199 auto f = boost::make_shared<double>(0.);
1200 auto fe_obj = get_objective_fe(f);
1201 CacheTupleSharedPtr tmp_cache_ptr = boost::make_shared<CacheTuple>();
1203 tmp_cache_ptr);
1204
1205 auto evaluate_objective = [&](double &objective_value) {
1207 *f = 0.0;
1209 MPI_Allreduce(MPI_IN_PLACE, f.get(), 1, MPI_DOUBLE, MPI_SUM,
1210 mField.get_comm());
1211 objective_value = *f;
1213 };
1214
1215 auto &adj_dofs =
1216 adj_problem_ptr->getNumeredRowDofsPtr()->get<PetscGlobalIdx_mi_tag>();
1217 auto &dofs = mField.get_dofs()->get<Unique_mi_tag>();
1218 auto geometry_bit_number = mField.get_field_bit_number("GEOMETRY");
1219
1220 // Returns the geometry DOF perturbed by adjoint DOF "mode" on this rank,
1221 // or nullptr when the entity is not present locally. Every rank still
1222 // takes part in the collective solves below.
1223 auto get_geometry_dof = [&](PetscInt mode) -> const DofEntity * {
1224 auto a_dof = adj_dofs.find(mode);
1225 if (a_dof == adj_dofs.end())
1226 return nullptr;
1228 (*a_dof)->getEntDofIdx(),
1229 FieldEntity::getLocalUniqueIdCalculate(geometry_bit_number,
1230 (*a_dof)->getEnt()));
1231 auto dof = dofs.find(uid);
1232 return dof != dofs.end() ? dof->get() : nullptr;
1233 };
1234
1235 auto solve_and_evaluate = [&](const DofEntity *dof, double geom_val,
1236 double &objective_value) {
1238 if (dof)
1239 dof->getFieldData() = geom_val;
1240 CHKERR KSPReset(kspElastic);
1242 CHKERR evaluate_objective(objective_value);
1244 };
1245
1246 for (PetscInt mode = 0; mode < nb_modes; ++mode) {
1247 auto dof = get_geometry_dof(mode);
1248 const double org_geom_val = dof ? dof->getFieldData() : 0.;
1249
1250 double f_plus, f_minus;
1251 CHKERR solve_and_evaluate(dof, org_geom_val + eps, f_plus);
1252 CHKERR solve_and_evaluate(dof, org_geom_val - eps, f_minus);
1253 if (dof)
1254 dof->getFieldData() = org_geom_val;
1255 const double finite_diff_grad = (f_plus - f_minus) / (2 * eps);
1256
1257 // The adjoint gradient entry lives on the owning rank only; share it so
1258 // the collective check below sees one value everywhere.
1259 double owner_data[2] = {0., 0.};
1260 auto a_dof = adj_dofs.find(mode);
1261 if (a_dof != adj_dofs.end() && (*a_dof)->getHasLocalIndex()) {
1262 const double *g_array;
1263 CHKERR VecGetArrayRead(gradient_vector, &g_array);
1264 owner_data[0] = g_array[(*a_dof)->getPetscLocalDofIdx()];
1265 owner_data[1] = 1.;
1266 CHKERR VecRestoreArrayRead(gradient_vector, &g_array);
1267 }
1268 CHKERR MPI_Allreduce(MPI_IN_PLACE, owner_data, 2, MPI_DOUBLE, MPI_SUM,
1269 mField.get_comm());
1270 if (owner_data[1] == 0.)
1272 "Adjoint DOF for mode %d not found on any rank", mode);
1273 const double adjoint_grad = owner_data[0] / owner_data[1];
1274
1275 MOFEM_LOG("WORLD", Sev::inform)
1276 << "Mode: " << mode << ", Adjoint gradient: " << adjoint_grad
1277 << ", Finite difference gradient: " << finite_diff_grad;
1278 CHKERR testing->checkValue<TestType::DERIVATIVE>(
1279 "gradient mode " + std::to_string(mode), adjoint_grad,
1280 finite_diff_grad, {0., 1e-4});
1281 }
1282
1284 });
1285
1286 CHKERR testing->runTests();
1287
1289}
virtual const DofEntity_multiIndex * get_dofs() const =0
Get the dofs object.
PetscErrorCode PetscOptionsGetInt(PetscOptions *, const char pre[], const char name[], PetscInt *ivalue, PetscBool *set)
boost::shared_ptr< CacheTuple > CacheTupleSharedPtr
auto getProblemPtr(DM dm)
get problem pointer from DM
Definition DMMoFEM.hpp:1224
MoFEMErrorCode solveElastic()
Solve forward elastic problem.
Definition adjoint.cpp:1240
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.
keeps information about DOF on the entity
FieldData & getFieldData() const
static UId getUniqueIdCalculate(const DofIdx dof, const UId &ent_uid)
UId getLocalUniqueIdCalculate()
Get the Local Unique Id Calculate object.
Index tags of the numbered DOF containers that also read the index.

◆ createCommonData() [1/8]

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, and mofem/tutorials/vec-4_shallow_wave/shallow_wave.cpp.

Definition at line 471 of file plastic.cpp.

471 {
473
474 auto get_command_line_parameters = [&]() {
476
477 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-scale", &scale, PETSC_NULLPTR);
478 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-young_modulus",
479 &young_modulus, PETSC_NULLPTR);
480 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-poisson_ratio",
481 &poisson_ratio, PETSC_NULLPTR);
482 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-hardening", &H, PETSC_NULLPTR);
483 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-hardening_viscous", &visH,
484 PETSC_NULLPTR);
485 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-yield_stress", &sigmaY,
486 PETSC_NULLPTR);
487 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-cn0", &cn0, PETSC_NULLPTR);
488 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-cn1", &cn1, PETSC_NULLPTR);
489 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-zeta", &zeta, PETSC_NULLPTR);
490 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-Qinf", &Qinf, PETSC_NULLPTR);
491 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-b_iso", &b_iso, PETSC_NULLPTR);
492 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-C1_k", &C1_k, PETSC_NULLPTR);
493 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-large_strains",
494 &is_large_strains, PETSC_NULLPTR);
495 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-set_timer", &set_timer,
496 PETSC_NULLPTR);
497 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-atom_test", &atom_test,
498 PETSC_NULLPTR);
499
500 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-order", &order, PETSC_NULLPTR);
501 PetscBool tau_order_is_set; ///< true if tau order is set
502 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-tau_order", &tau_order,
503 &tau_order_is_set);
504 PetscBool ep_order_is_set; ///< true if tau order is set
505 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-ep_order", &ep_order,
506 &ep_order_is_set);
507 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-geom_order", &geom_order,
508 PETSC_NULLPTR);
509
510 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-rho", &rho, PETSC_NULLPTR);
511 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-alpha_damping",
512 &alpha_damping, PETSC_NULLPTR);
513
514 MOFEM_LOG("PLASTICITY", Sev::inform) << "Young modulus " << young_modulus;
515 MOFEM_LOG("PLASTICITY", Sev::inform) << "Poisson ratio " << poisson_ratio;
516 MOFEM_LOG("PLASTICITY", Sev::inform) << "Yield stress " << sigmaY;
517 MOFEM_LOG("PLASTICITY", Sev::inform) << "Hardening " << H;
518 MOFEM_LOG("PLASTICITY", Sev::inform) << "Viscous hardening " << visH;
519 MOFEM_LOG("PLASTICITY", Sev::inform) << "Saturation yield stress " << Qinf;
520 MOFEM_LOG("PLASTICITY", Sev::inform) << "Saturation exponent " << b_iso;
521 MOFEM_LOG("PLASTICITY", Sev::inform) << "Kinematic hardening " << C1_k;
522 MOFEM_LOG("PLASTICITY", Sev::inform) << "cn0 " << cn0;
523 MOFEM_LOG("PLASTICITY", Sev::inform) << "cn1 " << cn1;
524 MOFEM_LOG("PLASTICITY", Sev::inform) << "zeta " << zeta;
525
526 if (tau_order_is_set == PETSC_FALSE)
527 tau_order = order - 2;
528 if (ep_order_is_set == PETSC_FALSE)
529 ep_order = order - 1;
530
531 MOFEM_LOG("PLASTICITY", Sev::inform) << "Approximation order " << order;
532 MOFEM_LOG("PLASTICITY", Sev::inform)
533 << "Ep approximation order " << ep_order;
534 MOFEM_LOG("PLASTICITY", Sev::inform)
535 << "Tau approximation order " << tau_order;
536 MOFEM_LOG("PLASTICITY", Sev::inform)
537 << "Geometry approximation order " << geom_order;
538
539 MOFEM_LOG("PLASTICITY", Sev::inform) << "Density " << rho;
540 MOFEM_LOG("PLASTICITY", Sev::inform) << "alpha_damping " << alpha_damping;
541
542 PetscBool is_scale = PETSC_TRUE;
543 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-is_scale", &is_scale,
544 PETSC_NULLPTR);
545 if (is_scale) {
547 }
548
549 MOFEM_LOG("PLASTICITY", Sev::inform) << "Scale " << scale;
550
551#ifdef ADD_CONTACT
552 CHKERR PetscOptionsGetScalar(PETSC_NULLPTR, "", "-cn_contact",
553 &ContactOps::cn_contact, PETSC_NULLPTR);
554 MOFEM_LOG("CONTACT", Sev::inform)
555 << "cn_contact " << ContactOps::cn_contact;
556#endif // ADD_CONTACT
557
558 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-quasi_static",
559 &is_quasi_static, PETSC_NULLPTR);
560 MOFEM_LOG("PLASTICITY", Sev::inform)
561 << "Is quasi static: " << (is_quasi_static ? "true" : "false");
562
564 };
565
566 CHKERR get_command_line_parameters();
567
568#ifdef ADD_CONTACT
569 #ifdef ENABLE_PYTHON_BINDING
570 auto file_exists = [](std::string myfile) {
571 std::ifstream file(myfile.c_str());
572 if (file) {
573 return true;
574 }
575 return false;
576 };
577 char sdf_file_name[255] = "sdf.py";
578 CHKERR PetscOptionsGetString(PETSC_NULLPTR, PETSC_NULLPTR, "-sdf_file",
579 sdf_file_name, 255, PETSC_NULLPTR);
580
581 if (file_exists(sdf_file_name)) {
582 MOFEM_LOG("CONTACT", Sev::inform) << sdf_file_name << " file found";
583 sdfPythonPtr = boost::make_shared<ContactOps::SDFPython>();
584 CHKERR sdfPythonPtr->sdfInit(sdf_file_name);
585 CHKERR sdfPythonPtr->requireDerivatives();
586 ContactOps::sdfPythonWeakPtr = sdfPythonPtr;
587 } else {
588 MOFEM_LOG("CONTACT", Sev::warning) << sdf_file_name << " file NOT found";
589 }
590 #endif
591#endif // ADD_CONTACT
592
594}
constexpr double poisson_ratio
Poisson's ratio ν
Definition gradient.cpp:30
constexpr double young_modulus
[Material properties for linear elasticity]
Definition gradient.cpp:29
double cn_contact
Definition contact.cpp:88
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)
double C1_k
Kinematic hardening.
Definition plastic.cpp:134
double Qinf
Saturation yield stress.
Definition plastic.cpp:132
double rho
Definition plastic.cpp:145
int atom_test
Atom test.
Definition plastic.cpp:122
PetscBool is_quasi_static
Definition plastic.cpp:144
double alpha_damping
Definition plastic.cpp:146
double visH
Viscous hardening.
Definition plastic.cpp:130
PetscBool set_timer
Set timer.
Definition plastic.cpp:119
double zeta
Viscous hardening.
Definition plastic.cpp:131
int tau_order
Order of tau files.
Definition plastic.cpp:140
double cn0
Definition plastic.cpp:136
double b_iso
Saturation exponent.
Definition plastic.cpp:133
PetscBool is_large_strains
Large strains.
Definition plastic.cpp:118
int geom_order
Order if fixed.
Definition plastic.cpp:142
double sigmaY
Yield stress.
Definition plastic.cpp:128
int ep_order
Order of ep files.
Definition plastic.cpp:141
double cn1
Definition plastic.cpp:137
SmartPetscObj< Mat > H
std::array< double, 3 > scale

◆ createCommonData() [2/8]

MoFEMErrorCode Example::createCommonData ( )
private

◆ createCommonData() [3/8]

MoFEMErrorCode Example::createCommonData ( )
private

◆ createCommonData() [4/8]

MoFEMErrorCode Example::createCommonData ( )
private

◆ createCommonData() [5/8]

MoFEMErrorCode Example::createCommonData ( )
private

◆ createCommonData() [6/8]

MoFEMErrorCode Example::createCommonData ( )
private

◆ createCommonData() [7/8]

MoFEMErrorCode Example::createCommonData ( )
private

◆ createCommonData() [8/8]

MoFEMErrorCode Example::createCommonData ( )
private

◆ deformMesh()

MoFEMErrorCode Example::deformMesh ( PetscInt  step)
inlineprivate
Examples
mesh_reconstruction.cpp.

Definition at line 169 of file mesh_reconstruction.cpp.

169 {
171 auto &mb = currentPtr->field.get_moab();
172 const double before = static_cast<double>(step - 1) / stretchSteps;
173 const double after = static_cast<double>(step) / stretchSteps;
174 std::array<double, 3> old_scale, new_scale;
175 for (int d = 0; d != 3; ++d) {
176 old_scale[d] = 1. + before * (scale[d] - 1.);
177 new_scale[d] = 1. + after * (scale[d] - 1.);
178 }
179 // Relative twist between the original unit sphere's x=-1 and x=+1 planes.
180 const double angle_increment =
181 .5 * twistDegrees * std::acos(-1.) / 180. / stretchSteps;
182 for (auto vertex : currentPtr->entities.subset_by_type(MBVERTEX)) {
183 std::array<double, 3> x;
184 CHKERR mb.get_coords(&vertex, 1, x.data());
185 for (int d = 0; d != 3; ++d)
186 x[d] /= old_scale[d];
187 const double angle = angle_increment * x[0];
188 const double c = std::cos(angle), s = std::sin(angle);
189 const std::array<double, 3> rotated{x[0], c * x[1] - s * x[2],
190 s * x[1] + c * x[2]};
191 for (int d = 0; d != 3; ++d)
192 x[d] = new_scale[d] * rotated[d];
193 CHKERR mb.set_coords(&vertex, 1, x.data());
194 }
196 }
bool angle
Radians between candidate face and target plane.
moab::Interface & mb
const double c
speed of light (cm/ns)
PetscInt stretchSteps

◆ deleteGeometry()

MoFEMErrorCode Example::deleteGeometry ( const Range &  remove)
inlineprivate
Examples
mesh_reconstruction.cpp.

Definition at line 331 of file mesh_reconstruction.cpp.

331 {
333 if (remove.empty())
335 auto &field = currentPtr->field;
336 auto &mb = field.get_moab();
337 CHKERR field.remove_ents(remove, QUIET);
338 Range sets;
339 CHKERR mb.get_entities_by_type(0, MBENTITYSET, sets);
340 for (auto set : sets)
341 CHKERR mb.remove_entities(set, remove);
342 for (int dim = 3; dim >= 0; --dim) {
343 const auto batch = remove.subset_by_dimension(dim);
344 if (!batch.empty())
345 CHKERR mb.delete_entities(batch);
346 }
348 }
ProjectedField & field
@ QUIET

◆ distributeSphere()

MoFEMErrorCode Example::distributeSphere ( )
inlineprivate
Examples
mesh_reconstruction.cpp.

Definition at line 163 of file mesh_reconstruction.cpp.

163 {
167 }
MoFEMErrorCode prepareDistributed(Mesh &mesh, const Range &preserve={})

◆ edgeFlips()

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

[Output results]

[Edge flips]

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

Definition at line 582 of file between_meshes_dg_projection.cpp.

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

◆ generateSphere()

MoFEMErrorCode Example::generateSphere ( )
inlineprivate
Examples
mesh_reconstruction.cpp.

Definition at line 117 of file mesh_reconstruction.cpp.

117 {
119 if (currentPtr->field.get_comm_rank())
122 CHKERR sdf.sdfInit(script);
123 auto cgal = currentPtr->field.getInterface<CGALInterface>();
125 sphere, [&](const auto &point) { return sdf.value(point); }, settings);
126 EntityHandle volume, surface;
127 CHKERR cgal->importMesh(sphere, BitRefLevel().set(0), currentPtr->entities,
128 volume, surface);
129 const int owner = 0;
130 auto *pc =
131 ParallelComm::get_pcomm(&currentPtr->field.get_moab(), MYPCOMM_INDEX);
132 CHKERR currentPtr->field.get_moab().tag_clear_data(
133 pc->part_tag(), currentPtr->entities, &owner);
135 }
CGALInterfaceImpl & owner
#define MYPCOMM_INDEX
default communicator number PCOMM
Definition sdf.py:1
CGALTools::ImplicitMeshSettings settings
std::string script
virtual CGALTools & getTools()=0
void createImplicitMesh(TetMeshData &result, const std::function< double(const std::array< double, 3 > &)> &sdf, const ImplicitMeshSettings &settings) const

◆ 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

◆ 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() [1/2]

static int Example::integrationRule ( int  ,
int  ,
int  order 
)
inlinestaticprivate

Definition at line 138 of file adjoint.cpp.

138{ return 2 * order + 1; }

◆ integrationRule() [2/2]

static int Example::integrationRule ( int  ,
int  ,
int  p_data 
)
inlinestaticprivate

◆ 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 // Solver convergence is cheap to check and carries no magic numbers, so it
247 // stands alone rather than waiting on a "-test_nb"/"-test_name" selector.
248 {
249 PetscReal expected_time;
250 CHKERR TSGetTime(ts, &expected_time);
252 ts, expected_time, {1e-12, 1e-12}, "heat radiation");
253 }
254 CHKERR TSGetTime(ts, &ftime);
255
256 PetscInt steps, snesfails, rejects, nonlinits, linits;
257 CHKERR TSGetStepNumber(ts, &steps);
258 CHKERR TSGetSNESFailures(ts, &snesfails);
259 CHKERR TSGetStepRejections(ts, &rejects);
260 CHKERR TSGetSNESIterations(ts, &nonlinits);
261 CHKERR TSGetKSPIterations(ts, &linits);
262 MOFEM_LOG_C("EXAMPLE", Sev::inform,
263 "steps %d (%d rejected, %d SNES fails), ftime %g, nonlinits "
264 "%d, linits %d",
265 steps, rejects, snesfails, ftime, nonlinits, linits);
266
268}
#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

◆ OPs() [1/2]

MoFEMErrorCode Example::OPs ( )
private

[Boundary condition]

[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, and mofem/tutorials/scl-8_heat_radiation/radiation.cpp.

Definition at line 642 of file plastic.cpp.

642 {
644 auto pip_mng = mField.getInterface<PipelineManager>();
645
646 auto integration_rule_bc = [](int, int, int ao) { return 2 * ao; };
647
648 auto vol_rule = [](int, int, int ao) { return 2 * ao + geom_order - 1; };
649
650 auto add_boundary_ops_lhs_mechanical = [&](auto &pip) {
652
654 pip, {HDIV}, "GEOMETRY");
655 pip.push_back(new OpSetHOWeightsOnSubDim<SPACE_DIM>());
656
657 // Add Natural BCs to LHS
659 pip, mField, "U", Sev::inform);
660
661#ifdef ADD_CONTACT
663 CHKERR
664 ContactOps::opFactoryBoundaryLhs<SPACE_DIM, AT, GAUSS, BoundaryEleOp>(
665 pip, "SIGMA", "U");
666 CHKERR
667 ContactOps::opFactoryBoundaryToDomainLhs<SPACE_DIM, AT, IT, DomainEle>(
668 mField, pip, simple->getDomainFEName(), "SIGMA", "U", "GEOMETRY",
669 vol_rule);
670#endif // ADD_CONTACT
671
673 };
674
675 auto add_boundary_ops_rhs_mechanical = [&](auto &pip) {
677
679 pip, {HDIV}, "GEOMETRY");
680 pip.push_back(new OpSetHOWeightsOnSubDim<SPACE_DIM>());
681
682 // Add Natural BCs to RHS
684 pip, mField, "U", {boost::make_shared<ScaledTimeScale>()}, Sev::inform);
685
686#ifdef ADD_CONTACT
687 CHKERR ContactOps::opFactoryBoundaryRhs<SPACE_DIM, AT, IT, BoundaryEleOp>(
688 pip, "SIGMA", "U");
689#endif // ADD_CONTACT
690
692 };
693
694 auto add_domain_ops_lhs = [this](auto &pip) {
697 pip, {H1, HDIV}, "GEOMETRY");
698
699 if (is_quasi_static == PETSC_FALSE) {
700
701 //! [Only used for dynamics]
704 //! [Only used for dynamics]
705
706 auto get_inertia_and_mass_damping = [this](const double, const double,
707 const double) {
708 auto *pip = mField.getInterface<PipelineManager>();
709 auto &fe_domain_lhs = pip->getDomainLhsFE();
710 return (rho / scale) * fe_domain_lhs->ts_aa +
711 (alpha_damping / scale) * fe_domain_lhs->ts_a;
712 };
713 pip.push_back(new OpMass("U", "U", get_inertia_and_mass_damping));
714 }
715
716 CHKERR PlasticOps::opFactoryDomainLhs<SPACE_DIM, AT, IT, DomainEleOp>(
717 mField, "MAT_PLASTIC", pip, "U", "EP", "TAU");
718
720 };
721
722 auto add_domain_ops_rhs = [this](auto &pip) {
724
726 pip, {H1, HDIV}, "GEOMETRY");
727
729 pip, mField, "U",
730 {boost::make_shared<ScaledTimeScale>("body_force_hist.txt")},
731 Sev::inform);
732
733 // only in case of dynamics
734 if (is_quasi_static == PETSC_FALSE) {
735
736 //! [Only used for dynamics]
739 //! [Only used for dynamics]
740
741 auto mat_acceleration = boost::make_shared<MatrixDouble>();
743 "U", mat_acceleration));
744 pip.push_back(
745 new OpInertiaForce("U", mat_acceleration, [](double, double, double) {
746 return rho / scale;
747 }));
748 if (alpha_damping > 0) {
749 auto mat_velocity = boost::make_shared<MatrixDouble>();
750 pip.push_back(
751 new OpCalculateVectorFieldValuesDot<SPACE_DIM>("U", mat_velocity));
752 pip.push_back(
753 new OpInertiaForce("U", mat_velocity, [](double, double, double) {
754 return alpha_damping / scale;
755 }));
756 }
757 }
758
759 CHKERR PlasticOps::opFactoryDomainRhs<SPACE_DIM, AT, IT, DomainEleOp>(
760 mField, "MAT_PLASTIC", pip, "U", "EP", "TAU");
761
762#ifdef ADD_CONTACT
763 CHKERR ContactOps::opFactoryDomainRhs<SPACE_DIM, AT, IT, DomainEleOp>(
764 pip, "SIGMA", "U");
765#endif // ADD_CONTACT
766
768 };
769
770 CHKERR add_domain_ops_lhs(pip_mng->getOpDomainLhsPipeline());
771 CHKERR add_domain_ops_rhs(pip_mng->getOpDomainRhsPipeline());
772
773 // Boundary
774 CHKERR add_boundary_ops_lhs_mechanical(pip_mng->getOpBoundaryLhsPipeline());
775 CHKERR add_boundary_ops_rhs_mechanical(pip_mng->getOpBoundaryRhsPipeline());
776
777 CHKERR pip_mng->setDomainRhsIntegrationRule(vol_rule);
778 CHKERR pip_mng->setDomainLhsIntegrationRule(vol_rule);
779
780 CHKERR pip_mng->setBoundaryLhsIntegrationRule(integration_rule_bc);
781 CHKERR pip_mng->setBoundaryRhsIntegrationRule(integration_rule_bc);
782
783 auto create_reaction_pipeline = [&](auto &pip) {
786 pip, {H1}, "GEOMETRY");
787 CHKERR PlasticOps::opFactoryDomainReactions<SPACE_DIM, AT, IT, DomainEleOp>(
788 mField, "MAT_PLASTIC", pip, "U", "EP", "TAU");
790 };
791
792 CHKERR pip_mng->setEvaluationIntegrationRule(vol_rule);
793 CHKERR create_reaction_pipeline(pip_mng->getOpEvaluationPipeline());
794 auto &reaction_fe = pip_mng->getEvaluationFE();
795 reaction_fe->postProcessHook =
797
799}
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::LinearForm< GAUSS >::OpBaseTimesVector< 1, SPACE_DIM, SPACE_DIM > OpInertiaForce
FormsIntegrators< DomainEleOp >::Assembly< PETSC >::BiLinearForm< GAUSS >::OpMass< 1, SPACE_DIM > OpMass
[Only used with Hooke equation (linear material model)]
Definition seepage.cpp:56
Class (Function) to calculate residual side diagonal.
Definition Essential.hpp:49
Approximate field values for given petsc vector.
constexpr AssemblyType AT

◆ OPs() [2/2]

MoFEMErrorCode Example::OPs ( )
private

◆ outputResults() [1/11]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [2/11]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [3/11]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [4/11]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [5/11]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [6/11]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [7/11]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [8/11]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [9/11]

MoFEMErrorCode Example::outputResults ( )
private

◆ outputResults() [10/11]

MoFEMErrorCode Example::outputResults ( const int  i)
private

[Solve]

[Postprocess results]

Definition at line 493 of file phase.cpp.

493 {
496
498
499 auto post_proc_fe = boost::make_shared<PostProcEle>(mField);
500 CHKERR AddHOOps<2, 2, 2>::add(post_proc_fe->getOpPtrVector(), {HDIV, L2});
501
502 auto phi_ptr = boost::make_shared<VectorDouble>();
503 auto s_ptr = boost::make_shared<MatrixDouble>();
504 post_proc_fe->getOpPtrVector().push_back(
505 new OpCalculateScalarFieldValues("PHI", phi_ptr));
506 post_proc_fe->getOpPtrVector().push_back(
507 new OpCalculateHVecVectorField<3>("S", s_ptr));
508
510
511 post_proc_fe->getOpPtrVector().push_back(
512
513 new OpPPMap(post_proc_fe->getPostProcMesh(),
514 post_proc_fe->getMapGaussPts(),
515
516 OpPPMap::DataMapVec{{"PHI", phi_ptr}},
517
518 OpPPMap::DataMapMat{{"S", s_ptr}},
519
521
523
524 )
525
526 );
527
528 pipeline_mng->getDomainPostProcFE() = post_proc_fe;
529 CHKERR pipeline_mng->loopFiniteElementsPostProc();
530 CHKERR post_proc_fe->writeFile("out_" + boost::lexical_cast<std::string>(i) +
531 ".h5m");
533}
MoFEMErrorCode loopFiniteElementsPostProc(SmartPetscObj< DM > dm=nullptr)
Iterate postprocessing finite elements.
OpPostProcMapInMoab< SPACE_DIM, SPACE_DIM > OpPPMap
Specialization for double precision scalar field values calculation.
Post post-proc data at points from hash maps.
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() [11/11]

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

[Project results]

[Output results]

Definition at line 525 of file between_meshes_dg_projection.cpp.

525 {
527
528 auto pipeline_mng = mField.getInterface<PipelineManager>();
529
530 auto post_proc_fe = boost::make_shared<PostProcFaceEle>(mField);
532 post_proc_fe->getOpPtrVector(), {H1});
533
534 auto u_ptr = boost::make_shared<VectorDouble>();
535 post_proc_fe->getOpPtrVector().push_back(
537 auto s_ptr = boost::make_shared<VectorDouble>();
538 post_proc_fe->getOpPtrVector().push_back(
540
541 auto grad_u_ptr = boost::make_shared<MatrixDouble>();
542 post_proc_fe->getOpPtrVector().push_back(
544 auto grad_s_ptr = boost::make_shared<MatrixDouble>();
545 post_proc_fe->getOpPtrVector().push_back(
547
548
550
551 post_proc_fe->getOpPtrVector().push_back(
552
553 new OpPPMap(
554 post_proc_fe->getPostProcMesh(), post_proc_fe->getMapGaussPts(),
555
556 OpPPMap::DataMapVec{{FIELD_NAME_U, u_ptr}, {FIELD_NAME_S, s_ptr}},
557
559
560 {"GRAD_" + std::string(FIELD_NAME_U), grad_u_ptr},
561 {"GRAD_" + std::string(FIELD_NAME_S), grad_s_ptr}
562
563 },
564
566
568
569 )
570
571 );
572
573 pipeline_mng->getDomainPostProcFE() = post_proc_fe;
574 CHKERR pipeline_mng->loopFiniteElementsPostProc();
575 CHKERR post_proc_fe->writeFile(file_name);
576
578}
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.

◆ partitionSphere()

MoFEMErrorCode Example::partitionSphere ( )
inlineprivate
Examples
mesh_reconstruction.cpp.

Definition at line 137 of file mesh_reconstruction.cpp.

137 {
139 CHKERR currentPtr->hybrid.prepareUpdate();
140 currentPtr->sharingReady = false;
141 currentPtr->registrationChanged = true;
142 auto comm = currentPtr->field.getInterface<CommInterface>();
144 EntityHandle volume, surface;
145 MoFEMErrorCode error = 0;
146 if (currentPtr->field.get_comm_rank())
147 error = currentPtr->field.getInterface<CGALInterface>()->importMesh(
148 sphere, BitRefLevel().set(0), currentPtr->entities, volume, surface);
149 CHKERR agree(error);
150 const int initial_owner = 0;
151 auto &mb = currentPtr->field.get_moab();
152 auto *pc = ParallelComm::get_pcomm(&mb, MYPCOMM_INDEX);
153 CHKERR mb.tag_clear_data(pc->part_tag(), currentPtr->entities,
154 &initial_owner);
155 CHKERR comm->partitionMesh(currentPtr->entities.subset_by_type(MBTET), 3, 2,
156 currentPtr->field.get_comm_size(), nullptr,
157 nullptr, nullptr, QUIET, false, true);
161 }
MPI_Comm comm
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
MoFEMErrorCode agree(MoFEMErrorCode local_error)
virtual MoFEMErrorCode importMesh(const TetMeshData &data, BitRefLevel bit, Range &entities, EntityHandle &volume, EntityHandle &surface)=0
Managing BitRefLevels.
MoFEMErrorCode broadcastMesh(TetMeshData &data, int root=0)

◆ 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}
virtual int get_comm_rank() const =0

◆ 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 1368 of file adjoint.cpp.

1369 {
1371 auto simple = mField.getInterface<Simple>();
1375 "out_elastic_" + std::to_string(iter) + ".h5m",
1376 {{"ADJOINT", adjoint_vector}}, {}, Sev::noisy);
1378}
PipelineManager::ElementsAndOpsByDim< SPACE_DIM >::DomainEle DomainEle
Domain finite elements.
Definition gradient.cpp:45
PipelineManager::ElementsAndOpsByDim< SPACE_DIM >::BoundaryEle BoundaryEle
Boundary finite elements.
Definition gradient.cpp:47
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:62

◆ 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 674 of file gradient.cpp.

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

◆ prepareDistributed()

MoFEMErrorCode Example::prepareDistributed ( Mesh &  mesh,
const Range &  preserve = {} 
)
inlineprivate
Examples
mesh_reconstruction.cpp.

Definition at line 385 of file mesh_reconstruction.cpp.

385 {}) {
387 auto &field = mesh.field;
388 auto &mb = field.get_moab();
389 auto *pc = ParallelComm::get_pcomm(&mb, MYPCOMM_INDEX);
390 if (!mesh.sharingReady) {
391 CHKERR mesh.hybrid.prepareUpdate();
392 auto retain = [&]() {
394 if (!field.get_comm_rank())
396 const auto cells = mesh.entities.subset_by_type(MBTET);
397 Range owned, vertices, halo;
398 for (auto cell : cells) {
399 int owner;
400 CHKERR mb.tag_get_data(pc->part_tag(), &cell, 1, &owner);
401 if (owner == field.get_comm_rank())
402 owned.insert(cell);
403 }
404 CHKERR mb.get_connectivity(owned, vertices, true);
405 CHKERR mb.get_adjacencies(vertices, 3, false, halo,
406 moab::Interface::UNION);
407 // A reconstructed face inherits its representative parent's owner.
408 // That rank need not own either incident output cell. Keep both cells
409 // for every preferred-owned face as well as the owned-cell halo.
410 Range owned_faces, face_cells;
411 for (auto face : mesh.entities.subset_by_type(MBTRI)) {
412 int owner;
413 CHKERR mb.tag_get_data(pc->part_tag(), &face, 1, &owner);
414 if (owner == field.get_comm_rank())
415 owned_faces.insert(face);
416 }
417 CHKERR mb.get_adjacencies(owned_faces, 3, false, face_cells,
418 moab::Interface::UNION);
419 halo.merge(face_cells);
420 // Select only the active reconstruction, never inactive lineage cells.
421 halo = intersect(halo, cells);
422 for (int dim : {0, 1, 2}) {
424 CHKERR mb.get_adjacencies(halo.subset_by_type(MBTET), dim, false,
425 closure, moab::Interface::UNION);
426 halo.merge(closure);
427 }
428 Range protected_entities = preserve;
429 protected_entities.merge(halo);
430 CHKERR field.getInterface<CommInterface>()->retainPartition(
431 mesh.entities, mesh.activeBit, protected_entities);
432 mesh.entities = halo;
434 };
435 CHKERR agree(retain());
436 CHKERR field.getInterface<BitRefManager>()->setBitRefLevel(
437 mesh.entities, mesh.activeBit, false);
438 CHKERR field.getInterface<CommInterface>()->synchroniseByConnectivity(
439 mesh.entities, 0);
440 mesh.sharingReady = true;
441 }
442 int owned = 0, invalid = 0;
443 for (auto cell : mesh.entities.subset_by_type(MBTET)) {
444 int owner;
445 unsigned char status;
446 CHKERR mb.tag_get_data(pc->part_tag(), &cell, 1, &owner);
447 CHKERR pc->get_pstatus(cell, status);
448 owned += owner == field.get_comm_rank();
449 invalid += owner != field.get_comm_rank() && !(status & PSTATUS_GHOST);
450 }
451 int global_owned, root_cells = mesh.entities.num_of_type(MBTET);
452 CHKERR MPI_Allreduce(&owned, &global_owned, 1, MPI_INT, MPI_SUM,
453 field.get_comm());
454 CHKERR MPI_Bcast(&root_cells, 1, MPI_INT, 0, field.get_comm());
455 auto testing_ptr = field.getInterface<TestingInterface>();
456 const auto zero = TestingInterface::Tolerance{0, 0};
457 CHKERR testing_ptr->checkValue<TestType::SCALAR>(
458 "root retains every owned cell", root_cells, global_owned, zero);
459 CHKERR testing_ptr->checkValue<TestType::SCALAR>(
460 "off-partition residents are ghost cells", invalid, 0, zero);
461 CHKERR pc->check_all_shared_handles();
463 }
std::vector< int > vertices
bool closure
The face (f0, f1, w) closes a corner at f0 or f1.
unsigned char status

◆ 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 258 of file between_meshes_dg_projection.cpp.

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

◆ 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/13]

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, and mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 459 of file dynamic_first_order_con_law.cpp.

459 {
462
466}
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/13]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [3/13]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [4/13]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [5/13]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [6/13]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [7/13]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [8/13]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [9/13]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [10/13]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [11/13]

MoFEMErrorCode Example::readMesh ( )
private

◆ readMesh() [12/13]

MoFEMErrorCode Example::readMesh ( )
private

Read mesh from file and setup meshsets.

◆ readMesh() [13/13]

MoFEMErrorCode Example::readMesh ( )
private

◆ readOptions()

MoFEMErrorCode Example::readOptions ( )
inlineprivate
Examples
mesh_reconstruction.cpp.

Definition at line 76 of file mesh_reconstruction.cpp.

76 {
78 currentPtr->field.getInterface<JsonConfigManager>();
79 char file[PETSC_MAX_PATH_LEN] = "sphere.py";
80 CHKERR PetscOptionsGetString(nullptr, nullptr, "-sdf_file", file,
81 sizeof(file), nullptr);
82 script = file;
83 CHKERR PetscOptionsGetInt(nullptr, nullptr, "-hybrid_order", &hybridOrder,
84 nullptr);
85 if (hybridOrder < 1)
86 SETERRQ(currentPtr->field.get_comm(), MOFEM_INVALID_DATA,
87 "hybrid_order must be positive");
88 CHKERR PetscOptionsGetReal(nullptr, nullptr, "-twist_degrees",
89 &twistDegrees, nullptr);
90 if (!std::isfinite(twistDegrees))
91 SETERRQ(currentPtr->field.get_comm(), MOFEM_INVALID_DATA,
92 "twist_degrees must be finite");
93 CHKERR PetscOptionsGetInt(nullptr, nullptr, "-stretch_steps", &stretchSteps,
94 nullptr);
95 if (stretchSteps < 1)
96 SETERRQ(currentPtr->field.get_comm(), MOFEM_INVALID_DATA,
97 "stretch_steps must be positive");
98 CHKERR PetscOptionsGetReal(nullptr, nullptr, "-cell_size",
99 &settings.cellSize, nullptr);
100 CHKERR PetscOptionsGetReal(nullptr, nullptr, "-facet_size",
101 &settings.facetSize, nullptr);
102 CHKERR PetscOptionsGetReal(nullptr, nullptr, "-facet_distance",
103 &settings.facetDistance, nullptr);
104 CHKERR PetscOptionsGetReal(nullptr, nullptr, "-bounding_radius",
105 &settings.radius, nullptr);
106 for (int d = 0; d != 3; ++d) {
107 const std::string option = std::string("-scale_") + "xyz"[d];
108 CHKERR PetscOptionsGetReal(nullptr, nullptr, option.c_str(), &scale[d],
109 nullptr);
110 if (!std::isfinite(scale[d]) || scale[d] <= 0)
111 SETERRQ(currentPtr->field.get_comm(), MOFEM_INVALID_DATA,
112 "Scaling must be positive");
113 }
115 }
@ MOFEM_INVALID_DATA
Definition definitions.h:36
PetscInt hybridOrder

◆ rebuildHybrid()

MoFEMErrorCode Example::rebuildHybrid ( Mesh &  mesh,
const std::string &  tag 
)
inlineprivate
Examples
mesh_reconstruction.cpp.

Definition at line 465 of file mesh_reconstruction.cpp.

465 {
467 auto &field = mesh.field;
468 auto &mb = field.get_moab();
469 std::vector<std::array<int, 3>> keys;
470 auto find_interior = [&]() {
472 if (field.get_comm_rank())
474 const auto tets = mesh.entities.subset_by_type(MBTET);
476 CHKERR mb.get_adjacencies(tets, 2, true, faces, moab::Interface::UNION);
477 Skinner skinner(&mb);
478 CHKERR skinner.find_skin(0, tets, false, skin);
479 for (auto face : subtract(faces, skin)) {
480 const EntityHandle *conn;
481 int n;
482 std::array<int, 3> ids;
483 CHKERR mb.get_connectivity(face, conn, n, true);
484 CHKERR mb.tag_get_data(mb.globalId_tag(), conn, 3, ids.data());
485 std::sort(ids.begin(), ids.end());
486 keys.push_back(ids);
487 }
488 if (keys.size() > static_cast<size_t>(INT_MAX) / 3)
489 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_IMPLEMENTED,
490 "Skeleton exceeds MPI count range");
492 };
493 CHKERR agree(find_interior());
494 int count = keys.size();
495 CHKERR MPI_Bcast(&count, 1, MPI_INT, 0, field.get_comm());
496 keys.resize(count);
497 CHKERR MPI_Bcast(keys.data(), 3 * count, MPI_INT, 0, field.get_comm());
498 const std::set<std::array<int, 3>> interior(keys.begin(), keys.end());
499 Range skeleton;
500 auto select_skeleton = [&]() {
502 for (auto face : mesh.entities.subset_by_type(MBTRI)) {
503 const EntityHandle *conn;
504 int n;
505 std::array<int, 3> ids;
506 CHKERR mb.get_connectivity(face, conn, n, true);
507 CHKERR mb.tag_get_data(mb.globalId_tag(), conn, 3, ids.data());
508 std::sort(ids.begin(), ids.end());
509 if (interior.count(ids))
510 skeleton.insert(face);
511 }
513 };
514 CHKERR agree(select_skeleton());
515 CHKERR mesh.hybrid.build(mesh.entities, skeleton, mesh.activeBit,
516 hybridOrder, mesh.registrationChanged);
517 CHKERR mesh.hybrid.validate(mesh.entities, skeleton, hybridOrder, tag);
518 mesh.registrationChanged = false;
520 }
std::vector< std::array< EntityHandle, 3 > > faces
Sorted keys.
Tag tag
@ MOFEM_NOT_IMPLEMENTED
Definition definitions.h:32

◆ reconstructMesh()

MoFEMErrorCode Example::reconstructMesh ( PetscInt  step)
inlineprivate
Examples
mesh_reconstruction.cpp.

Definition at line 253 of file mesh_reconstruction.cpp.

253 {
255 CHKERR currentPtr->hybrid.prepareUpdate();
256 auto source_comm = currentPtr->field.getInterface<CommInterface>();
257 auto output_comm = currentPtr->field.getInterface<CommInterface>();
258 CHKERR source_comm->gatherMesh(currentPtr->entities.subset_by_type(MBTET),
259 sourceMesh);
261 CHKERR output_comm->broadcastMesh(sourceMesh);
262 CHKERR output_comm->broadcastMesh(reconstructed);
263
264 // Keep surviving handles and the parent DM; discard only older lineage.
266 currentPtr->activeBit = BitRefLevel().set(1);
267 currentPtr->sharingReady = false;
268 currentPtr->registrationChanged = true;
269 auto cgal = currentPtr->field.getInterface<CGALInterface>();
270 EntityHandle volume, surface;
271 CHKERR agree(cgal->importMesh(sourceMesh, BitRefLevel().set(0),
272 sourceLineage, volume, surface));
273 CHKERR agree(cgal->importMesh(reconstructed, BitRefLevel().set(1),
274 currentPtr->entities, volume, surface));
276 currentPtr->field.getInterface<MeshRefinement>()->setTopologicalParents(
277 sourceLineage, currentPtr->entities, BitRefLevel().set(1)));
279 output_comm->inheritPartitions(sourceLineage, currentPtr->entities));
284 }
TetMeshData reconstructed
TetMeshData sourceMesh
MoFEMErrorCode reconstructOnRoot(PetscInt step)
MoFEMErrorCode synchroniseMesh()
MoFEMErrorCode resetMeshForImport()
MoFEMErrorCode inheritPartitions(const Range &source_entities, const Range &child_entities)
MoFEMErrorCode gatherMesh(const Range &cells, TetMeshData &data, int root=0)
Mesh refinement interface.
MoFEMErrorCode setTopologicalParents(const Range &source_entities, const Range &child_entities, const BitRefLevel &output_bit, int verb=QUIET, const std::map< EntityHandle, EntityHandle > &moved_vertices={})

◆ reconstructOnRoot()

MoFEMErrorCode Example::reconstructOnRoot ( PetscInt  step)
inlineprivate
Examples
mesh_reconstruction.cpp.

Definition at line 229 of file mesh_reconstruction.cpp.

229 {
231 if (!currentPtr->field.get_comm_rank()) {
232 currentPtr->field.getInterface<CGALInterface>()
233 ->getTools()
235 auto changes = [](const auto &before, const auto &after) {
236 const auto retained =
237 std::count_if(before.begin(), before.end(),
238 [&](const auto &key) { return after.count(key); });
239 return std::make_pair(before.size() - retained,
240 after.size() - retained);
241 };
242 const auto cells = changes(cellKeys(sourceMesh), cellKeys(reconstructed));
243 const auto skin = changes(skinKeys(sourceMesh), skinKeys(reconstructed));
244 MOFEM_LOG("WORLD", Sev::inform)
245 << "Step " << step << "/" << stretchSteps << ": tetrahedra removed "
246 << cells.first << ", added " << cells.second
247 << "; skin triangles removed " << skin.first << ", added "
248 << skin.second;
249 }
251 }
ParentChildKey key
static auto skinKeys(const TetMeshData &mesh)
static auto cellKeys(const TetMeshData &mesh)
virtual MoFEMErrorCode reconstructDelaunay(const Range &source_entities, BitRefLevel output_bit, Range &entities, CGALTools::ReconstructionMethod method=CGALTools::ReconstructionMethod::PointDelaunay)=0

◆ 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 770 of file between_meshes_dg_projection.cpp.

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

◆ resetMeshForImport()

MoFEMErrorCode Example::resetMeshForImport ( )
inlineprivate
Examples
mesh_reconstruction.cpp.

Definition at line 288 of file mesh_reconstruction.cpp.

288 {
290 auto &field = currentPtr->field;
291 auto &mb = field.get_moab();
292 Range geometry;
293 CHKERR mb.get_entities_by_handle(0, geometry);
294 geometry = subtract(geometry, geometry.subset_by_type(MBENTITYSET));
295 // Surviving handles keep their field DOFs. Obsolete lineage is removed
296 // below; the hybrid update prunes field/FE membership to the active set.
297 // BitRefManager's setters add bits. Clear the previous active selector
298 // before assigning this step's source/output bits to surviving handles.
299 const BitRefLevel no_bits;
300 CHKERR mb.tag_clear_data(field.get_basic_entity_data_ptr()->th_RefBitLevel,
301 geometry, &no_bits);
302 const EntityHandle no_parent = 0;
303 CHKERR mb.tag_clear_data(
304 field.getInterface<BitRefManager>()->get_th_RefParentHandle(), geometry,
305 &no_parent);
306 auto *pc = ParallelComm::get_pcomm(&mb, MYPCOMM_INDEX);
307 const unsigned char unshared = 0;
308 CHKERR mb.tag_clear_data(pc->pstatus_tag(), geometry, &unshared);
309 for (auto tag : {pc->sharedp_tag(), pc->sharedps_tag(), pc->sharedh_tag(),
310 pc->sharedhs_tag()}) {
311 const auto error = mb.tag_delete_data(tag, geometry);
312 if (error != MB_SUCCESS && error != MB_TAG_NOT_FOUND)
313 CHKERR error;
314 }
315 Range sets = pc->interface_sets();
316 sets.merge(pc->partition_sets());
317 CHKERR mb.delete_entities(sets);
318 // Recreating this registry clears MOAB's private shared-entity table too.
319 // The mesh, MoFEM core and parent DM remain alive.
320 delete pc;
321 int id = -1;
322 pc = new ParallelComm(&mb, field.get_comm(), &id);
323 if (id != MYPCOMM_INDEX)
324 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
325 "Unexpected ParallelComm index after resetting sharing");
326 CHKERR deleteGeometry(subtract(geometry, currentPtr->entities));
327 sourceLineage.clear();
329 }
MoFEMErrorCode deleteGeometry(const Range &remove)
Tag get_th_RefParentHandle() const

◆ 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 802 of file between_meshes_dg_projection.cpp.

802 {
804 simpleInterface->getBitRefLevel() = child_bit;
807}
MoFEMErrorCode reSetUp(bool only_dm=false)
Rebuild internal MoFEM data structures.
Definition Simple.cpp:764
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}
int w
Free node.
const double v
phase velocity of light in medium (cm/ns)
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

◆ run()

MoFEMErrorCode Example::run ( )
inline
Examples
mesh_reconstruction.cpp.

Definition at line 11 of file mesh_reconstruction.cpp.

11 {
13 currentPtr = std::make_unique<Mesh>();
15
18 CHKERR saveStage(*currentPtr, "01_sphere", false);
19
21 CHKERR saveStage(*currentPtr, "02_partitioned", false);
22
24 CHKERR saveStage(*currentPtr, "03_distributed", true);
25
26 for (PetscInt step = 1; step <= stretchSteps; ++step) {
27 CHKERR deformMesh(step);
28 CHKERR saveStage(*currentPtr, "04_ellipsoid", true, step);
29
31 CHKERR saveStage(*currentPtr, "05_reconstructed", false, step);
32
34 CHKERR saveStage(*currentPtr, "06_synchronised", true, step);
35 }
36 MOFEM_LOG("WORLD", Sev::inform)
37 << "Completed " << stretchSteps << " stretch/reconstruction steps";
39 }
MoFEMErrorCode generateSphere()
MoFEMErrorCode distributeSphere()
MoFEMErrorCode reconstructMesh(PetscInt step)
MoFEMErrorCode saveStage(Mesh &mesh, const char *stage, bool distributed, PetscInt step=0)
MoFEMErrorCode readOptions()
MoFEMErrorCode deformMesh(PetscInt step)
MoFEMErrorCode partitionSphere()

◆ runProblem() [1/16]

MoFEMErrorCode Example::runProblem ( )

[Run problem]

[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, and mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 247 of file plastic.cpp.

247 {
251 CHKERR bC();
252 CHKERR OPs();
253 PetscBool test_ops = PETSC_FALSE;
254 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-test_operators", &test_ops,
255 PETSC_NULLPTR);
256 if (test_ops == PETSC_FALSE) {
257 CHKERR tsSolve();
258 } else {
260 }
262}
MoFEMErrorCode testOperators()
[Solve]
Definition plastic.cpp:1486
MoFEMErrorCode tsSolve()
Definition plastic.cpp:826
MoFEMErrorCode createCommonData()
[Set up problem]
Definition plastic.cpp:471
MoFEMErrorCode OPs()
[Boundary condition]
Definition plastic.cpp:642
MoFEMErrorCode setupProblem()
[Run problem]
Definition plastic.cpp:266
MoFEMErrorCode bC()
[Create common data]
Definition plastic.cpp:598

◆ runProblem() [2/16]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [3/16]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [4/16]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [5/16]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [6/16]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [7/16]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [8/16]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [9/16]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [10/16]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [11/16]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [12/16]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [13/16]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [14/16]

MoFEMErrorCode Example::runProblem ( )

◆ runProblem() [15/16]

MoFEMErrorCode Example::runProblem ( )

Main driver function for the optimization process.

◆ runProblem() [16/16]

MoFEMErrorCode Example::runProblem ( )

Main driver function for the optimization process.

◆ saveStage()

MoFEMErrorCode Example::saveStage ( Mesh &  mesh,
const char *  stage,
bool  distributed,
PetscInt  step = 0 
)
inlineprivate
Examples
mesh_reconstruction.cpp.

Definition at line 522 of file mesh_reconstruction.cpp.

523 {
525 auto &m_field = mesh.field;
526 const auto &entities = mesh.entities;
527 std::ostringstream name;
528 name << stage;
529 if (step)
530 name << "_" << std::setw(4) << std::setfill('0') << step;
531 name << ".h5m";
532 const auto file = name.str();
534 auto write = [&]() {
536 if (!distributed && m_field.get_comm_rank())
538 auto &mb = m_field.get_moab();
539 EntityHandle selection;
540 CHKERR mb.create_meshset(MESHSET_SET, selection);
541 CHKERR mb.add_entities(selection, entities);
542 Tag summary_tag;
543 CHKERR mb.tag_get_handle("FUN3_HYBRID_COUNTS", 5, MB_TYPE_DOUBLE,
544 summary_tag, MB_TAG_SPARSE | MB_TAG_CREAT);
545 CHKERR mb.tag_set_data(summary_tag, &selection, 1,
546 mesh.hybrid.summary.data());
547 auto pc = ParallelComm::get_pcomm(&mb, MYPCOMM_INDEX);
548 auto tag = pc->part_tag();
549 Range parts;
550 CHKERR mb.get_entities_by_type_and_tag(0, MBENTITYSET, &tag, nullptr, 1,
551 parts);
552 CHKERR mb.add_entities(selection, parts);
553 CHKERR mb.write_file(file.c_str(), "MOAB",
554 distributed ? "PARALLEL=WRITE_PART" : "", &selection,
555 1);
556 CHKERR mb.delete_entities(&selection, 1);
558 };
559 CHKERR agree(write());
560 MOFEM_LOG("WORLD", Sev::inform) << "Saved " << file;
562 }
std::string name
MoFEMErrorCode rebuildHybrid(Mesh &mesh, const std::string &tag)

◆ 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:264
MoFEMErrorCode setFieldOrder(const std::string field_name, const int order, const Range *ents=NULL)
Set field order.
Definition Simple.cpp:578
MoFEMErrorCode setUp(const PetscBool is_partitioned=PETSC_TRUE)
Setup problem.
Definition Simple.cpp:738

◆ 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 652 of file adjoint.cpp.

652 {
655
656 // Create adjoint data manager and field
657 auto create_adjoint_dm = [&]() {
658 auto adjoint_dm = createDM(mField.get_comm(), "DMMOFEM");
659
660 auto add_field = [&]() {
662 CHKERR mField.add_field("ADJOINT_FIELD", H1, base, SPACE_DIM);
664 "ADJOINT_FIELD");
665 for (auto tt = MBEDGE; tt <= moab::CN::TypeDimensionMap[SPACE_DIM].second;
666 ++tt)
667 CHKERR mField.set_field_order(simple->getMeshset(), tt, "ADJOINT_FIELD",
668 fieldOrder);
670 "ADJOINT_FIELD", 1);
673 };
674
675 auto add_adjoint_fe_impl = [&]() {
677 CHKERR mField.add_finite_element("ADJOINT_DOMAIN_FE");
679 "ADJOINT_FIELD");
681 "ADJOINT_FIELD");
683 "ADJOINT_FIELD");
685 "GEOMETRY");
687 simple->getMeshset(), SPACE_DIM, "ADJOINT_DOMAIN_FE");
688 CHKERR mField.build_finite_elements("ADJOINT_DOMAIN_FE");
689
690 CHKERR mField.add_finite_element("ADJOINT_BOUNDARY_FE");
692 "ADJOINT_FIELD");
694 "ADJOINT_FIELD");
696 "ADJOINT_FIELD");
698 "GEOMETRY");
699
700 auto block_name = "OPTIMISE";
701 auto mesh_mng = mField.getInterface<MeshsetsManager>();
702 auto bcs = mesh_mng->getCubitMeshsetPtr(
703
704 std::regex((boost::format("%s(.*)") % block_name).str())
705
706 );
707
708 for (auto bc : bcs) {
710 bc->getMeshset(), SPACE_DIM - 1, "ADJOINT_BOUNDARY_FE");
711 }
712
713 CHKERR mField.build_finite_elements("ADJOINT_BOUNDARY_FE");
714
717
719 };
720
721 auto set_adjoint_dm_imp = [&]() {
723 CHKERR DMMoFEMCreateMoFEM(adjoint_dm, &mField, "ADJOINT",
726 CHKERR DMMoFEMSetDestroyProblem(adjoint_dm, PETSC_TRUE);
727 CHKERR DMSetFromOptions(adjoint_dm);
728 CHKERR DMMoFEMAddElement(adjoint_dm, "ADJOINT_DOMAIN_FE");
729 CHKERR DMMoFEMAddElement(adjoint_dm, "ADJOINT_BOUNDARY_FE");
730 CHKERR DMMoFEMSetSquareProblem(adjoint_dm, PETSC_TRUE);
731 CHKERR DMMoFEMSetIsPartitioned(adjoint_dm, PETSC_TRUE);
732 mField.getInterface<ProblemsManager>()->buildProblemFromFields =
733 PETSC_TRUE;
734 CHKERR DMSetUp(adjoint_dm);
735 mField.getInterface<ProblemsManager>()->buildProblemFromFields =
736 PETSC_FALSE;
738 };
739
740 CHK_THROW_MESSAGE(add_field(), "add adjoint field");
741 CHK_THROW_MESSAGE(add_adjoint_fe_impl(), "add adjoint fe");
742 CHK_THROW_MESSAGE(set_adjoint_dm_imp(), "set adjoint dm");
743
744 return adjoint_dm;
745 };
746
747 adjointDM = create_adjoint_dm();
748
750}
PetscErrorCode DMMoFEMSetIsPartitioned(DM dm, PetscBool is_partitioned)
Definition DMMoFEM.cpp:1199
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:143
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:463
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/15]

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, and mofem/tutorials/vec-7_shape_optimisation/adjoint.cpp.

Definition at line 266 of file plastic.cpp.

266 {
269
270 Range domain_ents;
271 CHKERR mField.get_moab().get_entities_by_dimension(0, SPACE_DIM, domain_ents,
272 true);
273 auto get_ents_by_dim = [&](const auto dim) {
274 if (dim == SPACE_DIM) {
275 return domain_ents;
276 } else {
277 Range ents;
278 if (dim == 0)
279 CHKERR mField.get_moab().get_connectivity(domain_ents, ents, true);
280 else
281 CHKERR mField.get_moab().get_entities_by_dimension(0, dim, ents, true);
282 return ents;
283 }
284 };
285
286 auto get_base = [&]() {
287 auto domain_ents = get_ents_by_dim(SPACE_DIM);
288 if (domain_ents.empty())
289 CHK_THROW_MESSAGE(MOFEM_NOT_FOUND, "Empty mesh");
290 const auto type = type_from_handle(domain_ents[0]);
291 switch (type) {
292 case MBQUAD:
294 case MBHEX:
296 case MBTRI:
298 case MBTET:
300 default:
301 CHK_THROW_MESSAGE(MOFEM_NOT_FOUND, "Element type not handled");
302 }
303 return NOBASE;
304 };
305
306 const auto base = get_base();
307 MOFEM_LOG("PLASTICITY", Sev::inform)
308 << "Base " << ApproximationBaseNames[base];
309
312 CHKERR simple->addDomainField("TAU", L2, base, 1);
314
315 CHKERR simple->addDataField("GEOMETRY", H1, base, SPACE_DIM);
316
317 PetscBool order_edge = PETSC_FALSE;
318 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-order_edge", &order_edge,
319 PETSC_NULLPTR);
320 PetscBool order_face = PETSC_FALSE;
321 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-order_face", &order_face,
322 PETSC_NULLPTR);
323 PetscBool order_volume = PETSC_FALSE;
324 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-order_volume", &order_volume,
325 PETSC_NULLPTR);
326
327 if (order_edge || order_face || order_volume) {
328
329 MOFEM_LOG("PLASTICITY", Sev::inform) << "Order edge " << order_edge
330 ? "true"
331 : "false";
332 MOFEM_LOG("PLASTICITY", Sev::inform) << "Order face " << order_face
333 ? "true"
334 : "false";
335 MOFEM_LOG("PLASTICITY", Sev::inform) << "Order volume " << order_volume
336 ? "true"
337 : "false";
338
339 auto ents = get_ents_by_dim(0);
340 if (order_edge)
341 ents.merge(get_ents_by_dim(1));
342 if (order_face)
343 ents.merge(get_ents_by_dim(2));
344 if (order_volume)
345 ents.merge(get_ents_by_dim(3));
346 CHKERR simple->setFieldOrder("U", order, &ents);
347 } else {
349 }
352
354
355#ifdef ADD_CONTACT
357 SPACE_DIM);
359 SPACE_DIM);
360
361 auto get_skin = [&]() {
362 Range body_ents;
363 CHKERR mField.get_moab().get_entities_by_dimension(0, SPACE_DIM, body_ents);
364 Skinner skin(&mField.get_moab());
365 Range skin_ents;
366 CHKERR skin.find_skin(0, body_ents, false, skin_ents);
367 return skin_ents;
368 };
369
370 auto filter_blocks = [&](auto skin) {
371 bool is_contact_block = true;
372 Range contact_range;
373 for (auto m :
374 mField.getInterface<MeshsetsManager>()->getCubitMeshsetPtr(std::regex(
375
376 (boost::format("%s(.*)") % "CONTACT").str()
377
378 ))
379
380 ) {
381 is_contact_block =
382 true; ///< blocs interation is collective, so that is set irrespective
383 ///< if there are entities in given rank or not in the block
384 MOFEM_LOG("CONTACT", Sev::inform)
385 << "Find contact block set: " << m->getName();
386 auto meshset = m->getMeshset();
387 Range contact_meshset_range;
388 CHKERR mField.get_moab().get_entities_by_dimension(
389 meshset, SPACE_DIM - 1, contact_meshset_range, true);
390
391 CHKERR mField.getInterface<CommInterface>()->synchroniseEntities(
392 contact_meshset_range);
393 contact_range.merge(contact_meshset_range);
394 }
395 if (is_contact_block) {
396 MOFEM_LOG("SYNC", Sev::inform)
397 << "Nb entities in contact surface: " << contact_range.size();
399 skin = intersect(skin, contact_range);
400 }
401 return skin;
402 };
403
404 auto filter_true_skin = [&](auto skin) {
405 Range boundary_ents;
406 ParallelComm *pcomm =
407 ParallelComm::get_pcomm(&mField.get_moab(), MYPCOMM_INDEX);
408 CHKERR pcomm->filter_pstatus(skin, PSTATUS_SHARED | PSTATUS_MULTISHARED,
409 PSTATUS_NOT, -1, &boundary_ents);
410 return boundary_ents;
411 };
412
413 auto boundary_ents = filter_true_skin(filter_blocks(get_skin()));
414 CHKERR simple->setFieldOrder("SIGMA", 0);
415 CHKERR simple->setFieldOrder("SIGMA", order - 1, &boundary_ents);
416#endif
417
420
421 auto project_ho_geometry = [&]() {
422 Projection10NodeCoordsOnField ent_method(mField, "GEOMETRY");
423 return mField.loop_dofs("GEOMETRY", ent_method);
424 };
425 PetscBool project_geometry = PETSC_TRUE;
426 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-project_geometry",
427 &project_geometry, PETSC_NULLPTR);
428 if (project_geometry) {
429 CHKERR project_ho_geometry();
430 }
431
432 auto get_volume = [&]() {
433 using VolOp = DomainEle::UserDataOperator;
434 auto *op_ptr = new VolOp(NOSPACE, VolOp::OPSPACE);
435 std::array<double, 2> volume_and_count;
436 op_ptr->doWorkRhsHook = [&](DataOperator *base_op_ptr, int side,
437 EntityType type,
440 auto op_ptr = static_cast<VolOp *>(base_op_ptr);
441 volume_and_count[VOL] += op_ptr->getMeasure();
442 volume_and_count[COUNT] += 1;
443 // in necessary at integration over Gauss points.
445 };
446 volume_and_count = {0, 0};
447 auto fe = boost::make_shared<DomainEle>(mField);
448 fe->getOpPtrVector().push_back(op_ptr);
449
450 auto dm = simple->getDM();
453 "cac volume");
454 std::array<double, 2> tot_volume_and_count;
455 MPI_Allreduce(volume_and_count.data(), tot_volume_and_count.data(),
456 volume_and_count.size(), MPI_DOUBLE, MPI_SUM,
457 mField.get_comm());
458 return tot_volume_and_count;
459 };
460
461 meshVolumeAndCount = get_volume();
462 MOFEM_LOG("PLASTICITY", Sev::inform)
463 << "Mesh volume " << meshVolumeAndCount[VOL] << " nb. of elements "
465
467}
#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
constexpr auto size_symm
Definition plastic.cpp:42
constexpr FieldSpace CONTACT_SPACE
Definition plastic.cpp:52
static std::array< double, 2 > meshVolumeAndCount
Definition plastic.cpp:216
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:358
MoFEMErrorCode addFieldToEmptyFieldBlocks(const std::string row_field, const std::string col_field) const
Add empty block to problem.
Definition Simple.cpp:837
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:396
static auto get_skin(MoFEM::Interface &m_field, Range body_ents)
static auto filter_true_skin(MoFEM::Interface &m_field, Range &&skin)
VolEle::UserDataOperator VolOp

◆ setupProblem() [2/15]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [3/15]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [4/15]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [5/15]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [6/15]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [7/15]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [8/15]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [9/15]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [10/15]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [11/15]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [12/15]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [13/15]

MoFEMErrorCode Example::setupProblem ( )
private

◆ setupProblem() [14/15]

MoFEMErrorCode Example::setupProblem ( )
private

Setup fields, approximation spaces and DOFs.

◆ setupProblem() [15/15]

MoFEMErrorCode Example::setupProblem ( )
private

◆ skinKeys()

static auto Example::skinKeys ( const TetMeshData &  mesh)
inlinestaticprivate
Examples
mesh_reconstruction.cpp.

Definition at line 210 of file mesh_reconstruction.cpp.

210 {
211 std::map<std::array<int, 3>, int> incidence;
212 for (const auto &cell : mesh.cells)
213 for (int opposite = 0; opposite != 4; ++opposite) {
214 std::array<int, 3> face;
215 int n = 0;
216 for (int d = 0; d != 4; ++d)
217 if (d != opposite)
218 face[n++] = mesh.vertexIds[cell[d]];
219 std::sort(face.begin(), face.end());
220 ++incidence[face];
221 }
222 std::set<std::array<int, 3>> skin;
223 for (const auto &[face, count] : incidence)
224 if (count == 1)
225 skin.insert(face);
226 return skin;
227 }

◆ 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 1240 of file adjoint.cpp.

1240 {
1242 auto simple = mField.getInterface<Simple>();
1243 auto dm = simple->getDM();
1244 auto f = createDMVector(dm);
1245 auto d = vectorDuplicate(f);
1246 CHKERR VecZeroEntries(d);
1247 CHKERR DMoFEMMeshToLocalVector(dm, d, INSERT_VALUES, SCATTER_REVERSE);
1248
1249 auto set_essential_bc = [&]() {
1251 // This is low level pushing finite elements (pipelines) to solver
1252
1253 auto ksp_ctx_ptr = getDMKspCtx(dm);
1254 auto pre_proc_rhs = boost::make_shared<FEMethod>();
1255 auto post_proc_rhs = boost::make_shared<FEMethod>();
1256 auto post_proc_lhs = boost::make_shared<FEMethod>();
1257
1258 auto get_pre_proc_hook = [&]() {
1260 {});
1261 };
1262 pre_proc_rhs->preProcessHook = get_pre_proc_hook();
1263
1264 auto get_post_proc_hook_rhs = [this, post_proc_rhs]() {
1266
1268 post_proc_rhs, 1.)();
1270 };
1271
1272 auto get_post_proc_hook_lhs = [this, post_proc_lhs]() {
1274
1276 post_proc_lhs, 1.)();
1278 };
1279
1280 post_proc_rhs->postProcessHook = get_post_proc_hook_rhs;
1281 post_proc_lhs->postProcessHook = get_post_proc_hook_lhs;
1282
1283 ksp_ctx_ptr->getPreProcComputeRhs().push_front(pre_proc_rhs);
1284 ksp_ctx_ptr->getPostProcComputeRhs().push_back(post_proc_rhs);
1285 ksp_ctx_ptr->getPostProcSetOperators().push_back(post_proc_lhs);
1287 };
1288
1289 auto setup_and_solve = [&](auto solver) {
1291 BOOST_LOG_SCOPED_THREAD_ATTR("Timeline", attrs::timer());
1292 MOFEM_LOG("TIMER", Sev::noisy) << "KSPSetUp";
1293 CHKERR KSPSetUp(solver);
1294 MOFEM_LOG("TIMER", Sev::noisy) << "KSPSetUp <= Done";
1295 MOFEM_LOG("TIMER", Sev::noisy) << "KSPSolve";
1296 CHKERR KSPSolve(solver, f, d);
1297 MOFEM_LOG("TIMER", Sev::noisy) << "KSPSolve <= Done";
1299 };
1300
1301 MOFEM_LOG_CHANNEL("TIMER");
1302 MOFEM_LOG_TAG("TIMER", "timer");
1303
1304 CHKERR set_essential_bc();
1305 CHKERR setup_and_solve(kspElastic);
1306
1307 CHKERR VecGhostUpdateBegin(d, INSERT_VALUES, SCATTER_FORWARD);
1308 CHKERR VecGhostUpdateEnd(d, INSERT_VALUES, SCATTER_FORWARD);
1309 CHKERR DMoFEMMeshToLocalVector(dm, d, INSERT_VALUES, SCATTER_REVERSE);
1310
1311 auto evaluate_field_at_the_point = [&]() {
1313
1314 int coords_dim = 3;
1315 std::array<double, 3> field_eval_coords{0.0, 0.0, 0.0};
1316 PetscBool do_eval_field = PETSC_FALSE;
1317 CHKERR PetscOptionsGetRealArray(NULL, NULL, "-field_eval_coords",
1318 field_eval_coords.data(), &coords_dim,
1319 &do_eval_field);
1320
1321 if (do_eval_field) {
1322
1323 auto vector_field_ptr = boost::make_shared<MatrixDouble>();
1324 auto field_eval_data =
1325 mField.getInterface<FieldEvaluatorInterface>()->getData<DomainEle>();
1326
1328 ->buildTree<SPACE_DIM>(field_eval_data, simple->getDomainFEName());
1329
1330 field_eval_data->setEvalPoints(field_eval_coords.data(), 1);
1331 auto no_rule = [](int, int, int) { return -1; };
1332 auto field_eval_fe_ptr = field_eval_data->feMethodPtr;
1333 field_eval_fe_ptr->getRuleHook = no_rule;
1334
1335 field_eval_fe_ptr->getOpPtrVector().push_back(
1336 new OpCalculateVectorFieldValues<SPACE_DIM>("U", vector_field_ptr));
1337
1339 ->evalFEAtThePoint<SPACE_DIM>(
1340 field_eval_coords.data(), 1e-12, simple->getProblemName(),
1341 simple->getDomainFEName(), field_eval_data,
1343 QUIET);
1344
1345 if (vector_field_ptr->size1()) {
1346 auto t_disp = getFTensor1FromMat<SPACE_DIM>(*vector_field_ptr);
1347 if constexpr (SPACE_DIM == 2)
1348 MOFEM_LOG("FieldEvaluator", Sev::inform)
1349 << "U_X: " << t_disp(0) << " U_Y: " << t_disp(1);
1350 else
1351 MOFEM_LOG("FieldEvaluator", Sev::inform)
1352 << "U_X: " << t_disp(0) << " U_Y: " << t_disp(1)
1353 << " U_Z: " << t_disp(2);
1354 }
1355
1357 }
1359 };
1360
1361 CHKERR evaluate_field_at_the_point();
1362
1364}
#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:1293
PetscBool do_eval_field
Evaluate field.
Definition plastic.cpp:120
Class (Function) to enforce essential constrains on the left hand side diagonal.
Definition Essential.hpp:33

◆ solveElastic() [2/2]

MoFEMErrorCode Example::solveElastic ( )
private

Solve forward elastic problem.

◆ solveSystem() [1/11]

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, and mofem/tutorials/vec-4_shallow_wave/shallow_wave.cpp.

Definition at line 873 of file dynamic_first_order_con_law.cpp.

873 {
875 auto *simple = mField.getInterface<Simple>();
876 auto *pipeline_mng = mField.getInterface<PipelineManager>();
877
878 auto dm = simple->getDM();
879
880 auto calculate_stress_ops = [&](auto &pip) {
882
883 auto v_ptr = boost::make_shared<MatrixDouble>();
884 pip.push_back(new OpCalculateVectorFieldValues<SPACE_DIM>("V", v_ptr));
885 auto X_ptr = boost::make_shared<MatrixDouble>();
886 pip.push_back(
887 new OpCalculateVectorFieldValues<SPACE_DIM>("GEOMETRY", X_ptr));
888
889 auto x_ptr = boost::make_shared<MatrixDouble>();
890 pip.push_back(new OpCalculateVectorFieldValues<SPACE_DIM>("x_1", x_ptr));
891
892 // Calculate unknown F
893 auto mat_H_tensor_ptr = boost::make_shared<MatrixDouble>();
895 "F", mat_H_tensor_ptr));
896
897 auto u_ptr = boost::make_shared<MatrixDouble>();
898 pip.push_back(new OpCalculateDisplacement<SPACE_DIM>(x_ptr, X_ptr, u_ptr));
899 // Calculate P
900
901 auto mat_F_ptr = boost::make_shared<MatrixDouble>();
903 mat_F_ptr, mat_H_tensor_ptr));
904
905 PetscBool is_linear_elasticity = PETSC_TRUE;
906 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-is_linear_elasticity",
907 &is_linear_elasticity, PETSC_NULLPTR);
908
909 auto mat_P_ptr = boost::make_shared<MatrixDouble>();
910 if (is_linear_elasticity) {
913 mat_F_ptr));
914 } else {
915 auto inv_F = boost::make_shared<MatrixDouble>();
916 auto det_ptr = boost::make_shared<VectorDouble>();
917
918 pip.push_back(new OpInvertMatrix<SPACE_DIM>(mat_F_ptr, det_ptr, inv_F));
919
922 mat_F_ptr, inv_F, det_ptr));
923 }
924
925 auto mat_v_grad_ptr = boost::make_shared<MatrixDouble>();
927 "V", mat_v_grad_ptr));
928
929 return boost::make_tuple(v_ptr, X_ptr, x_ptr, mat_P_ptr, mat_F_ptr, u_ptr);
930 };
931
932 auto post_proc_boundary = [&]() {
933 auto boundary_post_proc_fe = boost::make_shared<PostProcFaceEle>(mField);
934
936 boundary_post_proc_fe->getOpPtrVector(), {}, "GEOMETRY");
937 auto op_loop_side =
939 // push ops to side element, through op_loop_side operator
940 auto [boundary_v_ptr, boundary_X_ptr, boundary_x_ptr, boundary_mat_P_ptr,
941 boundary_mat_F_ptr, boundary_u_ptr] =
942 calculate_stress_ops(op_loop_side->getOpPtrVector());
943 boundary_post_proc_fe->getOpPtrVector().push_back(op_loop_side);
944
946
947 boundary_post_proc_fe->getOpPtrVector().push_back(
948
949 new OpPPMap(
950
951 boundary_post_proc_fe->getPostProcMesh(),
952 boundary_post_proc_fe->getMapGaussPts(),
953
955
956 OpPPMap::DataMapMat{{"V", boundary_v_ptr},
957 {"GEOMETRY", boundary_X_ptr},
958 {"x", boundary_x_ptr},
959 {"U", boundary_u_ptr}},
960
961 OpPPMap::DataMapMat{{"FIRST_PIOLA", boundary_mat_P_ptr},
962 {"F", boundary_mat_F_ptr}},
963
965
966 )
967
968 );
969 return boundary_post_proc_fe;
970 };
971
972 // Add monitor to time solver
973
974 double rho = 1.;
975 CHKERR PetscOptionsGetReal(PETSC_NULLPTR, "", "-density", &rho, PETSC_NULLPTR);
976 auto get_rho = [rho](const double, const double, const double) {
977 return rho;
978 };
979
980 SmartPetscObj<Mat> M; ///< Mass matrix
981 SmartPetscObj<KSP> ksp; ///< Linear solver
982
983 auto ts_pre_post_proc = boost::make_shared<TSPrePostProc>();
984 tsPrePostProc = ts_pre_post_proc;
985
987 CHKERR MatZeroEntries(M);
988
989 boost::shared_ptr<DomainEle> vol_mass_ele(new DomainEle(mField));
990
991 vol_mass_ele->B = M;
992
993 auto integration_rule = [](int, int, int approx_order) {
994 return 2 * approx_order;
995 };
996
997 vol_mass_ele->getRuleHook = integration_rule;
998
999 vol_mass_ele->getOpPtrVector().push_back(new OpMassV("V", "V", get_rho));
1000 vol_mass_ele->getOpPtrVector().push_back(new OpMassF("F", "F"));
1001
1003 CHKERR MatAssemblyBegin(M, MAT_FINAL_ASSEMBLY);
1004 CHKERR MatAssemblyEnd(M, MAT_FINAL_ASSEMBLY);
1005
1006 auto lumpVec = createDMVector(simple->getDM());
1007 CHKERR MatGetRowSum(M, lumpVec);
1008
1009 CHKERR MatZeroEntries(M);
1010 CHKERR MatDiagonalSet(M, lumpVec, INSERT_VALUES);
1011
1012 // Create and septup KSP (linear solver), we need this to calculate g(t,u) =
1013 // M^-1G(t,u)
1015 CHKERR KSPSetOperators(ksp, M, M);
1016 CHKERR KSPSetFromOptions(ksp);
1017 CHKERR KSPSetUp(ksp);
1018
1019 auto solve_boundary_for_g = [&]() {
1021 if (*(pipeline_mng->getBoundaryExplicitRhsFE()->vecAssembleSwitch)) {
1022
1023 CHKERR VecGhostUpdateBegin(pipeline_mng->getBoundaryExplicitRhsFE()->ts_F,
1024 ADD_VALUES, SCATTER_REVERSE);
1025 CHKERR VecGhostUpdateEnd(pipeline_mng->getBoundaryExplicitRhsFE()->ts_F,
1026 ADD_VALUES, SCATTER_REVERSE);
1027 CHKERR VecAssemblyBegin(pipeline_mng->getBoundaryExplicitRhsFE()->ts_F);
1028 CHKERR VecAssemblyEnd(pipeline_mng->getBoundaryExplicitRhsFE()->ts_F);
1029 *(pipeline_mng->getBoundaryExplicitRhsFE()->vecAssembleSwitch) = false;
1030
1031 auto D =
1032 vectorDuplicate(pipeline_mng->getBoundaryExplicitRhsFE()->ts_F);
1033 CHKERR KSPSolve(ksp, pipeline_mng->getBoundaryExplicitRhsFE()->ts_F, D);
1034 CHKERR VecGhostUpdateBegin(D, INSERT_VALUES, SCATTER_FORWARD);
1035 CHKERR VecGhostUpdateEnd(D, INSERT_VALUES, SCATTER_FORWARD);
1036 CHKERR VecCopy(D, pipeline_mng->getBoundaryExplicitRhsFE()->ts_F);
1037 }
1038
1040 };
1041
1042 pipeline_mng->getBoundaryExplicitRhsFE()->postProcessHook =
1043 solve_boundary_for_g;
1044
1046 ts = pipeline_mng->createTSEX(dm);
1047
1048 // Field eval
1049 PetscBool field_eval_flag = PETSC_TRUE;
1050 boost::shared_ptr<MatrixDouble> velocity_field_ptr;
1051 boost::shared_ptr<MatrixDouble> geometry_field_ptr;
1052 boost::shared_ptr<MatrixDouble> spatial_position_field_ptr;
1053 boost::shared_ptr<SetPtsData> field_eval_data;
1054
1055 std::array<double, 3> field_eval_coords = {0.5, 0.5, 5.};
1056 int dim = 3;
1057 CHKERR PetscOptionsGetRealArray(NULL, NULL, "-field_eval_coords",
1058 field_eval_coords.data(), &dim,
1059 &field_eval_flag);
1060
1061 if (field_eval_flag) {
1062 field_eval_data =
1063 mField.getInterface<FieldEvaluatorInterface>()->getData<DomainEle>();
1064 CHKERR mField.getInterface<FieldEvaluatorInterface>()->buildTree<SPACE_DIM>(
1065 field_eval_data, simple->getDomainFEName());
1066
1067 field_eval_data->setEvalPoints(field_eval_coords.data(), 1);
1068
1069 auto no_rule = [](int, int, int) { return -1; };
1070
1071 auto fe_ptr = field_eval_data->feMethodPtr;
1072 fe_ptr->getRuleHook = no_rule;
1073 velocity_field_ptr = boost::make_shared<MatrixDouble>();
1074 geometry_field_ptr = boost::make_shared<MatrixDouble>();
1075 spatial_position_field_ptr = boost::make_shared<MatrixDouble>();
1076 fe_ptr->getOpPtrVector().push_back(
1077 new OpCalculateVectorFieldValues<SPACE_DIM>("V", velocity_field_ptr));
1078 fe_ptr->getOpPtrVector().push_back(
1080 geometry_field_ptr));
1081 fe_ptr->getOpPtrVector().push_back(
1083 "x_2", spatial_position_field_ptr));
1084 }
1085
1086 auto post_proc_domain = [&]() {
1087 auto post_proc_fe_vol = boost::make_shared<PostProcEle>(mField);
1088
1090
1091 auto [boundary_v_ptr, boundary_X_ptr, boundary_x_ptr, boundary_mat_P_ptr,
1092 boundary_mat_F_ptr, boundary_u_ptr] =
1093 calculate_stress_ops(post_proc_fe_vol->getOpPtrVector());
1094
1095 post_proc_fe_vol->getOpPtrVector().push_back(
1096
1097 new OpPPMap(
1098
1099 post_proc_fe_vol->getPostProcMesh(),
1100 post_proc_fe_vol->getMapGaussPts(),
1101
1102 {},
1103
1104 {{"V", boundary_v_ptr},
1105 {"GEOMETRY", boundary_X_ptr},
1106 {"x", boundary_x_ptr},
1107 {"U", boundary_u_ptr}},
1108
1109 {{"FIRST_PIOLA", boundary_mat_P_ptr}, {"F", boundary_mat_F_ptr}},
1110
1111 {}
1112
1113 )
1114
1115 );
1116 return post_proc_fe_vol;
1117 };
1118
1119 boost::shared_ptr<FEMethod> null_fe;
1120 auto monitor_ptr = boost::make_shared<Monitor>(
1121 SmartPetscObj<DM>(dm, true), mField, post_proc_domain(),
1122 post_proc_boundary(), velocity_field_ptr, spatial_position_field_ptr,
1123 geometry_field_ptr, field_eval_coords, field_eval_data);
1124
1126 null_fe, monitor_ptr);
1127
1128 double ftime = 1;
1129 // CHKERR TSSetMaxTime(ts, ftime);
1130 CHKERR TSSetExactFinalTime(ts, TS_EXACTFINALTIME_MATCHSTEP);
1131
1132 auto T = createDMVector(simple->getDM());
1133 CHKERR DMoFEMMeshToLocalVector(simple->getDM(), T, INSERT_VALUES,
1134 SCATTER_FORWARD);
1135 CHKERR TSSetSolution(ts, T);
1136 CHKERR TSSetFromOptions(ts);
1137
1138 CHKERR TSSetPostStage(ts, TSPrePostProc::tsPostStage);
1139 CHKERR TSSetPostStep(ts, TSPrePostProc::tsPostStep);
1140 CHKERR TSSetPreStep(ts, TSPrePostProc::tsPreStep);
1141
1142 boost::shared_ptr<ForcesAndSourcesCore> null;
1143
1144 if (auto ptr = tsPrePostProc.lock()) {
1145 ptr->fsRawPtr = this;
1146 CHKERR TSSetUp(ts);
1147 CHKERR TSSolve(ts, NULL);
1148 // Solver convergence is cheap to check and carries no magic numbers, so
1149 // it stands alone rather than waiting on a "-test_nb"/"-test_name"
1150 // selector.
1151 {
1152 PetscReal expected_time;
1153 CHKERR TSGetMaxTime(ts, &expected_time);
1155 ts, expected_time, {1e-12, 1e-12}, "conservation law");
1156 }
1157 CHKERR TSGetTime(ts, &ftime);
1158 }
1159
1161}
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:1274
double D
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:1132
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/11]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [3/11]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [4/11]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [5/11]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [6/11]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [7/11]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [8/11]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [9/11]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [10/11]

MoFEMErrorCode Example::solveSystem ( )
private

◆ solveSystem() [11/11]

MoFEMErrorCode Example::solveSystem ( )
private

◆ synchroniseMesh()

MoFEMErrorCode Example::synchroniseMesh ( )
inlineprivate
Examples
mesh_reconstruction.cpp.

Definition at line 350 of file mesh_reconstruction.cpp.

350 {
353 auto trim_lineage = [&]() {
355 if (!currentPtr->field.get_comm_rank())
357 auto &field = currentPtr->field;
358 auto &mb = field.get_moab();
359 Range needed = currentPtr->entities;
360 const auto parent_tag =
361 field.getInterface<BitRefManager>()->get_th_RefParentHandle();
362 for (auto entity : currentPtr->entities) {
363 EntityHandle parent = 0;
364 CHKERR mb.tag_get_data(parent_tag, &entity, 1, &parent);
365 if (parent)
366 needed.insert(parent);
367 }
368 for (int dim = 2; dim >= 0; --dim) {
370 CHKERR mb.get_adjacencies(needed.subset_by_dimension(dim + 1), dim,
371 false, closure, moab::Interface::UNION);
372 needed.merge(closure);
373 }
374 const auto remove = subtract(sourceLineage, needed);
375 if (remove.empty())
377 CHKERR deleteGeometry(remove);
378 sourceLineage = intersect(sourceLineage, needed);
380 };
381 CHKERR agree(trim_lineage());
383 }
std::vector< int > parent

◆ testOperators()

MoFEMErrorCode Example::testOperators ( )
private

[Solve]

[TestOperators]

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

Definition at line 1486 of file plastic.cpp.

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

◆ 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 775 of file adjoint.cpp.

775 {
777
778 auto opt_ents = get_range_from_block(mField, "OPTIMISE", SPACE_DIM - 1);
779 auto subset_dm_bdy = createDM(mField.get_comm(), "DMMOFEM");
780 CHKERR DMMoFEMSetSquareProblem(subset_dm_bdy, PETSC_TRUE);
781 CHKERR DMMoFEMCreateSubDM(subset_dm_bdy, adjointDM, "SUBSET_BDY");
782 CHKERR DMMoFEMAddElement(subset_dm_bdy, "ADJOINT_BOUNDARY_FE");
783 CHKERR DMMoFEMAddSubFieldRow(subset_dm_bdy, "ADJOINT_FIELD",
784 boost::make_shared<Range>(opt_ents));
785 CHKERR DMMoFEMAddSubFieldCol(subset_dm_bdy, "ADJOINT_FIELD",
786 boost::make_shared<Range>(opt_ents));
787 CHKERR DMSetUp(subset_dm_bdy);
788
789 auto subset_dm_domain = createDM(mField.get_comm(), "DMMOFEM");
790 CHKERR DMMoFEMSetSquareProblem(subset_dm_domain, PETSC_TRUE);
791 CHKERR DMMoFEMCreateSubDM(subset_dm_domain, adjointDM, "SUBSET_DOMAIN");
792 CHKERR DMMoFEMAddElement(subset_dm_domain, "ADJOINT_DOMAIN_FE");
793 CHKERR DMMoFEMAddSubFieldRow(subset_dm_domain, "ADJOINT_FIELD");
794 CHKERR DMMoFEMAddSubFieldCol(subset_dm_domain, "ADJOINT_FIELD");
795 CHKERR DMSetUp(subset_dm_domain);
796
797 // remove dofs on boundary of the domain
798 auto remove_dofs = [&]() {
800
801 std::array<Range, 3> remove_dim_ents;
802 remove_dim_ents[0] =
803 get_range_from_block(mField, "OPT_REMOVE_X", SPACE_DIM - 1);
804 remove_dim_ents[1] =
805 get_range_from_block(mField, "OPT_REMOVE_Y", SPACE_DIM - 1);
806 remove_dim_ents[2] =
807 get_range_from_block(mField, "OPT_REMOVE_Z", SPACE_DIM - 1);
808
809 for (int d = 0; d != 3; ++d) {
810 MOFEM_LOG("WORLD", Sev::inform)
811 << "Removing topology modes on block OPT_REMOVE_" << (char)('X' + d)
812 << " with " << remove_dim_ents[d].size() << " entities";
813 }
814
815 Range body_ents;
816 CHKERR mField.get_moab().get_entities_by_dimension(0, SPACE_DIM, body_ents,
817 true);
818 auto skin = moab::Skinner(&mField.get_moab());
819 Range boundary_ents;
820 CHKERR skin.find_skin(0, body_ents, false, boundary_ents);
821 for (int d = 0; d != 3; ++d) {
822 boundary_ents = subtract(boundary_ents, remove_dim_ents[d]);
823 }
824 ParallelComm *pcomm =
825 ParallelComm::get_pcomm(&mField.get_moab(), MYPCOMM_INDEX);
826 CHKERR pcomm->filter_pstatus(boundary_ents,
827 PSTATUS_SHARED | PSTATUS_MULTISHARED,
828 PSTATUS_NOT, -1, nullptr);
829 for (auto d = SPACE_DIM - 2; d >= 0; --d) {
830 if (d >= 0) {
831 Range ents;
832 CHKERR mField.get_moab().get_adjacencies(boundary_ents, d, false, ents,
833 moab::Interface::UNION);
834 boundary_ents.merge(ents);
835 } else {
836 Range verts;
837 CHKERR mField.get_moab().get_connectivity(boundary_ents, verts);
838 boundary_ents.merge(verts);
839 }
840 CHKERR mField.getInterface<CommInterface>()->synchroniseEntities(
841 boundary_ents);
842 }
843 boundary_ents.merge(opt_ents);
844 CHKERR mField.getInterface<ProblemsManager>()->removeDofsOnEntities(
845 "SUBSET_DOMAIN", "ADJOINT_FIELD", boundary_ents);
846 for (int d = 0; d != 3; ++d) {
847 CHKERR mField.getInterface<ProblemsManager>()->removeDofsOnEntities(
848 "SUBSET_DOMAIN", "ADJOINT_FIELD", remove_dim_ents[d], d, d);
849 }
850
851 // #ifndef NDEBUG
852 if (mField.get_comm_rank() == 0) {
853 CHKERR save_range(mField.get_moab(), "topoMode_boundary_ents.vtk",
854 boundary_ents);
855 }
856 // #endif
857
859 };
860
861 CHKERR remove_dofs();
862
863 auto get_lhs_fe = [&]() {
864 auto fe_lhs = boost::make_shared<BoundaryEle>(mField);
865 fe_lhs->getRuleHook = [](int, int, int p_data) {
866 return 2 * p_data + p_data - 1;
867 };
868 auto &pip = fe_lhs->getOpPtrVector();
870 "GEOMETRY");
873 pip.push_back(new OpMass("ADJOINT_FIELD", "ADJOINT_FIELD",
874 [](double, double, double) { return 1.; }));
875 return fe_lhs;
876 };
877
878 auto get_rhs_fe = [&]() {
879 auto fe_rhs = boost::make_shared<BoundaryEle>(mField);
880 fe_rhs->getRuleHook = [](int, int, int p_data) {
881 return 2 * p_data + p_data - 1;
882 };
883 auto &pip = fe_rhs->getOpPtrVector();
885 "GEOMETRY");
886
887 return fe_rhs;
888 };
889
890 auto block_name = "OPTIMISE";
891 auto mesh_mng = mField.getInterface<MeshsetsManager>();
892 auto bcs = mesh_mng->getCubitMeshsetPtr(
893
894 std::regex((boost::format("%s(.*)") % block_name).str())
895
896 );
897
898 for (auto &v : modeVecs) {
899 v = createDMVector(subset_dm_bdy);
900 }
901
903 struct OpMode : public OP {
904 OpMode(const std::string name,
905 boost::shared_ptr<ObjectiveFunctionData> python_ptr, int id,
906 std::vector<SmartPetscObj<Vec>> mode_vecs,
907 std::vector<std::array<double, 3>> mode_centroids,
908 std::vector<std::array<double, 6>> mode_bboxes, int block_counter,
909 int mode_counter, boost::shared_ptr<Range> range = nullptr)
910 : OP(name, name, OP::OPROW, range), pythonPtr(python_ptr), iD(id),
911 modeVecs(mode_vecs), modeCentroids(mode_centroids),
912 modeBboxes(mode_bboxes), blockCounter(block_counter),
913 modeCounter(mode_counter) {}
914
915 MoFEMErrorCode doWork(int side, EntityType type, EntData &data) {
917
918 if (OP::entsPtr) {
919 if (OP::entsPtr->find(this->getFEEntityHandle()) == OP::entsPtr->end())
921 }
922
923 auto nb_rows = data.getIndices().size();
924 if (!nb_rows) {
926 }
927 auto nb_base_functions = data.getN().size2();
928
930 CHKERR pythonPtr->blockModes(iD, OP::getCoordsAtGaussPts(),
931 modeCentroids[blockCounter],
932 modeBboxes[blockCounter], blockModes);
933
934 auto nb_integration_pts = getGaussPts().size2();
935 if (blockModes.size2() != 3 * nb_integration_pts) {
936 MOFEM_LOG("WORLD", Sev::error)
937 << "Number of modes does not match number of integration points: "
938 << blockModes.size2() << "!=" << 3 * nb_integration_pts;
939 CHK_THROW_MESSAGE(MOFEM_DATA_INCONSISTENCY, "modes/integration points");
940 }
941
942 VectorDouble nf(nb_rows);
943
944 int nb_modes = blockModes.size1();
945 for (auto mode = 0; mode != nb_modes; ++mode) {
946 nf.clear();
947 // get mode
948 auto t_mode = getFTensor1FromPtr<3>(&blockModes(mode, 0));
949 // get element volume
950 const double vol = OP::getMeasure();
951 // get integration weights
952 auto t_w = OP::getFTensor0IntegrationWeight();
953 // get base function gradient on rows
954 auto t_base = data.getFTensor0N();
955 // loop over integration points
956 for (int gg = 0; gg != nb_integration_pts; gg++) {
957
958 // take into account Jacobian
959 const double alpha = t_w * vol;
960 // loop over rows base functions
961 auto t_nf = getFTensor1FromPtr<SPACE_DIM>(nf.data().data());
962 int rr = 0;
963 for (; rr != nb_rows / SPACE_DIM; ++rr) {
964 t_nf(i) += alpha * t_base * t_mode(i);
965 ++t_base;
966 ++t_nf;
967 }
968 for (; rr < nb_base_functions; ++rr)
969 ++t_base;
970 ++t_w; // move to another integration weight
971 ++t_mode; // move to another mode
972 }
973 Vec vec = modeVecs[modeCounter + mode];
974 auto size = data.getIndices().size();
975 auto *indices = data.getIndices().data().data();
976 auto *nf_data = nf.data().data();
977 CHKERR VecSetValues(vec, size, indices, nf_data, ADD_VALUES);
978 }
979
981 }
982
983 private:
984 boost::shared_ptr<ObjectiveFunctionData> pythonPtr;
985 MatrixDouble blockModes;
986 std::vector<std::array<double, 3>> modeCentroids;
987 std::vector<std::array<double, 6>> modeBboxes;
988 int iD;
989 std::vector<SmartPetscObj<Vec>> modeVecs;
990 int blockCounter;
991 int modeCounter;
992 };
993
994 auto solve_bdy = [&]() {
996
997 auto fe_lhs = get_lhs_fe();
998 auto fe_rhs = get_rhs_fe();
999 int block_counter = 0;
1000 int mode_counter = 0;
1001 for (auto &bc : bcs) {
1002 auto id = bc->getMeshsetId();
1003 Range ents;
1004 CHKERR mField.get_moab().get_entities_by_handle(bc->getMeshset(), ents,
1005 true);
1006 auto range = boost::make_shared<Range>(ents);
1007 auto &pip_rhs = fe_rhs->getOpPtrVector();
1008 pip_rhs.push_back(new OpMode("ADJOINT_FIELD", pythonPtr, id, modeVecs,
1009 modeCentroids, modeBBoxes, block_counter,
1010 mode_counter, range));
1011 CHKERR DMoFEMLoopFiniteElements(subset_dm_bdy, "ADJOINT_BOUNDARY_FE",
1012 fe_rhs);
1013 pip_rhs.pop_back();
1014 int nb_modes;
1015 CHKERR pythonPtr->numberOfModes(id, nb_modes);
1016 ++block_counter;
1017 mode_counter += nb_modes;
1018 MOFEM_LOG("WORLD", Sev::inform)
1019 << "Setting mode block block: " << bc->getName()
1020 << " with ID: " << bc->getMeshsetId()
1021 << " total modes: " << mode_counter;
1022 }
1023
1024 for (auto &v : modeVecs) {
1025 CHKERR VecAssemblyBegin(v);
1026 CHKERR VecAssemblyEnd(v);
1027 CHKERR VecGhostUpdateBegin(v, ADD_VALUES, SCATTER_REVERSE);
1028 CHKERR VecGhostUpdateEnd(v, ADD_VALUES, SCATTER_REVERSE);
1029 }
1030
1031 auto M = createDMMatrix(subset_dm_bdy);
1032 fe_lhs->B = M;
1033 CHKERR DMoFEMLoopFiniteElements(subset_dm_bdy, "ADJOINT_BOUNDARY_FE",
1034 fe_lhs);
1035 CHKERR MatAssemblyBegin(M, MAT_FINAL_ASSEMBLY);
1036 CHKERR MatAssemblyEnd(M, MAT_FINAL_ASSEMBLY);
1037
1038 auto solver = createKSP(mField.get_comm());
1039 CHKERR KSPSetOperators(solver, M, M);
1040 CHKERR KSPSetFromOptions(solver);
1041 CHKERR KSPSetUp(solver);
1042 auto v = createDMVector(subset_dm_bdy);
1043 for (auto &f : modeVecs) {
1044 CHKERR KSPSolve(solver, f, v);
1045 CHKERR VecSwap(f, v);
1046 }
1047
1048 for (auto &v : modeVecs) {
1049 CHKERR VecGhostUpdateBegin(v, INSERT_VALUES, SCATTER_FORWARD);
1050 CHKERR VecGhostUpdateEnd(v, INSERT_VALUES, SCATTER_FORWARD);
1051 }
1052
1054 };
1055
1056 CHKERR solve_bdy();
1057
1058 auto get_elastic_fe_lhs = [&]() {
1059 auto fe = boost::make_shared<DomainEle>(mField);
1060 fe->getRuleHook = [](int, int, int p_data) {
1061 return 2 * p_data + p_data - 1;
1062 };
1063 auto &pip = fe->getOpPtrVector();
1065 "GEOMETRY");
1066 CHKERR HookeOps::opFactoryDomainLhs<SPACE_DIM, A, I, DomainEleOp>(
1067 mField, pip, "ADJOINT_FIELD", "MAT_ADJOINT", Sev::noisy);
1068 return fe;
1069 };
1070
1071 auto get_elastic_fe_rhs = [&]() {
1072 auto fe = boost::make_shared<DomainEle>(mField);
1073 fe->getRuleHook = [](int, int, int p_data) {
1074 return 2 * p_data + p_data - 1;
1075 };
1076 auto &pip = fe->getOpPtrVector();
1078 "GEOMETRY");
1079 CHKERR HookeOps::opFactoryDomainRhs<SPACE_DIM, A, I, DomainEleOp>(
1080 mField, pip, "ADJOINT_FIELD", "MAT_ADJOINT", Sev::noisy);
1081 return fe;
1082 };
1083
1084 auto adjoint_gradient_postprocess = [&](auto mode) {
1086 auto post_proc_mesh = boost::make_shared<moab::Core>();
1087 auto post_proc_begin =
1088 boost::make_shared<PostProcBrokenMeshInMoabBaseBegin>(mField,
1089 post_proc_mesh);
1090 auto post_proc_end = boost::make_shared<PostProcBrokenMeshInMoabBaseEnd>(
1091 mField, post_proc_mesh);
1092
1093 auto geom_vec = boost::make_shared<MatrixDouble>();
1094
1095 auto post_proc_fe =
1096 boost::make_shared<PostProcEleDomain>(mField, post_proc_mesh);
1098 post_proc_fe->getOpPtrVector(), {H1}, "GEOMETRY");
1099 post_proc_fe->getOpPtrVector().push_back(
1100 new OpCalculateVectorFieldValues<SPACE_DIM>("ADJOINT_FIELD", geom_vec,
1101 modeVecs[mode]));
1102
1104
1105 post_proc_fe->getOpPtrVector().push_back(
1106
1107 new OpPPMap(
1108
1109 post_proc_fe->getPostProcMesh(), post_proc_fe->getMapGaussPts(),
1110
1111 {},
1112
1113 {{"MODE", geom_vec}},
1114
1115 {},
1116
1117 {}
1118
1119 )
1120
1121 );
1122
1124 post_proc_begin->getFEMethod());
1125 CHKERR DMoFEMLoopFiniteElements(adjointDM, "ADJOINT_DOMAIN_FE",
1126 post_proc_fe);
1128 post_proc_begin->getFEMethod());
1129
1130 CHKERR post_proc_end->writeFile("mode_" + std::to_string(mode) + ".h5m");
1131
1133 };
1134
1135 auto solve_domain = [&]() {
1137 auto fe_lhs = get_elastic_fe_lhs();
1138 auto fe_rhs = get_elastic_fe_rhs();
1139 auto v = createDMVector(subset_dm_domain);
1140 auto F = vectorDuplicate(v);
1141 fe_rhs->f = F;
1142
1143 auto M = createDMMatrix(subset_dm_domain);
1144 fe_lhs->B = M;
1145 CHKERR DMoFEMLoopFiniteElements(subset_dm_domain, "ADJOINT_DOMAIN_FE",
1146 fe_lhs);
1147 CHKERR MatAssemblyBegin(M, MAT_FINAL_ASSEMBLY);
1148 CHKERR MatAssemblyEnd(M, MAT_FINAL_ASSEMBLY);
1149
1150 auto solver = createKSP(mField.get_comm());
1151 CHKERR KSPSetOperators(solver, M, M);
1152 CHKERR KSPSetFromOptions(solver);
1153 CHKERR KSPSetUp(solver);
1154
1155 int mode_counter = 0;
1156 for (auto &f : modeVecs) {
1157 CHKERR mField.getInterface<FieldBlas>()->setField(0, "ADJOINT_FIELD");
1158 CHKERR DMoFEMMeshToLocalVector(subset_dm_bdy, f, INSERT_VALUES,
1159 SCATTER_REVERSE);
1160 CHKERR VecZeroEntries(F);
1161 CHKERR DMoFEMLoopFiniteElements(subset_dm_domain, "ADJOINT_DOMAIN_FE",
1162 fe_rhs);
1163 CHKERR VecAssemblyBegin(F);
1164 CHKERR VecAssemblyEnd(F);
1165 CHKERR VecGhostUpdateBegin(F, ADD_VALUES, SCATTER_REVERSE);
1166 CHKERR VecGhostUpdateEnd(F, ADD_VALUES, SCATTER_REVERSE);
1167 CHKERR KSPSolve(solver, F, v);
1168 CHKERR VecGhostUpdateBegin(v, INSERT_VALUES, SCATTER_FORWARD);
1169 CHKERR VecGhostUpdateEnd(v, INSERT_VALUES, SCATTER_FORWARD);
1170 CHKERR DMoFEMMeshToLocalVector(subset_dm_domain, v, INSERT_VALUES,
1171 SCATTER_REVERSE);
1172 auto m = createDMVector(adjointDM);
1173 CHKERR DMoFEMMeshToLocalVector(adjointDM, m, INSERT_VALUES,
1174 SCATTER_FORWARD);
1175 f = m;
1176 ++mode_counter;
1177 }
1179 };
1180
1181 CHKERR solve_domain();
1182
1183 for (int i = 0; i < modeVecs.size(); ++i) {
1184 CHKERR adjoint_gradient_postprocess(i);
1185 }
1186
1188}
std::vector< int > size
It end
Definition Schur.cpp:2568
constexpr int SPACE_DIM
@ F
constexpr IntegrationType I
Use Gauss quadrature for integration.
Definition gradient.cpp:25
constexpr AssemblyType A
[Define dimension]
Definition gradient.cpp:23
PetscErrorCode DMoFEMPreProcessFiniteElements(DM dm, MoFEM::FEMethod *method)
execute finite element method for each element in dm (problem)
Definition DMMoFEM.cpp:622
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()

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

Definition at line 826 of file plastic.cpp.

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

◆ UmatExample()

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

Definition at line 25 of file UmatExample.hpp.

25: mField(m_field) {}

Friends And Related Symbol Documentation

◆ TSPrePostProc

friend struct TSPrePostProc
friend

Definition at line 431 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

◆ currentPtr

std::unique_ptr<Mesh> Example::currentPtr
private
Examples
mesh_reconstruction.cpp.

Definition at line 54 of file mesh_reconstruction.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.

◆ focalIndex

int Example::focalIndex
staticprivate

◆ H

SmartPetscObj<Mat> Example::H
private
Examples
mofem/tutorials/adv-0_plasticity/plastic.cpp.

Definition at line 74 of file schrod_eig.cpp.

◆ hybridOrder

PetscInt Example::hybridOrder = 2
private
Examples
mesh_reconstruction.cpp.

Definition at line 57 of file mesh_reconstruction.cpp.

◆ 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

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

Definition at line 216 of file plastic.cpp.

216{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

◆ 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.

◆ reconstructed

TetMeshData Example::reconstructed
private
Examples
mesh_reconstruction.cpp.

Definition at line 59 of file mesh_reconstruction.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

◆ scale

std::array<double, 3> Example::scale {2, 1, .75}
private

◆ script

std::string Example::script
private
Examples
mesh_reconstruction.cpp.

Definition at line 62 of file mesh_reconstruction.cpp.

◆ settings

CGALTools::ImplicitMeshSettings Example::settings
private
Examples
mesh_reconstruction.cpp.

Definition at line 60 of file mesh_reconstruction.cpp.

◆ simple

Simple* Example::simple
private

◆ simpleInterface

Simple * Example::simpleInterface
private

◆ sourceLineage

Range Example::sourceLineage
private
Examples
mesh_reconstruction.cpp.

Definition at line 55 of file mesh_reconstruction.cpp.

◆ sourceMesh

TetMeshData Example::sourceMesh
private
Examples
mesh_reconstruction.cpp.

Definition at line 59 of file mesh_reconstruction.cpp.

◆ space

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

Definition at line 69 of file plot_base.cpp.

◆ sphere

TetMeshData Example::sphere
private
Examples
mesh_reconstruction.cpp.

Definition at line 59 of file mesh_reconstruction.cpp.

◆ stretchSteps

PetscInt Example::stretchSteps = 10
private
Examples
mesh_reconstruction.cpp.

Definition at line 56 of file mesh_reconstruction.cpp.

◆ twistDegrees

double Example::twistDegrees = 60
private
Examples
mesh_reconstruction.cpp.

Definition at line 58 of file mesh_reconstruction.cpp.

◆ uXScatter

std::tuple<SmartPetscObj<Vec>, SmartPetscObj<VecScatter> > Example::uXScatter
private
Examples
mofem/tutorials/adv-0_plasticity/plastic.cpp.

Definition at line 228 of file plastic.cpp.

◆ uYScatter

std::tuple<SmartPetscObj<Vec>, SmartPetscObj<VecScatter> > Example::uYScatter
private
Examples
mofem/tutorials/adv-0_plasticity/plastic.cpp.

Definition at line 229 of file plastic.cpp.

◆ uZScatter

std::tuple<SmartPetscObj<Vec>, SmartPetscObj<VecScatter> > Example::uZScatter
private
Examples
mofem/tutorials/adv-0_plasticity/plastic.cpp.

Definition at line 230 of file plastic.cpp.


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