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

Member Typedef Documentation

◆ OP [1/2]

using OpAdJointObjective::OP = ForcesAndSourcesCore::UserDataOperator

Definition at line 1552 of file adjoint.cpp.

◆ OP [2/2]

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

1564 : OP(NOSPACE, OP::OPSPACE), pythonPtr(python_ptr), commPtr(comm_ptr),
1565 jacPtr(jac_ptr), diffJacPtr(diff_jac), cofVals(cof_vals),
1566 dGradPtr(d_grad_ptr), dUPtr(d_u_ptr), uPtr(u_ptr),
1567 globObjectivePtr(glob_objective_ptr),
1568 globObjectiveGradPtr(glob_objective_grad_ptr) {}
@ NOSPACE
Definition definitions.h:83
boost::shared_ptr< MatrixDouble > dGradPtr
Definition adjoint.cpp:1718
boost::shared_ptr< double > globObjectiveGradPtr
Definition adjoint.cpp:1723
boost::shared_ptr< HookeOps::CommonData > commPtr
Definition adjoint.cpp:1714
ForcesAndSourcesCore::UserDataOperator OP
Definition adjoint.cpp:1552
boost::shared_ptr< double > globObjectivePtr
Definition adjoint.cpp:1722
boost::shared_ptr< ObjectiveFunctionData > pythonPtr
Definition adjoint.cpp:1713
boost::shared_ptr< VectorDouble > cofVals
Definition adjoint.cpp:1717
boost::shared_ptr< MatrixDouble > uPtr
Definition adjoint.cpp:1720
boost::shared_ptr< MatrixDouble > jacPtr
Definition adjoint.cpp:1715
boost::shared_ptr< MatrixDouble > diffJacPtr
Definition adjoint.cpp:1716
boost::shared_ptr< MatrixDouble > dUPtr
Definition adjoint.cpp:1719

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

867 : OP("ADJOINT_FIELD", "ADJOINT_FIELD", OP::OPROW), pythonPtr(python_ptr),
868 commPtr(comm_ptr), jacPtr(jac_ptr), uPtr(u_ptr),
869 gradLambdaUPtr(grad_lambda_u_ptr),
870 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 1576 of file adjoint.cpp.

1577 {
1579
1580 // Define tensor indices for calculations
1585
1588
1589 constexpr auto symm_size = (SPACE_DIM * (SPACE_DIM + 1)) /
1590 2; // size of symmetric tensor in Voigt notation
1591
1592 auto t_diff_symm = FTensor::diff_symmetrize<double>();
1593 // fourth order tensor for symmetrization to convert from gradient to strain
1594
1595 auto nb_gauss_pts =
1596 getGaussPts().size2(); // number of gauss points in the element
1597 auto objective_ptr = boost::make_shared<MatrixDouble>(
1598 1, nb_gauss_pts); // objective function values at gauss points
1599 auto objective_dstress = boost::make_shared<MatrixDouble>(
1600 nb_gauss_pts,
1601 symm_size); // objective function gradient w.r.t. stress at gauss points
1602 auto objective_dstrain = boost::make_shared<MatrixDouble>(
1603 nb_gauss_pts,
1604 symm_size); // objective function gradient w.r.t. strain at gauss points
1605 auto objective_du =
1606 boost::make_shared<MatrixDouble>(nb_gauss_pts, SPACE_DIM);
1607 // We have dJ/dsigma and dJ/depsilon at gauss points, we need to convert it to dJ/du
1608
1609 auto evaluate_python = [&]() {
1611 auto &coords = OP::getCoordsAtGaussPts();
1612 CHKERR pythonPtr->evalInteriorObjectiveFunction(
1613 coords, uPtr, commPtr->getMatCauchyStress(), commPtr->getMatStrain(),
1614 objective_ptr);
1615 CHKERR pythonPtr->evalInteriorObjectiveGradientStress(
1616 coords, uPtr, commPtr->getMatCauchyStress(), commPtr->getMatStrain(),
1617 objective_dstress);
1618 CHKERR pythonPtr->evalInteriorObjectiveGradientStrain(
1619 coords, uPtr, commPtr->getMatCauchyStress(), commPtr->getMatStrain(),
1620 objective_dstrain);
1621 CHKERR pythonPtr->evalInteriorObjectiveGradientU(
1622 coords, uPtr, commPtr->getMatCauchyStress(), commPtr->getMatStrain(),
1623 objective_du);
1624
1625 auto t_grad_u =
1626 getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(*(commPtr->matGradPtr));
1627 auto t_D =
1628 getFTensor4DdgFromMat<SPACE_DIM, SPACE_DIM, 0>(*(commPtr->matDPtr));
1629 auto t_jac = getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(*(jacPtr));
1630 auto t_diff_jac = getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(*(diffJacPtr));
1631 auto t_cof = getFTensor0FromVec(*(cofVals));
1632 auto t_d_grad = getFTensor2FromMat<SPACE_DIM, SPACE_DIM>(*(dGradPtr));
1633
1634 auto t_obj = getFTensor0FromMat(*objective_ptr);
1635 auto t_obj_dstress =
1636 getFTensor2SymmetricFromMat<SPACE_DIM>(*objective_dstress);
1637 auto t_obj_dstrain =
1638 getFTensor2SymmetricFromMat<SPACE_DIM>(*objective_dstrain);
1639 auto t_obj_du = getFTensor1FromMat<SPACE_DIM>(*objective_du);
1640 auto t_d_u = getFTensor1FromMat<SPACE_DIM>(*dUPtr);
1641
1642 auto vol = OP::getMeasure();
1643 auto t_w = getFTensor0IntegrationWeight();
1644 for (auto gg = 0; gg != nb_gauss_pts; ++gg) {
1645
1646 auto t_det = determinantTensor(t_jac);
1648 CHKERR invertTensor(t_jac, t_det, t_inv_jac);
1649
1651 t_diff_inv_jac(i, j) =
1652 -(t_inv_jac(i, I) * t_diff_jac(I, J)) * t_inv_jac(J, j);
1654 t_diff_grad(i, j) = t_grad_u(i, k) * t_diff_inv_jac(k, j);
1655
1657 t_d_strain(i, j) = t_diff_symm(i, j, k, l) * (
1658
1659 t_d_grad(k, l)
1660
1661 +
1662
1663 t_diff_grad(k, l)
1664
1665 );
1666
1667 auto alpha = t_w * vol;
1668
1669 (*globObjectivePtr) += alpha * t_obj;
1670 (*globObjectiveGradPtr) +=
1671 alpha *
1672 (
1673
1674 t_obj_dstress(i, j) * (t_D(i, j, k, l) * t_d_strain(k, l))
1675
1676 +
1677
1678 t_obj_dstrain(i, j) * t_d_strain(i, j)
1679
1680 +
1681
1682 t_obj_du(i) * t_d_u(i)
1683
1684 +
1685
1686 t_obj * t_cof
1687
1688 );
1689
1690 ++t_w;
1691 ++t_jac;
1692 ++t_diff_jac;
1693 ++t_cof;
1694
1695 ++t_obj;
1696 ++t_obj_dstress;
1697 ++t_obj_dstrain;
1698 ++t_obj_du;
1699
1700 ++t_grad_u;
1701 ++t_d_grad;
1702 ++t_d_u;
1703 }
1705 };
1706
1707 CHKERR evaluate_python();
1708
1710 }
#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)
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 878 of file gradient.cpp.

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