From 6c3d5c471a77da4d48de948c8ba06fd729521292 Mon Sep 17 00:00:00 2001 From: Piotr Bartman-Szwarc Date: Thu, 12 Sep 2024 17:20:00 +0200 Subject: [PATCH] small cleanup --- conmech/solvers/optimization/optimization.py | 47 ++++++++++--------- .../solvers/optimization/schur_complement.py | 1 - conmech/solvers/solver_methods.py | 12 ++--- examples/Bagirrov_Bartman_Ochal_2024.py | 2 +- examples/Makela_et_al_1998.py | 8 ++-- .../regression/test_Makela_et_al_1998.py | 2 +- tests/test_conmech/regression/test_static.py | 2 +- .../test_conmech/regression/test_static_3d.py | 18 +++---- 8 files changed, 48 insertions(+), 44 deletions(-) diff --git a/conmech/solvers/optimization/optimization.py b/conmech/solvers/optimization/optimization.py index 672f13c9..68d947e5 100644 --- a/conmech/solvers/optimization/optimization.py +++ b/conmech/solvers/optimization/optimization.py @@ -39,6 +39,22 @@ from kosopt.qsmlm import make_minimizer +QSMLM_NAMES = { + "quasi secant method", + "limited memory quasi secant method", + "quasi secant method limited memory", + "qsm", + "qsmlm", +} +GLOBAL_QSMLM_NAMES = { + "global quasi secant method", + "global limited memory quasi secant method", + "global quasi secant method limited memory", + "globqsm", + "globqsmlm", +} + + class Optimization(Solver): def __init__( self, @@ -131,34 +147,25 @@ def _solve_impl( sols.append(solution) loss.append(self.loss(solution, *args)[0]) - if self.minimizer is None and method.lower() in ( - "quasi secant method", - "limited memory quasi secant method", - "quasi secant method limited memory", - "qsm", - "qsmlm", - "subgradient", - ): + if self.minimizer is None and method.lower() in QSMLM_NAMES.union(GLOBAL_QSMLM_NAMES): self.minimizer = make_minimizer(self.loss, self.subgradient) while norm >= fixed_point_abs_tol: - if method.lower() in ( - "quasi secant method", - "limited memory quasi secant method", - "quasi secant method limited memory", - "qsm", - "qsmlm", - ): + if method.lower() in QSMLM_NAMES: solution = self.minimizer(solution, args, maxiter=maxiter) sols.append(solution.copy()) loss.append(self.loss(solution, *args)[0]) - elif method.lower() in ("subgradient",): + elif method.lower() in GLOBAL_QSMLM_NAMES: # pylint: disable=import-outside-toplevel,import-error) from kosopt import subgradient solution = subgradient.minimize( - self.minimizer, self.loss, solution, args, - maxiter=maxiter, subgradient=self.subgradient + self.minimizer, + self.loss, + solution, + args, + maxiter=maxiter, + subgradient=self.subgradient, ) sols.append(solution.copy()) loss.append(self.loss(solution, *args)[0]) @@ -195,7 +202,6 @@ def constr(x): solution = result.x sols.append(solution) loss.append(self.loss(solution, *args)[0]) - break else: result = scipy.optimize.minimize( self.loss, @@ -208,12 +214,11 @@ def constr(x): solution = result.x sols.append(solution.copy()) loss.append(self.loss(solution, *args)[0]) - break norm = np.linalg.norm(np.subtract(solution, old_solution)) old_solution = solution.copy() min_index = loss.index(np.min(loss)) - print(method, np.min(loss)) # TODO + print(method, np.min(loss)) # TODO solution = sols[min_index] return solution diff --git a/conmech/solvers/optimization/schur_complement.py b/conmech/solvers/optimization/schur_complement.py index cfbcff60..8475e7fa 100644 --- a/conmech/solvers/optimization/schur_complement.py +++ b/conmech/solvers/optimization/schur_complement.py @@ -2,7 +2,6 @@ Created at 22.02.2021 """ -import math from typing import Tuple import numpy as np diff --git a/conmech/solvers/solver_methods.py b/conmech/solvers/solver_methods.py index 2d12aff0..dfb2e256 100644 --- a/conmech/solvers/solver_methods.py +++ b/conmech/solvers/solver_methods.py @@ -252,12 +252,12 @@ def contact_subgradient( # ASSUMING `u_vector` and `nodes` have the same order! vm = interpolate_node_between(edge, var, var_old, dimension=variable_dimension) if variable_dimension == 1: - raise NotImplementedError() - vm_normal = vm[0] - vm_tangential = np.empty(0) - else: - vm_normal = (vm * normal_vector).sum() - vm_tangential = vm - vm_normal * normal_vector + raise NotImplementedError() # TODO + # vm_normal = vm[0] + # vm_tangential = np.empty(0) + # else: + vm_normal = (vm * normal_vector).sum() + vm_tangential = vm - vm_normal * normal_vector static_displacement_mean = interpolate_node_between( edge, diff --git a/examples/Bagirrov_Bartman_Ochal_2024.py b/examples/Bagirrov_Bartman_Ochal_2024.py index 3dd3534a..cb8378cb 100644 --- a/examples/Bagirrov_Bartman_Ochal_2024.py +++ b/examples/Bagirrov_Bartman_Ochal_2024.py @@ -228,7 +228,7 @@ def outer_forces(x, t=None): # plt.legend() # # plt.loglog() # plt.show() - methods = ("BFGS", "CG", "qsm", "Powell", "subgradient") + methods = ("BFGS", "CG", "qsm", "Powell", "globqsm") forces = ( 23e3 * kN, 25e3 * kN, diff --git a/examples/Makela_et_al_1998.py b/examples/Makela_et_al_1998.py index ee3fc52c..fe65f58e 100644 --- a/examples/Makela_et_al_1998.py +++ b/examples/Makela_et_al_1998.py @@ -82,10 +82,10 @@ def potential_tangential_direction( @staticmethod def subderivative_tangential_direction( - var_tau: float, static_displacement_tau: float, dt: float + var_tau: float, static_displacement_tau: float, dt: float ) -> float: - quadsum = np.sum(var_tau ** 2) - norm = quadsum ** 0.5 + quadsum = np.sum(var_tau**2) + norm = quadsum**0.5 denom = norm + quadsum coef = 1 / denom if denom != 0.0 else 0.0 return var_tau * coef @@ -273,5 +273,5 @@ def outer_forces(x, t=None): # plt.xlabel(r"Load [kN/m$^2$]") # plt.grid() # plt.show() - methods = ("BFGS", "CG", "Powell", "subgradient")[-2:] + methods = ("BFGS", "CG", "Powell", "globqsm")[-2:] main(Config(save=False, show=True, force=True).init(), methods, forces) diff --git a/tests/test_conmech/regression/test_Makela_et_al_1998.py b/tests/test_conmech/regression/test_Makela_et_al_1998.py index 3a572508..7b699e3b 100644 --- a/tests/test_conmech/regression/test_Makela_et_al_1998.py +++ b/tests/test_conmech/regression/test_Makela_et_al_1998.py @@ -120,7 +120,7 @@ def generate_test_suits(): ] test_suites.append((optimization_mtd_pow, expected_displacement_vector_pow)) - optimization_mtd_subg = "subgradient" + optimization_mtd_subg = "globqsm" expected_displacement_vector_subg = [ [0.0, 0.0], [-0.00006594, -0.00004315], diff --git a/tests/test_conmech/regression/test_static.py b/tests/test_conmech/regression/test_static.py index 52f056c5..f38bed6b 100644 --- a/tests/test_conmech/regression/test_static.py +++ b/tests/test_conmech/regression/test_static.py @@ -20,7 +20,7 @@ def solving_method(request): return request.param -@pytest.fixture(params=["BFGS", "qsm", "subgradient"]) +@pytest.fixture(params=["BFGS", "qsm", "globqsm"]) def opt_method(request): return request.param diff --git a/tests/test_conmech/regression/test_static_3d.py b/tests/test_conmech/regression/test_static_3d.py index afc48e60..61ca2d06 100644 --- a/tests/test_conmech/regression/test_static_3d.py +++ b/tests/test_conmech/regression/test_static_3d.py @@ -50,9 +50,9 @@ def outer_forces(x, t=None): setup_m02_m02 = StaticSetup(mesh_descr) expected_displacement_vector_m02_m02 = [ - [0., 0., 0.], - [0., 0., 0.], - [0., 0., 0.], + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], [0.00887781, 0.00938318, 0.01303338], [0.01254745, 0.0164677, 0.02173087], [0.00484257, 0.01694291, 0.02137632], @@ -60,18 +60,18 @@ def outer_forces(x, t=None): [0.00322087, 0.01678243, 0.0214544], [0.00253174, 0.00921583, 0.01132137], [-0.00196061, 0.00997206, 0.01218131], - [0., 0., 0.], - [0., 0., 0.], + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], [-0.0062681, 0.01092748, 0.0123151], [-0.00694282, 0.01796747, 0.021246], [-0.00016377, 0.01769789, 0.02100172], [-0.00086678, 0.0100699, 0.0117306], - [0., 0., 0.], - [0., 0., 0.], + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], [0.00494897, 0.00864805, 0.0115685], [0.00615472, 0.0164003, 0.02059693], - [0., 0., 0.], - [0., 0., 0.], + [0.0, 0.0, 0.0], + [0.0, 0.0, 0.0], [0.01361542, 0.01025, 0.01362368], [0.01573552, 0.01474276, 0.02232607], [0.01065649, 0.01625437, 0.02177309],