Skip to content

Commit

Permalink
cleanup subgradient calc
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Bartman-Szwarc committed Jul 26, 2024
1 parent f030e65 commit ac85877
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
10 changes: 4 additions & 6 deletions conmech/solvers/optimization/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from conmech.solvers.solver_methods import (
make_cost_functional,
make_equation,
make_cost_functional_subgradient,
make_subgradient,
)


Expand Down Expand Up @@ -62,11 +62,9 @@ def __init__(
variable_dimension=statement.dimension_out,
problem_dimension=statement.dimension_in,
)
if hasattr(contact_law, "subderivative_normal_direction"):
self.subgradient = make_cost_functional_subgradient(
djn=contact_law.subderivative_normal_direction, # TODO
djt=None,
dh_functional=None,
if hasattr(contact_law, "subderivative_normal_direction"): # TODO
self.subgradient = make_subgradient(
djn=contact_law.subderivative_normal_direction,
)
if isinstance(statement, WaveStatement):
if isinstance(contact_law, InteriorContactLaw):
Expand Down
23 changes: 8 additions & 15 deletions conmech/solvers/solver_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,8 @@ def contact_cost(length, normal, normal_bound, tangential, tangential_bound):
def contact_cost_functional(
var, var_old, static_displacement, nodes, contact_boundary, contact_normals, dt
):
offset = len(var) // problem_dimension

cost = 0.0

# pylint: disable=not-an-iterable
for ei in numba.prange(len(contact_boundary)):
edge = contact_boundary[ei]
Expand All @@ -188,10 +187,6 @@ def contact_cost_functional(
static_displacement_mean - static_displacement_normal * normal_vector
)

for node_id in edge:
if node_id >= offset:
continue

cost += contact_cost(
nph.length(edge, nodes),
normal_condition(vm_normal, static_displacement_normal, dt),
Expand Down Expand Up @@ -226,18 +221,16 @@ def cost_functional(
return cost_functional


def make_cost_functional_subgradient(
djn: Callable, djt: Optional[Callable] = None, dh_functional: Optional[Callable] = None
def make_subgradient(
djn: Callable,
problem_dimension=2,
):
djn = njit(djn)
djt = njit(djt)
dh_functional = njit(dh_functional)
DIMENSION = 2

@numba.njit()
def contact_subgradient(u_vector, u_vector_old, nodes, contact_boundary, contact_normals):
def contact_subgradient(u_vector, nodes, contact_boundary):
cost = np.zeros_like(u_vector)
offset = len(u_vector) // DIMENSION
offset = len(u_vector) // problem_dimension

for edge in contact_boundary:
n_id_0 = edge[0]
Expand All @@ -254,7 +247,7 @@ def contact_subgradient(u_vector, u_vector_old, nodes, contact_boundary, contact
cost[n_id_1 + offset] = cost[n_id_1]
return cost

# pylint: disable=unused-argument # 'dt'
# pylint: disable=unused-argument # takes the same args as cost_functional
@numba.njit()
def subgradient(
var,
Expand All @@ -268,7 +261,7 @@ def subgradient(
base_integrals,
dt,
):
dj = contact_subgradient(var, var_old, nodes, contact_boundary, contact_normals)
dj = contact_subgradient(var, nodes, contact_boundary)
result = np.dot(lhs, var) - rhs + dj
result = result.ravel()
return result
Expand Down

0 comments on commit ac85877

Please sign in to comment.