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

Private Attributes

boost::shared_ptr< ObjectiveFunctionDatapythonPtr
 
boost::shared_ptr< HookeOps::CommonDatacommPtr
 
boost::shared_ptr< MatrixDoublejacPtr
 
boost::shared_ptr< MatrixDoublediffJacPtr
 
boost::shared_ptr< VectorDoublecofVals
 
boost::shared_ptr< MatrixDoubledGradPtr
 
boost::shared_ptr< MatrixDoubledUPtr
 
boost::shared_ptr< MatrixDoubleuPtr
 
boost::shared_ptr< doubleglobObjectivePtr
 
boost::shared_ptr< doubleglobObjectiveGradPtr
 
boost::shared_ptr< MatrixDoublegradLambdaUPtr
 

Detailed Description

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

Definition at line 1587 of file adjoint.cpp.

Member Typedef Documentation

◆ OP [1/2]

using OpAdJointObjective::OP = ForcesAndSourcesCore::UserDataOperator

Definition at line 1588 of file adjoint.cpp.

◆ OP [2/2]

Definition at line 895 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 1590 of file adjoint.cpp.

1600 : OP(NOSPACE, OP::OPSPACE), pythonPtr(python_ptr), commPtr(comm_ptr),
1601 jacPtr(jac_ptr), diffJacPtr(diff_jac), cofVals(cof_vals),
1602 dGradPtr(d_grad_ptr), dUPtr(d_u_ptr), uPtr(u_ptr),
1603 globObjectivePtr(glob_objective_ptr),
1604 globObjectiveGradPtr(glob_objective_grad_ptr) {}
@ NOSPACE
Definition definitions.h:83
boost::shared_ptr< MatrixDouble > dGradPtr
Definition adjoint.cpp:1755
boost::shared_ptr< double > globObjectiveGradPtr
Definition adjoint.cpp:1760
boost::shared_ptr< HookeOps::CommonData > commPtr
Definition adjoint.cpp:1751
ForcesAndSourcesCore::UserDataOperator OP
Definition adjoint.cpp:1588
boost::shared_ptr< double > globObjectivePtr
Definition adjoint.cpp:1759
boost::shared_ptr< ObjectiveFunctionData > pythonPtr
Definition adjoint.cpp:1750
boost::shared_ptr< VectorDouble > cofVals
Definition adjoint.cpp:1754
boost::shared_ptr< MatrixDouble > uPtr
Definition adjoint.cpp:1757
boost::shared_ptr< MatrixDouble > jacPtr
Definition adjoint.cpp:1752
boost::shared_ptr< MatrixDouble > diffJacPtr
Definition adjoint.cpp:1753
boost::shared_ptr< MatrixDouble > dUPtr
Definition adjoint.cpp:1756

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

903 : OP("ADJOINT_FIELD", "ADJOINT_FIELD", OP::OPROW), pythonPtr(python_ptr),
904 commPtr(comm_ptr), jacPtr(jac_ptr), uPtr(u_ptr),
905 gradLambdaUPtr(grad_lambda_u_ptr),
906 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 1612 of file adjoint.cpp.

1613 {
1615
1616 // Define tensor indices for calculations
1621
1624
1625 constexpr auto symm_size = (SPACE_DIM * (SPACE_DIM + 1)) /
1626 2; // size of symmetric tensor in Voigt notation
1627
1628 auto t_diff_symm = diff_symmetrize(
1630 SPACE_DIM>()); // fourth order tensor for symmetrization to convert from gradient to strain
1631
1632 auto nb_gauss_pts =
1633 getGaussPts().size2(); // number of gauss points in the element
1634 auto objective_ptr = boost::make_shared<MatrixDouble>(
1635 1, nb_gauss_pts); // objective function values at gauss points
1636 auto objective_dstress = boost::make_shared<MatrixDouble>(
1637 nb_gauss_pts,
1638 symm_size); // objective function gradient w.r.t. stress at gauss points
1639 auto objective_dstrain = boost::make_shared<MatrixDouble>(
1640 nb_gauss_pts,
1641 symm_size); // objective function gradient w.r.t. strain at gauss points
1642 auto objective_du =
1643 boost::make_shared<MatrixDouble>(nb_gauss_pts, SPACE_DIM);
1644 // We have dJ/dsigma and dJ/depsilon at gauss points, we need to convert it to dJ/du
1645
1646 auto evaluate_python = [&]() {
1648 auto &coords = OP::getCoordsAtGaussPts();
1649 CHKERR pythonPtr->evalInteriorObjectiveFunction(
1650 coords, uPtr, commPtr->getMatCauchyStress(), commPtr->getMatStrain(),
1651 objective_ptr);
1652 CHKERR pythonPtr->evalInteriorObjectiveGradientStress(
1653 coords, uPtr, commPtr->getMatCauchyStress(), commPtr->getMatStrain(),
1654 objective_dstress);
1655 CHKERR pythonPtr->evalInteriorObjectiveGradientStrain(
1656 coords, uPtr, commPtr->getMatCauchyStress(), commPtr->getMatStrain(),
1657 objective_dstrain);
1658 CHKERR pythonPtr->evalInteriorObjectiveGradientU(
1659 coords, uPtr, commPtr->getMatCauchyStress(), commPtr->getMatStrain(),
1660 objective_du);
1661
1662 auto t_grad_u =
1663 getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(*(commPtr->matGradPtr));
1664 auto t_D =
1665 getFTensor4DdgFromMat<SPACE_DIM, SPACE_DIM, 0>(*(commPtr->matDPtr));
1666 auto t_jac = getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(*(jacPtr));
1667 auto t_diff_jac = getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(*(diffJacPtr));
1668 auto t_cof = getFTensor0FromVec(*(cofVals));
1669 auto t_d_grad = getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(*(dGradPtr));
1670
1671 auto t_obj = getFTensor0FromMat(*objective_ptr);
1672 auto t_obj_dstress =
1673 getFTensor2SymmetricFromMat<SPACE_DIM>(*objective_dstress);
1674 auto t_obj_dstrain =
1675 getFTensor2SymmetricFromMat<SPACE_DIM>(*objective_dstrain);
1676 auto t_obj_du = getFTensor1FromMat<SPACE_DIM>(*objective_du);
1677 auto t_d_u = getFTensor1FromMat<SPACE_DIM>(*dUPtr);
1678
1679 auto vol = OP::getMeasure();
1680 auto t_w = getFTensor0IntegrationWeight();
1681 for (auto gg = 0; gg != nb_gauss_pts; ++gg) {
1682
1683 auto t_det = determinantTensor(t_jac);
1685 CHKERR invertTensor(t_jac, t_det, t_inv_jac);
1686
1688 t_diff_inv_jac(i, j) =
1689 -(t_inv_jac(i, I) * t_diff_jac(I, J)) * t_inv_jac(J, j);
1691 t_diff_grad(i, j) = t_grad_u(i, k) * t_diff_inv_jac(k, j);
1692
1694 t_d_strain(i, j) = t_diff_symm(i, j, k, l) * (
1695
1696 t_d_grad(k, l)
1697
1698 +
1699
1700 t_diff_grad(k, l)
1701
1702 );
1703
1704 auto alpha = t_w * vol;
1705
1706 (*globObjectivePtr) += alpha * t_obj;
1707 (*globObjectiveGradPtr) +=
1708 alpha *
1709 (
1710
1711 t_obj_dstress(i, j) * (t_D(i, j, k, l) * t_d_strain(k, l))
1712
1713 +
1714
1715 t_obj_dstrain(i, j) * t_d_strain(i, j)
1716
1717 +
1718
1719 t_obj_du(i) * t_d_u(i)
1720
1721 +
1722
1723 t_obj * t_cof
1724
1725 );
1726
1727 ++t_w;
1728 ++t_jac;
1729 ++t_diff_jac;
1730 ++t_cof;
1731
1732 ++t_obj;
1733 ++t_obj_dstress;
1734 ++t_obj_dstrain;
1735 ++t_obj_du;
1736
1737 ++t_grad_u;
1738 ++t_d_grad;
1739 ++t_d_u;
1740 }
1742 };
1743
1744 CHKERR evaluate_python();
1745
1747 }
#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.
auto diff_symmetrize(FTensor::Number< DIM >)
Definition gradient.cpp:705
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)
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.

Definition at line 914 of file gradient.cpp.

914 {
916
917 const auto nb_gauss_pts =
918 getGaussPts().size2(); // number of gauss points in the element
919 const auto nb_dofs = data.getIndices().size();
920 const auto nb_base_funcs = data.getN().size2() / SPACE_DIM;
921
922 // Define tensor indices for calculations
927
930
931 constexpr auto symm_size = (SPACE_DIM * (SPACE_DIM + 1)) /
932 2; // size of symmetric tensor in Voigt notation
933
934 auto t_diff_symm = diff_symmetrize(
936 SPACE_DIM>()); // fourth order tensor for symmetrization to convert from gradient to strain
937
938 auto objective_ptr = boost::make_shared<MatrixDouble>(
939 1, nb_gauss_pts); // objective function values at gauss points
940 auto objective_dstress = boost::make_shared<MatrixDouble>(
941 nb_gauss_pts,
942 symm_size); // objective function gradient w.r.t. stress at gauss points
943 auto objective_dstrain = boost::make_shared<MatrixDouble>(
944 nb_gauss_pts,
945 symm_size); // objective function gradient w.r.t. strain at gauss points
946 // The dJ/du contribution is assembled separately by OpStateSensitivity.
947
948 auto evaluate_python = [&]() {
950 auto &coords = OP::getCoordsAtGaussPts();
951 CHKERR pythonPtr->evalInteriorObjectiveFunction(
952 coords, uPtr, commPtr->getMatCauchyStress(), commPtr->getMatStrain(),
953 objective_ptr);
954 CHKERR pythonPtr->evalInteriorObjectiveGradientStress(
955 coords, uPtr, commPtr->getMatCauchyStress(), commPtr->getMatStrain(),
956 objective_dstress);
957 CHKERR pythonPtr->evalInteriorObjectiveGradientStrain(
958 coords, uPtr, commPtr->getMatCauchyStress(), commPtr->getMatStrain(),
959 objective_dstrain);
960
961 auto t_grad_u =
962 getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(*(commPtr->matGradPtr));
963 auto t_D =
964 getFTensor4DdgFromMat<SPACE_DIM, SPACE_DIM, 0>(*(commPtr->matDPtr));
965 auto t_jac = getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(*(jacPtr));
966 auto t_grad_lambda_u =
967 getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(*gradLambdaUPtr);
968
969 auto t_obj = getFTensor0FromMat(*objective_ptr);
970 auto t_obj_dstress =
971 getFTensor2SymmetricFromMat<SPACE_DIM>(*objective_dstress);
972 auto t_obj_dstrain =
973 getFTensor2SymmetricFromMat<SPACE_DIM>(*objective_dstrain);
974
975 auto vol = OP::getMeasure();
976 auto t_w = getFTensor0IntegrationWeight();
977 auto t_base_diff = data.getFTensor1DiffN<SPACE_DIM>();
978 for (auto gg = 0; gg != nb_gauss_pts; ++gg) {
979 auto alpha = t_w * vol;
980 (*globObjectivePtr) += alpha * t_obj;
981
982 auto t_det = determinantTensor(t_jac);
984 CHKERR invertTensor(t_jac, t_det, t_inv_jac);
985
987 t_diff_inv_jac;
988 t_diff_inv_jac(i, j, I, J) =
989 -t_inv_jac(i, I) * t_inv_jac(J, j);
991 t_diff_grad;
992 t_diff_grad(i, j, I, J) = t_grad_u(i, k) * t_diff_inv_jac(k, j, I, J);
994 t_d_strain;
995 t_d_strain(i, j, I, J) =
996 t_diff_symm(i, j, k, l) * t_diff_grad(k, l, I, J);
997
998 auto t_local_grad_vector = OP::template getNf<SPACE_DIM>();
999 int bb = 0;
1000 for (; bb != nb_dofs/ SPACE_DIM; bb++) {
1001
1003 t_coef(I) = (t_inv_jac(J, I) * t_base_diff(J)) * t_det;
1004
1005 t_local_grad_vector(I) +=
1006 alpha * (
1007
1008 ((t_obj_dstress(k, l) * t_D(k, l, i, j)) +
1009 t_obj_dstrain(i, j)) *
1010 (t_d_strain(i, j, I, J) * t_base_diff(J))
1011
1012 +
1013
1014 t_obj * t_coef(I)
1015
1016 );
1017
1018 t_local_grad_vector(I) -=
1019 alpha * (t_grad_lambda_u(i, j) * t_D(i, j, k, l)) *
1020 (t_d_strain(k, l, I, J) * t_base_diff(J));
1021
1022 ++t_local_grad_vector;
1023 ++t_base_diff;
1024 }
1025 for (; bb < nb_base_funcs; ++bb) {
1026 ++t_base_diff;
1027 }
1028
1029 ++t_w;
1030 ++t_jac;
1031
1032 ++t_obj;
1033 ++t_obj_dstress;
1034 ++t_obj_dstrain;
1035
1036 ++t_grad_u;
1037 ++t_grad_lambda_u;
1038 }
1040 };
1041
1042 CHKERR evaluate_python();
1043
1045 }
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 1052 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: