MPh icon indicating copy to clipboard operation
MPh copied to clipboard

Access material property groups

Open shomikverma opened this issue 4 years ago • 2 comments

Hi, thanks for creating such a useful scripting tool!

I was wondering if a method of querying and changing material properties i.e. thermal conductivity could be implemented. Currently node.properties() on a material returns a dict of "display" properties such as color, etc. It would be nice to have access to material properties to allow parametric sweeps and optimization.

Appreciate it!

shomikverma avatar Apr 26 '22 19:04 shomikverma

A workaround is to define a global parameter or function and set the material property to that in COMSOL. Then can edit the parameter in MPh with model.parameter() or the function with node = model/'functions'/'function_name' and changing the node properties.

shomikverma avatar Apr 26 '22 19:04 shomikverma

Hi, and thanks for bringing this up.

You're right, the property groups don't show up in the model tree, as displayed by mph.tree(). I never noticed that before, I've always used the approach you call a workaround. I think of it as the proper way to do it :wink:, because when I sweep a parameter I think it should be defined as a "global parameter" in the Comsol model.

It does make sense to have these property groups show up like anything else in the model tree. It's not an easy fix though. That's because the underlying Java objects for the "property groups" are different from the "feature groups" that are used for everything else. So this needs some special-casing in the code that wasn't necessary until now. I'll think about including it, but it would take a while and I may also decide against it if it turns out to be too complicated.

That being said, you can of course change these values if you use the Java methods directly. Refer to section "Access the full Comsol API" and consider this Python session changing the relative permittivity of one material in the demo model:

>>> import mph
>>> client = mph.start()
>>> model = client.load('demos/capacitor.mph')
>>> mph.tree(model)
capacitor
├─ parameters
│  └─ parameters
├─ functions
...
├─ materials
│  ├─ medium 1
│  └─ medium 2
├─ meshes
...
>>> medium1 = model/'materials'/'medium 1'
>>> medium1.properties()
{'alpha': 1.0, 'ambient': 'custom', 'bndType': None, ...
>>> medium1.java.propertyGroup('def').label()
'Basic'
>>> print(medium1.java.propertyGroup('def').params())
['relpermittivity', 'relpermittivity_symmetry', 'electricconductivity', 'electricconductivity_symmetry']
>>> medium1.java.propertyGroup('def').getString('relpermittivity')
'1'
>>> medium1.java.propertyGroup('def').set('relpermittivity', 2)
<java object 'com.comsol.model.impl.MaterialModelImpl'>
>>> medium1.java.propertyGroup('def').getString('relpermittivity')
'2'

john-hen avatar Apr 27 '22 18:04 john-hen

Included in MPh 1.2.0, released today.

john-hen avatar Aug 31 '22 22:08 john-hen