cashocs.space_mapping.optimal_control#
Space mapping for optimal control problems.
Classes
|
Coarse Model for space mapping optimal control. |
Base class for the fine model in space mapping. |
|
|
Parameter extraction for optimal control problems. |
|
Space mapping method for optimal control problems. |
- class cashocs.space_mapping.optimal_control.CoarseModel(state_forms, bcs_list, cost_functional_form, states, controls, adjoints, config=None, riesz_scalar_products=None, control_constraints=None, initial_guess=None, ksp_options=None, adjoint_ksp_options=None, gradient_ksp_options=None, desired_weights=None, control_bcs_list=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 optimal control.
Initializes self.
- Parameters:
state_forms (Union[List[ufl.Form], ufl.Form]) – The list of weak forms for the coare state problem
bcs_list (Union[fenics.DirichletBC, List[fenics.DirichletBC], List[List[fenics.DirichletBC]]]) – 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[List[fenics.Function], fenics.Function]) – The state variables for the coarse problem
controls (Union[List[fenics.Function], fenics.Function]) – The control variables for the coarse problem
adjoints (Union[List[fenics.Function], fenics.Function]) – The adjoint variables for the coarse problem
config (Optional[io.Config]) – config: The config file for the problem, generated via
cashocs.load_config()
. Alternatively, this can also beNone
, in which case the default configurations are used, except for the optimization algorithm. This has then to be specified in thesolve
method. The default isNone
.riesz_scalar_products (Optional[Union[ufl.Form, List[ufl.Form]]]) – The scalar products for the coarse problem
control_constraints (Optional[List[List[Union[float, fenics.Function]]]]) – The box constraints for the 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.
control_bcs_list (Optional[Union[List[List[fenics.DirichletBC]], List[fenics.DirichletBC], fenics.DirichletBC]]) – A list of boundary conditions for the control variables. This is passed analogously to
bcs_list
. Default isNone
.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.
- class cashocs.space_mapping.optimal_control.FineModel[source]#
Bases:
ABC
Base class for the fine model in space mapping.
- Variables:
controls (List[fenics.Function]) – The control variables of the fine model.
cost_functional_value (float) – The current cost functional value of the fine model.
Initializes self.
- abstract solve_and_evaluate()[source]#
Solves and evaluates the fine model.
This needs to be overwritten with a custom implementation.
- Return type:
None
- controls: List[fenics.Function]#
- cost_functional_value: float#
- class cashocs.space_mapping.optimal_control.ParameterExtraction(coarse_model, cost_functional_form, states, controls, config=None, desired_weights=None, mode='initial')[source]#
Bases:
object
Parameter extraction for optimal control problems.
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[List[fenics.Function], fenics.Function]) – The state variables for the parameter extraction
controls (Union[List[fenics.Function], fenics.Function]) – The control 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 beNone
, in which case the default configurations are used, except for the optimization algorithm. This has then to be specified in thesolve
method. The default isNone
.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.optimal_control.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, scaling_factor=1.0, verbose=True, save_history=False)[source]#
Bases:
object
Space mapping method for optimal control problems.
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
scaling_factor (float) – A factor, which can be used to appropriately scale the control variables between fine and coarse model.
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