-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathChemChasteDefinitions.py
237 lines (176 loc) · 7.24 KB
/
ChemChasteDefinitions.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
import multiprocessing
import os
import subprocess
# This is a helper function that allows to run a bash command in a separate process
def execute_command(cmd):
return subprocess.call(cmd, shell=True)
def TestDimensions(problem_dim,space_dim=2,element_dim=2):
# cast dimensions if not already strings
problem_dim = str(problem_dim)
space_dim = str(space_dim)
element_dim = str(element_dim)
config_file = open("/home/chaste/projects/ChemChaste/DataInput/ChemChaste_configuration.txt", 'r')
pde_substring = "domain_problem_dimensions ="
space_substring = "domain_space_dimensions ="
element_substring = "domain_FE_element_dimensions ="
while True:
# Get next line from file
line = config_file.readline()
# if line is empty
# end of file is reached
if not line:
break
line=line.strip()
if pde_substring in line:
pde_number_container = line.split("=")[1].strip()
pde_number_list = pde_number_container.split(',')
if problem_dim not in pde_number_list:
print("not found "+ problem_dim + " in "+ pde_number_container)
return False
elif space_substring in line:
space_number_container = line.split("=")[1].strip()
space_number_list = space_number_container.split(',')
if space_dim not in space_number_list:
print("found "+ space_dim + " in "+ space_number_container)
return False
elif element_substring in line:
element_number_container = line.split("=")[1].strip()
element_number_list = element_number_container.split(',')
if element_dim not in element_number_list:
print("found "+ element_dim + " in "+ element_number_container)
return False
return True
def UpdateConfig(problem_dim,space_dim=2,element_dim=2):
# cast dimensions if not already strings
problem_dim = str(problem_dim)
space_dim = str(space_dim)
element_dim = str(element_dim)
config_filename = "/home/chaste/projects/ChemChaste/DataInput/ChemChaste_configuration.txt"
config_file = open(config_filename, 'r')
pde_substring = "domain_problem_dimensions ="
space_substring = "domain_space_dimensions ="
element_substring = "domain_FE_element_dimensions ="
mod_config_file_content = ""
for line in config_file:
line=line.strip()
if pde_substring in line:
pde_number_container = line.split("=")[1].strip()
pde_number_list = pde_number_container.split(',')
if problem_dim not in pde_number_list:
newLine = line+","+problem_dim
else:
newLine = line
elif space_substring in line:
space_number_container = line.split("=")[1].strip()
space_number_list = space_number_container.split(',')
if space_dim not in space_number_list:
newLine = line+","+space_dim
else:
newLine = line
elif element_substring in line:
element_number_container = line.split("=")[1].strip()
element_number_list = element_number_container.split(',')
if element_dim not in element_number_list:
newLine = line+","+element_dim
else:
newLine = line
else:
newLine = line
mod_config_file_content += newLine +"\n"
config_file.close()
config_file = open(config_filename, "w")
config_file.write(mod_config_file_content)
config_file.close()
return True
def EditCppAndCompile(problem_dim,space_dim=2,element_dim=2):
# cast dimensions if not already a string
problem_dim = str(problem_dim).strip()
space_dim = str(space_dim).strip()
element_dim = str(element_dim).strip()
config_file = "/home/chaste/projects/ChemChaste/DataInput/ChemChaste_configuration.txt"
substring = "ChemChaste_cpp_location ="
app_location = ""
config = open(config_file, 'r')
while True:
# Get next line from file
line = config.readline()
# if line is empty
# end of file is reached
if not line:
break
line=line.strip()
if substring in line:
print()
app_location = line.split("=")[1].strip()
print("cpp loc ="+app_location)
config.close()
exe_filename = app_location
substring = "ChemChaste_cpp_location ="
exe_file = open(exe_filename, 'r')
mod_exe_filename = exe_filename.split(".cpp")[0].strip()+"_"+element_dim+"_"+space_dim+"_"+problem_dim+".cpp"
problem_dim_string = "const unsigned probDim ="
space_dim_string = "const unsigned spaceDim="
element_dim_string = "const unsigned elementDim="
mod_file_content = ""
for line in exe_file:
line=line.strip()
if problem_dim_string in line:
newLine = problem_dim_string+problem_dim+";"
elif space_dim_string in line:
newLine = space_dim_string+space_dim+";"
elif element_dim_string in line:
newLine = element_dim_string+element_dim+";"
else:
newLine = line
mod_file_content += newLine +"\n"
exe_file.close()
mod_exe_file = open(mod_exe_filename, "w")
mod_exe_file.write(mod_file_content)
mod_exe_file.close()
# new .cpp formed, make
mod_exe = ("ChemChaste"+"_"+element_dim+"_"+space_dim+"_"+problem_dim).strip()
exe_string = "cd ~/lib && sudo make -j4 "+mod_exe
os.system("cd ~/lib && sudo cmake ~/src")
os.system(exe_string)
os.system("cd ~/projects/ChemChaste")
return True
def RetriveConfigLocation():
return location
def determineExecutable(config_file):
#with open(config_file) as file:
# file_contents = file.read()
# print(file_contents)
pde_substring = "number_of_reaction_pdes ="
space_substring = "spatial_dimensions ="
element_substring = "FE_element_dimension ="
pde_number = 0
space_dim = 2
element_dim = 2
config = open(config_file, 'r')
while True:
# Get next line from file
line = config.readline()
# if line is empty
# end of file is reached
if not line:
break
line=line.strip()
if pde_substring in line:
pde_number = line.split("=")[1].strip()
#print("found number_of_reaction_pdes ="+str(pde_number))
elif space_substring in line:
space_dim = line.split("=")[1].strip()
#print("found spatial_dimensions ="+str(space_dim))
elif element_substring in line:
element_dim = line.split("=")[1].strip()
#print("found FE_element_dimension ="+str(element_dim))
config.close()
executableName = 'hello'
if TestDimensions(pde_number,space_dim,element_dim):
print("Template dimensions known")
else:
print("Makeing new executable app")
EditCppAndCompile(pde_number,space_dim,element_dim)
UpdateConfig(pde_number,space_dim,element_dim)
executableName = "cd ~/lib/projects/ChemChaste/apps/ && ./ChemChaste"+"_"+str(element_dim)+"_"+str(space_dim)+"_"+str(pde_number)+" --ID "
return executableName