-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
54 lines (43 loc) · 1.69 KB
/
functions.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
import subprocess
import re
import os
def run(cmd):
completed = subprocess.run(["powershell", "-Command", cmd], capture_output=True)
return completed
def bb_pynomad(var):
try:
x = [var.get_coord(i) for i in range(var.size())] # convert the Nomad input to list
# modify the launching script
with open("Launch.py", "w") as f:
f.write("from fem_model import *\n")
f.write("from post_process import *\n")
f.write("model(" + str(x) + ")\n") # compute the model
f.write("post_process(" + str(x) + ")\n") # extract the data
f.close()
# launch the script to the powershell
cmd_output = run("abaqus cae nogui=Launch.py")
result = re.search("stdout=b'(.*)', stderr", str(cmd_output))
print(cmd_output)
# delete all the computation files
for file in os.listdir(os.getcwd()):
if file.startswith("waterbomb") or file.startswith("abaqus"):
os.remove(os.path.join(file))
# read the FEM result located in the Report.txt file
energy = report(result)
# Print the result in the python console
print("result:" + str(-energy))
# Send the result to Nomad
var.setBBO(str(energy).encode("UTF-8"))
return 1
except Exception as e:
print(f"An error occurred: {e}")
def report(result):
with open("Report.txt", "r") as file:
last_line = file.readlines()[-1]
file.close()
last_line = last_line[last_line.rfind("\t") + len("\t"):]
if result.group(1) == "" and last_line != '-inf\n':
value = -float(last_line)
else:
value = float('inf')
return value