Skip to content

Commit

Permalink
add dg
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrbartman committed Nov 13, 2023
1 parent 6a4a335 commit 3053bd5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
3 changes: 2 additions & 1 deletion conmech/plotting/drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@author: Michał Jureczka
@author: Piotr Bartman
"""
import time

import matplotlib.pyplot as plt
import networkx as nx
Expand Down Expand Up @@ -245,7 +246,7 @@ def draw_stress(self, axes):
def get_output_path(config, format_, name):
output_dir = config.output_dir or str(config.timestamp)
directory = f"{config.outputs_path}/{output_dir}"
name = name if name else config.timestamp
name = name if name else time.time_ns()
path = f"{directory}/{name}.{format_}"
return path

Expand Down
11 changes: 10 additions & 1 deletion conmech/solvers/optimization/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,19 @@ def _solve_impl(
"quasi secant method limited memory",
"qsm",
"qsmlm",
):
# pylint: disable=import-outside-toplevel,import-error)
from kosopt import qsmlm
solution = qsmlm.minimize(
self.loss, solution, args=args, maxiter=maxiter
)
elif method.lower() in ( # TODO
"discontinuous gradient",
"discontinuous gradient method",
"dg",
):
# pylint: disable=import-outside-toplevel,import-error)
from kosopt import qsmlmi

solution = qsmlmi.minimize(
self.loss, solution, args=args, maxiter=maxiter
)
Expand Down
6 changes: 6 additions & 0 deletions conmech/solvers/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Callable, Optional

import numpy as np
import time

from conmech.dynamics.statement import Statement, Variables
from conmech.scenarios.problems import ContactLaw
Expand Down Expand Up @@ -46,6 +47,8 @@ def __init__(
)
)

self.last_timing = None

def __str__(self) -> str:
raise NotImplementedError()

Expand All @@ -58,6 +61,7 @@ def _solve_impl(
raise NotImplementedError()

def solve(self, initial_guess: np.ndarray, **kwargs) -> np.ndarray:
start = time.time()
solution = self._solve_impl(
initial_guess, velocity=self.v_vector, displacement=self.u_vector, **kwargs
)
Expand All @@ -70,4 +74,6 @@ def solve(self, initial_guess: np.ndarray, **kwargs) -> np.ndarray:
):
solution[i] = c[j]

self.last_timing = time.time() - start

return solution
11 changes: 8 additions & 3 deletions examples/Bagirov_Bartman_Ochal_2023.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def main(config: Config):

setup = StaticSetup(mesh_descr=mesh_descr)

for method in ("Powell", "BFGS", "CG", "qsm")[3:]:
for method in ("Powell", "BFGS", "CG", "qsm", "dg")[:]:
for force in (
np.asarray([23e3 * kN, 26.2e3 * kN, 27e3 * kN, 30e3 * kN]) * surface
):
Expand All @@ -130,7 +130,12 @@ def outer_forces(x, t=None):
)
drawer = Drawer(state=state, config=config)
drawer.colorful = True
drawer.draw(show=config.show, save=config.save, title=f"{method}: {force}")
drawer.draw(
show=config.show,
save=config.save,
title=f"{method}: {force}, "
f"time: {runner.step_solver.last_timing}"
)


if __name__ == "__main__":
Expand All @@ -142,4 +147,4 @@ def outer_forces(x, t=None):
Y[i] = MMLV99.potential_normal_direction(X[i])
plt.plot(X, Y)
plt.show()
main(Config().init())
main(Config(save=True, show=False).init())

0 comments on commit 3053bd5

Please sign in to comment.