Skip to content

Commit

Permalink
Merge pull request #21 from tpgillam/tg/sif_header
Browse files Browse the repository at this point in the history
Add ability to configure Header section
  • Loading branch information
arvedes authored Jan 4, 2023
2 parents 9ead297 + 58f06fb commit 6d25fee
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
23 changes: 16 additions & 7 deletions pyelmer/elmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
of a dictionary called data.
"""

import os
import yaml


Expand All @@ -21,6 +22,10 @@ class Simulation:

def __init__(self):
self.intro_text = ""
self.header = {
"CHECK KEYWORDS": '"Warn"',
"Mesh DB": '"." "."',
}
self.materials = {}
self.bodies = {}
self.boundaries = {}
Expand All @@ -30,6 +35,7 @@ def __init__(self):
self.equations = {}
self.constants = {"Stefan Boltzmann": 5.6704e-08}
self.settings = {}
self.sif_filename = "case.sif"

def write_sif(self, simulation_dir):
"""Write sif file.
Expand All @@ -38,11 +44,13 @@ def write_sif(self, simulation_dir):
simulation_dir (str): Path of simulation directory
"""
self._set_ids()
with open(simulation_dir + "/case.sif", "w") as f:
with open(os.path.join(simulation_dir, self.sif_filename), "w") as f:
if self.intro_text != "":
f.write(self.intro_text)
f.write("\n\n")
f.write("""Header\n CHECK KEYWORDS "Warn"\n Mesh DB "." "."\nEnd\n\n""")
f.write("Header\n")
f.write(self._dict_to_str(self.header, key_value_separator=" "))
f.write("End\n\n")
f.write("Simulation\n")
f.write(self._dict_to_str(self.settings))
f.write("End\n\n")
Expand Down Expand Up @@ -101,8 +109,9 @@ def write_startinfo(self, simulation_dir):
Args:
simulation_dir (str): simulation directory
"""
with open(simulation_dir + "/ELMERSOLVER_STARTINFO", "w") as f:
f.write("case.sif\n")
with open(os.path.join(simulation_dir, "ELMERSOLVER_STARTINFO"), "w") as f:
f.write(self.sif_filename)
f.write("\n")

def write_boundary_ids(self, simulation_dir):
"""Write yaml-file containing the boundary names and the
Expand All @@ -115,10 +124,10 @@ def write_boundary_ids(self, simulation_dir):
with open(simulation_dir + "/boundaries.yml", "w") as f:
yaml.dump(data, f, sort_keys=False)

def _dict_to_str(self, dictionary):
def _dict_to_str(self, dictionary, *, key_value_separator=" = "):
text = ""
for key, value in dictionary.items():
text = "".join([text, " ", key, " = ", str(value), "\n"])
text = "".join([text, " ", key, key_value_separator, str(value), "\n"])
return text

def _set_ids(self):
Expand Down Expand Up @@ -508,7 +517,7 @@ def load_body_force(name, simulation, setup_file=""):
BodyForce object.
"""
if setup_file == "":
setup_file = f"{data_dir}/body_forces.yml"
setup_file = os.path.join(data_dir, "body_forces.yml")
with open(setup_file) as f:
data = yaml.safe_load(f)[name]
return BodyForce(simulation, name, data)
19 changes: 16 additions & 3 deletions pyelmer/test/test_elmer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import pytest
import yaml
import filecmp
import gmsh
import os
import re
import tempfile
from objectgmsh import add_physical_group, get_boundaries_in_box
from pyelmer import elmer

Expand Down Expand Up @@ -282,8 +284,16 @@ def test_write_sif():
test_boundary = elmer.Boundary(sim, "test_boundary", [ph_test_boundary])
test_boundary.data.update({"test_parameter": "1.0"})

sim.write_startinfo(sim_dir)
sim.write_sif(sim_dir)
with tempfile.TemporaryDirectory() as tempdir:
sim.write_startinfo(tempdir)
sim.write_sif(tempdir)

files = os.listdir(tempdir)
# Ensure that we have generated the same files as stored in git.
assert os.listdir(sim_dir) == files
for file in files:
# Check that the contents of each file agrees with the reference.
assert filecmp.cmp(os.path.join(sim_dir, file), os.path.join(tempdir, file))

# check format of sif file
simulation = {}
Expand Down Expand Up @@ -318,7 +328,9 @@ def test_write_sif():
initial_conditions,
]

with open(sim_dir + "/case.sif", "r") as f:
# We already know that the reference sif file is equal to that which we generated,
# so read the reference sif file here.
with open(os.path.join(sim_dir, "case.sif"), "r") as f:
read_object = False
write_index = None
write_name = None
Expand Down Expand Up @@ -367,6 +379,7 @@ def test_write_sif():
else:
objects[write_index].update({key: value})

assert simulation == sim.settings
assert equations["test_equation"] == test_eqn.get_data()
assert solvers["test_solver"] == test_solver.get_data()
assert solvers["test_post_solver"] == test_post_solver.get_data()
Expand Down

0 comments on commit 6d25fee

Please sign in to comment.