Skip to content

openEMS ReadFromXML API - Unable to extract CSX structure. #217

@MohamoudAli12

Description

@MohamoudAli12

I have a structure structure.xml file I have created using openEMS. I am trying to read the FDTD data and CSX structure using ReadFromXML function in openEMS python API, using the below code

def simulate_model(structure_xml_path):
    ems= openEMS()
    ems.ReadFromXML(structure_xml_path)
    subprocess.run(["openEMS", str(structure_xml_path)], check=True)
    CSX=ems.GetCSX()
    prop = CSX.GetAllProperties()
    print(prop)

simulate_model("structure.xml")

I was able to run the simulation successfully, but was unable to get the CSX geometry data. CSX=ems.GetCSX() returns an empty list.
I tried to use the ReadFromXML API from CSXCAD class, but it is unable to parse the xml file and returns and error Error: No ContinuousStructure found!!! due to FirstChild being openEMS.

const char* ContinuousStructure::ReadFromXML(TiXmlNode* rootNode)
{
	clear();
	TiXmlNode* root = rootNode->FirstChild("ContinuousStructure");
	if (root==NULL) { ErrString.append("Error: No ContinuousStructure found!!!\n"); return ErrString.c_str();}

	TiXmlElement* rootElem = root->ToElement();
	if (rootElem)
	{
		int CS_mesh = 0;
		if (rootElem->QueryIntAttribute("CoordSystem",&CS_mesh) == TIXML_SUCCESS)
			SetCoordInputType((CoordinateSystem)CS_mesh);
	}

I did an investigation why GetCSX python function did not work and found that this getter does not return the cpp object created when you ReadFromXML.
it works when you explicitly call SetCSX python function.

I solved the issue but with help form dumb public ChatGPT and after many errors and iterations I was able to get my code to work and extract the CSX structure.

Image

Please see the attached diff for the changes I have made, as I am not familiar with both cpp and cython and the code base as well, I don't feel that my approach is best.

diff --git a/openems.cpp b/openems.cpp
index 237edcd..f2b83ed 100644
--- a/openems.cpp
+++ b/openems.cpp
@@ -1125,6 +1125,11 @@ void openEMS::SetCSX(ContinuousStructure* csx)
 	m_CSX = csx;
 }
 
+ContinuousStructure* openEMS::GetCSX() const
+{
+    return m_CSX;
+}
+
 int openEMS::SetupFDTD()
 {
 	timeval startTime;
diff --git a/openems.h b/openems.h
index e5d245b..7ad4d51 100644
--- a/openems.h
+++ b/openems.h
@@ -115,6 +115,7 @@ public:
 	Excitation* InitExcitation();
 
 	void SetCSX(ContinuousStructure* csx);
+  ContinuousStructure* GetCSX() const;
 
 	Engine_Interface_FDTD* NewEngineInterface(int multigridlevel = 0);
 
diff --git a/python/openEMS/openEMS.pxd b/python/openEMS/openEMS.pxd
index 22e0305..89cd49b 100644
--- a/python/openEMS/openEMS.pxd
+++ b/python/openEMS/openEMS.pxd
@@ -29,6 +29,7 @@ cdef extern from "openEMS/openems.h":
 
         void SetNumberOfTimeSteps(unsigned int val)
         void SetCSX(_ContinuousStructure* csx)
+        _ContinuousStructure* GetCSX()
 
         void SetEndCriteria(double val)
         void SetOverSampling(int val)
diff --git a/python/openEMS/openEMS.pyx b/python/openEMS/openEMS.pyx
index 8ae632b..5d81f4a 100644
--- a/python/openEMS/openEMS.pyx
+++ b/python/openEMS/openEMS.pyx
@@ -428,6 +428,17 @@ cdef class openEMS:
         self.thisptr.SetCSX(CSX.thisptr)
 
     def GetCSX(self):
+        cdef _ContinuousStructure* ptr = self.thisptr.GetCSX()
+        cdef ContinuousStructure csx  # declare here
+
+        if ptr == NULL:
+            self.__CSX = None
+            return None
+
+        if self.__CSX is None or self.__CSX.thisptr != ptr:
+            csx = ContinuousStructure.__new__(ContinuousStructure)
+            csx.thisptr = ptr
+            self.__CSX = csx
         return self.__CSX
 
     def AddEdges2Grid(self, dirs, primitives=None, properties=None, **kw):

Let me know if you have any questions or need more info.

BR,
Mohamoud.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions