-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_mesh.py
105 lines (91 loc) · 4.05 KB
/
generate_mesh.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
import dolfin as df
import subprocess
import os
import numpy as np
def create_mesh(fname, dim):
subprocess.call(['gmsh -%d %s.geo' %(dim, fname)], shell=True)
assert os.path.exists(fname + '.msh')
# assert all(os.path.exists(f) for f in (fname+'.msh',
# fname + '_facet_region.msh',
# fname + '_physical_region.msh'))
def convert_msh_to_xml(fname, dim):
subprocess.call(['dolfin-convert %s.msh %s.xml'%(fname, fname)], shell=True)
assert os.path.exists(fname + '.xml')
# assert all(os.path.exists(f) for f in (fname + '.xml',
# fname + '_facet_region.xml',
# fname + '_physical_region.xml'))
def convert_xml_to_hdf5(fname):
mesh = df.Mesh(fname + ".xml")
if os.path.exists(fname + '_physical_region.xml'):
subdomains = df.MeshFunction(
"size_t", mesh, fname + "_physical_region.xml")
if os.path.exists(fname + '_facet_region.xml'):
boundaries = df.MeshFunction("size_t", mesh, fname + "_facet_region.xml")
hdf = df.HDF5File(mesh.mpi_comm(), fname + ".h5", "w")
hdf.write(mesh, "/mesh")
if os.path.exists(fname + '_physical_region.xml'):
hdf.write(subdomains, "/subdomains")
if os.path.exists(fname + '_facet_region.xml'):
hdf.write(boundaries, "/boundaries")
def get_dim(fname):
geometric_shapes = ["Line", "Surface", "Volume"]
found = [False]*3
with open(fname+'.geo') as f:
f = f.readlines()
for line in f:
for i, shape in enumerate(geometric_shapes):
if shape in line:
found[i] = True
dim = max([j+1 for j, truth in enumerate(found) if truth])
print("dim: ", dim)
return dim
if __name__ == '__main__':
import sys
from datetime import datetime
fname = sys.argv[1]
if os.path.exists(os.path.abspath(fname)+'.geo'):
dim = get_dim(fname)
geo_mtime = os.path.getmtime(os.path.abspath(fname) + '.geo')
g_mtime = datetime.fromtimestamp(geo_mtime)
print("geo-file exists and was last modified at ", g_mtime)
if os.path.exists(os.path.abspath(fname) + '.msh'):
msh_mtime = os.path.getmtime(os.path.abspath(fname) + '.msh')
if msh_mtime < geo_mtime:
print("msh-file has not been updated.")
print("Creating a new mesh.")
create_mesh(fname, dim)
else:
print("msh-file exists and it's up to date.")
if os.path.exists(os.path.abspath(fname) + '.xml'):
xml_mtime = os.path.getmtime(os.path.abspath(fname) + '.xml')
if xml_mtime < msh_mtime:
print("xml-file has not been updated.")
print("Generating a new xml-file")
convert_msh_to_xml(fname, dim)
else:
print("xml-file exists and it's up to date.")
if os.path.exists(os.path.abspath(fname) + '.h5'):
h5_mtime = os.path.getmtime(os.path.abspath(fname) + '.h5')
if h5_mtime < xml_mtime:
print("h5-file has not been updated.")
print("Generating a new h5-file")
convert_xml_to_hdf5(fname)
else:
print("h5-file exists and it's up to date.")
else:
print("Generating a new h5-file")
convert_xml_to_hdf5(fname)
else:
print("Generating a new xml-file")
convert_msh_to_xml(fname, dim)
print("Generating a new h5-file")
convert_xml_to_hdf5(fname)
else:
print("Creating the mesh")
create_mesh(fname, dim)
print("Generating a new xml-file")
convert_msh_to_xml(fname, dim)
print("Generating a new h5-file")
convert_xml_to_hdf5(fname)
else:
print("geo-file does not exists in folder.")