-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShapeGradientMethod.py
214 lines (172 loc) · 7.18 KB
/
ShapeGradientMethod.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
from SuperMethod import *
from DiscreteShapeProblem import *
import csv
# This class extends the SuperMethod class by methods specific for the
# shape gradient method.
class ShapeGradientMethod(SuperMethod):
def __init__(self, mesh, rhs, JSONinput = None):
# Set default parameters
self.options = {}
# Set directory where the output is stored
self.options["directory"] = "solutions/gradient/"
# Set export and verbosity options
self.options["export"] = 1
self.options["verbosity"] = 1
# Set default algorithmic parameters
self.options["maxiter"] = 1000 # maximum number of iterations
self.options["sigma"] = 0.1 # Armijo line search slope parameter
self.options["beta"] = 0.5 # Armijo line search backtracking parameter
self.options["alpha0"] = 1.0 # initial step size
# Set absolute tolerance for the elasticity norm of the restricted gradient
self.options["StoppingTolerance"] = 1e-7
# Set rhs function in the state equation
if isinstance(rhs, str):
self.options["rhs"] = rhs
else:
raise Exception("The rhs for the state equation has to be a string.")
# Verify the mesh
if type(mesh) == cpp.mesh.Mesh:
self.mesh = mesh
else:
raise Exception("No valid dolfin mesh was passed to the class.")
# Set elasticity parameters (E,nu) for the gradient computation
self.options["E"] = 1.0
self.options["nu"] = 0.4
# Set the damping factor. Later used as delta > 0, with delta = damping_factor * E,
# for the elasticity operator (elasticity inner product)
self.options["damping_factor"] = 0.2
# If we have an input file, read it
if JSONinput:
if isinstance(JSONinput, str):
# Call function to overwrite default variables
adjusted_options = self.load_config(JSONinput)
else:
raise Exception("The name of the JSON input file has to be a string.")
# Open output files
if self.options["export"] > 0:
self.open_files()
# End of __init__()
# This function opens files for export
def open_files(self):
directory = self.options["directory"] if self.options["directory"] else "./"
self.f1 = File(directory + 'solution.pvd')
self.f2 = File(directory + 'shape_grad_L2.pvd')
self.f3 = File(directory + 'shape_grad.pvd')
self.f6 = open(directory + 'history.txt','w')
super().open_files()
# This function closes files for export, called by __del__()
def close_files(self):
self.f6.close()
super().close_files()
# This function implements the restricted shape Newton method
def run(self):
# Get some local variables
maxiter = self.options["maxiter"]
sigma = self.options["sigma"]
beta = self.options["beta"]
# Setup the shape problem
dsp = DiscreteShapeProblem(mesh = self.mesh, rhs = self.options["rhs"], E = self.options["E"], nu = self.options["nu"], damping_factor = self.options["damping_factor"])
# Setup the function space for the shape displacements
V = Function(dsp.U)
V.rename("disp", "disp")
V_step = Function(dsp.U)
# Create a Form for the directional shape derivative (depends on V)
directional_shape_derivative = Form(action(dsp.get_shape_derivative_form(),V))
# Write history header to file/bash if export/verbosity
if self.options["export"] > 0:
csv.writer(self.f6).writerow(('%4s' % "iter", '%13s' % "objective", '%13s' % "dirderivative", '%13s' % "alpha", '%13s' % "grad_norm"))
if self.options["verbosity"] > 0:
csv.writer(sys.stdout).writerow(('%4s' % "iter", '%13s' % "objective", '%13s' % "dirderivative", '%13s' % "alpha", '%13s' % "grad_norm"))
# Enter the gradient loop
for j in range(maxiter):
# Compute the objective
obj = dsp.compute_objective()
if self.options["export"] > 0:
# Output the solution
self.f1 << dsp.u
if self.options["export"] > 0:
# Compute the (negative) shape gradient w.r.t. the L^2 inner product in \Omega (for visualization)
dsp.compute_shape_gradient_L2(V)
# Output the L2 shape gradient
self.f2 << V
# Compute the (negative) shape gradient w.r.t. the elasticity inner product in \Omega
dsp.compute_shape_gradient(V)
if self.options["export"] > 0:
# Output the elasticity shape gradient
self.f3 << V
# Compute the directional shape derivative and the norm of the shape gradient
d_obj = assemble(directional_shape_derivative) # depends on V
grad_norm = sqrt(abs(d_obj))
# Check for convergence
if grad_norm < self.options["StoppingTolerance"]:
# Prepare the output string
output = ('%4d' % j, '%13.4e' % obj, '%13.4e' % d_obj, '%13.4e' % 0, '%13.4e' % grad_norm)
# Output iteration data to history file and stdout
if self.options["export"] > 0:
csv.writer(self.f6).writerow(output)
if self.options["verbosity"] > 0:
csv.writer(sys.stdout).writerow(output)
break
# Set the initial step size for the subsequent line search
if j==0:
# Set initial step size (at first iteration)
alpha = self.options["alpha0"]
else:
# Increase step size alpha a little bit if the previous step has been accepted
alpha /= beta
# Perform a line search
while True:
# Prepare the output string
output = ('%4d' % j, '%13.4e' % obj, '%13.4e' % d_obj, '%13.4e' % alpha, '%13.4e' % grad_norm)
# Check for validity of step size
if dsp.is_step_valid(alpha, V):
# Move the mesh
V_step.vector()[:] = alpha * V.vector()[:]
dsp.move_mesh(V_step)
# Compute the objective
obj_step = dsp.compute_objective()
# Check the Armijo condition
if obj_step < obj + sigma * alpha * d_obj:
# Successful: break while loop and go to next iteration
break
else:
# Report Armijo condition failed and restore the mesh
# Output iteration data to history file and stdout
if self.options["export"] > 0:
csv.writer(self.f6).writerow(output + ('%s' % "Armijo condition failed",))
if self.options["verbosity"] > 0:
csv.writer(sys.stdout).writerow(output + ('%s' % "Armijo condition failed",))
# Restore the mesh
dsp.restore_mesh()
else:
# Report geometric condition violated
# Output iteration data to history file and stdout
if self.options["export"] > 0:
csv.writer(self.f6).writerow(output + ('%s' % "geometry condition failed",))
if self.options["verbosity"] > 0:
csv.writer(sys.stdout).writerow(output + ('%s' % "geometry condition failed",))
# Reduce step size if Armijo or geometric condition failed
alpha *= beta
# If alpha is too small, something went wrong
if alpha < 1e-10:
raise Exception("Line search failed")
# Output iteration data to history file and stdout
if self.options["export"] > 0:
csv.writer(self.f6).writerow(output)
if self.options["verbosity"] > 0:
csv.writer(sys.stdout).writerow(output)
# End of run()
# End of ShapeGradientMethod class
# Do a standard run of the gradient method
if __name__ == "__main__":
print("Run standard configuration")
# Create a 2D mesh
meshlevel = 12
degree = 1
dim = 2
mesh = UnitDiscMesh.create(MPI.comm_world, meshlevel, degree, dim)
# Set the rhs for the PDE
rhs = "Constant(2.5)*(x + Constant(0.4) - y**2)**2 + x**2 + y**2 + - Constant(1.0)"
problem = ShapeGradientMethod(mesh = Mesh(mesh), rhs = rhs)
problem.run()
# vim: fdm=marker noet