MPh-py / MPh

Pythonic scripting interface for Comsol Multiphysics
https://mph.readthedocs.io
MIT License
263 stars 67 forks source link

Exporting meshes as `.mphtxt` files #73

Closed banduser closed 2 years ago

banduser commented 2 years ago

Hi,

Im currently writing a Python optimization script with which I want to vary geometric parametrs of a Comsol model using MPh and create and export Meshes of the Model.

Since I'm not running any actual Simulations on the Model and only want the Mesh, my only export Node is a Mesh export Node. With this export node I want to generate an .mphtxt file of my model. However, when I attempt to run the code I get the following error message:

TypeError: Node "exports/Mesh 1" is of the unexpected type "Mesh".

Is the exporting of Meshes not implmented or am I simply overlooking something? Attached are screenshots of my code.

Thanks in advance! mph code mph

john-hen commented 2 years ago

Hi. Yeah, looks like it's not implemented. I did not even know there are export nodes for meshes.

By "not implemented" I mean that MPh's Model.export() method is just as unaware of it as I am. Obviously, if it can be done in Java, then it can be done in Python. So there's definitely a way to do it.

banduser commented 2 years ago

Thanks John! Any chance of this being implemented in the near future? If not, is there a workaround I could use in the meantime?

john-hen commented 2 years ago

Yeah, I'll fix that eventually. Unless you want to contribute a pull request?

The work-around is to use (what I call) the "Java layer" directly, instead of the high-level functions of MPh's API. This is what's explained in section "Access the full Comsol API".

So, whenever some functionality is missing, open your model in the Comsol GUI, perform whatever action you want to automate (here: export the mesh), then save the model as a .java file, and look at the Java code. The lines you're interested in will be at the very end. (It may help to "Compact History" before saving, which removes redundant code lines from the Java code.) Then translate that Java code to Python. That usually just means replacing model.* with model.java.* at the start of the line.

john-hen commented 2 years ago

Fixed in MPh 1.1.6, released today.