Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/elmerice/Meshers/makemoulin.py
3196 views
1
#!/usr/bin/python
2
#==========================================
3
#
4
# FILE: makemoulin.py
5
# USAGE: python makemoulin.py --meshdir mesh_dir --moulin moulin_file --partition number_of_partition
6
# DESCRIPTION: Add Moulins to partition mesh
7
#
8
# BUGS: ---
9
# 20/10/2019: Correct indexes that were read as float
10
# 10/12/2019: Use panda to read the mesh files instead of numpy
11
# Columns do not have the same size in the mesh.boundary file
12
# This leads to an error
13
# 22/12/2019: Add more info whether the Moulins has distributed or not
14
# between partitions
15
#
16
# AUTHOR: mchekki
17
# ORGANIZATION: CNRS
18
#
19
# VERSION: V1
20
# CREATED: 2017-09-05 16:00:35
21
# MODIFIED: 2019-12-23 12:40:21
22
#
23
#==========================================
24
import numpy as np
25
import sys
26
import os
27
import getopt
28
import pandas as pd
29
30
def get_opts(options):
31
try:
32
opts, args = getopt.getopt(options,"m:o:p:hv",["meshdir=","moulin=","partition=","help","verbose"])
33
except getopt.GetoptError as err:
34
print (err)
35
usage()
36
sys.exit(2)
37
nbpartition =1
38
for o, a in opts:
39
if o in ("-m", "--meshdir"):
40
mesh= a
41
elif o in ("-o", "--moulin"):
42
moulin_file= a
43
elif o in ("-p", "--partition"):
44
nbpartition= np.int(a)
45
elif o in ("-h", "--help"):
46
usage()
47
sys.exit(2)
48
else:
49
assert False, "unhandled option"
50
51
return mesh, moulin_file, nbpartition
52
def usage():
53
print ('USAGE : python makempoulin.py --meshdir mesh_dir --moulin moulin_file --partition number_of_partition')
54
55
56
def exit_error(message):
57
print('\nERROR: ',message)
58
sys.exit(1)
59
60
def file_exists(filename):
61
if os.path.exists(filename):
62
return True
63
else:
64
return False
65
66
def file_donot_exists(filename):
67
if os.path.exists(filename) == False:
68
return True
69
else:
70
return False
71
72
if __name__=='__main__':
73
74
options=sys.argv[1:]
75
mesh, moulin_file, nbpartition = get_opts(options)
76
77
Err = 0.01
78
# Define the mesh directory
79
if nbpartition >1:
80
mesh_dir = '%s/partitioning.%s'%(mesh,np.str(nbpartition))
81
if file_donot_exists(mesh_dir):
82
exit_error("Directory %s does not exit: Please use the ElmerGrid command for mesh partitioning"%(mesh_dir))
83
else:
84
mesh_dir = '%s'%(mesh)
85
if file_donot_exists(mesh_dir):
86
exit_error("Directory %s does not exit"%(mesh_dir))
87
if file_donot_exists(moulin_file):
88
exit_error("Moulin file %s does not exit"%(moulin_file))
89
90
# Read global node file and get the number of all nodes
91
# Depending on the mesh, columns do not have the same size
92
# In order to fix this , fill columns with -999999 value
93
94
nodeAll=pd.read_table('%s/mesh.nodes'%(mesh), dtype=float , header=None, sep='\s+').fillna(-999999).values
95
NnodeAll=np.size(nodeAll,0)
96
MoulinAssign=np.zeros(NnodeAll)
97
# Read the moulin coordinates
98
Moulin=pd.read_table(moulin_file, dtype=float , header=None, sep='\s+').fillna(-999999).values
99
if Moulin.size == 2:
100
Moulin=Moulin.reshape(1,2)
101
ms=np.size(Moulin,0)
102
NodeMoulin=np.zeros(ms)
103
gbc_file='%s/mesh.boundary'%(mesh)
104
gbc=pd.read_table(gbc_file, dtype=float , header=None, sep='\s+').fillna(-999999).values
105
MaxBC = np.max(gbc[:,1])
106
107
for kk in np.arange(nbpartition):
108
if nbpartition >1:
109
nodes_file='%s/part.%s.nodes'%(mesh_dir,np.str(kk+1))
110
elements_file='%s/part.%s.elements'%(mesh_dir,np.str(kk+1))
111
bc_file='%s/part.%s.boundary'%(mesh_dir,np.str(kk+1))
112
header_file='%s/part.%s.header'%(mesh_dir,np.str(kk+1))
113
else:
114
nodes_file='%s/mesh.nodes'%(mesh_dir)
115
elements_file='%s/mesh.elements'%(mesh_dir)
116
bc_file='%s/mesh.boundary'%(mesh_dir)
117
header_file='%s/mesh.header'%(mesh_dir)
118
# Open the mesh.nodes file to find the nodes number
119
nodes=pd.read_table(nodes_file, dtype=float , header=None, sep='\s+').fillna(-999999).values
120
# Open the mesh.elements
121
elements=pd.read_table(elements_file, dtype=float , header=None, sep='\s+').fillna(-999999).values
122
# Read the BC file
123
bc=pd.read_table(bc_file, dtype=float , header=None, sep='\s+').fillna(-999999).values
124
# Read the header file
125
head=open(header_file)
126
header=[]
127
for row in head:
128
header.append(np.fromstring(row, sep=' '))
129
130
if np.int(header[2][0]) == 101 :
131
exit_error("Found 101 elements in the mesh header. No need to proceed...")
132
133
Nnode=np.size(nodes,0)
134
nBC=np.size(bc,0)
135
nheader=np.size(header,0)
136
137
NodeMoulinFound=0
138
NodeMoulin=np.zeros(ms)
139
for ii in np.arange(ms):
140
xm = Moulin[ii,0]
141
ym = Moulin[ii,1]
142
for jj in np.arange(Nnode):
143
xn = nodes[jj,2]
144
yn = nodes[jj,3]
145
if (np.abs((xm-xn)*(xm-xn))+np.abs((ym-yn)*(ym-yn))) < Err:
146
idx = np.int(nodes[jj,0]-1)
147
if MoulinAssign[idx] == 0:
148
# Save Moulin nodes
149
if NodeMoulin[ii] == 0:
150
NodeMoulin[ii] = nodes[jj,0]
151
NodeMoulinFound=NodeMoulinFound+1
152
MoulinAssign[idx] = kk+1
153
else:
154
print('WARNING part.%d: Remove Moulin node %d already assigned to partition %d '%(kk+1,idx+1,MoulinAssign[idx]))
155
# Test if each Moulin has been associated a mesh node
156
if np.count_nonzero(NodeMoulin)==0:
157
print('WARNING: No moulin nodes found on partition: %d'%(kk+1))
158
else:
159
160
ms_partition=np.count_nonzero(NodeMoulin)
161
print('%d moulin nodes found on partition %d'%(ms_partition, kk+1))
162
163
# Write the 101 BC at the end of the mesh.boundary file
164
# get the minimum element index to add for 101 BC
165
MinEIndex= np.min(elements[:,0])
166
167
# Rewrite the file and add the 101 elements
168
fid1=open(bc_file,'w');
169
for ii in np.arange(nBC):
170
boundline = bc[ii,:]
171
# Remove columns with -999999 before writing
172
indexes = np.where( boundline != -999999)
173
fid1.write(" ".join(["%g"%(v) for v in boundline[indexes]]))
174
fid1.write("\n")
175
jj=0
176
for ii in np.arange(ms):
177
if (NodeMoulin[ii] >0):
178
jj = jj + 1
179
fid1.write('%g %g %g %g %g %g \n'%(nBC+jj,MaxBC+1+ii,MinEIndex,0,101,NodeMoulin[ii]))
180
fid1.close()
181
182
# Change the header file
183
fid1=open(header_file,'w')
184
if nbpartition >1:
185
fid1.write('%g %g %g \n'%(header[0][0],header[0][1],header[0][2]+ms_partition))
186
else:
187
fid1.write('%g %g %g \n'%(header[0][0],header[0][1],header[0][2]+ms))
188
fid1.write('%g \n'%(header[1][0]+1))
189
if nbpartition >1:
190
fid1.write('%g %g \n'%(101,ms_partition))
191
else:
192
fid1.write('%g %g \n'%(101,ms))
193
for ii in np.arange(2,nheader):
194
fid1.write('%g %g \n'%(header[ii][0],header[ii][1]))
195
196
fid1.close()
197
if NodeMoulinFound == 0:
198
exit_error(' ERROR: No moulin node found on all partitions ')
199
200