cashocs.space_mapping.shape_optimization#

Space mapping for shape optimization problems.

Classes

CoarseModel(state_forms, bcs_list, ...[, ...])

Coarse Model for space mapping shape optimization.

FineModel(mesh)

Base class for the fine model in space mapping shape optimization.

ParameterExtraction(coarse_model, ...[, ...])

Parameter extraction for space mapping shape optimization.

SpaceMappingProblem(fine_model, ...[, ...])

Space mapping method for shape optimization.

class cashocs.space_mapping.shape_optimization.CoarseModel(state_forms, bcs_list, cost_functional_form, states, adjoints, boundaries, config=None, shape_scalar_product=None, initial_guess=None, ksp_options=None, adjoint_ksp_options=None, gradient_ksp_options=None, desired_weights=None, preconditioner_forms=None, pre_callback=None, post_callback=None, linear_solver=None, adjoint_linear_solver=None, newton_linearizations=None)[source]#

Bases: object

Coarse Model for space mapping shape optimization.

Initializes self.

Parameters:
  • state_forms (Union[ufl.Form, List[ufl.Form]]) – The list of weak forms for the coarse state problem

  • bcs_list (Union[fenics.DirichletBC, List[fenics.DirichletBC], List[List[fenics.DirichletBC]], None]) – The list of boundary conditions for the coarse problem

  • cost_functional_form (Union[List[_typing.CostFunctional], _typing.CostFunctional]) – The cost functional for the coarse problem

  • states (Union[fenics.Function, List[fenics.Function]]) – The state variables for the coarse problem

  • adjoints (Union[fenics.Function, List[fenics.Function]]) – The adjoint variables for the coarse problem

  • boundaries (fenics.MeshFunction) – A fenics MeshFunction which marks the boundaries.

  • config (Optional[io.Config]) – config: The config file for the problem, generated via cashocs.load_config(). Alternatively, this can also be None, in which case the default configurations are used, except for the optimization algorithm. This has then to be specified in the solve method. The default is None.

  • shape_scalar_product (Optional[ufl.Form]) – The scalar product for the shape optimization problem

  • initial_guess (Optional[List[fenics.Function]]) – The initial guess for solving a nonlinear state equation

  • ksp_options (Optional[Union[_typing.KspOption, List[_typing.KspOption]]]) – The list of PETSc options for the state equations.

  • adjoint_ksp_options (Optional[Union[_typing.KspOption, List[_typing.KspOption]]]) – The list of PETSc options for the adjoint equations.

  • gradient_ksp_options (Optional[Union[_typing.KspOption, List[_typing.KspOption]]]) – A list of dicts corresponding to command line options for PETSc, used to compute the (shape) gradient. If this is None, either a direct or an iterative method is used (depending on the configuration, section OptimizationRoutine, key gradient_method).

  • desired_weights (Optional[List[float]]) – The desired weights for the cost functional

  • preconditioner_forms (Optional[List[ufl.Form]]) – The list of forms for the preconditioner. The default is None, so that the preconditioner matrix is the same as the system matrix.

  • pre_callback (Optional[Callable]) – A function (without arguments) that will be called before each solve of the state system

  • post_callback (Optional[Callable]) – A function (without arguments) that will be called after the computation of the gradient.

  • linear_solver (Optional[_utils.linalg.LinearSolver]) – The linear solver (KSP) which is used to solve the linear systems arising from the discretized PDE.

  • adjoint_linear_solver (Optional[_utils.linalg.LinearSolver]) – The linear solver (KSP) which is used to solve the (linear) adjoint system.

  • newton_linearizations (Optional[Union[ufl.Form, List[ufl.Form]]]) – A (list of) UFL forms describing which (alternative) linearizations should be used for the (nonlinear) state equations when solving them (with Newton’s method). The default is None, so that the Jacobian of the supplied state forms is used.

optimize()[source]#

Solves the coarse model optimization problem.

Return type:

None

class cashocs.space_mapping.shape_optimization.FineModel(mesh)[source]#

Bases: ABC

Base class for the fine model in space mapping shape optimization.

Variables:
  • mesh – The FEM mesh for the fine model.

  • cost_functional_value (float) – The current cost functional value of the fine model.

Parameters:

mesh (fenics.Mesh) –

Initializes self.

Parameters:

mesh (fenics.Mesh) – The finite element mesh of the coarse model, used for the space mapping with the fine model.

abstract solve_and_evaluate()[source]#

Solves and evaluates the fine model.

This needs to be overwritten with a custom implementation.

Return type:

None

cost_functional_value: float#
class cashocs.space_mapping.shape_optimization.ParameterExtraction(coarse_model, cost_functional_form, states, config=None, desired_weights=None, mode='initial')[source]#

Bases: object

Parameter extraction for space mapping shape optimization.

Initializes self.

Parameters:
  • coarse_model (CoarseModel) – The coarse model optimization problem

  • cost_functional_form (Union[List[_typing.CostFunctional], _typing.CostFunctional]) – The cost functional for the parameter extraction

  • states (Union[fenics.Function, List[fenics.Function]]) – The state variables for the parameter extraction

  • config (Optional[io.Config]) – config: The config file for the problem, generated via cashocs.load_config(). Alternatively, this can also be None, in which case the default configurations are used, except for the optimization algorithm. This has then to be specified in the solve method. The default is None.

  • desired_weights (Optional[List[float]]) – The list of desired weights for the parameter extraction

  • mode (str) – The mode used for the initial guess of the parameter extraction. If this is coarse_optimum, the default, then the coarse model optimum is used as initial guess, if this is initial, then the initial guess for the optimization is used.

class cashocs.space_mapping.shape_optimization.SpaceMappingProblem(fine_model, coarse_model, parameter_extraction, method='broyden', max_iter=25, tol=0.01, use_backtracking_line_search=False, broyden_type='good', cg_type='FR', memory_size=10, verbose=True, save_history=False)[source]#

Bases: object

Space mapping method for shape optimization.

Initializes self.

Parameters:
  • fine_model (FineModel) – The fine model optimization problem

  • coarse_model (CoarseModel) – The coarse model optimization problem

  • parameter_extraction (ParameterExtraction) – The parameter extraction problem

  • method (Literal['broyden', 'bfgs', 'lbfgs', 'sd', 'steepest_descent', 'ncg']) – A string, which indicates which method is used to solve the space mapping. Can be one of “broyden”, “bfgs”, “lbfgs”, “sd”, “steepest descent”, or “ncg”. Default is “broyden”.

  • max_iter (int) – Maximum number of space mapping iterations

  • tol (float) – The tolerance used for solving the space mapping iteration

  • use_backtracking_line_search (bool) – A boolean flag, which indicates whether a backtracking line search should be used for the space mapping.

  • broyden_type (Literal['good', 'bad']) – A string, either “good” or “bad”, determining the type of Broyden’s method used. Default is “good”

  • cg_type (Literal['FR', 'PR', 'HS', 'DY', 'HZ']) – A string, either “FR”, “PR”, “HS”, “DY”, “HZ”, which indicates which NCG variant is used for solving the space mapping. Default is “FR”

  • memory_size (int) – The size of the memory for Broyden’s method and the BFGS method

  • verbose (bool) – A boolean flag which indicates, whether the output of the space mapping method should be verbose. Default is True.

  • save_history (bool) – A boolean flag which indicates, whether the history of the space mapping method should be saved to a .json file. Default is False.

inject_post_callback(function)[source]#

Changes the a-posteriori callback of the OptimizationProblem.

Parameters:

function (Callable | None) – A custom function without arguments, which will be called after the computation of the gradient(s)

Return type:

None

inject_pre_callback(function)[source]#

Changes the a-priori callback of the OptimizationProblem.

Parameters:

function (Callable | None) – A custom function without arguments, which will be called before each solve of the state system

Return type:

None

inject_pre_post_callback(pre_function, post_function)[source]#

Changes the a-priori (pre) and a-posteriori (post) callbacks of the problem.

Parameters:
  • pre_function (Callable | None) – A function without arguments, which is to be called before each solve of the state system

  • post_function (Callable | None) – A function without arguments, which is to be called after each computation of the (shape) gradient

Return type:

None

smooth_deformation(a)[source]#

Smooths a deformation vector field with a Poincaré-Steklov operator.

Parameters:

a (List[fenics.Function]) – The deformation vector field

Returns:

A smoothed deformation vector field, for the use in the scalar product.

Return type:

List[fenics.Function]

solve()[source]#

Solves the problem with the space mapping method.

Return type:

None

test_for_nonconvergence()[source]#

Tests, whether maximum number of iterations are exceeded.

Return type:

None

update_history()[source]#

Updates the space mapping history.

Return type:

None