diff --git a/olaflow/processing/olaflow.py b/olaflow/processing/olaflow.py new file mode 100644 index 0000000..9556a84 --- /dev/null +++ b/olaflow/processing/olaflow.py @@ -0,0 +1,21 @@ +import re + + +class OFModel: + def __init__(self, root): + self._root = root + + def write_field(self, field, values): + with open(self._root.joinpath("0", field), "r") as aw_file: + aw_raw = aw_file.read() + + with open(self._root.joinpath("0", field), "w") as aw_file: + aw_file.write( + re.sub( + r"(?<=\(\n).*?(?=\n\))", + "\n".join(values.astype(str)), + aw_raw, + count=1, + flags=re.S, + ) + ) diff --git a/olaflow/processing/sws_ola.py b/olaflow/processing/sws_ola.py index 0eb4ddb..97940ec 100644 --- a/olaflow/processing/sws_ola.py +++ b/olaflow/processing/sws_ola.py @@ -9,6 +9,8 @@ import numpy as np from fluidfoam import readof from scipy import interpolate +from .olaflow import OFModel + parser = argparse.ArgumentParser( description="Convert swash output to olaFlow input" ) @@ -32,18 +34,7 @@ of_x, of_y, of_z = mesh = readof.readmesh(str(olaflow_root)) watl = interpolate.interp1d(x, sws["watl"][680]) -alpha_water = np.where(of_z < watl(of_x), "1", "0") +alpha_water = np.where(of_z < watl(of_x), 1, 0) -with open(olaflow_root.joinpath("0", "alpha.water"), "r") as aw_file: - aw_raw = aw_file.read() - -with open(olaflow_root.joinpath("0", "alpha.water"), "w") as aw_file: - aw_file.write( - re.sub( - r"(?<=\(\n).*?(?=\n\))", - "\n".join(alpha_water), - aw_raw, - count=1, - flags=re.S, - ) - ) +model = OFModel(olaflow_root) +model.write_field("alpha.water", alpha_water)