Skip to content

Commit

Permalink
Merge branch 'main' into ralberd/material_qoi
Browse files Browse the repository at this point in the history
  • Loading branch information
ralberd committed Apr 5, 2024
2 parents 1cd4543 + b4561d9 commit 4d13fd8
Show file tree
Hide file tree
Showing 31 changed files with 138 additions and 121 deletions.
8 changes: 4 additions & 4 deletions examples/arch_bc/ArchBc.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ def run(self):

disp -= maxDisp/steps
p = Objective.param_index_update(p, 0, disp)
Uu = EqSolver.nonlinear_equation_solve(objective,
Uu,
p,
self.trSettings)
Uu, solverSuccess = EqSolver.nonlinear_equation_solve(objective,
Uu,
p,
self.trSettings)

self.write_output(Uu, p, i)

Expand Down
4 changes: 2 additions & 2 deletions examples/arch_bc/ArchTraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def run(self):

force += maxForce/steps
p = Objective.param_index_update(p, 0, force)
Uu = EqSolver.nonlinear_equation_solve(objective, Uu, p, self.trSettings, solver_algorithm=solver)
Uu, solverSuccess = EqSolver.nonlinear_equation_solve(objective, Uu, p, self.trSettings, solver_algorithm=solver)

self.write_output(Uu, p, i)

Expand All @@ -184,7 +184,7 @@ def run(self):

force -= maxForce/steps
p = Objective.param_index_update(p, 0, force)
Uu = EqSolver.nonlinear_equation_solve(objective, Uu, p, self.trSettings, solver_algorithm=solver)
Uu, solverSuccess = EqSolver.nonlinear_equation_solve(objective, Uu, p, self.trSettings, solver_algorithm=solver)

self.write_output(Uu, p, i)

Expand Down
3 changes: 1 addition & 2 deletions examples/configurational_forces/CrackConfigurationalForce.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ def run():
appliedK += 1.0
p = Objective.param_index_update(p, 0, appliedK)

Uu = EquationSolver.nonlinear_equation_solve(objective, Uu, p, solverSettings)

Uu, solverSuccess = EquationSolver.nonlinear_equation_solve(objective, Uu, p, solverSettings)

Wu = np.zeros_like(Uu)
_, configForcesOnUnknowns = sensit(Wu, Uu, p, mesh)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@
"\n",
"force = 2.0\n",
"p = Objective.param_index_update(p, 0, force) # put current total force in parameter set\n",
"Uu = EquationSolver.nonlinear_equation_solve(objective, Uu, p, solverSettings, useWarmStart=False)\n"
"Uu, solverSuccess = EquationSolver.nonlinear_equation_solve(objective, Uu, p, solverSettings, useWarmStart=False)\n"
]
},
{
Expand Down Expand Up @@ -718,7 +718,7 @@
"df = 1e-4\n",
"forcePlus = force + df\n",
"p = Objective.param_index_update(p, 0, forcePlus)\n",
"UuPlus = EquationSolver.nonlinear_equation_solve(objective, Uu, p, solverSettings, useWarmStart=False)"
"UuPlus, solverSuccess = EquationSolver.nonlinear_equation_solve(objective, Uu, p, solverSettings, useWarmStart=False)"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions examples/euler_buckle/EulerBuckle.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def run(self):
print('LOAD STEP ', i)
force += maxForce/steps
p = Objective.param_index_update(p, 0, force)
Uu = EqSolver.nonlinear_equation_solve(objective, Uu, p, self.trSettings, solver_algorithm=solver)
Uu, solverSuccess = EqSolver.nonlinear_equation_solve(objective, Uu, p, self.trSettings, solver_algorithm=solver)

self.write_output(Uu, p, i)

Expand All @@ -165,7 +165,7 @@ def run(self):
print('LOAD STEP ', i)
force -= maxForce/steps
p = Objective.param_index_update(p, 0, force)
Uu = EqSolver.nonlinear_equation_solve(objective, Uu, p, self.trSettings, solver_algorithm=solver)
Uu, solverSuccess = EqSolver.nonlinear_equation_solve(objective, Uu, p, self.trSettings, solver_algorithm=solver)

self.write_output(Uu, p, i)

Expand Down
5 changes: 2 additions & 3 deletions examples/hemisphere_cap/hemisphere_plastic_disp_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ def run(self):

disp += maxDisp/steps
p = Objective.param_index_update(p, 0, disp)
Uu = EqSolver.nonlinear_equation_solve(objective, Uu, p, self.trSettings)

Uu, solverSuccess = EqSolver.nonlinear_equation_solve(objective, Uu, p, self.trSettings)

U = self.dofManager.create_field(Uu, self.get_ubcs(p))
ivs = self.bvpFuncs.\
Expand All @@ -175,7 +174,7 @@ def run(self):

disp -= maxDisp/steps
p = Objective.param_index_update(p, 0, disp)
Uu = EqSolver.nonlinear_equation_solve(objective, Uu, p, self.trSettings)
Uu, solverSuccess = EqSolver.nonlinear_equation_solve(objective, Uu, p, self.trSettings)

U = self.dofManager.create_field(Uu, self.get_ubcs(p))
ivs = self.bvpFuncs.\
Expand Down
4 changes: 2 additions & 2 deletions examples/hemisphere_cap/hemisphere_plastic_load_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def run(self):

force += maxForce/steps
p = Objective.param_index_update(p, 0, force)
Uu = EqSolver.nonlinear_equation_solve(objective, Uu, p, self.trSettings)
Uu, solverSuccess = EqSolver.nonlinear_equation_solve(objective, Uu, p, self.trSettings)

U = self.dofManager.create_field(Uu, self.get_ubcs(p))
ivs = self.bvpFuncs.compute_updated_internal_variables(U, p[1])
Expand All @@ -214,7 +214,7 @@ def run(self):

force -= maxForce/steps
p = Objective.param_index_update(p, 0, force)
Uu = EqSolver.nonlinear_equation_solve(objective, Uu, p, self.trSettings)
Uu, solverSuccess = EqSolver.nonlinear_equation_solve(objective, Uu, p, self.trSettings)

U = self.dofManager.create_field(Uu, self.get_ubcs(p))
ivs = self.bvpFuncs.compute_updated_internal_variables(U, p[1])
Expand Down
2 changes: 1 addition & 1 deletion examples/inverse/BuckleInverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def run(self):
endDispOrTraction = maxDispOrTraction*(i+1)/N

p = Objective.param_index_update(p, 0, np.array([endDispOrTraction, 0.0]))
self.Uu = EquationSolver.nonlinear_equation_solve(self.objective, self.Uu, p, settings)
self.Uu, solverSuccess = EquationSolver.nonlinear_equation_solve(self.objective, self.Uu, p, settings)

# post process
print('step ', i)
Expand Down
2 changes: 1 addition & 1 deletion examples/multi_material_tutorial/optimism_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@
"p = Objective.param_index_update(p, 0, appliedDisp)\n",
"\n",
"# Find unknown displacements by minimizing the objective\n",
"Uu = EquationSolver.nonlinear_equation_solve(objective, Uu, p, solverSettings)\n",
"Uu, solverSuccess = EquationSolver.nonlinear_equation_solve(objective, Uu, p, solverSettings)\n",
"\n"
]
},
Expand Down
2 changes: 1 addition & 1 deletion examples/multi_material_tutorial/uniaxial_two_material.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def assemble_sparse_preconditioner_matrix(Uu, p):
p = Objective.param_index_update(p, 0, appliedDisp)

# Find unknown displacements by minimizing the objective
Uu = EquationSolver.nonlinear_equation_solve(objective, Uu, p, solverSettings)
Uu, solverSuccess = EquationSolver.nonlinear_equation_solve(objective, Uu, p, solverSettings)

# update the state variables in the new equilibrium configuration
U = create_field(Uu, p)
Expand Down
10 changes: 5 additions & 5 deletions examples/surfing_fracture/SurfingFracture.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,11 @@ def run_alternating_min(self):
print("------------------------------")

print("Minimizing disp: objective = ", dispObjective.get_value(UuDisp))
UuDisp = EqSolver.nonlinear_equation_solve(dispObjective,
UuDisp,
p,
dispSettings,
useWarmStart=False)
UuDisp, solverSuccess = EqSolver.nonlinear_equation_solve(dispObjective,
UuDisp,
p,
dispSettings,
useWarmStart=False)

Uu = Uu.at[self.dispIds].set(UuDisp)
print("Minimized disp: objective = ", dispObjective.get_value(UuDisp))
Expand Down
2 changes: 1 addition & 1 deletion examples/uniaxial/Uniaxial.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def run(self):
xDisp = i/N*maxDisp
p = Objective.param_index_update(p, 0, xDisp)

Uu = EqSolver.nonlinear_equation_solve(objective, Uu, p, settings)
Uu, solverSuccess = EqSolver.nonlinear_equation_solve(objective, Uu, p, settings)

state = self.mechanicsFunctions.\
compute_updated_internal_variables(self.create_field(Uu, p), p[1])
Expand Down
10 changes: 5 additions & 5 deletions examples/uniaxial_dynamic/UniaxialDynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ def run(self):
#print('Predicted Vx = \n', self.create_field(Vu,p)[:,0])
p = Objective.param_index_update(p, 5, UuPredicted)

Uu = EquationSolver.nonlinear_equation_solve(objective,
Uu,
p,
solverSettings,
useWarmStart=False)
Uu, solverSuccess = EquationSolver.nonlinear_equation_solve(objective,
Uu,
p,
solverSettings,
useWarmStart=False)

UuCorrection = Uu - UuPredicted
#print('Corrected Ux = \n', self.create_field(UuCorrection,p)[:,0])
Expand Down
24 changes: 12 additions & 12 deletions optimism/AlSolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,21 @@ def get_settings(penalty_scaling=4.,


def solve_sub_step(alObjective, x, ncpErrorOld, alSettings, subSettings, sub_problem_solver, sub_problem_callback=None):
x = sub_problem_solver(alObjective, x, subSettings, sub_problem_callback)
x, solverSuccess = sub_problem_solver(alObjective, x, subSettings, sub_problem_callback)

c = alObjective.constraint(x)
kappa = alObjective.kappa
alObjective.lam = np.maximum(alObjective.lam-kappa*c, 0.0)

ncpError = np.abs( alObjective.ncp(x) )
poorProgress = ncpError > alSettings.target_constraint_decrease_factor * ncpErrorOld
# check if each constraint is making progress, or if they are already small relative to the specificed constraint tolerances
poorProgress = ncpError > np.maximum(alSettings.target_constraint_decrease_factor * ncpErrorOld, 10 * alSettings.tol / np.sqrt(len(ncpError)))

if (np.any(poorProgress)):
if (np.any(poorProgress) and solverSuccess):
print('Poor progress on ncp detected, increasing some penalty parameters')
alObjective.kappa = kappa.at[poorProgress].set(alSettings.penalty_scaling*kappa[poorProgress])

return x, ncpError
return x, ncpError, solverSuccess


def linear_update(alObjective, x, rhs_func, alSettings):
Expand Down Expand Up @@ -168,13 +169,13 @@ def augmented_lagrange_solve(alObjective, x, p, alSettings, subSettings,
alObjective.update_precond(x)

if not alSettings.use_newton_only:
x, ncpError = solve_sub_step(alObjective,
x,
ncpError,
alSettings,
settings,
sub_problem_solver,
sub_problem_callback)
x, ncpError, solverSuccess = solve_sub_step(alObjective,
x,
ncpError,
alSettings,
settings,
sub_problem_solver,
sub_problem_callback)

forceErrorNorm = norm(alObjective.gradient(x))
print('force error = ', forceErrorNorm)
Expand All @@ -187,6 +188,5 @@ def augmented_lagrange_solve(alObjective, x, p, alSettings, subSettings,
return x

raise NameError('Loadstep failed to converge in', maxAlIters, 'iterations.')

return x

23 changes: 11 additions & 12 deletions optimism/EquationSolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def trust_region_least_squares_solve(objective, x, settings):
print_banner(oy, modelObjective, cgIters, trSize, stepType, True)
if settings.check_stability:
objective.check_stability(y)
return y
return y, True

modelImprove = o - modelObjective
realImprove = o - oy
Expand All @@ -300,11 +300,10 @@ def trust_region_least_squares_solve(objective, x, settings):
o = oy
J.update( objective.hessian(x) )


print("Reached the maximum number of trust region iterations or trust region is too small.")
if settings.check_stability:
objective.check_stability(x)
return x
return x, False


def is_converged(objective, x, realO, modelO, realRes, modelRes, cgIters, trSize, settings):
Expand Down Expand Up @@ -369,7 +368,7 @@ def trust_region_minimize(objective, x, settings, callback=None):
# this could potentially return an unstable solution
if is_converged(objective, x, 0.0, 0.0, g, g, 0, trSize, settings):
if callback: callback(x, objective)
return x
return x, True

cumulativeCgIters=0

Expand Down Expand Up @@ -427,7 +426,7 @@ def trust_region_minimize(objective, x, settings, callback=None):
if is_converged(objective, y, realObjective, modelObjective,
gy, g + Jd, cgIters, trSizeUsed, settings):
if callback: callback(y, objective)
return y
return y, True

modelImprove = -modelObjective
realImprove = -realObjective
Expand Down Expand Up @@ -490,14 +489,14 @@ def trust_region_minimize(objective, x, settings, callback=None):
else:
print("The trust region is still too small. Accepting, but be careful.")
if callback: callback(x, objective)
return x
return x, False

print("Reached the maximum number of trust region iterations.")
if settings.check_stability:
objective.check_stability(x)

if callback: callback(x, objective)
return x
return x, False


def newton(objective, x, settings, callback=None):
Expand Down Expand Up @@ -546,12 +545,12 @@ def apply_a(v):
o = objective.value(x)
if is_converged(objective, x, o, o,
g, g, 1, np.inf, settings):
return x
return x, True

print("Reached the maximum number of newton iterations.")
if settings.check_stability:
objective.check_stability(x)
return x
return x, False


# This solve uses a different interface. There is no globalization yet
Expand Down Expand Up @@ -579,7 +578,7 @@ def newton_solve(objective_func, solution, maxSteps=1):
with Timer(name="LinearSolve"):
solution = solution - np.reshape(np.linalg.solve(hess,gradients), gradShape)

return solution
return solution, True


def nonlinear_equation_solve(objective, x0, p, settings,
Expand All @@ -603,7 +602,7 @@ def nonlinear_equation_solve(objective, x0, p, settings,
if updatePrecond:
objective.update_precond(xBar0)

xBar = solver_algorithm(objective, xBar0, settings, callback=callback)
xBar, solverSuccess = solver_algorithm(objective, xBar0, settings, callback=callback)

return objective.invScaling * xBar
return objective.invScaling * xBar, solverSuccess

12 changes: 6 additions & 6 deletions optimism/TrustRegionSPG.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def bound_constrained_trust_region_minimize(objective, x, bounds, settings, call
if is_converged(objective, x, 0.0, 0.0, prevOptimality, prevOptimality, 0,
trSize, settings):
if callback: callback(x, objective)
return x
return x, True

# Set guess for first cauchy point step length
gHg = g@objective.hessian_vec(x, g)
Expand Down Expand Up @@ -391,7 +391,7 @@ def bound_constrained_trust_region_minimize(objective, x, bounds, settings, call
realOptimality, modelOptimality, spgIters, trSizeUsed,
settings):
if callback: callback(y, objective)
return y
return y, True

modelImprove = -modelObjective
realImprove = -realObjective
Expand Down Expand Up @@ -445,14 +445,14 @@ def bound_constrained_trust_region_minimize(objective, x, bounds, settings, call
else:
print("The trust region is still too small. Accepting, but be careful.")
if callback: callback(x, objective)
return x
return x, False

print("Reached the maximum number of trust region iterations.")
if settings.check_stability:
objective.check_stability(x)

if callback: callback(x, objective)
return x
return x, False


def solve(objective, x0, p, lowerBounds, upperBounds, settings, callback=None,
Expand All @@ -477,8 +477,8 @@ def solve(objective, x0, p, lowerBounds, upperBounds, settings, callback=None,

bounds = np.column_stack((lBar, uBar))

xBar = bound_constrained_trust_region_minimize(objective, xBar0, bounds, settings,
xBar, solverSuccess = bound_constrained_trust_region_minimize(objective, xBar0, bounds, settings,
callback=callback)

return objective.invScaling * xBar
return objective.invScaling * xBar, solverSuccess

Loading

0 comments on commit 4d13fd8

Please sign in to comment.