|  | v0.15.0 | 
Implementation of ObjectiveFunctionData interface using Python integration. More...
| Public Member Functions | |
| ObjectiveFunctionDataImpl ()=default | |
| virtual | ~ObjectiveFunctionDataImpl ()=default | 
| MoFEMErrorCode | initPython (const std::string py_file) | 
| Initialize Python interpreter and load objective function script. | |
| MoFEMErrorCode | evalObjectiveFunction (MatrixDouble &coords, boost::shared_ptr< MatrixDouble > u_ptr, boost::shared_ptr< MatrixDouble > stress_ptr, boost::shared_ptr< MatrixDouble > strain_ptr, boost::shared_ptr< VectorDouble > o_ptr) | 
| Evaluate objective function at current state. | |
| MoFEMErrorCode | evalObjectiveGradientStress (MatrixDouble &coords, boost::shared_ptr< MatrixDouble > u_ptr, boost::shared_ptr< MatrixDouble > stress_ptr, boost::shared_ptr< MatrixDouble > strain_ptr, boost::shared_ptr< MatrixDouble > o_ptr) | 
| Compute gradient of objective function with respect to stress. | |
| MoFEMErrorCode | evalObjectiveGradientStrain (MatrixDouble &coords, boost::shared_ptr< MatrixDouble > u_ptr, boost::shared_ptr< MatrixDouble > stress_ptr, boost::shared_ptr< MatrixDouble > strain_ptr, boost::shared_ptr< MatrixDouble > o_ptr) | 
| Compute gradient of objective function with respect to strain. | |
| MoFEMErrorCode | evalObjectiveGradientU (MatrixDouble &coords, boost::shared_ptr< MatrixDouble > u_ptr, boost::shared_ptr< MatrixDouble > stress_ptr, boost::shared_ptr< MatrixDouble > strain_ptr, boost::shared_ptr< MatrixDouble > o_ptr) | 
| Compute gradient of objective function with respect to displacement. | |
| MoFEMErrorCode | numberOfModes (int block_id, int &modes) | 
| Return number of topology optimization modes for given material block. | |
| MoFEMErrorCode | blockModes (int block_id, MatrixDouble &coords, std::array< double, 3 > ¢roid, std::array< double, 6 > &bbodx, MatrixDouble &o_ptr) | 
| Define spatial topology modes for design optimization. | |
|  Public Member Functions inherited from ObjectiveFunctionData | |
| virtual | ~ObjectiveFunctionData ()=default | 
| Private Member Functions | |
| MoFEMErrorCode | objectiveFunctionImpl (np::ndarray coords, np::ndarray u, np::ndarray stress, np::ndarray strain, np::ndarray &o) | 
| Internal implementation for objective function evaluation. | |
| MoFEMErrorCode | objectiveGradientStressImpl (np::ndarray coords, np::ndarray u, np::ndarray stress, np::ndarray strain, np::ndarray &o) | 
| Internal implementation for stress gradient computation. | |
| MoFEMErrorCode | objectiveGradientStrainImpl (np::ndarray coords, np::ndarray u, np::ndarray stress, np::ndarray strain, np::ndarray &o) | 
| Internal implementation for strain gradient computation. | |
| MoFEMErrorCode | objectiveGradientUImpl (np::ndarray coords, np::ndarray u, np::ndarray stress, np::ndarray strain, np::ndarray &o) | 
| Internal implementation for displacement gradient computation. | |
| MoFEMErrorCode | blockModesImpl (int block_id, np::ndarray coords, np::ndarray centroid, np::ndarray bbodx, np::ndarray &o_ptr) | 
| Internal implementation for topology mode generation. | |
| np::ndarray | convertToNumPy (std::vector< double > &data, int rows, int nb_gauss_pts) | 
| Convert std::vector to NumPy array for Python interface. | |
| np::ndarray | convertToNumPy (double *ptr, int s) | 
| Convert raw pointer to NumPy array for Python interface. | |
| MatrixDouble | copyToFull (MatrixDouble &s) | 
| Convert symmetric tensor storage to full matrix format. | |
| void | copyToSymmetric (double *ptr, MatrixDouble &s) | 
| Convert full matrix to symmetric tensor storage format | |
| Private Attributes | |
| bp::object | mainNamespace | 
| Main Python namespace for script execution. | |
| bp::object | objectiveFunction | 
| Python function: f(coords,u,stress,strain) -> objective. | |
| bp::object | objectiveGradientStress | 
| Python function: ∂f/∂σ(coords,u,stress,strain) -> gradient | |
| bp::object | objectiveGradientStrain | 
| Python function: ∂f/∂ε(coords,u,stress,strain) -> gradient. | |
| bp::object | objectiveGradientU | 
| Python function: ∂f/∂u(coords,u,stress,strain) -> gradient. | |
| bp::object | objectNumberOfModes | 
| Python function: numberOfModes(block_id) -> int. | |
| bp::object | objectBlockModes | 
| Python function: blockModes(block_id,coords,centroid,bbox) -> modes. | |
Implementation of ObjectiveFunctionData interface using Python integration.
This class provides a concrete implementation of the ObjectiveFunctionData interface that bridges MoFEM C++ data structures with Python-defined objective functions. It enables flexible definition of optimization objectives through Python scripting while maintaining high-performance computation in the C++ finite element framework.
Key features:
The class handles:
Example Python interface functions that must be defined:
Definition at line 2384 of file adjoint.cpp.
| 
 | default | 
| 
 | virtualdefault | 
| 
 | virtual | 
Define spatial topology modes for design optimization.
Generate spatial topology modes for design optimization.
Generates basis functions that define how the geometry can be modified during topology optimization. These modes serve as design variables and define the design space for optimization.
| block_id | Material block identifier | 
| coords | Element coordinates | 
| centroid | Block centroid coordinates | 
| bbodx | Bounding box dimensions [xmin,xmax,ymin,ymax,zmin,zmax] | 
| o_ptr | Output mode vectors | 
This method defines the design parameterization for topology optimization by generating spatial basis functions (modes) that describe how the geometry can be modified during optimization. These modes serve as design variables and define the feasible design space for the optimization problem.
Mathematical context: The geometry modification is parameterized as: x_new = x_original + Σ(αᵢ * φᵢ(x)) where αᵢ are design variables and φᵢ(x) are spatial mode functions
Common mode types:
Process:
The modes enable efficient design space exploration and gradient-based optimization while maintaining geometric feasibility and smoothness.
| block_id | Material block identifier for mode generation | 
| coords | Element coordinates where modes are evaluated | 
| centroid | Geometric centroid of the material block [x,y,z] | 
| bbodx | Bounding box dimensions [xmin,xmax,ymin,ymax,zmin,zmax] | 
| o_ptr | Output matrix: modes × (coordinates × spatial_dimension) | 
Implements ObjectiveFunctionData.
Definition at line 3093 of file adjoint.cpp.
| 
 | private | 
Internal implementation for topology mode generation.
Calls Python function to generate spatial basis functions for topology optimization. These modes define the design space and parametrize allowable geometry modifications during optimization.
| block_id | Material block identifier | 
| coords | NumPy array of element coordinates | 
| centroid | NumPy array of block centroid | 
| bbodx | NumPy array of bounding box dimensions | 
| o_ptr | Output NumPy array for mode vectors | 
Definition at line 3235 of file adjoint.cpp.
| 
 | inlineprivate | 
Convert raw pointer to NumPy array for Python interface.
Low-level conversion for direct memory access to create NumPy arrays. Provides zero-copy conversion when possible for performance.
| ptr | Raw data pointer | 
| s | Array size | 
Definition at line 3282 of file adjoint.cpp.
| 
 | inlineprivate | 
Convert std::vector to NumPy array for Python interface.
Converts a std::vector<double> to a NumPy ndarray.
Efficient conversion from MoFEM data structures to NumPy arrays for seamless Python function calls without data copying.
| data | Source vector data | 
| rows | Number of rows in resulting array | 
| nb_gauss_pts | Number of Gauss points (affects array structure) | 
This function wraps the given vector data into a NumPy array with the specified number of rows and Gauss points. The resulting ndarray shares memory with the input vector, so changes to one will affect the other.
| data | Reference to the vector containing double values to be converted. | 
| rows | Number of rows in the resulting NumPy array. | 
| nb_gauss_pts | Number of Gauss points (columns) in the resulting NumPy array. | 
size specifies the shape of the resulting ndarray as a tuple (rows, nb_gauss_pts).stride specifies the step size in bytes to move to the next element in memory. Here, it is set to sizeof(double), indicating contiguous storage for each element. Definition at line 3274 of file adjoint.cpp.
| 
 | private | 
Convert symmetric tensor storage to full matrix format.
Definition at line 2724 of file adjoint.cpp.
| 
 | private | 
Convert full matrix to symmetric tensor storage format 
 
Definition at line 2739 of file adjoint.cpp.
| 
 | virtual | 
Evaluate objective function at current state.
Evaluate objective function at current finite element state.
Calls Python-defined objective function with current displacement, stress, and strain fields. Used during optimization to compute the objective value that drives the optimization process.
| coords | Gauss point coordinates | 
| u_ptr | Displacement field values | 
| stress_ptr | Stress tensor values | 
| strain_ptr | Strain tensor values | 
| o_ptr | Output objective function values | 
This method bridges MoFEM finite element data with Python-defined objective functions for topology optimization. It handles the complete data conversion workflow from MoFEM matrices to NumPy arrays, calls the Python objective function, and converts results back to MoFEM format.
Process:
The objective function typically computes scalar quantities like:
| coords | Gauss point coordinates for current element | 
| u_ptr | Displacement field values at Gauss points | 
| stress_ptr | Cauchy stress tensor values (symmetric storage) | 
| strain_ptr | Strain tensor values (symmetric storage) | 
| o_ptr | Output objective function values at each Gauss point | 
Implements ObjectiveFunctionData.
Definition at line 2786 of file adjoint.cpp.
| 
 | virtual | 
Compute gradient of objective function with respect to strain.
Compute gradient of objective function with respect to strain tensor.
Evaluates ∂f/∂ε where f is objective function and ε is strain tensor. Used when objective function depends directly on strain measures, complementing stress-based gradients in adjoint sensitivity analysis.
| coords | Gauss point coordinates | 
| u_ptr | Displacement field values | 
| stress_ptr | Stress tensor values | 
| strain_ptr | Strain tensor values | 
| o_ptr | Output gradient values | 
This method evaluates ∂f/∂ε, the partial derivative of the objective function with respect to the strain tensor. While many structural objectives depend primarily on stress, strain-based gradients are important for certain optimization formulations and provide additional sensitivity information.
Mathematical context: For strain energy-based objectives: f = ½ε:C:ε The gradient is: ∂f/∂ε = C:ε = σ (stress tensor)
For strain-based constraints or objectives like strain concentration: ∂f/∂ε = ∂/∂ε[∫(ε_vm - ε_target)² dΩ] = 2(ε_vm - ε_target) * ∂ε_vm/∂ε
Process:
This gradient complements stress-based gradients in comprehensive sensitivity analysis for topology optimization problems.
| coords | Gauss point coordinates | 
| u_ptr | Displacement field values | 
| stress_ptr | Current stress tensor values (symmetric storage) | 
| strain_ptr | Current strain tensor values (symmetric storage) | 
| o_ptr | Output strain gradients ∂f/∂ε (symmetric storage) | 
Implements ObjectiveFunctionData.
Definition at line 2939 of file adjoint.cpp.
| 
 | virtual | 
Compute gradient of objective function with respect to stress.
Compute gradient of objective function with respect to stress tensor.
Evaluates ∂f/∂σ where f is objective function and σ is stress tensor. This gradient is used in the adjoint method to compute sensitivities efficiently. Essential for gradient-based topology optimization.
| coords | Gauss point coordinates | 
| u_ptr | Displacement field values | 
| stress_ptr | Stress tensor values | 
| strain_ptr | Strain tensor values | 
| o_ptr | Output gradient values | 
This method evaluates ∂f/∂σ, the partial derivative of the objective function with respect to the Cauchy stress tensor. This gradient is fundamental to the adjoint method for topology optimization, as it provides the driving force for the adjoint equation solution.
Mathematical context: The adjoint method requires ∂f/∂σ to compute sensitivities efficiently. For stress-based objectives like von Mises stress constraints: ∂f/∂σ = ∂/∂σ[∫(σ_vm - σ_target)² dΩ] = 2(σ_vm - σ_target) * ∂σ_vm/∂σ
Process:
The resulting gradients drive the adjoint solution that enables efficient computation of design sensitivities independent of design variable count.
| coords | Gauss point coordinates | 
| u_ptr | Displacement field values | 
| stress_ptr | Current stress tensor values (symmetric storage) | 
| strain_ptr | Current strain tensor values (symmetric storage) | 
| o_ptr | Output stress gradients ∂f/∂σ (symmetric storage) | 
Implements ObjectiveFunctionData.
Definition at line 2862 of file adjoint.cpp.
| MoFEMErrorCode ObjectiveFunctionDataImpl::evalObjectiveGradientU | ( | MatrixDouble & | coords, | 
| boost::shared_ptr< MatrixDouble > | u_ptr, | ||
| boost::shared_ptr< MatrixDouble > | stress_ptr, | ||
| boost::shared_ptr< MatrixDouble > | strain_ptr, | ||
| boost::shared_ptr< MatrixDouble > | o_ptr | ||
| ) | 
Compute gradient of objective function with respect to displacement.
Compute gradient of objective function with respect to displacement field.
Evaluates ∂f/∂u where f is objective function and u is displacement field. This provides direct sensitivity of objective to displacement changes, used as right-hand side in adjoint equation K^T * λ = ∂f/∂u.
| coords | Gauss point coordinates | 
| u_ptr | Displacement field values | 
| stress_ptr | Stress tensor values | 
| strain_ptr | Strain tensor values | 
| o_ptr | Output gradient values | 
This method evaluates ∂f/∂u, the partial derivative of the objective function 
 with respect to the displacement field. This gradient is crucial for the adjoint method as it forms the right-hand side of the adjoint equation: K^T * λ = ∂f/∂u
Mathematical context: The adjoint method solves: K^T * λ = ∂f/∂u where λ are the adjoint variables (Lagrange multipliers)
For compliance minimization: f = ½u^T * K * u The gradient is: ∂f/∂u = K * u (applied forces)
For displacement-based constraints: f = ||u - u_target||² The gradient is: ∂f/∂u = 2(u - u_target)
Process:
This gradient is fundamental to adjoint-based topology optimization, enabling efficient sensitivity computation for any number of design variables.
| coords | Gauss point coordinates | 
| u_ptr | Displacement field values | 
| stress_ptr | Current stress tensor values | 
| strain_ptr | Current strain tensor values | 
| o_ptr | Output displacement gradients ∂f/∂u | 
Definition at line 3013 of file adjoint.cpp.
| MoFEMErrorCode ObjectiveFunctionDataImpl::initPython | ( | const std::string | py_file | ) | 
Initialize Python interpreter and load objective function script.
This method sets up the Python environment for objective function evaluation by loading a Python script that defines the required optimization functions. It establishes the bridge between MoFEM's C++ finite element computations and user-defined Python objective functions.
The Python script must define the following functions:
All functions receive NumPy arrays and must return NumPy arrays of appropriate dimensions for the finite element computation.
| py_file | Path to Python script containing objective function definitions | 
| MOFEM_OPERATION_UNSUCCESSFUL | if Python script has errors | 
Definition at line 2697 of file adjoint.cpp.
| 
 | virtual | 
Return number of topology optimization modes for given material block.
Implements ObjectiveFunctionData.
Definition at line 3220 of file adjoint.cpp.
| 
 | private | 
Internal implementation for objective function evaluation.
Handles low-level Python function call with NumPy array conversion. Converts MoFEM matrices to NumPy arrays, calls Python function, and handles return value conversion.
| coords | NumPy array of coordinates | 
| u | NumPy array of displacements | 
| stress | NumPy array of stress tensors | 
| strain | NumPy array of strain tensors | 
| o | Output NumPy array for objective values | 
Definition at line 3134 of file adjoint.cpp.
| 
 | private | 
Internal implementation for strain gradient computation.
Evaluates ∂f/∂ε through Python interface with NumPy array handling. Provides strain-based sensitivities for comprehensive gradient computation.
| coords | NumPy array of coordinates | 
| u | NumPy array of displacements | 
| stress | NumPy array of stress tensors | 
| strain | NumPy array of strain tensors | 
| o | Output NumPy array for gradient values | 
Definition at line 3177 of file adjoint.cpp.
| 
 | private | 
Internal implementation for stress gradient computation.
Calls Python function to compute ∂f/∂σ with automatic array conversion. Essential for adjoint-based sensitivity analysis in topology optimization.
| coords | NumPy array of coordinates | 
| u | NumPy array of displacements | 
| stress | NumPy array of stress tensors | 
| strain | NumPy array of strain tensors | 
| o | Output NumPy array for gradient values | 
Definition at line 3155 of file adjoint.cpp.
| 
 | private | 
Internal implementation for displacement gradient computation.
Computes ∂f/∂u through Python interface for adjoint equation right-hand side. This gradient drives the adjoint solution that enables efficient sensitivity computation independent of design variable count.
| coords | NumPy array of coordinates | 
| u | NumPy array of displacements | 
| stress | NumPy array of stress tensors | 
| strain | NumPy array of strain tensors | 
| o | Output NumPy array for gradient values | 
Definition at line 3199 of file adjoint.cpp.
| 
 | private | 
Main Python namespace for script execution.
Definition at line 2498 of file adjoint.cpp.
| 
 | private | 
Python function: blockModes(block_id,coords,centroid,bbox) -> modes.
Definition at line 2504 of file adjoint.cpp.
| 
 | private | 
Python function: f(coords,u,stress,strain) -> objective.
Definition at line 2499 of file adjoint.cpp.
| 
 | private | 
Python function: ∂f/∂ε(coords,u,stress,strain) -> gradient.
Definition at line 2501 of file adjoint.cpp.
| 
 | private | 
Python function: ∂f/∂σ(coords,u,stress,strain) -> gradient 
 
Definition at line 2500 of file adjoint.cpp.
| 
 | private | 
Python function: ∂f/∂u(coords,u,stress,strain) -> gradient.
Definition at line 2502 of file adjoint.cpp.
| 
 | private | 
Python function: numberOfModes(block_id) -> int.
Definition at line 2503 of file adjoint.cpp.
