-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
604 lines (522 loc) · 21.3 KB
/
utils.py
File metadata and controls
604 lines (522 loc) · 21.3 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
import meshio
import os
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.tri import Triangulation
import matplotlib.ticker as ticker
from dolfin import *
from fenics import *
from fenicstools.Interpolation import interpolate_nonmatching_mesh
from configparser import ConfigParser
try:
from dolfin import XDMFFile, Mesh, MeshValueCollection
from dolfin.cpp.mesh import MeshFunctionSizet
except ImportError:
print("Could not import dolfin. Continuing without Dolfin support.")
def msh2xdmf(mesh_name, dim=2, directory="."):
"""
Function converting a MSH mesh into XDMF files.
The XDMF files are:
- "domain.xdmf": the domain;
- "boundaries.xdmf": the boundaries physical groups from GMSH;
"""
# Get the mesh name has prefix
prefix = mesh_name.split('.')[0]
# Read the input mesh
msh = meshio.read("{}/{}".format(directory, mesh_name))
# Generate the domain XDMF file
export_domain(msh, dim, directory, prefix)
# Generate the boundaries XDMF file
export_boundaries(msh, dim, directory, prefix)
# Export association table
export_association_table(msh, prefix, directory)
def export_domain(msh, dim, directory, prefix):
"""
Export the domain XDMF file as well as the subdomains values.
"""
# Set cell type
if dim == 2:
cell_type = "triangle"
elif dim == 3:
cell_type = "tetra"
# Generate the cell block for the domain cells
for i in msh.cells:
if i.type == cell_type:
data_array = i.data
# data_array = [arr for (t, arr) in msh.cells if t == cell_type]
if len(data_array) == 0:
print("WARNING: No domain physical group found.")
return
else:
# data = np.concatenate(data_array) # Use this expression if more than 1 domain
data = data_array
cells = [
meshio.CellBlock(
cell_type=cell_type,
data=data,
)
]
# Generate the domain cells data (for the subdomains)
try:
cell_data = {
"subdomains": [
np.concatenate(
[
msh.cell_data["gmsh:physical"][i]
for i, cellBlock in enumerate(msh.cells)
if cellBlock.type == cell_type
]
)
]
}
except KeyError:
raise ValueError(
"""
No physical group found for the domain.
Define the domain physical group.
- if dim=2, the domain is a surface
- if dim=3, the domain is a volume
"""
)
# Generate a meshio Mesh for the domain
domain = meshio.Mesh(
points=msh.points[:, :dim],
cells=cells,
cell_data=cell_data,
)
# Export the XDMF mesh of the domain
meshio.write(
"{}/{}_{}".format(directory, prefix, "domain.xdmf"),
domain,
file_format="xdmf"
)
def export_boundaries(msh, dim, directory, prefix):
"""
Export the boundaries XDMF file.
"""
# Set the cell type
if dim == 2:
cell_type = "line"
elif dim == 3:
cell_type = "triangle"
# Generate the cell block for the boundaries cells
# data_array = [arr for (t, arr) in msh.cells if t == cell_type]
data_array = []
for i in msh.cells:
if i.type == cell_type:
data_array.append(i.data)
if len(data_array) == 0:
print("WARNING: No boundary physical group found.")
return
else:
data = np.concatenate(data_array)
# data = data_array
boundaries_cells = [
meshio.CellBlock(
cell_type=cell_type,
data=data,
)
]
# Generate the boundaries cells data
cell_data = {
"boundaries": [
np.concatenate(
[
msh.cell_data["gmsh:physical"][i]
for i, cellBlock in enumerate(msh.cells)
if cellBlock.type == cell_type
]
)
]
}
# Generate the meshio Mesh for the boundaries physical groups
boundaries = meshio.Mesh(
points=msh.points[:, :dim],
cells=boundaries_cells,
cell_data=cell_data,
)
# Export the XDMF mesh of the lines boundaries
meshio.write(
"{}/{}_{}".format(directory, prefix, "boundaries.xdmf"),
boundaries,
file_format="xdmf"
)
def export_association_table(msh, prefix='mesh', directory='.', verbose=True):
"""
Display the association between the physical group label and the mesh
value.
"""
# Create association table
association_table = {}
# Display the correspondance
formatter = "|{:^20}|{:^20}|"
topbot = "+{:-^41}+".format("")
separator = "+{:-^20}+{:-^20}+".format("", "")
# Display
if verbose:
print('\n' + topbot)
print(formatter.format("GMSH label", "MeshFunction value"))
print(separator)
for label, arrays in msh.cell_sets.items():
# Get the index of the array in arrays
for i, array in enumerate(arrays):
if array.size != 0:
index = i
# Added check to make sure that the association table
# doesn't try to import irrelevant information.
if label != "gmsh:bounding_entities":
value = msh.cell_data["gmsh:physical"][index][0]
# Store the association table in a dictionnary
association_table[label] = value
# Display the association
if verbose:
print(formatter.format(label, value))
if verbose:
print(topbot)
# Export the association table
file_content = ConfigParser()
file_content["ASSOCIATION TABLE"] = association_table
file_name = "{}/{}_{}".format(directory, prefix, "association_table.ini")
with open(file_name, 'w') as f:
file_content.write(f)
def import_mesh(
prefix="mesh",
subdomains=False,
dim=2,
directory=".",
):
"""Function importing a dolfin mesh.
Arguments:
prefix (str, optional): mesh files prefix (eg. my_mesh.msh,
my_mesh_domain.xdmf, my_mesh_bondaries.xdmf). Defaults to "mesh".
subdomains (bool, optional): True if there are subdomains. Defaults to
False.
dim (int, optional): dimension of the domain. Defaults to 2.
directory (str, optional): directory of the mesh files. Defaults to ".".
Output:
- dolfin Mesh object containing the domain;
- dolfin MeshFunction object containing the physical lines (dim=2) or
surfaces (dim=3) defined in the msh file and the sub-domains;
- association table
"""
# Set the file name
domain = "{}_domain.xdmf".format(prefix)
boundaries = "{}_boundaries.xdmf".format(prefix)
# create 2 xdmf files if not converted before
if not os.path.exists("{}/{}".format(directory, domain)) or \
not os.path.exists("{}/{}".format(directory, boundaries)):
msh2xdmf("{}.msh".format(prefix), dim=dim, directory=directory)
# Import the converted domain
mesh = Mesh()
with XDMFFile("{}/{}".format(directory, domain)) as infile:
infile.read(mesh)
# Import the boundaries
boundaries_mvc = MeshValueCollection("size_t", mesh, dim=dim)
with XDMFFile("{}/{}".format(directory, boundaries)) as infile:
infile.read(boundaries_mvc, 'boundaries')
boundaries_mf = MeshFunctionSizet(mesh, boundaries_mvc)
# Import the subdomains
if subdomains:
subdomains_mvc = MeshValueCollection("size_t", mesh, dim=dim)
with XDMFFile("{}/{}".format(directory, domain)) as infile:
infile.read(subdomains_mvc, 'subdomains')
subdomains_mf = MeshFunctionSizet(mesh, subdomains_mvc)
# Import the association table
association_table_name = "{}/{}_{}".format(
directory, prefix, "association_table.ini")
file_content = ConfigParser()
file_content.read(association_table_name)
association_table = dict(file_content["ASSOCIATION TABLE"])
# Convert the value from string to int
for key, value in association_table.items():
association_table[key] = int(value)
# Return the Mesh and the MeshFunction objects
if not subdomains:
return mesh, boundaries_mf, association_table
else:
return mesh, boundaries_mf, subdomains_mf, association_table
def read_timeseries_to_npy(mesh_name, type):
"""
Read Oasis TimeSeries and export to numpy arrays.
Input:
mesh_name: mesh prefix to read
type: 'circle' or 'ellipses' or 'channel'
Output:
None, value of all timesteps stored to numpy files
"""
mesh_l, mf_boundaries_l, association_table_l = import_mesh(prefix=mesh_name, directory='mesh/{}/las'.format(type))
mesh_h, mf_boundaries_h, association_table_h = import_mesh(prefix=mesh_name, directory='mesh/{}/has'.format(type))
geometry_index = mesh_name
gdim = mesh_l.geometry().dim()
tdim = mesh_l.topology().dim()
V = FunctionSpace(mesh_h, 'CG', 2)
Q = FunctionSpace(mesh_h, 'CG', 1)
u0_ = Function(V)
u1_ = Function(V)
p_ = Function(Q)
Vl = FunctionSpace(mesh_l, 'CG', 2)
Ql = FunctionSpace(mesh_l, 'CG', 1)
u0_l = Function(Vl)
u1_l = Function(Vl)
p_l = Function(Ql)
# nv = mesh_l.num_vertices()
X = mesh_h.coordinates()
X = [X[:, i] for i in range(gdim)]
# Store mesh edges
lines = np.zeros((2*mesh_h.num_edges(), 2))
line_length = np.zeros(2*mesh_h.num_edges())
cells = np.array(mesh_h.cells())
for i, edge in enumerate(edges(mesh_h)):
lines[2*i, :] = edge.entities(0)
lines[2*i+1, :] = np.flipud(edge.entities(0))
line_length[2*i] = edge.length()
line_length[2*i+1] = edge.length()
# Read solution
x = X[0]
y = X[1]
velocity_x = TimeSeries('solution/{}_has/data/1/Timeseries/u0_from_tstep_0'.format(mesh_name))
velocity_y = TimeSeries('solution/{}_has/data/1/Timeseries/u1_from_tstep_0'.format(mesh_name))
pressure = TimeSeries('solution/{}_has/data/1/Timeseries/p_from_tstep_0'.format(mesh_name))
velocity_xl = TimeSeries('solution/{}_las/data/1/Timeseries/u0_from_tstep_0'.format(mesh_name))
velocity_yl = TimeSeries('solution/{}_las/data/1/Timeseries/u1_from_tstep_0'.format(mesh_name))
pressure_l = TimeSeries('solution/{}_las/data/1/Timeseries/p_from_tstep_0'.format(mesh_name))
for t in range(1000, 1001, 1):
time_index = t
velocity_x.retrieve(u0_.vector(), t)
velocity_y.retrieve(u1_.vector(), t)
pressure.retrieve(p_.vector(), t)
# # plot velocity contour
# plot(u0_, title='u0')
# plt.savefig('data/has/{}_has_{}_u0.png'.format(mesh_name, t))
# plt.figure()
# plot(u1_, title='u1')
# plt.savefig('data/has/{}_has_{}_u1.png'.format(mesh_name, t))
# plt.show()
w0 = u0_.compute_vertex_values(mesh_h)
w1 = u1_.compute_vertex_values(mesh_h)
C = p_.compute_vertex_values(mesh_h)
result_hx = w0
result_hy = w1
result_hp = C
if os.path.exists('data/has'):
np.savez('data/has/{}_has_{}'.format(mesh_name, t), ux=result_hx, uy=result_hy, p=result_hp)
else:
os.makedirs('data/has')
np.savez('data/has/{}_has_{}'.format(mesh_name, t), ux=result_hx, uy=result_hy, p=result_hp)
velocity_xl.retrieve(u0_l.vector(), t)
velocity_yl.retrieve(u1_l.vector(), t)
pressure_l.retrieve(p_l.vector(), t)
###############################################
# plot velocity contour of las and has and overlay with mesh
fig, ax = plt.subplots(2, 1, figsize=(18, 12))
plt.rcParams.update({'font.size': 20})
w = np.sqrt(w0**2 + w1**2)
ax[1].tricontourf(x, y, cells, w, cmap='jet', levels=100)
tri_h = Triangulation(x, y, cells)
# ax[1].triplot(tri_h, 'k-', linewidth=0.5)
ax[1].axis('off')
ax[1].set_title('(b) High resolution simulation', y=-0.1)
w0_l = u0_l.compute_vertex_values(mesh_l)
w1_l = u1_l.compute_vertex_values(mesh_l)
w_l = np.sqrt(w0_l**2 + w1_l**2)
x_l = mesh_l.coordinates()[:, 0]
y_l = mesh_l.coordinates()[:, 1]
cells_l = np.array(mesh_l.cells())
ax[0].tricontourf(x_l, y_l, cells_l, w_l, cmap='jet', levels=100)
tri_l = Triangulation(x_l, y_l, cells_l)
ax[0].triplot(tri_l, 'k-', linewidth=0.5)
ax[0].axis('off')
ax[0].set_title('(a) Low resolution simulation', y=-0.1)
fig.tight_layout()
cb1 = fig.colorbar(ax[1].tricontourf(x, y, cells, w, cmap='jet', levels=100), ax=ax.ravel().tolist(), shrink=0.95, location='right', pad=0.05, label='Velocity magnitude (m/s)')
cb1.locator = ticker.MaxNLocator(nbins=6)
# cb1.update_ticks()
cb1.set_ticks([0.0, 1.0, 2.0, 3.0, 4.0, 5.0])
ax[1].triplot(tri_h, 'k-', linewidth=0.5)
plt.savefig('data/{}_compare_{}_velocity.png'.format(mesh_name, t))
# plot pressure contour of las and has and overlay with mesh
fig, ax = plt.subplots(2, 1, figsize=(18, 12))
plt.rcParams.update({'font.size': 20})
ax[1].tricontourf(x, y, cells, C, cmap='jet', levels=100)
tri_h = Triangulation(x, y, cells)
ax[1].axis('off')
ax[1].set_title('(b) High resolution simulation', y=-0.1)
C_l = p_l.compute_vertex_values(mesh_l)
ax[0].tricontourf(x_l, y_l, cells_l, C_l, cmap='jet', levels=100)
tri_l = Triangulation(x_l, y_l, cells_l)
ax[0].triplot(tri_l, 'k-', linewidth=0.5)
ax[0].axis('off')
ax[0].set_title('(a) Low resolution simulation', y=-0.1)
fig.tight_layout()
cb2 = fig.colorbar(ax[1].tricontourf(x, y, cells, C, cmap='jet', levels=100), ax=ax.ravel().tolist(), shrink=0.95, location='right', pad=0.05, label='Pressure (Pa)')
cb2.locator = ticker.MaxNLocator(nbins=8)
# cb2.update_ticks()
cb2.set_ticks([-4.5, -3.0, -1.0, 1.0, 3.0, 5.0, 7.0, 9.0])
ax[1].triplot(tri_h, 'k-', linewidth=0.5)
plt.savefig('data/{}_compare_{}_pressure.png'.format(mesh_name, t))
###############################################
u0_l_ = interpolate_nonmatching_mesh(u0_l, V)
u1_l_ = interpolate_nonmatching_mesh(u1_l, V)
p_l_ = interpolate_nonmatching_mesh(p_l, Q)
# plt.figure()
# plot(u0_l_, title='u0')
# plt.savefig('data/las/{}_las_{}_u0_interpolate.png'.format(mesh_name, t))
# plt.figure()
# plot(u1_l_, title='u1')
# plt.savefig('data/las/{}_las_{}_u1_interpolate.png'.format(mesh_name, t))
u0_l_ = u0_l_.compute_vertex_values(mesh_h)
u1_l_ = u1_l_.compute_vertex_values(mesh_h)
p_l_ = p_l_.compute_vertex_values(mesh_h)
result_lx = u0_l_
result_ly = u1_l_
result_lp = p_l_
if os.path.exists('data/las'):
np.savez('data/las/{}_las_{}'.format(mesh_name, t), ux=result_lx, uy=result_ly, p=result_lp, geometry_index=geometry_index, time_index=time_index)
else:
os.makedirs('data/las')
np.savez('data/las/{}_las_{}'.format(mesh_name, t), ux=result_lx, uy=result_ly, p=result_lp, geometry_index=geometry_index, time_index=time_index)
# save mesh
if os.path.exists('data/mesh'):
np.savez('data/mesh/{}'.format(mesh_name), x=x, y=y, edges=lines, edge_properties=line_length, cells=cells, geometry_index=geometry_index)
else:
os.makedirs('data/mesh')
np.savez('data/mesh/{}'.format(mesh_name), x=x, y=y, edges=lines, edge_properties=line_length, cells=cells, geometry_index=geometry_index)
def plot_velocity_contour(mesh_name, t, type='circle'):
"""
Plot velocity contour of las and has and overlay with mesh
Input:
mesh_name: mesh prefix to read
t: time index
Output:
None, plot saved to file
"""
mesh_h, mf_boundaries_h, association_table_h = import_mesh(prefix=mesh_name, directory='mesh/{}/has'.format(type))
gdim = mesh_h.geometry().dim()
tdim = mesh_h.topology().dim()
V = FunctionSpace(mesh_h, 'CG', 2)
Q = FunctionSpace(mesh_h, 'CG', 1)
u0_ = Function(V)
u1_ = Function(V)
p_ = Function(Q)
# nv = mesh_l.num_vertices()
X = mesh_h.coordinates()
X = [X[:, i] for i in range(gdim)]
# Store mesh edges
lines = np.zeros((2*mesh_h.num_edges(), 2))
line_length = np.zeros(2*mesh_h.num_edges())
cells = np.array(mesh_h.cells())
for i, edge in enumerate(edges(mesh_h)):
lines[2*i, :] = edge.entities(0)
lines[2*i+1, :] = np.flipud(edge.entities(0))
line_length[2*i] = edge.length()
line_length[2*i+1] = edge.length()
# Read solution
x = X[0]
y = X[1]
velocity_x = TimeSeries('solution/{}_has/data/3/Timeseries/u0_from_tstep_0'.format(mesh_name))
velocity_y = TimeSeries('solution/{}_has/data/3/Timeseries/u1_from_tstep_0'.format(mesh_name))
pressure = TimeSeries('solution/{}_has/data/3/Timeseries/p_from_tstep_0'.format(mesh_name))
velocity_x.retrieve(u0_.vector(), t)
velocity_y.retrieve(u1_.vector(), t)
pressure.retrieve(p_.vector(), t)
# plot velocity contour
w0 = u0_.compute_vertex_values(mesh_h)
w1 = u1_.compute_vertex_values(mesh_h)
C = p_.compute_vertex_values(mesh_h)
# result_hx = w0
# result_hy = w1
# result_hp = C
w = np.sqrt(w0**2 + w1**2)
fig, ax = plt.subplots(1, 1, figsize=(26, 12))
plt.rcParams.update({'font.size': 20})
w = np.sqrt(w0**2 + w1**2)
ax.tricontourf(x, y, cells, w, cmap='jet', levels=100)
# tri_h = Triangulation(x, y, cells)
# ax.triplot(tri_h, 'k-', linewidth=0.5)
ax.axis('off')
ax.set_title('High resolution simulation', y=-0.1)
fig.tight_layout()
plt.savefig('{}_has_{}_velocity.png'.format(mesh_name, t))
def ensure_stable_calculation(mesh_name, type):
"""
A small tool to check if the calculation is stable by examining the value at 1000th timestep.
Input:
mesh_name: mesh prefix to read
type: 'circle' or 'ellipses' or 'channel'
Output:
flag: True if stable, False if not
"""
mesh_l, mf_boundaries_l, association_table_l = import_mesh(prefix=mesh_name, directory='mesh/{}/las'.format(type))
mesh_h, mf_boundaries_h, association_table_h = import_mesh(prefix=mesh_name, directory='mesh/{}/has'.format(type))
gdim = mesh_l.geometry().dim()
tdim = mesh_l.topology().dim()
Q = FunctionSpace(mesh_h, 'CG', 1)
p_ = Function(Q)
p1_ = Function(Q)
Ql = FunctionSpace(mesh_l, 'CG', 1)
pl_ = Function(Ql)
p1l_ = Function(Ql)
X = mesh_l.coordinates()
X = [X[:, i] for i in range(gdim)]
pressure = TimeSeries('solution/{}_has/data/1/Timeseries/p_from_tstep_0'.format(mesh_name))
pressure_l = TimeSeries('solution/{}_las/data/1/Timeseries/p_from_tstep_0'.format(mesh_name))
# check if 1000 timestep can be retrieved
pressure.retrieve(p_.vector(), 998)
pressure.retrieve(p1_.vector(), 999)
pressure_l.retrieve(pl_.vector(), 998)
pressure_l.retrieve(p1l_.vector(), 999)
if np.allclose(p_.compute_vertex_values(mesh_h), p1_.compute_vertex_values(mesh_h)) or np.allclose(pl_.compute_vertex_values(mesh_l), p1l_.compute_vertex_values(mesh_l)):
flag = False
else:
flag = True
return flag
def read_mesh_to_npy(mesh_name, type):
"""
Read mesh and solution from .xdmf file and save them as .npy file
Input:
mesh_name: mesh prefix to read
type: 'circle' or 'ellipses' or 'channel'
Output:
None
"""
mesh_l, mf_boundaries_l, association_table_l = import_mesh(prefix=mesh_name, directory='mesh/{}/las'.format(type))
mesh_h, mf_boundaries_h, association_table_h = import_mesh(prefix=mesh_name, directory='mesh/{}/has'.format(type))
gdim = mesh_l.geometry().dim()
X = mesh_l.coordinates()
X = [X[:, i] for i in range(gdim)]
# Store mesh edges
lines = np.zeros((2*mesh_l.num_edges(), 2))
line_length = np.zeros(2*mesh_l.num_edges())
for i, edge in enumerate(edges(mesh_l)):
lines[2*i, :] = edge.entities(0)
lines[2*i+1, :] = np.flipud(edge.entities(0))
line_length[2*i] = edge.length()
line_length[2*i+1] = edge.length()
# Read solution
x = X[0]
y = X[1]
# save mesh
if os.path.exists('data/mesh/las'):
np.savez('data/mesh/las/{}'.format(mesh_name), x=x, y=y, edges=lines, edge_properties=line_length)
else:
os.makedirs('data/mesh/las', exist_ok=True)
np.savez('data/mesh/las/{}'.format(mesh_name), x=x, y=y, edges=lines, edge_properties=line_length)
X = mesh_h.coordinates()
X = [X[:, i] for i in range(gdim)]
# Store mesh edges
lines = np.zeros((2*mesh_h.num_edges(), 2))
line_length = np.zeros(2*mesh_h.num_edges())
for i, edge in enumerate(edges(mesh_h)):
lines[2*i, :] = edge.entities(0)
lines[2*i+1, :] = np.flipud(edge.entities(0))
line_length[2*i] = edge.length()
line_length[2*i+1] = edge.length()
# Read solution
x = X[0]
y = X[1]
# save mesh
if os.path.exists('data/mesh/has'):
np.savez('data/mesh/has/{}'.format(mesh_name), x=x, y=y, edges=lines, edge_properties=line_length)
else:
os.makedirs('data/mesh/has', exist_ok=True)
np.savez('data/mesh/has/{}'.format(mesh_name), x=x, y=y, edges=lines, edge_properties=line_length)