Skip to content

Commit 9f58612

Browse files
committed
fix writer and cleanup
1 parent bd0b4bd commit 9f58612

3 files changed

Lines changed: 51 additions & 14 deletions

File tree

calculix/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ def main(scenario_cfg, precice_cfg_path, cleanup=True):
2626
mesh_name=mesh_name,
2727
interface_name=interface_name,
2828
)
29-
writer.convert_to_vtk(sim_out, "out/output.vtk")
29+
writer.convert_to_vtk(sim_out, "out/solid.vtk")
3030
if cleanup:
31-
simulation.cleanup(simulation_folder, remove_spooles=True)
31+
simulation.cleanup(simulation_folder)
3232

3333

3434
# entry point for solid simulation with calculix and precice

calculix/simulation.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def precice_env_setup(environment):
3333
if yaml_target.exists():
3434
yaml_target.unlink()
3535

36+
3637
def run(inp_file, precice_cfg, participant="Solid", ccx_cmd="ccx_preCICE",
3738
mesh_name="Solid-Nodes-Mesh", interface_name="Solid-Interface",
3839
read_data="DisplacementDelta", write_data="Force"):
@@ -53,21 +54,49 @@ def run(inp_file, precice_cfg, participant="Solid", ccx_cmd="ccx_preCICE",
5354
with precice_env_setup(environment):
5455
run_cmd = [
5556
ccx_cmd,
56-
"-i", inp_stem_name,
57+
"-i",
58+
inp_stem_name,
5759
"-precice-participant", participant
5860
]
5961
subprocess.run(run_cmd, check=True, cwd=work_dir)
6062

6163
return work_dir / f"{inp_stem_name}.frd"
6264

6365

64-
def cleanup(sim_folder, remove_spooles=True):
65-
"""Method to clean up all calculix files except paraview output."""
66+
def cleanup(sim_folder, exclude=None):
67+
"""Method to clean up all calculix files except specifically excluded."""
6668
sim_folder = Path(sim_folder)
67-
spooles_file = Path(__file__).resolve().parent / "spooles.out"
6869

69-
if remove_spooles and spooles_file.exists():
70-
spooles_file.unlink()
70+
patterns = [
71+
"precice*",
72+
"exchange*",
73+
"m2n*",
74+
"*.nam",
75+
"*.sur",
76+
"*.log",
77+
"*.lock",
78+
"spooles.out",
79+
"*.cvg",
80+
"*.dat",
81+
"*.inp",
82+
"*.sta",
83+
"*.12d"
84+
]
85+
86+
if not exclude:
87+
exclude = []
88+
89+
if isinstance(exclude, str):
90+
exclude = [exclude]
91+
92+
if not isinstance(exclude, list):
93+
raise TypeError("exclude must be a string or list")
7194

72-
if sim_folder.exists() and sim_folder.is_dir():
73-
shutil.rmtree(sim_folder)
95+
for pattern in patterns:
96+
for p in sim_folder.glob(pattern):
97+
if p.name in exclude:
98+
continue
99+
if p.is_dir():
100+
shutil.rmtree(p, ignore_errors=True)
101+
else:
102+
p.unlink()

calculix/writer.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,23 @@ def convert_to_vtk(file_to_convert, output_path):
1414

1515
converter = Converter(str(file_to_convert), [cleaned_suffix])
1616
converter.run()
17-
# The converter does not let us specify an output file
18-
tmp_output_file = (file_to_convert.parent
19-
/ f"{file_to_convert.stem}.{cleaned_suffix}")
2017

18+
# The converter does not let us specify an output filename or path
2119
output_path = Path(output_path)
20+
converter_output = (file_to_convert.parent
21+
/ f"{file_to_convert.stem}.{cleaned_suffix}")
22+
23+
if converter_output == output_path:
24+
return output_path
2225

2326
if not output_path.parent.exists():
2427
output_path.parent.mkdir(parents=True, exist_ok=True)
2528

26-
shutil.copy(tmp_output_file, output_path)
29+
vtk_files = sorted(file_to_convert.parent.glob(f"{file_to_convert.stem}*.{cleaned_suffix}"))
2730

31+
for idx, vtk_file in enumerate(vtk_files):
32+
shutil.copy(
33+
vtk_file,
34+
output_path.parent / f"{output_path.stem}_{idx:04d}.{cleaned_suffix}"
35+
)
2836
return output_path

0 commit comments

Comments
 (0)