Skip to content

Commit

Permalink
support for reading and writing neqsim objects to xml (#212)
Browse files Browse the repository at this point in the history
* feat: added support for reading and writing neqsim objects to xml
* refact: clean up imports

---------

Co-authored-by: Åsmund Våge Fannemel <[email protected]>
  • Loading branch information
EvenSol and asmfstatoil authored Nov 16, 2023
1 parent 3387328 commit a3feabe
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
18 changes: 16 additions & 2 deletions neqsim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
It uses the Jpype module for bridging python and Java.
"""

from .neqsimpython import *
from neqsim.neqsimpython import jNeqSim, jpype

def methods(checkClass):
methods = checkClass.getClass().getMethods()
Expand All @@ -23,6 +23,20 @@ def has_tabulate():


def setDatabase(connectionString):
from neqsim.neqsimpython import jNeqSim
jNeqSim.util.database.NeqSimDataBase.setConnectionString(connectionString)
jNeqSim.util.database.NeqSimDataBase.setCreateTemporaryTables(True)

def save_xml(javaobject, filename):
xstream = jpype.JPackage('com.thoughtworks.xstream')
streamer = xstream.XStream()
xml = streamer.toXML(javaobject)
print(xml, file=open(filename, 'w'))
return xml

def open_xml(filename):
xstream = jpype.JPackage('com.thoughtworks.xstream')
streamer = xstream.XStream()
streamer.addPermission(xstream.security.AnyTypePermission.ANY)
str = open(filename, 'r').read()
neqsimobj = streamer.fromXML(str)
return neqsimobj
16 changes: 16 additions & 0 deletions tests/test_NeqSim.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@author: ESOL
"""
import os
from neqsim.neqsimpython import jNeqSim


Expand Down Expand Up @@ -231,3 +232,18 @@ def test_fullOffshoreProcess():
exportCompressor.setOutletPressure(200.0, 'bara')
exportGas = stream(exportCompressor.getOutStream())
runProcess()

def testwriteandopen(tmp_path):
import neqsim
from neqsim.thermo import createfluid

temp_dir = tmp_path / "test"
os.mkdir(temp_dir)

temp_file = temp_dir / "name.xml"

fluid1 = createfluid('dry gas')
neqsim.save_xml(fluid1, temp_file)
fluid2 = neqsim.open_xml(temp_file)

assert fluid1.getTemperature() == fluid2.getTemperature()

0 comments on commit a3feabe

Please sign in to comment.