Skip to content

How to modify a physical group on Python API

Hello,

I am using the Python API for Gmsh and I was wondering if there was a way to add new entities to a physical group that is already created.

For example :

import gmsh

model = gmsh.model
factory = model.occ

gmsh.initialize()

b1 = factory.addBox(0.0, 0.0, 0.0, 1.0, 1.0, 1.0)
b2 = factory.addBox(0.0, 0.0, 1.0, 1.0, 1.0, 1.0)

factory.synchronize()

g1 = model.addPhysicalGroup(3, [b1], 1)

#Later in the code...
#I want to add the box b2 to the physical group g1

#writing file
model.mesh.generate(3)
gmsh.write('test.msh')

I have also tried to bypass the problem by using removePhysicalGroups() :

import gmsh

model = gmsh.model
factory = model.occ

gmsh.initialize()

b1 = factory.addBox(0.0, 0.0, 0.0, 1.0, 1.0, 1.0)
b2 = factory.addBox(0.0, 0.0, 1.0, 1.0, 1.0, 1.0)

factory.synchronize()

model.addPhysicalGroup(3, [b1], 1)

model.removePhysicalGroups([3, 1])

model.addPhysicalGroup(3, [b1, b2], 1)

#writing file
model.mesh.generate(3)
gmsh.write('test.msh')

But the code is returning Error : Physical volume 1 already exists which is surprising to me as I thought I removed the physical group with model.removePhysicalGroups([3, 1]).

Thank you for your help,

Guillaume