From 5a96fcd7fe3477c960ad7d86091e3175455ca519 Mon Sep 17 00:00:00 2001 From: Iglesia Dolci Date: Fri, 6 Dec 2024 10:31:29 +0000 Subject: [PATCH 1/6] Add all reduced functional arguments. --- .../adjoint/ensemble_reduced_functional.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/firedrake/adjoint/ensemble_reduced_functional.py b/firedrake/adjoint/ensemble_reduced_functional.py index a2f9ea7915..0ee4fd9b90 100644 --- a/firedrake/adjoint/ensemble_reduced_functional.py +++ b/firedrake/adjoint/ensemble_reduced_functional.py @@ -59,8 +59,22 @@ class EnsembleReducedFunctional(ReducedFunctional): `_. """ def __init__(self, J, control, ensemble, scatter_control=True, - gather_functional=None): - super(EnsembleReducedFunctional, self).__init__(J, control) + gather_functional=None, + derivative_components=None, + scale=1.0, tape=None, + eval_cb_pre=lambda *args: None, + eval_cb_post=lambda *args: None, + derivative_cb_pre=lambda controls: controls, + derivative_cb_post=lambda checkpoint, derivative_components, controls: derivative_components, + hessian_cb_pre=lambda *args: None, + hessian_cb_post=lambda *args: None): + super(EnsembleReducedFunctional, self).__init__( + J, control, derivative_components=derivative_components, + scale=scale, tape=tape, eval_cb_pre=eval_cb_pre, + eval_cb_post=eval_cb_post, derivative_cb_pre=derivative_cb_pre, + derivative_cb_post=derivative_cb_post, + hessian_cb_pre=hessian_cb_pre, + hessian_cb_post=hessian_cb_post) self.ensemble = ensemble self.scatter_control = scatter_control self.gather_functional = gather_functional From eb30cc7d1805a2b9612430e784ff04d9ec302848 Mon Sep 17 00:00:00 2001 From: Iglesia Dolci Date: Sun, 8 Dec 2024 10:29:59 +0000 Subject: [PATCH 2/6] Docs --- .../adjoint/ensemble_reduced_functional.py | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/firedrake/adjoint/ensemble_reduced_functional.py b/firedrake/adjoint/ensemble_reduced_functional.py index 0ee4fd9b90..38145c5cd8 100644 --- a/firedrake/adjoint/ensemble_reduced_functional.py +++ b/firedrake/adjoint/ensemble_reduced_functional.py @@ -28,7 +28,9 @@ class EnsembleReducedFunctional(ReducedFunctional): operation is employed to sum the functionals and their gradients over an ensemble communicator. - If gather_functional is present, then all the values of J are communicated to all ensemble ranks, and passed in a list to gather_functional, which is a reduced functional that expects a list of that size of the relevant types. + If gather_functional is present, then all the values of J are communicated to all ensemble + ranks, and passed in a list to gather_functional, which is a reduced functional that expects + a list of that size of the relevant types. Parameters ---------- @@ -45,6 +47,24 @@ class EnsembleReducedFunctional(ReducedFunctional): ``Ensemble.ensemble comm``. gather_functional : An instance of the :class:`pyadjoint.ReducedFunctional`. that takes in all of the Js. + derivative_components : list of int + The indices of the controls that the derivative should be computed with respect to. + If present, it overwrites ``derivative_cb_pre`` and ``derivative_cb_post``. + scale : float + A scaling factor applied to the functional and its gradient(with respect to the control). + tape : pyadjoint.Tape + A tape object that the reduced functional will use to evaluate the functional and + its gradient (or gradients). + eval_cb_pre : callable + Callback function before evaluating the functional. Input is a list of Controls. + derivative_cb_pre : callable + Callback function before evaluating derivatives. Input is a list of derivatives. + Should return a list of Controls (usually the same list as the input) to be passed + to :func:`pyadjoint.compute_gradient`. + derivative_cb_post : callable + Callback function after evaluating derivatives. Inputs are the functional, the derivative, + and the controls. All of them are the checkpointed versions. Should return a list of + derivatives (usually the same list as the input)to be returned from ``self.derivative``. See Also @@ -59,22 +79,17 @@ class EnsembleReducedFunctional(ReducedFunctional): `_. """ def __init__(self, J, control, ensemble, scatter_control=True, - gather_functional=None, - derivative_components=None, - scale=1.0, tape=None, - eval_cb_pre=lambda *args: None, + gather_functional=None, derivative_components=None, + scale=1.0, tape=None, eval_cb_pre=lambda *args: None, eval_cb_post=lambda *args: None, derivative_cb_pre=lambda controls: controls, - derivative_cb_post=lambda checkpoint, derivative_components, controls: derivative_components, - hessian_cb_pre=lambda *args: None, - hessian_cb_post=lambda *args: None): + derivative_cb_post=lambda checkpoint, derivative_components, controls: derivative_components + ): super(EnsembleReducedFunctional, self).__init__( J, control, derivative_components=derivative_components, scale=scale, tape=tape, eval_cb_pre=eval_cb_pre, eval_cb_post=eval_cb_post, derivative_cb_pre=derivative_cb_pre, - derivative_cb_post=derivative_cb_post, - hessian_cb_pre=hessian_cb_pre, - hessian_cb_post=hessian_cb_post) + derivative_cb_post=derivative_cb_post) self.ensemble = ensemble self.scatter_control = scatter_control self.gather_functional = gather_functional From e62992280cae817eefce9c7483ed7d82d8c9c26a Mon Sep 17 00:00:00 2001 From: Ig-dolci Date: Mon, 9 Dec 2024 11:25:58 +0000 Subject: [PATCH 3/6] Small fix --- .../adjoint/ensemble_reduced_functional.py | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/firedrake/adjoint/ensemble_reduced_functional.py b/firedrake/adjoint/ensemble_reduced_functional.py index 38145c5cd8..274166eb6d 100644 --- a/firedrake/adjoint/ensemble_reduced_functional.py +++ b/firedrake/adjoint/ensemble_reduced_functional.py @@ -54,17 +54,18 @@ class EnsembleReducedFunctional(ReducedFunctional): A scaling factor applied to the functional and its gradient(with respect to the control). tape : pyadjoint.Tape A tape object that the reduced functional will use to evaluate the functional and - its gradient (or gradients). - eval_cb_pre : callable + its gradients (or derivatives). + eval_cb_pre : :func: Callback function before evaluating the functional. Input is a list of Controls. - derivative_cb_pre : callable - Callback function before evaluating derivatives. Input is a list of derivatives. - Should return a list of Controls (usually the same list as the input) to be passed - to :func:`pyadjoint.compute_gradient`. - derivative_cb_post : callable - Callback function after evaluating derivatives. Inputs are the functional, the derivative, - and the controls. All of them are the checkpointed versions. Should return a list of - derivatives (usually the same list as the input)to be returned from ``self.derivative``. + derivative_cb_pre : :func: + Callback function before evaluating gradients (or derivatives). Input is a list of gradients + (or derivatives). Should return a list of Controls (usually the same list as the input) to + be passed to :func:`pyadjoint.compute_gradient`. + derivative_cb_post : :func: + Callback function after evaluating derivatives. Inputs are the functional, a list of + gradients (or derivatives), and controls. All of them are the checkpointed versions. Should + return a list of gradients (or derivatives) (usually the same list as the input)to be returned + from ``self.derivative``. See Also From ae9415cf6b4ed3090f865a55df633f3f2817b680 Mon Sep 17 00:00:00 2001 From: Daiane Iglesia Dolci <63597005+Ig-dolci@users.noreply.github.com> Date: Tue, 10 Dec 2024 15:21:16 +0000 Subject: [PATCH 4/6] Update firedrake/adjoint/ensemble_reduced_functional.py --- firedrake/adjoint/ensemble_reduced_functional.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firedrake/adjoint/ensemble_reduced_functional.py b/firedrake/adjoint/ensemble_reduced_functional.py index 274166eb6d..28ed23c140 100644 --- a/firedrake/adjoint/ensemble_reduced_functional.py +++ b/firedrake/adjoint/ensemble_reduced_functional.py @@ -51,7 +51,7 @@ class EnsembleReducedFunctional(ReducedFunctional): The indices of the controls that the derivative should be computed with respect to. If present, it overwrites ``derivative_cb_pre`` and ``derivative_cb_post``. scale : float - A scaling factor applied to the functional and its gradient(with respect to the control). + A scaling factor applied to the functional and its gradient (with respect to the control). tape : pyadjoint.Tape A tape object that the reduced functional will use to evaluate the functional and its gradients (or derivatives). From 4399b32e093d84d667f8ebd8843905b9501a0408 Mon Sep 17 00:00:00 2001 From: Iglesia Dolci Date: Tue, 10 Dec 2024 15:48:50 +0000 Subject: [PATCH 5/6] More docs --- firedrake/adjoint/ensemble_reduced_functional.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/firedrake/adjoint/ensemble_reduced_functional.py b/firedrake/adjoint/ensemble_reduced_functional.py index 28ed23c140..099831f6b6 100644 --- a/firedrake/adjoint/ensemble_reduced_functional.py +++ b/firedrake/adjoint/ensemble_reduced_functional.py @@ -84,13 +84,15 @@ def __init__(self, J, control, ensemble, scatter_control=True, scale=1.0, tape=None, eval_cb_pre=lambda *args: None, eval_cb_post=lambda *args: None, derivative_cb_pre=lambda controls: controls, - derivative_cb_post=lambda checkpoint, derivative_components, controls: derivative_components - ): + derivative_cb_post=lambda checkpoint, derivative_components, controls: derivative_components, + hessian_cb_pre=lambda *args: None, hessian_cb_post=lambda *args: None): super(EnsembleReducedFunctional, self).__init__( J, control, derivative_components=derivative_components, scale=scale, tape=tape, eval_cb_pre=eval_cb_pre, eval_cb_post=eval_cb_post, derivative_cb_pre=derivative_cb_pre, - derivative_cb_post=derivative_cb_post) + derivative_cb_post=derivative_cb_post, + hessian_cb_pre=hessian_cb_pre, hessian_cb_post=hessian_cb_post) + self.ensemble = ensemble self.scatter_control = scatter_control self.gather_functional = gather_functional From 14cb0aec8d8e840e1e99fe40c3a0bb8853be520c Mon Sep 17 00:00:00 2001 From: Iglesia Dolci Date: Tue, 10 Dec 2024 15:55:11 +0000 Subject: [PATCH 6/6] More docs --- .../adjoint/ensemble_reduced_functional.py | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/firedrake/adjoint/ensemble_reduced_functional.py b/firedrake/adjoint/ensemble_reduced_functional.py index 099831f6b6..d0272d8e09 100644 --- a/firedrake/adjoint/ensemble_reduced_functional.py +++ b/firedrake/adjoint/ensemble_reduced_functional.py @@ -51,22 +51,29 @@ class EnsembleReducedFunctional(ReducedFunctional): The indices of the controls that the derivative should be computed with respect to. If present, it overwrites ``derivative_cb_pre`` and ``derivative_cb_post``. scale : float - A scaling factor applied to the functional and its gradient (with respect to the control). + A scaling factor applied to the functional and its gradient(with respect to the control). tape : pyadjoint.Tape A tape object that the reduced functional will use to evaluate the functional and its gradients (or derivatives). eval_cb_pre : :func: Callback function before evaluating the functional. Input is a list of Controls. + eval_cb_pos : :func: + Callback function after evaluating the functional. Inputs are the functional value + and a list of Controls. derivative_cb_pre : :func: - Callback function before evaluating gradients (or derivatives). Input is a list of gradients - (or derivatives). Should return a list of Controls (usually the same list as the input) to - be passed to :func:`pyadjoint.compute_gradient`. + Callback function before evaluating gradients (or derivatives). Input is a list of + gradients (or derivatives). Should return a list of Controls (usually the same list as + the input) to be passed to :func:`pyadjoint.compute_gradient`. derivative_cb_post : :func: Callback function after evaluating derivatives. Inputs are the functional, a list of - gradients (or derivatives), and controls. All of them are the checkpointed versions. Should - return a list of gradients (or derivatives) (usually the same list as the input)to be returned - from ``self.derivative``. - + gradients (or derivatives), and controls. All of them are the checkpointed versions. + Should return a list of gradients (or derivatives) (usually the same list as the input) + to be returned from ``self.derivative``. + hessian_cb_pre : :func: + Callback function before evaluating the Hessian. Input is a list of Controls. + hessian_cb_post : :func: + Callback function after evaluating the Hessian. Inputs are the functional, a list of + Hessian, and controls. See Also --------