v0.16.3
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Private Attributes | List of all members
OpAdJointObjective Struct Reference
Inheritance diagram for OpAdJointObjective:
[legend]
Collaboration diagram for OpAdJointObjective:
[legend]

Public Types

using OP = ForcesAndSourcesCore::UserDataOperator
 
using OP = DomainBaseOp
 
- Public Types inherited from MoFEM::OpBaseImpl< A, EleOp >
using OpType = typename EleOp::OpType
 
using EntData = EntitiesFieldData::EntData
 
using MatSetValuesHook = boost::function< MoFEMErrorCode(ForcesAndSourcesCore::UserDataOperator *op_ptr, const EntitiesFieldData::EntData &row_data, const EntitiesFieldData::EntData &col_data, MatrixDouble &m)>
 

Public Member Functions

 OpAdJointObjective (boost::shared_ptr< ObjectiveFunctionData > python_ptr, boost::shared_ptr< HookeOps::CommonData > comm_ptr, boost::shared_ptr< MatrixDouble > jac_ptr, boost::shared_ptr< MatrixDouble > diff_jac, boost::shared_ptr< VectorDouble > cof_vals, boost::shared_ptr< MatrixDouble > d_grad_ptr, boost::shared_ptr< MatrixDouble > d_u_ptr, boost::shared_ptr< MatrixDouble > u_ptr, boost::shared_ptr< double > glob_objective_ptr, boost::shared_ptr< double > glob_objective_grad_ptr)
 
MoFEMErrorCode doWork (int side, EntityType type, EntitiesFieldData::EntData &data)
 Compute objective function contributions at element level.
 
 OpAdJointObjective (boost::shared_ptr< ObjectiveFunctionData > python_ptr, boost::shared_ptr< HookeOps::CommonData > comm_ptr, boost::shared_ptr< MatrixDouble > jac_ptr, boost::shared_ptr< MatrixDouble > u_ptr, boost::shared_ptr< MatrixDouble > grad_lambda_u_ptr, boost::shared_ptr< double > glob_objective_ptr)
 
MoFEMErrorCode iNtegrate (EntitiesFieldData::EntData &data)
 Compute objective function contributions at element level.
 
- Public Member Functions inherited from MoFEM::OpBaseImpl< A, EleOp >
 OpBaseImpl (const std::string row_field_name, const std::string col_field_name, const OpType type, boost::shared_ptr< Range > ents_ptr=nullptr)
 Constructor for base operator implementation.
 
MoFEMErrorCode doWork (int row_side, int col_side, EntityType row_type, EntityType col_type, EntData &row_data, EntData &col_data)
 Do calculations for the left hand side.
 
MoFEMErrorCode doWork (int row_side, EntityType row_type, EntData &row_data)
 Do calculations for the right hand side.
 

Private Attributes

boost::shared_ptr< ObjectiveFunctionData > pythonPtr
 
boost::shared_ptr< HookeOps::CommonData > commPtr
 
boost::shared_ptr< MatrixDouble > jacPtr
 
boost::shared_ptr< MatrixDouble > diffJacPtr
 
boost::shared_ptr< VectorDouble > cofVals
 
boost::shared_ptr< MatrixDouble > dGradPtr
 
boost::shared_ptr< MatrixDouble > dUPtr
 
boost::shared_ptr< MatrixDouble > uPtr
 
boost::shared_ptr< double > globObjectivePtr
 
boost::shared_ptr< double > globObjectiveGradPtr
 
boost::shared_ptr< MatrixDouble > gradLambdaUPtr
 

Additional Inherited Members

- Public Attributes inherited from MoFEM::OpBaseImpl< A, EleOp >
TimeFun timeScalingFun
 assumes that time variable is set
 
FEFun feScalingFun
 set by fe entity handle
 
boost::shared_ptr< Range > entsPtr
 Entities on which element is run.
 
- Static Public Attributes inherited from MoFEM::OpBaseImpl< A, EleOp >
static MatSetValuesHook matSetValuesHook
 
- Protected Member Functions inherited from MoFEM::OpBaseImpl< A, EleOp >
template<int DIM>
auto getNf ()
 Get local vector tensor for assembly.
 
template<int ROW_DIM, int COL_DIM = ROW_DIM>
FTensor::Tensor2< FTensor::PackPtr< double *, COL_DIM >, ROW_DIM, COL_DIM > getLocMat (const int rr)
 Get local matrix tensor for assembly.
 
virtual MoFEMErrorCode iNtegrate (EntData &, EntData &)
 Integrate grad-grad operator.
 
virtual MoFEMErrorCode aSsemble (EntData &row_data, EntData &col_data, const bool trans)
 Assemble local matrix into global matrix.
 
virtual MoFEMErrorCode aSsemble (EntData &data)
 Assemble local vector into global vector.
 
virtual size_t getNbOfBaseFunctions (EntitiesFieldData::EntData &data)
 Get number of base functions.
 
- Protected Attributes inherited from MoFEM::OpBaseImpl< A, EleOp >
int nbRows
 number of dofs on rows
 
int nbCols
 number if dof on column
 
int nbIntegrationPts
 number of integration points
 
int nbRowBaseFunctions
 number or row base functions
 
int rowSide
 row side number
 
int colSide
 column side number
 
EntityType rowType
 row type
 
EntityType colType
 column type
 
bool assembleTranspose
 
bool onlyTranspose
 
MatrixDouble locMat
 local entity block matrix
 
MatrixDouble locMatTranspose
 local entity block matrix
 
VectorDouble locF
 local entity vector
 

Detailed Description

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

Definition at line 1593 of file adjoint.cpp.

Member Typedef Documentation

◆ OP [1/2]

using OpAdJointObjective::OP = ForcesAndSourcesCore::UserDataOperator

Definition at line 1594 of file adjoint.cpp.

◆ OP [2/2]

Definition at line 855 of file gradient.cpp.

Constructor & Destructor Documentation

◆ OpAdJointObjective() [1/2]

OpAdJointObjective::OpAdJointObjective ( boost::shared_ptr< ObjectiveFunctionData >  python_ptr,
boost::shared_ptr< HookeOps::CommonData >  comm_ptr,
boost::shared_ptr< MatrixDouble >  jac_ptr,
boost::shared_ptr< MatrixDouble >  diff_jac,
boost::shared_ptr< VectorDouble >  cof_vals,
boost::shared_ptr< MatrixDouble >  d_grad_ptr,
boost::shared_ptr< MatrixDouble >  d_u_ptr,
boost::shared_ptr< MatrixDouble >  u_ptr,
boost::shared_ptr< double >  glob_objective_ptr,
boost::shared_ptr< double >  glob_objective_grad_ptr 
)
inline

Definition at line 1596 of file adjoint.cpp.

1606 : OP(NOSPACE, OP::OPSPACE), pythonPtr(python_ptr), commPtr(comm_ptr),
1607 jacPtr(jac_ptr), diffJacPtr(diff_jac), cofVals(cof_vals),
1608 dGradPtr(d_grad_ptr), dUPtr(d_u_ptr), uPtr(u_ptr),
1609 globObjectivePtr(glob_objective_ptr),
1610 globObjectiveGradPtr(glob_objective_grad_ptr) {}
@ NOSPACE
Definition definitions.h:83
boost::shared_ptr< MatrixDouble > dGradPtr
Definition adjoint.cpp:1760
boost::shared_ptr< double > globObjectiveGradPtr
Definition adjoint.cpp:1765
boost::shared_ptr< HookeOps::CommonData > commPtr
Definition adjoint.cpp:1756
ForcesAndSourcesCore::UserDataOperator OP
Definition adjoint.cpp:1594
boost::shared_ptr< double > globObjectivePtr
Definition adjoint.cpp:1764
boost::shared_ptr< ObjectiveFunctionData > pythonPtr
Definition adjoint.cpp:1755
boost::shared_ptr< VectorDouble > cofVals
Definition adjoint.cpp:1759
boost::shared_ptr< MatrixDouble > uPtr
Definition adjoint.cpp:1762
boost::shared_ptr< MatrixDouble > jacPtr
Definition adjoint.cpp:1757
boost::shared_ptr< MatrixDouble > diffJacPtr
Definition adjoint.cpp:1758
boost::shared_ptr< MatrixDouble > dUPtr
Definition adjoint.cpp:1761

◆ OpAdJointObjective() [2/2]

OpAdJointObjective::OpAdJointObjective ( boost::shared_ptr< ObjectiveFunctionData >  python_ptr,
boost::shared_ptr< HookeOps::CommonData >  comm_ptr,
boost::shared_ptr< MatrixDouble >  jac_ptr,
boost::shared_ptr< MatrixDouble >  u_ptr,
boost::shared_ptr< MatrixDouble >  grad_lambda_u_ptr,
boost::shared_ptr< double >  glob_objective_ptr 
)
inline

Definition at line 857 of file gradient.cpp.

863 : OP("ADJOINT_FIELD", "ADJOINT_FIELD", OP::OPROW), pythonPtr(python_ptr),
864 commPtr(comm_ptr), jacPtr(jac_ptr), uPtr(u_ptr),
865 gradLambdaUPtr(grad_lambda_u_ptr),
866 globObjectivePtr(glob_objective_ptr) {}
boost::shared_ptr< MatrixDouble > gradLambdaUPtr

Member Function Documentation

◆ doWork()

MoFEMErrorCode OpAdJointObjective::doWork ( int  side,
EntityType  type,
EntitiesFieldData::EntData &  data 
)
inline

Compute objective function contributions at element level.

Evaluates Python objective function with current displacement and stress state, and accumulates global objective value and gradients.

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

Definition at line 1618 of file adjoint.cpp.

1619 {
1621
1622 // Define tensor indices for calculations
1627
1630
1631 constexpr auto symm_size = (SPACE_DIM * (SPACE_DIM + 1)) /
1632 2; // size of symmetric tensor in Voigt notation
1633
1634 auto t_diff_symm = FTensor::diff_symmetrize<double>();
1635 // fourth order tensor for symmetrization to convert from gradient to strain
1636
1637 auto nb_gauss_pts =
1638 getGaussPts().size2(); // number of gauss points in the element
1639 auto objective_ptr = boost::make_shared<MatrixDouble>(
1640 1, nb_gauss_pts); // objective function values at gauss points
1641 auto objective_dstress = boost::make_shared<MatrixDouble>(
1642 nb_gauss_pts,
1643 symm_size); // objective function gradient w.r.t. stress at gauss points
1644 auto objective_dstrain = boost::make_shared<MatrixDouble>(
1645 nb_gauss_pts,
1646 symm_size); // objective function gradient w.r.t. strain at gauss points
1647 auto objective_du =
1648 boost::make_shared<MatrixDouble>(nb_gauss_pts, SPACE_DIM);
1649 // We have dJ/dsigma and dJ/depsilon at gauss points, we need to convert it to dJ/du
1650
1651 auto evaluate_python = [&]() {
1653 auto &coords = OP::getCoordsAtGaussPts();
1654 CHKERR pythonPtr->evalInteriorObjectiveFunction(
1655 coords, uPtr, commPtr->getMatCauchyStress(), commPtr->getMatStrain(),
1656 objective_ptr);
1657 CHKERR pythonPtr->evalInteriorObjectiveGradientStress(
1658 coords, uPtr, commPtr->getMatCauchyStress(), commPtr->getMatStrain(),
1659 objective_dstress);
1660 CHKERR pythonPtr->evalInteriorObjectiveGradientStrain(
1661 coords, uPtr, commPtr->getMatCauchyStress(), commPtr->getMatStrain(),
1662 objective_dstrain);
1663 CHKERR pythonPtr->evalInteriorObjectiveGradientU(
1664 coords, uPtr, commPtr->getMatCauchyStress(), commPtr->getMatStrain(),
1665 objective_du);
1666
1667 auto t_grad_u =
1668 getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(*(commPtr->matGradPtr));
1669 auto t_D =
1670 getFTensor4DdgFromMat<SPACE_DIM, SPACE_DIM, 0>(*(commPtr->matDPtr));
1671 auto t_jac = getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(*(jacPtr));
1672 auto t_diff_jac = getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(*(diffJacPtr));
1673 auto t_cof = getFTensor0FromVec(*(cofVals));
1674 auto t_d_grad = getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(*(dGradPtr));
1675
1676 auto t_obj = getFTensor0FromMat(*objective_ptr);
1677 auto t_obj_dstress =
1678 getFTensor2SymmetricFromMat<SPACE_DIM>(*objective_dstress);
1679 auto t_obj_dstrain =
1680 getFTensor2SymmetricFromMat<SPACE_DIM>(*objective_dstrain);
1681 auto t_obj_du = getFTensor1FromMat<SPACE_DIM>(*objective_du);
1682 auto t_d_u = getFTensor1FromMat<SPACE_DIM>(*dUPtr);
1683
1684 auto vol = OP::getMeasure();
1685 auto t_w = getFTensor0IntegrationWeight();
1686 for (auto gg = 0; gg != nb_gauss_pts; ++gg) {
1687
1688 auto t_det = determinantTensor(t_jac);
1690 CHKERR invertTensor(t_jac, t_det, t_inv_jac);
1691
1693 t_diff_inv_jac(i, j) =
1694 -(t_inv_jac(i, I) * t_diff_jac(I, J)) * t_inv_jac(J, j);
1696 t_diff_grad(i, j) = t_grad_u(i, k) * t_diff_inv_jac(k, j);
1697
1699 t_d_strain(i, j) = t_diff_symm(i, j, k, l) * (
1700
1701 t_d_grad(k, l)
1702
1703 +
1704
1705 t_diff_grad(k, l)
1706
1707 );
1708
1709 auto alpha = t_w * vol;
1710
1711 (*globObjectivePtr) += alpha * t_obj;
1712 (*globObjectiveGradPtr) +=
1713 alpha *
1714 (
1715
1716 t_obj_dstress(i, j) * (t_D(i, j, k, l) * t_d_strain(k, l))
1717
1718 +
1719
1720 t_obj_dstrain(i, j) * t_d_strain(i, j)
1721
1722 +
1723
1724 t_obj_du(i) * t_d_u(i)
1725
1726 +
1727
1728 t_obj * t_cof
1729
1730 );
1731
1732 ++t_w;
1733 ++t_jac;
1734 ++t_diff_jac;
1735 ++t_cof;
1736
1737 ++t_obj;
1738 ++t_obj_dstress;
1739 ++t_obj_dstrain;
1740 ++t_obj_du;
1741
1742 ++t_grad_u;
1743 ++t_d_grad;
1744 ++t_d_u;
1745 }
1747 };
1748
1749 CHKERR evaluate_python();
1750
1752 }
#define FTENSOR_INDEX(DIM, I)
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
constexpr int SPACE_DIM
[Define dimension]
Definition gradient.cpp:19
constexpr IntegrationType I
Use Gauss quadrature for integration.
Definition gradient.cpp:25
FTensor::Index< 'i', SPACE_DIM > i
FTensor::Index< 'J', DIM1 > J
Definition level_set.cpp:30
FTensor::Index< 'l', 3 > l
FTensor::Index< 'j', 3 > j
FTensor::Index< 'k', 3 > k
static MoFEMErrorCode invertTensor(FTensor::Tensor2< T1, DIM, DIM > &t, T2 &det, FTensor::Tensor2< T3, DIM, DIM > &inv_t)
static auto determinantTensor(FTensor::Tensor2< T, DIM, DIM > &t)
Calculate the determinant of a tensor of rank DIM.
auto getFTensor0FromMat(M &data)
Get tensor rank 0 (scalar) form data vector.
static auto getFTensor0FromVec(V &data)
Get tensor rank 0 (scalar) form data vector.

◆ iNtegrate()

MoFEMErrorCode OpAdJointObjective::iNtegrate ( EntitiesFieldData::EntData &  data)
inlinevirtual

Compute objective function contributions at element level.

Evaluates Python objective function with current displacement and stress state, and accumulates global objective value and gradients.

Reimplemented from MoFEM::OpBaseImpl< A, EleOp >.

Definition at line 874 of file gradient.cpp.

874 {
876
877 const auto nb_gauss_pts =
878 getGaussPts().size2(); // number of gauss points in the element
879 const auto nb_dofs = data.getIndices().size();
880 const auto nb_base_funcs = data.getN().size2() / SPACE_DIM;
881
882 // Define tensor indices for calculations
887
890
891 constexpr auto symm_size = (SPACE_DIM * (SPACE_DIM + 1)) /
892 2; // size of symmetric tensor in Voigt notation
893
894 auto t_diff_symm = FTensor::diff_symmetrize<double>();
895 // fourth order tensor for symmetrization to convert from gradient to strain
896
897 auto objective_ptr = boost::make_shared<MatrixDouble>(
898 1, nb_gauss_pts); // objective function values at gauss points
899 auto objective_dstress = boost::make_shared<MatrixDouble>(
900 nb_gauss_pts,
901 symm_size); // objective function gradient w.r.t. stress at gauss points
902 auto objective_dstrain = boost::make_shared<MatrixDouble>(
903 nb_gauss_pts,
904 symm_size); // objective function gradient w.r.t. strain at gauss points
905 // The dJ/du contribution is assembled separately by OpStateSensitivity.
906
907 auto evaluate_python = [&]() {
909 auto &coords = OP::getCoordsAtGaussPts();
910 CHKERR pythonPtr->evalInteriorObjectiveFunction(
911 coords, uPtr, commPtr->getMatCauchyStress(), commPtr->getMatStrain(),
912 objective_ptr);
913 CHKERR pythonPtr->evalInteriorObjectiveGradientStress(
914 coords, uPtr, commPtr->getMatCauchyStress(), commPtr->getMatStrain(),
915 objective_dstress);
916 CHKERR pythonPtr->evalInteriorObjectiveGradientStrain(
917 coords, uPtr, commPtr->getMatCauchyStress(), commPtr->getMatStrain(),
918 objective_dstrain);
919
920 auto t_grad_u =
921 getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(*(commPtr->matGradPtr));
922 auto t_D =
923 getFTensor4DdgFromMat<SPACE_DIM, SPACE_DIM, 0>(*(commPtr->matDPtr));
924 auto t_jac = getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(*(jacPtr));
925 auto t_grad_lambda_u =
926 getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(*gradLambdaUPtr);
927
928 auto t_obj = getFTensor0FromMat(*objective_ptr);
929 auto t_obj_dstress =
930 getFTensor2SymmetricFromMat<SPACE_DIM>(*objective_dstress);
931 auto t_obj_dstrain =
932 getFTensor2SymmetricFromMat<SPACE_DIM>(*objective_dstrain);
933
934 auto vol = OP::getMeasure();
935 auto t_w = getFTensor0IntegrationWeight();
936 auto t_base_diff = data.getFTensor1DiffN<SPACE_DIM>();
937 for (auto gg = 0; gg != nb_gauss_pts; ++gg) {
938 auto alpha = t_w * vol;
939 (*globObjectivePtr) += alpha * t_obj;
940
941 auto t_det = determinantTensor(t_jac);
943 CHKERR invertTensor(t_jac, t_det, t_inv_jac);
944
946 t_diff_inv_jac;
947 t_diff_inv_jac(i, j, I, J) =
948 -t_inv_jac(i, I) * t_inv_jac(J, j);
950 t_diff_grad;
951 t_diff_grad(i, j, I, J) = t_grad_u(i, k) * t_diff_inv_jac(k, j, I, J);
953 t_d_strain;
954 t_d_strain(i, j, I, J) =
955 t_diff_symm(i, j, k, l) * t_diff_grad(k, l, I, J);
956
957 auto t_local_grad_vector = OP::template getNf<SPACE_DIM>();
958 int bb = 0;
959 for (; bb != nb_dofs/ SPACE_DIM; bb++) {
960
962 t_coef(I) = (t_inv_jac(J, I) * t_base_diff(J)) * t_det;
963
964 t_local_grad_vector(I) +=
965 alpha * (
966
967 ((t_obj_dstress(k, l) * t_D(k, l, i, j)) +
968 t_obj_dstrain(i, j)) *
969 (t_d_strain(i, j, I, J) * t_base_diff(J))
970
971 +
972
973 t_obj * t_coef(I)
974
975 );
976
977 t_local_grad_vector(I) -=
978 alpha * (t_grad_lambda_u(i, j) * t_D(i, j, k, l)) *
979 (t_d_strain(k, l, I, J) * t_base_diff(J));
980
981 ++t_local_grad_vector;
982 ++t_base_diff;
983 }
984 for (; bb < nb_base_funcs; ++bb) {
985 ++t_base_diff;
986 }
987
988 ++t_w;
989 ++t_jac;
990
991 ++t_obj;
992 ++t_obj_dstress;
993 ++t_obj_dstrain;
994
995 ++t_grad_u;
996 ++t_grad_lambda_u;
997 }
999 };
1000
1001 CHKERR evaluate_python();
1002
1004 }
auto getFTensor1DiffN(const FieldApproximationBase base)
Get derivatives of base functions.
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.

Member Data Documentation

◆ cofVals

boost::shared_ptr<VectorDouble> OpAdJointObjective::cofVals
private

◆ commPtr

boost::shared_ptr< HookeOps::CommonData > OpAdJointObjective::commPtr
private

◆ dGradPtr

boost::shared_ptr<MatrixDouble> OpAdJointObjective::dGradPtr
private

◆ diffJacPtr

boost::shared_ptr<MatrixDouble> OpAdJointObjective::diffJacPtr
private

◆ dUPtr

boost::shared_ptr<MatrixDouble> OpAdJointObjective::dUPtr
private

◆ globObjectiveGradPtr

boost::shared_ptr<double> OpAdJointObjective::globObjectiveGradPtr
private

◆ globObjectivePtr

boost::shared_ptr< double > OpAdJointObjective::globObjectivePtr
private

◆ gradLambdaUPtr

boost::shared_ptr<MatrixDouble> OpAdJointObjective::gradLambdaUPtr
private

Definition at line 1011 of file gradient.cpp.

◆ jacPtr

boost::shared_ptr< MatrixDouble > OpAdJointObjective::jacPtr
private

◆ pythonPtr

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

◆ uPtr

boost::shared_ptr< MatrixDouble > OpAdJointObjective::uPtr
private

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