From 37e502188f242cd6c473dd795071afda47e161a8 Mon Sep 17 00:00:00 2001 From: Iglesia Dolci Date: Sun, 8 Dec 2024 10:49:21 +0000 Subject: [PATCH 1/2] Enable the user to set the options to be passed to the inner product solver. --- pyadjoint/optimization/rol_solver.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pyadjoint/optimization/rol_solver.py b/pyadjoint/optimization/rol_solver.py index 8a533e09..15683aec 100644 --- a/pyadjoint/optimization/rol_solver.py +++ b/pyadjoint/optimization/rol_solver.py @@ -21,6 +21,7 @@ def value(self, x, tol): def gradient(self, g, x, tol): opts = {"riesz_representation": x.inner_product} + opts.update(x.inner_product_solver_opts) self.deriv = self.rf.derivative(options=opts) g.dat = Enlist(self.deriv) @@ -63,10 +64,11 @@ def update(self, x, flag, iteration): self._val = self.rf(x.dat) class ROLVector(ROL.Vector): - def __init__(self, dat, inner_product="L2"): + def __init__(self, dat, inner_product="L2", inner_product_solver_opts={}): super(ROLVector, self).__init__() self.dat = dat self.inner_product = inner_product + self.inner_product_solver_opts = inner_product_solver_opts def plus(self, yy): for (x, y) in zip(self.dat, yy.dat): @@ -97,7 +99,8 @@ def clone(self): dat = [] for x in self.dat: dat.append(x._ad_copy()) - res = ROLVector(dat, inner_product=self.inner_product) + res = ROLVector(dat, inner_product=self.inner_product, + inner_product_solver_opts=self.inner_product_solver_opts) res.scale(0.0) return res @@ -143,7 +146,8 @@ class ROLSolver(OptimizationSolver): Use ROL to solve the given optimisation problem. """ - def __init__(self, problem, parameters, inner_product="L2"): + def __init__(self, problem, parameters, inner_product="L2", + inner_product_solver_opts=None): """ Create a new ROLSolver. @@ -155,9 +159,9 @@ def __init__(self, problem, parameters, inner_product="L2"): OptimizationSolver.__init__(self, problem, parameters) self.rolobjective = ROLObjective(problem.reduced_functional) x = [p.tape_value() for p in self.problem.reduced_functional.controls] - self.rolvector = ROLVector(x, inner_product=inner_product) + self.rolvector = ROLVector(x, inner_product=inner_product, + inner_product_solver_opts=inner_product_solver_opts) self.params_dict = parameters - self.bounds = self.__get_bounds() self.constraints = self.__get_constraints() From f9413f73646c517a780d7bb5fceb89ad3d331bb1 Mon Sep 17 00:00:00 2001 From: Iglesia Dolci Date: Sun, 8 Dec 2024 10:51:08 +0000 Subject: [PATCH 2/2] flake8 --- pyadjoint/reduced_functional.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyadjoint/reduced_functional.py b/pyadjoint/reduced_functional.py index 88186fe6..3ea159da 100644 --- a/pyadjoint/reduced_functional.py +++ b/pyadjoint/reduced_functional.py @@ -211,7 +211,7 @@ def __call__(self, values): raise TypeError( f"The control at index {i} must be an `OverloadedType` object " f"with the same type as the control, which is {control_type}" - ) + ) # Call callback. self.eval_cb_pre(self.controls.delist(values))