Skip to content

Commit 2921090

Browse files
committed
fix: Cleaning up srlife-moose code
1 parent 9a49fff commit 2921090

1 file changed

Lines changed: 29 additions & 23 deletions

File tree

srlife/receiver.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
import h5py
1515

1616
# BPM: moose interface
17-
from subprocess import run
17+
import subprocess
1818
import pyhit
1919
from pyhit import moosetree
2020
import os
2121
import sys
22-
23-
ACCESS = os.getenv("ACCESS", "/Users/bmazurowski/miniforge/envs/srlife/seacas")
22+
conda_env_dir = os.environ.get("CONDA_PREFIX")
23+
# Needed to use exodus.py
24+
# NOTE: you will need to change this
25+
ACCESS = os.getenv("ACCESS", f"{conda_env_dir}/seacas")
2426
sys.path.append(os.path.join(ACCESS, "lib"))
2527
sys.path.append(os.path.join(ACCESS, "lib64"))
2628
import exodus as exo
@@ -482,7 +484,7 @@ def create_moose_thm_front_matter(self, moose_root, press, fluid_inlet_T):
482484
closure_node.append("thm_closure", type="Closures1PhaseTHM")
483485

484486
# Functions
485-
functions_node = moose_root.append("Functions")
487+
moose_root.append("Functions")
486488
# user objects needed for flux SolutionFunctions
487489
moose_root.append("UserObjects")
488490

@@ -531,7 +533,7 @@ def create_moose_thm_back_matter(
531533
if useControls:
532534
m_dot_fun = "m_dot_time_fun"
533535
func_node = moosetree.find(moose_root, func=lambda n: n.name == "Functions")
534-
if func_node == None:
536+
if func_node is None:
535537
func_node = moose_root.append("Functions")
536538
times = np.arange(start_time, end_time, dtmax)
537539
times = np.append(times, end_time)
@@ -825,10 +827,13 @@ def run_moose_thm_model(self, moose_exec, moose_input_filename):
825827
moose_input_filename (String): filename to call moose with
826828
827829
"""
828-
if run(["moose_thm-opt", "-i", moose_input_filename]):
829-
print("MOOSE FAILED!!!")
830-
else:
831-
print("MOOSE FINISHED!!!")
830+
try:
831+
print("Running MOOSE!")
832+
result = subprocess.run(["moose_thm-opt", "-i", moose_input_filename], check=True,
833+
capture_output=False, text=True)
834+
except subprocess.CalledProcessError as e:
835+
print(f"MOOSE returned error {e.returncode}")
836+
print(f"stderr: {e.stderr}")
832837

833838
def get_moose_thm_results(self, moose_input_filenames):
834839
"""
@@ -901,7 +906,8 @@ def read_moose_thm_exodus_file(moose_output_filename):
901906
if name[0:5] == "panel":
902907
if "fch_tube" in name:
903908
pressure_results = []
904-
for iStep, time in enumerate(times):
909+
for iStep in range(len(times)):
910+
# NOTE: exodus.py indexes times steps from 1
905911
press_data = model.get_variable_values(
906912
"EX_ELEM_BLOCK", iElemBlk, "p", iStep + 1
907913
)
@@ -916,7 +922,8 @@ def read_moose_thm_exodus_file(moose_output_filename):
916922
x, y, z = model.get_coord(node)
917923
nodeCoord.append([x, y, z])
918924

919-
for iStep, time in enumerate(times):
925+
for iStep in range(len(times)):
926+
# NOTE: exodus.py indexes times steps from 1
920927
temp_data = model.get_variable_values(
921928
"EX_NODAL", iElemBlk, "T_solid", iStep + 1
922929
)
@@ -1404,8 +1411,7 @@ def create_panel_components(
14041411
x_tube_prev = tube_xs[iTube - 1]
14051412
y_tube_prev = tube_ys[iTube - 1]
14061413
if (
1407-
thetas[iTube - 1] < panel_center_theta
1408-
and panel_center_theta < thetas[iTube]
1414+
thetas[iTube - 1] < panel_center_theta < thetas[iTube]
14091415
):
14101416
# this connector crosses centerline
14111417
self.create_moose_thm_split_connector_tube(
@@ -1546,7 +1552,7 @@ def add_tube_to_moose_thm_jct(panel_node, jct_name, tube_name, tube_in_out):
15461552
tube_in_out (string): specifies tube_name:in or :out connection
15471553
"""
15481554
jct_node = moosetree.find(panel_node, func=lambda n: n.name == jct_name)
1549-
if jct_node == None:
1555+
if jct_node is None:
15501556
print(f"COULD NOT FIND JCT!!! {jct_name}")
15511557
# BPMToDo: better error handling here
15521558
sys.exit()
@@ -2343,12 +2349,18 @@ def create_moose_thm_3D_tube_mesh(self, panel_node, tube_num, x_tube, y_tube):
23432349
transform="TRANSLATE",
23442350
vector_value=make_moose_hit_vector([x_tube, y_tube, 0.0]),
23452351
)
2346-
output_node = tube_mesh_root.append("Outputs", exodus="true")
2352+
tube_mesh_root.append("Outputs", exodus="true")
23472353
# write tube input to a file
23482354
tube_mesh_moose_input = f"{panel_node.name}_tube_{tube_num}"
23492355
pyhit.write(tube_mesh_moose_input + ".i", tube_mesh_root)
23502356
# run moose to generate tube mesh
2351-
run(["moose_thm-opt", "-i", tube_mesh_moose_input + ".i", "--mesh-only"])
2357+
try:
2358+
result = subprocess.run(["moose_thm-opt", "-i", tube_mesh_moose_input + ".i", "--mesh-only"],
2359+
check=True, capture_output=True, text=True)
2360+
except subprocess.CalledProcessError as e:
2361+
print(f"MOOSE returned error {e.returncode}")
2362+
print(f"stderr: {e.stderr}")
2363+
23522364
# if this runs, then output file will be below
23532365
tube_mesh_file = f"{tube_mesh_moose_input}_in.e"
23542366
# now I want to write flux bc data to this exodus file
@@ -2397,21 +2409,18 @@ def write_flux_bc_to_3d_tube_mesh(self, tube_mesh_file):
23972409
# we only want to operate on each node once
23982410
flux_bc_nodes = np.unique(flux_bc_nodes)
23992411
flux_data = np.zeros((len(self.times), model.num_nodes()))
2400-
count = 0
24012412
for node in flux_bc_nodes:
24022413
# get coords in MOOSE x,y,z coordinates (expects node index)
24032414
x, y, z = model.get_coord(node)
24042415
coord = np.array([x, y, z])
24052416
# get srlife tube coords (note: could just map coords)
2406-
r, theta, z = self.map_moose_coords_to_srlife_tube_coords(coord)
2417+
_, theta, z = self.map_moose_coords_to_srlife_tube_coords(coord)
24072418
z_tube = convert_m_to_mm(z)
24082419
for iTime, time in enumerate(self.times):
24092420
# Get the correct value of flux
24102421
flux_data[iTime, node - 1] = convert_Wmm2_to_Wm2(
24112422
self.outer_bc.flux(time, theta, z_tube)
24122423
)
2413-
if flux_data[iTime, node - 1] == 0.0:
2414-
count += 1
24152424
# make all times and variables in exodus
24162425
for iTime, time in enumerate(self.times):
24172426
model.put_time(iTime + 1, time * 3600)
@@ -2573,9 +2582,6 @@ def heat_flux_data_to_moose_thm_data(self, func_name):
25732582
x[iX], _, z = self.map_tube_coords_to_moose_thm_coords(
25742583
x_hat, y_hat, z_hat
25752584
)
2576-
flux = convert_Wmm2_to_Wm2(
2577-
bc.flux(time / 3600, theta, convert_m_to_mm(z))
2578-
)
25792585
hour_data[iX, iZ] = convert_Wmm2_to_Wm2(
25802586
bc.flux(time / 3600, theta, convert_m_to_mm(z))
25812587
)

0 commit comments

Comments
 (0)