From e21ebbd57824bd1ebd7fbc23af7ddfd8935e5820 Mon Sep 17 00:00:00 2001 From: Sebastian Blauth Date: Mon, 11 Nov 2024 15:25:10 +0100 Subject: [PATCH 1/2] Make the inner optimal control problem silent in topology optimization --- .../optimal_control/optimal_control_problem.py | 7 +++++-- .../optimization_algorithm.py | 1 + .../descent_topology_algorithm.py | 3 +++ cashocs/io/output.py | 17 +++++++++++------ 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/cashocs/_optimization/optimal_control/optimal_control_problem.py b/cashocs/_optimization/optimal_control/optimal_control_problem.py index b2d2010c..595b66c2 100755 --- a/cashocs/_optimization/optimal_control/optimal_control_problem.py +++ b/cashocs/_optimization/optimal_control/optimal_control_problem.py @@ -262,6 +262,7 @@ def __init__( self, self.box_constraints, self.db ) ) + self._silent = False if bool(desired_weights is not None): self._scale_cost_functional() @@ -405,7 +406,8 @@ def solve( + \texttt{rtol} || \nabla J(u_0) || """ - log.begin("Solving the optimal control problem.", level=log.INFO) + if not self._silent: + log.begin("Solving the optimal control problem.", level=log.INFO) super().solve(algorithm=algorithm, rtol=rtol, atol=atol, max_iter=max_iter) self._setup_control_bcs() @@ -413,7 +415,8 @@ def solve( self.solver = self._setup_solver() self.solver.run() self.solver.post_processing() - log.end() + if not self._silent: + log.end() def compute_gradient(self) -> List[fenics.Function]: """Solves the Riesz problem to determine the gradient. diff --git a/cashocs/_optimization/optimization_algorithms/optimization_algorithm.py b/cashocs/_optimization/optimization_algorithms/optimization_algorithm.py index 1ea57464..71beab26 100644 --- a/cashocs/_optimization/optimization_algorithms/optimization_algorithm.py +++ b/cashocs/_optimization/optimization_algorithms/optimization_algorithm.py @@ -244,6 +244,7 @@ def _exit(self, message: str) -> None: """ if self.soft_exit: log.error(message) + log.end() else: raise _exceptions.NotConvergedError("Optimization Algorithm", message) diff --git a/cashocs/_optimization/topology_optimization/descent_topology_algorithm.py b/cashocs/_optimization/topology_optimization/descent_topology_algorithm.py index 6dc3c2ae..f6504797 100755 --- a/cashocs/_optimization/topology_optimization/descent_topology_algorithm.py +++ b/cashocs/_optimization/topology_optimization/descent_topology_algorithm.py @@ -75,6 +75,9 @@ def __init__( self._cashocs_problem.config.set("OptimizationRoutine", "rtol", "0.0") self._cashocs_problem.config.set("OptimizationRoutine", "atol", "0.0") + self._cashocs_problem._silent = True + self._cashocs_problem.output_manager._silent = True + self.successful = False self.loop_restart = False diff --git a/cashocs/io/output.py b/cashocs/io/output.py index b106088f..0a34af87 100644 --- a/cashocs/io/output.py +++ b/cashocs/io/output.py @@ -44,6 +44,8 @@ def __init__(self, db: database.Database) -> None: self.result_dir = self.config.get("Output", "result_dir") self.result_dir = self.result_dir.rstrip("/") + self._silent = False + self.time_suffix = self.config.getboolean("Output", "time_suffix") if self.time_suffix: dt_current_time = dt.now() @@ -93,15 +95,18 @@ def __init__(self, db: database.Database) -> None: def output(self) -> None: """Writes the desired output to files and console.""" - for manager in self.managers: - manager.output() + if not self._silent: + for manager in self.managers: + manager.output() def output_summary(self) -> None: """Writes the summary to files and console.""" - for manager in self.managers: - manager.output_summary() + if not self._silent: + for manager in self.managers: + manager.output_summary() def post_process(self) -> None: """Performs a postprocessing of the output.""" - for manager in self.managers: - manager.post_process() + if not self._silent: + for manager in self.managers: + manager.post_process() From b55150d6516e59c8fecedd431a32c3e280804589 Mon Sep 17 00:00:00 2001 From: Sebastian Blauth Date: Mon, 11 Nov 2024 15:58:03 +0100 Subject: [PATCH 2/2] Fix a bug with the logging --- .../optimization_algorithms/optimization_algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cashocs/_optimization/optimization_algorithms/optimization_algorithm.py b/cashocs/_optimization/optimization_algorithms/optimization_algorithm.py index 71beab26..09ae6a02 100644 --- a/cashocs/_optimization/optimization_algorithms/optimization_algorithm.py +++ b/cashocs/_optimization/optimization_algorithms/optimization_algorithm.py @@ -244,7 +244,6 @@ def _exit(self, message: str) -> None: """ if self.soft_exit: log.error(message) - log.end() else: raise _exceptions.NotConvergedError("Optimization Algorithm", message) @@ -269,6 +268,7 @@ def post_processing(self) -> None: elif self.converged_reason == -2: self.iteration -= 1 self.post_process() + log.end() self._exit("Armijo rule failed.") # Mesh Quality is too low