Skip to content

Commit

Permalink
small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Bartman-Szwarc committed Sep 12, 2024
1 parent bf89113 commit 6c3d5c4
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 44 deletions.
47 changes: 26 additions & 21 deletions conmech/solvers/optimization/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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,
Expand All @@ -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
1 change: 0 additions & 1 deletion conmech/solvers/optimization/schur_complement.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Created at 22.02.2021
"""

import math
from typing import Tuple

import numpy as np
Expand Down
12 changes: 6 additions & 6 deletions conmech/solvers/solver_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion examples/Bagirrov_Bartman_Ochal_2024.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions examples/Makela_et_al_1998.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion tests/test_conmech/regression/test_Makela_et_al_1998.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion tests/test_conmech/regression/test_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 9 additions & 9 deletions tests/test_conmech/regression/test_static_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,28 @@ 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],
[-0.00172692, 0.01755931, 0.02152949],
[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],
Expand Down

0 comments on commit 6c3d5c4

Please sign in to comment.