v0.16.0
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 896 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 898 of file gradient.cpp.

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

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