Skip to content

Commit

Permalink
verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
jschueller committed Sep 28, 2023
1 parent db1ca1f commit 0487ba4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
32 changes: 19 additions & 13 deletions otfmi/function_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ def _write_modelica_wrapper(self, className, dirName, gui, move):
with open(os.path.join(self.workdir, className + ".mo"), "w") as mo:
mo.write(data)

def export_model(self, model_path, gui=False, verbose=False, move=True, mode="process"):
def export_model(self, model_path, gui=False, verbose=False, binary=True, mode="process", move=True):
"""
Export to model file (.mo).
Expand All @@ -626,10 +626,12 @@ def export_model(self, model_path, gui=False, verbose=False, move=True, mode="pr
In this case only, the model can be exported as FMU using OMC command line.
verbose : bool
Verbose output (default=False).
move : bool
Move the model from temporary folder to user folder (default=True)
binary : bool
Whether to generate binaries or source (default=True)
mode : 'process' or 'capi'
Use either a Python process or the Python C API to evaluate the model.
move : bool
Move the model from temporary folder to user folder (default=True)
"""

assert isinstance(model_path, str), "model_path must be str"
Expand All @@ -647,18 +649,22 @@ def export_model(self, model_path, gui=False, verbose=False, move=True, mode="pr
self._write_modelica_wrapper(className, dirName, gui, move)

if move:
if sys.platform.startswith("win"):
libname = "cwrapper.lib"
else:
if os.path.exists(os.path.join(self.workdir, "libcwrapper.so")):
libname = "libcwrapper.so"
list_file = [className + extension]
if binary:
if sys.platform.startswith("win"):
list_file.append("cwrapper.lib")
else:
libname = "libcwrapper.a"
list_file = [libname, className + extension]
if os.path.exists(os.path.join(self.workdir, "libcwrapper.so")):
list_file.append("libcwrapper.so")
else:
list_file.append("libcwrapper.a")
else:
list_file.append("wrapper.c")
list_file.append("CMakeLists.txt")
for file in list_file:
shutil.move(
os.path.join(self.workdir, file), os.path.join(dirName, file)
)
src = os.path.join(self.workdir, file)
dest = os.path.join(dirName, file)
shutil.move(src, dest)
shutil.rmtree(self.workdir)

def export_fmu(self, fmu_path, fmuType="me", verbose=False):
Expand Down
40 changes: 21 additions & 19 deletions test/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,27 @@ def test_export_model(self):
name_model = "Deviation.mo"

for mode in ["process", "capi"]:
temp_path = tempfile.mkdtemp()
assert os.path.isdir(temp_path), "temp_path not created"
className, _ = os.path.splitext(name_model)
path_model = os.path.join(temp_path, name_model)
fe = otfmi.FunctionExporter(f, start)
fe.export_model(path_model, gui=False, verbose=False, move=True, mode=mode)
assert os.path.isfile(path_model), "model not created"

# write simulation mos
path_mos = os.path.join(temp_path, "simulate.mos")
with open(path_mos, "w") as mos:
mos.write('cd("{}");\n'.format(temp_path))
#mos.write("loadModel(Modelica);\n")
mos.write('loadFile("{}"); getErrorString();\n'.format(name_model))
mos.write("simulate ({}, stopTime=3.0);\n".format(className))

subprocess.run(["omc", "{}".format(path_mos)], capture_output=True, check=True)

shutil.rmtree(temp_path)
for binary in [True, False]:
temp_path = tempfile.mkdtemp()
assert os.path.isdir(temp_path), "temp_path not created"
className, _ = os.path.splitext(name_model)
path_model = os.path.join(temp_path, name_model)
fe = otfmi.FunctionExporter(f, start)
fe.export_model(path_model, gui=False, verbose=True, binary=binary, mode=mode)
assert os.path.isfile(path_model), "model not created"

if binary:
# write simulation mos
path_mos = os.path.join(temp_path, "simulate.mos")
with open(path_mos, "w") as mos:
mos.write('cd("{}");\n'.format(temp_path))
mos.write('loadFile("{}"); getErrorString();\n'.format(name_model))
mos.write("simulate ({}, stopTime=3.0);\n".format(className))
subprocess.run(["omc", "{}".format(path_mos)], capture_output=True, check=True)
else:
assert os.path.isfile(os.path.join(temp_path, "wrapper.c")), "wrapper source not created"
assert os.path.isfile(os.path.join(temp_path, "CMakeLists.txt")), "cmakelists not created"
shutil.rmtree(temp_path)


if __name__ == "__main__":
Expand Down

0 comments on commit 0487ba4

Please sign in to comment.