-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_velocity_dataset.py
More file actions
176 lines (120 loc) · 4.86 KB
/
create_velocity_dataset.py
File metadata and controls
176 lines (120 loc) · 4.86 KB
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
from dolfin import *
import matplotlib.pyplot as plt
import numpy as np
#### UTILITY FUNCTIONS ####
def read_mesh_h5(mesh_path, mesh_name):
hdf = HDF5File(MPI.comm_world, mesh_path+mesh_name+'.h5', 'r')
mesh_h5 = Mesh()
hdf.read(mesh_h5, '/mesh', False)
markers = MeshFunction('size_t', mesh_h5, mesh_h5.topology().dim()) # Domain
boundaries = MeshFunction('size_t', mesh_h5, mesh_h5.topology().dim() -1)
hdf.read(markers, '/markers')
hdf.read(boundaries, '/boundaries')
return mesh_h5, boundaries, markers
def write_hdf5(mesh_path, mesh_name):
# Reads .xml file and returns .h5 file in postProcessing directory
mesh_xml = Mesh(mesh_path + mesh_name + '.xml')
boundaries = MeshFunction('size_t',mesh_xml, File + "_facet_region.xml")
markers = MeshFunction('size_t',meshObj, File + '_physical_region.xml')
hdfw = HDF5File(mesh_xml.mpi_comm(), mesh_path + mesh_name +'.h5', "w")
hdfw.write(mesh_xml, "/mesh")
hdfw.write(markers, "/markers")
hdfw.write(boundaries, '/boundaries')
hdfw.close()
#========= Mesh data ==========
mesh_path = '/mnt/d/Meshes/'
mesh_name = 'Chip_Micronit_14e5Elem'
# Load mesh from xml file ~ mesh created with gmsh
# mesh = Mesh(mesh_path + mesh_name + '.xml')
# boundaries = MeshFunction('size_t', mesh_xml,mesh_path + mesh_name + '_facet_region.xml')
# markers = MeshFunction('size_t', mesh_xml,mesh_path + mesh_name + '_physical_region.xml')
# Load mesh from HDF5 file
mesh, boundaries, markers = read_mesh_h5(mesh_path, mesh_name)
# Print mesh dimension
tdim = mesh.topology().dim()
print('Mesh dimension: ', tdim)
# Print number of vertexes in mesh
nvert = len(mesh.coordinates())
print("Number of vertexes: ", nvert)
#=========== Load boundaries and subdomains ===============
n = FacetNormal(mesh)
ds = Measure('ds', domain=mesh, subdomain_data=boundaries)
# Get boundary markers from mesh (created with gmsh)
obstacleTags = [1]
inletTag = 2
outletTag = 3
Wall1Tag = 4
Wall2Tag = 5
fluidTag = 6
# Fluid Properties
rho = 1000
mu = 1
nu = Constant(0.01)
#====== Boundary conditions for inlet and outlet ===========
pInlet = 1000.0 # Pa
pOutlet = 0.0 # Pa
# Correction factor considering physical etching of the microfluidic device
corr_factor = 2e-5 #m ~ chip thickness correction factor/ Quasi-3D domain
#================== Function Spaces: Velocity and Pressure ====================
# Get Element Shape: Triangle, etc...
elementShape = mesh.ufl_cell()
Uel = VectorElement('Lagrange', elementShape, 2) # Velocity vector field
Pel = FiniteElement('Lagrange', elementShape, 1) # Pressure field
UPel = MixedElement([Uel,Pel])
# Mixed Function Space: Pressure and Velocity
W = FunctionSpace(mesh, UPel)
# Define test functions
(v,q) = TestFunctions(W)
# Define trial functions
w = Function(W)
(u,p) = (as_vector((w[0], w[1])), w[2])
#=================== Apply boundary conditions =================
bc = []
# No-slip condition for walls
for i in obstacleTags:
bc0 = DirichletBC(W.sub(0), Constant((0.0,0.0)), boundaries, i)
bc.append(bc0)
bc.append(DirichletBC(W.sub(0), Constant((0.0,0.0)), boundaries, Wall1Tag))
bc.append(DirichletBC(W.sub(0), Constant((0.0,0.0)), boundaries, Wall2Tag))
# Inlet and outlet pressure conditions
bc.append(DirichletBC(W.sub(1), Constant(pInlet), boundaries, inletTag))
bc.append(DirichletBC(W.sub(1), Constant(pOutlet), boundaries, outletTag))
#====================== Variational form ==========================
# Linear Momentum Equation
# Inertia Term # Viscous Term # Pressure Term # Continuity
F = inner(grad(u)*u, v)*dx() + nu*inner(grad(u), grad(v))*dx() - div(v)*p*dx() + q*div(u)*dx()
dw = TrialFunction(W)
# Calculate Jacobian Matrix
J = derivative(F,w,dw)
#================= Problem and Solver definitions =================
nsproblem = NonlinearVariationalProblem(F, w, bc, J)
solver = NonlinearVariationalSolver(nsproblem)
######## Configure numerical method
system_parameters = solver.parameters
system_parameters['nonlinear_solver'] = 'newton'
system_parameters['newton_solver']['absolute_tolerance'] = 1e-10
system_parameters['newton_solver']['relative_tolerance'] = 1e-7
system_parameters['newton_solver']['maximum_iterations'] = 500
system_parameters['newton_solver']['linear_solver'] = 'mumps'
#================= Solve problem in serial =================
# solver.solve()
#================= Solve problem in parallel =================
com = MPI.comm_world
rank = MPI.rank(com)
print(rank)
MPI.barrier(com)
solver.solve()
#================= Save results =================
u,p = w.leaf_node().split()
File("u.pvd") << u
File("p.pvd") << p
# plot(u)
# plt.show()
# x = mesh.coordinates()[:,0]
# y = mesh.coordinates()[:,1]
# n_vertex = len(x)
# shape = (n_vertex, 2) # 2D data
# u_sol = np.zeros(shape)
# # Store Pressure and Velocity valures in
# u__ = u.compute_vertex_values(mesh)
# p__ = p.compute_vertex_values(mesh)