Modelling complex geometries — Where to start?

Geant4 solids are much more successful when we perform performance testing on large file sizes. however, it is really difficult to recognize the model and extract another model from it. :grinning_face_with_smiling_eyes:

In case anyone is interested, here’s what I did.

To elaborate a bit on my use case: my geometric set-up consisted of a voxel model of a patient; now I needed to add a model of the neutron source.

I finally decided to represent the neutron source in GDML format, as was generally suggested over here. The reason is that GDML supports all of Geant4 solids and it is the best supported persistency format.

Concerning the creation of the GDML files, the GDML files are generated by tailor-made Python scripts, and visualized/debugged with the Qt interactive visualization tool. The geometry I had to model was available in the form of MCNP input files, and was actually not that complex. Therefore, a CAD representation was an unnecessarily complicated detour, and it made more sense to directly create the GDML files. This approach is quite clunky though, since the editing-visualizing-validating loop is slow, and only suitable for simple geometric set-ups. This may change in the future if a more complex geometric model is needed, demanding a proper CAD tool for the design part.

One of the issues I encountered was that the Geant4 GDML parser assumes that the whole geometric setup is contained in the GDML file (or file + modules). In my case, this was a problem because of two reasons:

  • On one hand, I wanted to place the GDML under the world volume, together with the existing voxel model. The existing voxel model uses a material parameterisation which cannot be modelled in GDML.
  • On the other, the neutron source consists of parts which can be combined differently. Although GDML modules help with these situation, ultimately a GDML file is needed for each combination, which is not ideal.

Because of this, I had to create a modified version of G4GDMLParser which, instead of creating the whole geometry tree, instantiates all first-level daughter volumes under the desired mother volume. This allows me to create a world volume, and then add to it volumes coming from GDML files, or hard-coded, or whatever else.

And that’s about it :slight_smile:

1 Like

Sergio, this is still an open question to me as well when I am trying to represent an arbitrary geometry of materials. More in general, I am still looking for a way to import, e.g. survey or topographic maps

For “simple” cases I hacked my way through using data cards which I can easily modify/script and then read into Geant4. Since - in my case - we are not necessarily talking about combinations of cubes, trapezoids and other basic 3D shapes, I ended up using extruded volumes. In this way I can generate a 2.5D geometry, which is often good enough in my case since my problem is usually “infinite” along at least one axis, but with more work this could be made fully 3D.

So my approach is somewhere between approach 1) and 5). It seems to work however, it turns out that preparing the input data cards can be quite some work (which I still do “by hand”)

I hope the snapshot below of my data card is useful. This is building two trapezoids stacked onto each other but the approach is more general. You can PM if you want more details. Note this is my very first c++ project so when I say “hack” I really mean it… I have yet to learn about GDML and I hope to find an entry level but hands-on tutorial.

I suppose that your use case is specific enough that it makes sense. The approach you are taking is actually number 6) in my list, since you have your own file format.

As you probably have noticed, writing parsers is not fun, particularly with C++. GDML supports extruded solids, so I would recommend having a look at it. You may find that XML is too verbose for your taste compared to your specialized format, but it is quite easy to generate with another program (I do so with normally).