-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvergence_monolithic_mono.py
264 lines (208 loc) · 8.31 KB
/
convergence_monolithic_mono.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
"""
Convergence of the HM formulation at a minimum example
running and comparing several cases with numerical reference example
TODO
manufactured solution as reference
redesign in an all-in-one class (material, model, discretization)
"""
from __future__ import print_function
import fenics as fe
import numpy as np
import matplotlib.pyplot as plt
import minimal_model_parameters as mmp
import stress_and_strain as sas
# DECLARATION
model=mmp.MMP()
ZeroScalar = fe.Constant((0))
ZeroVector = fe.Constant((0,0))
p_ref, _, _, k, mu = model.get_physical_parameters()
Nx, Ny, dt, dt_prog, Nt, _, _, _ = model.get_fem_parameters()
Length, Width, K, Lame1, Lame2, k_mu, cc = model.get_dependent_parameters()
p_ic, p_bc, p_load = model.get_icbc()
material=sas.SAS(Lame1, Lame2, K) # stress and strain
dT=fe.Constant(dt) # make time step mutable
fe.set_log_level(30) # control info/warning/error messages
vtkfile_pu = fe.File('results/mini_mono_pressure_displacement.pvd')
#vtkfile_s_total = fe.File('results/mini_mono_totalstress.pvd') # xdmf for multiple fields
## MESH (simplex elements in 2D=triangles)
mesh = fe.UnitSquareMesh(Nx, Ny)
#mesh = fe.RectangleMesh.create([fe.Point(0, 0), fe.Point(Width, Length)], [Nx,Ny], fe.CellType.Type.quadrilateral)
Pp = fe.FiniteElement('P', fe.triangle, 1)
Pu = fe.VectorElement('P', fe.triangle, 2)
element = fe.MixedElement([Pp, Pu])
V = fe.FunctionSpace(mesh, element)
Vsigma = fe.TensorFunctionSpace(mesh, "P", 1)
pu_ = fe.Function(V, name="pressure_displacement") # function solved for and written to file
#sigma_ = fe.Function(Vsigma, name="total_stress") # function solved for and written to file
# INITIAL CONDITIONS: undeformed, at rest, same pressure everywhere
pu_ic = fe.Expression(
(
p_ic, # p
"0.0","0.0", # (ux, uy)
), degree = 2)
pu_n = fe.interpolate(pu_ic, V) # current value in time-stepping
pu_.assign(fe.interpolate(pu_ic, V)) # previous value in time-stepping
# BOUNDARY CONDITIONS
# DirichletBC, assign geometry via functions
tol = 1E-14
def top(x, on_boundary):
return on_boundary and fe.near(x[1], Length, tol)
bc_top = fe.DirichletBC(V.sub(0), p_bc, top) # drainage on top
def bottom(x, on_boundary):
return on_boundary and fe.near(x[1], 0.0, tol)
bc_bottom = fe.DirichletBC(V.sub(1), ZeroVector, bottom) # fixed bottom
def side(x, on_boundary):
#return True
return on_boundary and (fe.near(x[0], 0.0, tol) or fe.near(x[0], Width, tol))
bc_side = fe.DirichletBC(V.sub(1).sub(0), ZeroScalar, side) # rollers on side, i.e. fix only in x-direction
bc = [bc_top, bc_bottom, bc_side]
# Neumann BC, assign geometry via subdomains to have ds accessible in variational problem
boundaries = fe.MeshFunction("size_t", mesh, mesh.topology().dim() - 1)
boundaries.set_all(0)
topSD = fe.AutoSubDomain(lambda x: fe.near(x[1], Length, tol))
topSD.mark(boundaries, 1) # accessable via ds(1)
ds = fe.ds(subdomain_data=boundaries)
traction_topM = fe.Constant((0, -p_load))
# FEM SYSTEM
# Define variational HM problem a(v,p)=L(v)
vp, vu = fe.TestFunctions(V)
pu = fe.TrialFunction(V)
p, u = fe.split(pu)
#vp, vu =fe.split(vpvu)
p_n, u_n = fe.split(pu_n)
Fdx = ( (fe.div(u)-fe.div(u_n))*vp
+ dT*k_mu*fe.dot(fe.grad(p/2+p_n/2), fe.grad(vp)) # midpoint
+ fe.inner(material.sigma(p,u), material.epsilon(vu)) )*fe.dx
Fds = - fe.dot(vu, traction_topM)*ds(1)
F=Fdx+Fds
a, L=fe.lhs(F), fe.rhs(F)
# no non-zero Neumann BC for H, i.e. no prescribed in-outflows (only flow via DirichletBC possible)
# no sources (H)
# no body forces (M)
# TIME-STEPPING TODO first reference solution then cases
t = 0.0
vtkfile_pu << (pu_, t)
#vtkfile_s_total << (sigma_, t)
#y_ana=np.linspace(tol, Length-tol, 10*Ny+1)
#y_mono=np.linspace(tol, Length-tol, Ny+1)
#p_mono=np.zeros((Nt, Ny+1))
#points=[ ((1.0/2.0)*Width, y_) for y_ in y_mono ]
p_history = []
u_history = []
for n in range(Nt): # time steps
t += dt
#print("reference solution:", n+1,".step t=",t)
fe.solve(a == L, pu_, bc)
pu_n.assign(pu_)
p_n, u_n = pu_n.split() #fe.split(pu_n)
#sigma_.assign(fe.project(material.sigma(p_n, u_n), Vsigma))
print(p_n(0.5, 0.5))
#p_mono[n, :] = np.array([ p_n(point) for point in points])
vtkfile_pu << (pu_, t)
#vtkfile_s_total << (sigma_, t)
dt*=dt_prog
dT.assign(dt)
p_history.append(p_n.compute_vertex_values())
u_history.append(u_n.compute_vertex_values())
##############################################################################
#run cases and setup convergence plot
cases=[]
deg_p=1
deg_u=2
Nx=2
Ny=2
cases.append((deg_p, deg_u, Nx, Ny, dt, Nt))
#Nx=4
#Ny=4
deg_p=2
deg_u=3
cases.append((deg_p, deg_u, Nx, Ny, dt, Nt))
#Nx=8
#Ny=8
deg_p=3
deg_u=4
cases.append((deg_p, deg_u, Nx, Ny, dt, Nt))
#Nx=16
#Ny=16
#deg_p=3
#deg_u=4
#cases.append((deg_p, deg_u, Nx, Ny, dt, Nt))
print("case")
for case in cases:
(deg_p, deg_u, Nx, Ny, dt, Nt)=case
meshc = fe.UnitSquareMesh(Nx, Ny)
Ppc = fe.FiniteElement('P', fe.triangle, deg_p)
Puc = fe.VectorElement('P', fe.triangle, deg_u)
elementc = fe.MixedElement([Ppc, Puc])
Vc = fe.FunctionSpace(meshc, elementc)
puc_ = fe.Function(Vc, name="pressure_displacement") # case
# INITIAL CONDITIONS: undeformed, at rest, same pressure everywhere
puc_n = fe.interpolate(pu_ic, Vc) # current value in time-stepping
puc_.assign(fe.interpolate(pu_ic, Vc)) # previous value in time-stepping
# BOUNDARY CONDITIONS
# DirichletBC, assign geometry via functions
bcc_top = fe.DirichletBC(Vc.sub(0), p_bc, top) # drainage on top
bcc_bottom = fe.DirichletBC(Vc.sub(1), ZeroVector, bottom) # fixed bottom
bcc_side = fe.DirichletBC(Vc.sub(1).sub(0), ZeroScalar, side) # rollers on side, i.e. fix only in x-direction
bcc = [bcc_top, bcc_bottom, bcc_side]
# Neumann BC, assign geometry via subdomains to have ds accessible in variational problem
boundariesc = fe.MeshFunction("size_t", meshc, meshc.topology().dim() - 1)
boundariesc.set_all(0)
topSDc = fe.AutoSubDomain(lambda x: fe.near(x[1], Length, tol))
topSDc.mark(boundariesc, 1) # accessable via ds(1)
dsc = fe.ds(subdomain_data=boundariesc)
# FEM SYSTEM
# Define variational HM problem a(v,p)=L(v)
vpc, vuc = fe.TestFunctions(Vc)
puc = fe.TrialFunction(Vc)
pc, uc = fe.split(puc)
#vp, vu =fe.split(vpvu)
pc_n, uc_n = fe.split(puc_n)
Fcdx = ( (fe.div(uc) - fe.div(uc_n))*vpc
+ dT*k_mu*fe.dot(fe.grad(pc/2+pc_n/2), fe.grad(vpc)) # midpoint
+ fe.inner(material.sigma(pc,uc), material.epsilon(vuc)) )*fe.dx
Fcds = - fe.dot(vuc, traction_topM)*dsc(1)
Fc=Fcdx+Fcds
ac, Lc=fe.lhs(Fc), fe.rhs(Fc)
# no non-zero Neumann BC for H, i.e. no prescribed in-outflows (only flow via DirichletBC possible)
# no sources (H)
# no body forces (M)
# TIME-STEPPING case solution
t = 0.0
pc_history = []
uc_history = []
for n in range(Nt): # time steps
t += dt
#print("case solution:", n+1,".step t=",t)
fe.solve(ac == Lc, puc_, bcc)
puc_n.assign(puc_)
pc_n, uc_n = puc_n.split() # fe.split(puc_n)
print(pc_n(0.5, 0.5))
dt *= dt_prog
dT.assign(dt)
pucV = fe.project(puc_n, V) # for error computation on same mesh
pcV, ucV = pucV.split()
pc_history.append(pcV.compute_vertex_values())
uc_history.append(ucV.compute_vertex_values())
# postprocessing
for n in range(Nt):
color_code=[0.9*(1-(n+1)/Nt)]*3
h=2/(Nx+Ny)
PC = pc_history[n]
PREF = p_history[n]
p_error = np.max(np.abs(PC-PREF))
plt.figure(0)
#plt.plot(np.log(h), np.log(p_error), 'o', color=color_code)
plt.plot(deg_p, np.log(p_error), 'o', color=color_code)
#plt.xlabel("log(h)")
plt.xlabel("degree p")
plt.ylabel("log(p_error)")
UC = uc_history[n]
UREF = u_history[n]
u_error = np.max(np.abs(UC-UREF))
plt.figure(1)
#plt.plot(np.log(h), np.log(u_error), 'o', color=color_code)
plt.plot(deg_u, np.log(u_error), 'o', color=color_code)
#plt.xlabel("log(h)")
plt.xlabel("degree u")
plt.ylabel("log(u_error)")