v0.15.0
Loading...
Searching...
No Matches
adjoint.cpp File Reference
#include <boost/python.hpp>
#include <boost/python/def.hpp>
#include <boost/python/numpy.hpp>
#include <MoFEM.hpp>
#include <ElasticSpring.hpp>
#include <FluidLevel.hpp>
#include <CalculateTraction.hpp>
#include <NaturalDomainBC.hpp>
#include <NaturalBoundaryBC.hpp>
#include <HookeOps.hpp>

Go to the source code of this file.

Classes

struct  DomainBCs
 [Define entities] More...
 
struct  BoundaryBCs
 
struct  PostProcEleByDim< 2 >
 
struct  PostProcEleByDim< 3 >
 
struct  ObjectiveFunctionData
 
struct  Example
 [Example] More...
 
struct  OpAdJointTestOp< SPACE_DIM, IntegrationType::GAUSS, DomainBaseOp >
 
struct  OpAdJointGradTimesSymTensor< SPACE_DIM, IntegrationType::GAUSS, DomainBaseOp >
 
struct  OpAdJointObjective
 
struct  ObjectiveFunctionDataImpl
 

Typedefs

using DomainEle = PipelineManager::ElementsAndOpsByDim<SPACE_DIM>::DomainEle
 
using BoundaryEle
 
using DomainRhsBCs = NaturalBC<DomainEleOp>::Assembly<A>::LinearForm<I>
 
using OpDomainRhsBCs = DomainRhsBCs::OpFlux<DomainBCs, 1, SPACE_DIM>
 
using BoundaryRhsBCs = NaturalBC<BoundaryEleOp>::Assembly<A>::LinearForm<I>
 
using OpBoundaryRhsBCs = BoundaryRhsBCs::OpFlux<BoundaryBCs, 1, SPACE_DIM>
 
using BoundaryLhsBCs = NaturalBC<BoundaryEleOp>::Assembly<A>::BiLinearForm<I>
 
using OpBoundaryLhsBCs = BoundaryLhsBCs::OpFlux<BoundaryBCs, 1, SPACE_DIM>
 
using PostProcEleDomain = PostProcEleByDim<SPACE_DIM>::PostProcEleDomain
 
using SideEle = PostProcEleByDim<SPACE_DIM>::SideEle
 
using PostProcEleBdy = PostProcEleByDim<SPACE_DIM>::PostProcEleBdy
 
using DomainBaseOp = FormsIntegrators<DomainEleOp>::Assembly<A>::OpBase
 [Postprocess results]
 

Functions

boost::shared_ptr< ObjectiveFunctionDatacreate_python_objective_function (std::string py_file)
 
Range get_range_from_block (MoFEM::Interface &m_field, const std::string block_name, int dim)
 
MoFEMErrorCode save_range (moab::Interface &moab, const std::string name, const Range r)
 
template<int DIM>
auto diff_symmetrize (FTensor::Number< DIM >)
 
int main (int argc, char *argv[])
 

Variables

constexpr int BASE_DIM = 1
 
constexpr int SPACE_DIM
 [Define dimension]
 
constexpr AssemblyType A = AssemblyType::PETSC
 [Define dimension]
 
constexpr IntegrationType I
 
constexpr double young_modulus = 1
 
constexpr double poisson_ratio = 0.3
 
constexpr double bulk_modulus_K = young_modulus / (3 * (1 - 2 * poisson_ratio))
 
constexpr double shear_modulus_G = young_modulus / (2 * (1 + poisson_ratio))
 
PetscBool is_plane_strain = PETSC_FALSE
 
static char help [] = "...\n\n"
 [calculateGradient]
 

Typedef Documentation

◆ BoundaryEle

◆ BoundaryLhsBCs

Definition at line 46 of file adjoint.cpp.

◆ BoundaryRhsBCs

Definition at line 44 of file adjoint.cpp.

◆ DomainBaseOp

[Postprocess results]

[calculateGradient]

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

Definition at line 1355 of file adjoint.cpp.

◆ DomainEle

◆ DomainRhsBCs

Definition at line 42 of file adjoint.cpp.

◆ OpBoundaryLhsBCs

Definition at line 47 of file adjoint.cpp.

◆ OpBoundaryRhsBCs

Definition at line 45 of file adjoint.cpp.

◆ OpDomainRhsBCs

Definition at line 43 of file adjoint.cpp.

◆ PostProcEleBdy

◆ PostProcEleDomain

◆ SideEle

Definition at line 64 of file adjoint.cpp.

Function Documentation

◆ create_python_objective_function()

boost::shared_ptr< ObjectiveFunctionData > create_python_objective_function ( std::string py_file)
Examples
mofem/tutorials/vec-7/adjoint.cpp.

Definition at line 2185 of file adjoint.cpp.

2185 {
2186 auto ptr = boost::make_shared<ObjectiveFunctionDataImpl>();
2187 CHK_THROW_MESSAGE(ptr->initPython(py_file), "init python");
2188 return ptr;
2189}
#define CHK_THROW_MESSAGE(err, msg)
Check and throw MoFEM exception.

◆ diff_symmetrize()

template<int DIM>
auto diff_symmetrize ( FTensor::Number< DIM > )
inline
Examples
mofem/tutorials/vec-7/adjoint.cpp.

Definition at line 1441 of file adjoint.cpp.

1441 {
1442
1443 FTensor::Index<'i', DIM> i;
1444 FTensor::Index<'j', DIM> j;
1445 FTensor::Index<'k', DIM> k;
1446 FTensor::Index<'l', DIM> l;
1447
1449
1450 t_diff(i, j, k, l) = 0;
1451 t_diff(0, 0, 0, 0) = 1;
1452 t_diff(1, 1, 1, 1) = 1;
1453
1454 t_diff(1, 0, 1, 0) = 0.5;
1455 t_diff(1, 0, 0, 1) = 0.5;
1456
1457 t_diff(0, 1, 0, 1) = 0.5;
1458 t_diff(0, 1, 1, 0) = 0.5;
1459
1460 if constexpr (DIM == 3) {
1461 t_diff(2, 2, 2, 2) = 1;
1462
1463 t_diff(2, 0, 2, 0) = 0.5;
1464 t_diff(2, 0, 0, 2) = 0.5;
1465 t_diff(0, 2, 0, 2) = 0.5;
1466 t_diff(0, 2, 2, 0) = 0.5;
1467
1468 t_diff(2, 1, 2, 1) = 0.5;
1469 t_diff(2, 1, 1, 2) = 0.5;
1470 t_diff(1, 2, 1, 2) = 0.5;
1471 t_diff(1, 2, 2, 1) = 0.5;
1472 }
1473
1474 return t_diff;
1475};
FTensor::Index< 'i', SPACE_DIM > i
FTensor::Index< 'l', 3 > l
FTensor::Index< 'j', 3 > j
FTensor::Index< 'k', 3 > k

◆ get_range_from_block()

Range get_range_from_block ( MoFEM::Interface & m_field,
const std::string block_name,
int dim )
Examples
EshelbianPlasticity.cpp, and mofem/tutorials/vec-7/adjoint.cpp.

Definition at line 2579 of file adjoint.cpp.

2580 {
2581 Range r;
2582
2583 auto mesh_mng = m_field.getInterface<MeshsetsManager>();
2584 auto bcs = mesh_mng->getCubitMeshsetPtr(
2585
2586 std::regex((boost::format("%s(.*)") % block_name).str())
2587
2588 );
2589
2590 for (auto bc : bcs) {
2591 Range faces;
2592 CHK_MOAB_THROW(bc->getMeshsetIdEntitiesByDimension(m_field.get_moab(), dim,
2593 faces, true),
2594 "get meshset ents");
2595 r.merge(faces);
2596 }
2597
2598 for (auto dd = dim - 1; dd >= 0; --dd) {
2599 if (dd >= 0) {
2600 Range ents;
2601 CHK_MOAB_THROW(m_field.get_moab().get_adjacencies(r, dd, false, ents,
2602 moab::Interface::UNION),
2603 "get adjs");
2604 r.merge(ents);
2605 } else {
2606 Range verts;
2607 CHK_MOAB_THROW(m_field.get_moab().get_connectivity(r, verts),
2608 "get verts");
2609 r.merge(verts);
2610 }
2612 m_field.getInterface<CommInterface>()->synchroniseEntities(r), "comm");
2613 }
2614
2615 return r;
2616};
#define CHK_MOAB_THROW(err, msg)
Check error code of MoAB function and throw MoFEM exception.
MoFEMErrorCode getCubitMeshsetPtr(const int ms_id, const CubitBCType cubit_bc_type, const CubitMeshSets **cubit_meshset_ptr) const
get cubit meshset
const Tensor2_symmetric_Expr< const ddTensor0< T, Dim, i, j >, typename promote< T, double >::V, Dim, i, j > dd(const Tensor0< T * > &a, const Index< i, Dim > index1, const Index< j, Dim > index2, const Tensor1< int, Dim > &d_ijk, const Tensor1< double, Dim > &d_xyz)
Definition ddTensor0.hpp:33
int r
Definition sdf.py:8
Managing BitRefLevels.
MoFEMErrorCode synchroniseEntities(Range &ent, std::map< int, Range > *received_ents, int verb=DEFAULT_VERBOSITY)
synchronize entity range on processors (collective)
virtual moab::Interface & get_moab()=0
Interface for managing meshsets containing materials and boundary conditions.
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.

◆ main()

int main ( int argc,
char * argv[] )

[Register MoFEM discrete manager in PETSc]

[Register MoFEM discrete manager in PETSc

[Create MoAB]

< mesh database

< mesh database interface

[Create MoAB]

[Create MoFEM]

< finite element database

< finite element database interface

[Create MoFEM]

[Example]

[Example]

Definition at line 2036 of file adjoint.cpp.

2036 {
2037
2038 Py_Initialize();
2039 np::initialize();
2040
2041 // Initialisation of MoFEM/PETSc and MOAB data structures
2042 const char param_file[] = "param_file.petsc";
2043 MoFEM::Core::Initialize(&argc, &argv, param_file, help);
2044
2045 auto core_log = logging::core::get();
2046 core_log->add_sink(
2048
2049 core_log->add_sink(
2050 LogManager::createSink(LogManager::getStrmSync(), "FieldEvaluator"));
2051 LogManager::setLog("FieldEvaluator");
2052 MOFEM_LOG_TAG("FieldEvaluator", "field_eval");
2053
2054 try {
2055
2056 //! [Register MoFEM discrete manager in PETSc]
2057 DMType dm_name = "DMMOFEM";
2058 CHKERR DMRegister_MoFEM(dm_name);
2059 DMType dm_name_mg = "DMMOFEM_MG";
2061 //! [Register MoFEM discrete manager in PETSc
2062
2063 //! [Create MoAB]
2064 moab::Core mb_instance; ///< mesh database
2065 moab::Interface &moab = mb_instance; ///< mesh database interface
2066 //! [Create MoAB]
2067
2068 //! [Create MoFEM]
2069 MoFEM::Core core(moab); ///< finite element database
2070 MoFEM::Interface &m_field = core; ///< finite element database interface
2071 //! [Create MoFEM]
2072
2073 //! [Example]
2074 Example ex(m_field);
2075 CHKERR ex.runProblem();
2076 //! [Example]
2077 }
2079
2081
2082 if (Py_FinalizeEx() < 0) {
2083 exit(120);
2084 }
2085}
static char help[]
[calculateGradient]
Definition adjoint.cpp:2034
#define CATCH_ERRORS
Catch errors.
#define CHKERR
Inline error check.
PetscErrorCode DMRegister_MoFEM(const char sname[])
Register MoFEM problem.
Definition DMMoFEM.cpp:43
MoFEMErrorCode DMRegister_MGViaApproxOrders(const char sname[])
Register DM for Multi-Grid via approximation orders.
static LoggerType & setLog(const std::string channel)
Set ans resset chanel logger.
#define MOFEM_LOG_TAG(channel, tag)
Tag channel.
[Example]
Definition plastic.cpp:216
Core (interface) class.
Definition Core.hpp:82
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
Definition Core.cpp:72
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition Core.cpp:118
Deprecated interface functions.
static boost::shared_ptr< SinkType > createSink(boost::shared_ptr< std::ostream > stream_ptr, std::string comm_filter)
Create a sink object.
static boost::shared_ptr< std::ostream > getStrmWorld()
Get the strm world object.
static boost::shared_ptr< std::ostream > getStrmSync()
Get the strm sync object.

◆ save_range()

MoFEMErrorCode save_range ( moab::Interface & moab,
const std::string name,
const Range r )

Definition at line 2618 of file adjoint.cpp.

2619 {
2621 auto out_meshset = get_temp_meshset_ptr(moab);
2622 CHKERR moab.add_entities(*out_meshset, r);
2623 CHKERR moab.write_file(name.c_str(), "VTK", "", out_meshset->get_ptr(), 1);
2625}
#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()
auto get_temp_meshset_ptr(moab::Interface &moab)
Create smart pointer to temporary meshset.

Variable Documentation

◆ A

AssemblyType A = AssemblyType::PETSC
constexpr

[Define dimension]

Definition at line 26 of file adjoint.cpp.

◆ BASE_DIM

int BASE_DIM = 1
constexpr

Definition at line 19 of file adjoint.cpp.

◆ bulk_modulus_K

double bulk_modulus_K = young_modulus / (3 * (1 - 2 * poisson_ratio))
constexpr
Examples
dynamic_first_order_con_law.cpp, and eigen_elastic.cpp.

Definition at line 75 of file adjoint.cpp.

◆ help

char help[] = "...\n\n"
static

[calculateGradient]

Definition at line 2034 of file adjoint.cpp.

◆ I

IntegrationType I
constexpr
Initial value:
=
IntegrationType::GAUSS
Examples
mofem/tutorials/vec-7/adjoint.cpp.

Definition at line 27 of file adjoint.cpp.

◆ is_plane_strain

PetscBool is_plane_strain = PETSC_FALSE
Examples
mofem/tutorials/vec-7/adjoint.cpp.

Definition at line 78 of file adjoint.cpp.

◆ poisson_ratio

double poisson_ratio = 0.3
constexpr
Examples
eigen_elastic.cpp, and plastic.cpp.

Definition at line 74 of file adjoint.cpp.

◆ shear_modulus_G

double shear_modulus_G = young_modulus / (2 * (1 + poisson_ratio))
constexpr
Examples
dynamic_first_order_con_law.cpp, and eigen_elastic.cpp.

Definition at line 76 of file adjoint.cpp.

◆ SPACE_DIM

int SPACE_DIM
constexpr
Initial value:
=
#define EXECUTABLE_DIMENSION
Definition plastic.cpp:13

[Define dimension]

Examples
dynamic_first_order_con_law.cpp, eigen_elastic.cpp, mofem/tutorials/vec-7/adjoint.cpp, plastic.cpp, and plot_base.cpp.

Definition at line 22 of file adjoint.cpp.

◆ young_modulus

double young_modulus = 1
constexpr
Examples
eigen_elastic.cpp, and plastic.cpp.

Definition at line 73 of file adjoint.cpp.