20#include <boost/math/constants/constants.hpp>
61 double *f, Vec
g,
double epsilon);
68 Vec sol, PetscReal *f,
80 return boost::make_shared<TopologicalTAOCtxImpl>(
81 ep, set_integration_at_interior, set_integration_at_face, time_solver);
93 auto &ep = *ctx_impl_ptr->
ep_ptr;
101 INSERT_VALUES, SCATTER_FORWARD,
104 CHKERR TSSetFromOptions(ts);
105 CHKERR TSSetStepNumber(ts, 0);
111 <<
"evaluatePrimalProblemTopologicalImpl: Time step dt: " <<
dt;
112 CHKERR TSSolve(ts, PETSC_NULLPTR);
121 auto &ep = *ctx_impl_ptr->
ep_ptr;
122 auto &m_field = ep.
mField;
127 boost::make_shared<VolumeElementForcesAndSourcesCore>(m_field);
129 ep, fe_spatial, interior_integration_hook, boundary_integration_hook);
132 CHKERR VecAssemblyBegin(dJ_dx_vec);
133 CHKERR VecAssemblyEnd(dJ_dx_vec);
134 CHKERR VecGhostUpdateBegin(dJ_dx_vec, ADD_VALUES, SCATTER_REVERSE);
135 CHKERR VecGhostUpdateEnd(dJ_dx_vec, ADD_VALUES, SCATTER_REVERSE);
136 CHKERR VecGhostUpdateBegin(dJ_dx_vec, INSERT_VALUES, SCATTER_FORWARD);
137 CHKERR VecGhostUpdateEnd(dJ_dx_vec, INSERT_VALUES, SCATTER_FORWARD);
152 auto &ep = *ctx_impl_ptr->
ep_ptr;
153 auto &m_field = ep.
mField;
158 boost::make_shared<VolumeElementForcesAndSourcesCore>(m_field);
160 boost::make_shared<VolumeElementForcesAndSourcesCore>(m_field);
162 boost::shared_ptr<double> J_ptr(fe_material, f);
164 CHKERR VecZeroEntries(dJ_dX_vec);
165 CHKERR VecGhostUpdateBegin(dJ_dX_vec, INSERT_VALUES, SCATTER_FORWARD);
166 CHKERR VecGhostUpdateEnd(dJ_dX_vec, INSERT_VALUES, SCATTER_FORWARD);
169 boundary_integration_hook, J_ptr,
171 auto alpha = ep.alphaW;
172 auto rho = ep.alphaRho;
173 auto alpha_omega = ep.alphaOmega;
175 ep, fe_adjoint, interior_integration_hook, boundary_integration_hook,
182 CHKERR VecAssemblyBegin(dJ_dX_vec);
183 CHKERR VecAssemblyEnd(dJ_dX_vec);
184 CHKERR VecGhostUpdateBegin(dJ_dX_vec, ADD_VALUES, SCATTER_REVERSE);
185 CHKERR VecGhostUpdateEnd(dJ_dX_vec, ADD_VALUES, SCATTER_REVERSE);
186 CHKERR VecGhostUpdateBegin(dJ_dX_vec, INSERT_VALUES, SCATTER_FORWARD);
187 CHKERR VecGhostUpdateEnd(dJ_dX_vec, INSERT_VALUES, SCATTER_FORWARD);
193 Vec sol,
double *f, Vec
g,
197 auto &ep = *ctx_impl_ptr->
ep_ptr;
198 auto &m_field = ep.
mField;
203 boost::make_shared<VolumeElementForcesAndSourcesCore>(m_field);
204 auto J_ptr = boost::make_shared<double>(0);
206 boundary_integration_hook, J_ptr,
210 constexpr double eps = 1e-6;
211 auto random_vec = opt->setRandomFields(
212 ep.dmMaterial, {{ep.materialH1Positions, {-
eps,
eps}}},
nullptr,
215 CHKERR VecCopy(sol, a_vec);
216 CHKERR VecAXPY(a_vec, 1, random_vec);
218 CHKERR VecCopy(sol, b_vec);
219 CHKERR VecAXPY(b_vec, -1, random_vec);
222 CHKERR VecGhostUpdateBegin(a_vec, INSERT_VALUES, SCATTER_FORWARD);
223 CHKERR VecGhostUpdateEnd(a_vec, INSERT_VALUES, SCATTER_FORWARD);
228 double J_plus = *J_ptr;
230 CHKERR VecGhostUpdateBegin(b_vec, INSERT_VALUES, SCATTER_FORWARD);
231 CHKERR VecGhostUpdateEnd(b_vec, INSERT_VALUES, SCATTER_FORWARD);
236 double J_minus = *J_ptr;
237 double dJ_da = (J_plus - J_minus) / (2 *
eps);
240 CHKERR VecDot(
g, random_vec, &exact_dJ);
241 double error = std::abs(dJ_da - exact_dJ);
244 <<
"J = " << *
f <<
", dJ/da = " << dJ_da <<
", exact dJ/da = " << exact_dJ
245 <<
", error = " << error;
247 CHKERR VecGhostUpdateBegin(sol, INSERT_VALUES, SCATTER_FORWARD);
248 CHKERR VecGhostUpdateEnd(sol, INSERT_VALUES, SCATTER_FORWARD);
256 PetscReal *f, Vec
g) {
262 "Invalid context pointer type");
263 auto &ep = *ctx_impl_ptr->ep_ptr;
265 CHKERR VecGhostUpdateBegin(sol, INSERT_VALUES, SCATTER_FORWARD);
266 CHKERR VecGhostUpdateEnd(sol, INSERT_VALUES, SCATTER_FORWARD);
275 CHKERR VecNorm(ctx_impl_ptr->primalProblemVec, NORM_2, &norm2_x);
277 <<
"evaluatePrimalProblemTopologicalImpl: Norm of displacement vector: "
279 if (norm2_x < 1e-12) {
Auxilary functions for Eshelbian plasticity.
Eshelbian plasticity interface.
Interface for Python-based objective function evaluation in topology optimization.
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_DATA_INCONSISTENCY
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
PetscErrorCode DMoFEMMeshToLocalVector(DM dm, Vec l, InsertMode mode, ScatterMode scatter_mode, RowColData rc=RowColData::COL)
set local (or ghosted) vector values on mesh for partition only
PetscErrorCode DMoFEMLoopFiniteElements(DM dm, const char fe_name[], MoFEM::FEMethod *method, CacheTupleWeakPtr cache_ptr=CacheTupleSharedPtr())
Executes FEMethod for finite elements in DM.
auto createDMVector(DM dm, RowColData rc=RowColData::COL)
Get smart vector from DM.
#define MOFEM_LOG(channel, severity)
Log.
MoFEMErrorCode evaluateGradientImpl(TopologicalTAOCtxImpl *ctx_impl_ptr, double *f, Vec g)
boost::shared_ptr< TopologicalTAOCtx > createTopologicalTAOCtx(EshelbianCore *ep, ForcesAndSourcesCore::GaussHookFun set_integration_at_interior, ForcesAndSourcesCore::GaussHookFun set_integration_at_face, SmartPetscObj< TS > time_solver)
MoFEMErrorCode pushTopologicalOps_dJ_adjoint_gradient(EshelbianCore &ep, boost::shared_ptr< VolumeElementForcesAndSourcesCore > fe, ForcesAndSourcesCore::GaussHookFun interior_integration_hook, ForcesAndSourcesCore::GaussHookFun boundary_integration_hook, SmartPetscObj< Vec > lambda_vec, SmartPetscObj< Vec > dJ_dX_vec, const double alpha, const double rho, const double alpha_omega)
MoFEMErrorCode evaluatePrimalProblemTopologicalImpl(TopologicalTAOCtxImpl *ctx_impl_ptr)
MoFEMErrorCode pushTopologicalMaterialOps(EshelbianCore &ep, boost::shared_ptr< VolumeElementForcesAndSourcesCore > fe, ForcesAndSourcesCore::GaussHookFun interior_integration_hook, ForcesAndSourcesCore::GaussHookFun boundary_integration_hook, boost::shared_ptr< double > J_ptr, SmartPetscObj< Vec > dJ_dX_vec)
MoFEMErrorCode finiteDifferenceGradientTest(TopologicalTAOCtxImpl *ctx_impl_ptr, Vec sol, double *f, Vec g, double epsilon)
SmartPetscObj< Vec > pushTopologicalSpatialOps(EshelbianCore &ep, boost::shared_ptr< VolumeElementForcesAndSourcesCore > fe, ForcesAndSourcesCore::GaussHookFun interior_integration_hook, ForcesAndSourcesCore::GaussHookFun boundary_integration_hook)
MoFEMErrorCode testTopologicalDerivative(TopologicalTAOCtx *ctx_ptr, Vec sol, PetscReal *f, Vec g)
MoFEMErrorCode topologicalEvaluateObjectiveAndGradient(Tao tao, Vec sol, PetscReal *f, Vec g, void *ctx)
MoFEMErrorCode evaluateAdjointProblemImpl(TopologicalTAOCtxImpl *ctx_impl_ptr)
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
auto snesGetKSP(SNES snes)
SmartPetscObj< Vec > vectorDuplicate(Vec vec)
Create duplicate vector of smart vector.
MoFEM::Interface & mField
SmartPetscObj< DM > dmElastic
Elastic problem.
SmartPetscObj< Vec > adjointProblemVec
friend MoFEMErrorCode topologicalEvaluateObjectiveAndGradient(Tao tao, Vec sol, PetscReal *f, Vec g, void *ctx)
friend MoFEMErrorCode testTopologicalDerivative(TopologicalTAOCtx *ctx_ptr, Vec sol, PetscReal *f, Vec g)
SmartPetscObj< TS > timeSolver
friend MoFEMErrorCode evaluatePrimalProblemTopologicalImpl(TopologicalTAOCtxImpl *ctx_impl_ptr)
friend MoFEMErrorCode evaluateGradientImpl(TopologicalTAOCtxImpl *ctx_impl_ptr, double *f, Vec g)
friend MoFEMErrorCode finiteDifferenceGradientTest(TopologicalTAOCtxImpl *ctx_impl_ptr, Vec sol, double *f, Vec g, double epsilon)
ForcesAndSourcesCore::GaussHookFun integrationAtFace
SmartPetscObj< Vec > primalProblemVec
ForcesAndSourcesCore::GaussHookFun integrationAtInterior
TopologicalTAOCtxImpl(EshelbianCore *ep, ForcesAndSourcesCore::GaussHookFun set_integration_at_interior, ForcesAndSourcesCore::GaussHookFun set_integration_at_face, SmartPetscObj< TS > time_solver)
friend MoFEMErrorCode evaluateAdjointProblemImpl(TopologicalTAOCtxImpl *ctx_impl_ptr)
boost::function< MoFEMErrorCode(ForcesAndSourcesCore *fe_raw_ptr, int order_row, int order_col, int order_data)> GaussHookFun
Calculate directional derivative of the right hand side and compare it with tangent matrix derivative...
intrusive_ptr for managing petsc objects