Add Magnetic Field from COMSOL to Geant4

Please fill out the following information to help in answering your question, and also see tips for posting code snippets. If you don’t provide this information it will take more time to help with your problem!

_Geant4 Version:_11
_Operating System:_Window11
Compiler/Version:
CMake Version:


Dear all,
I have a task where I need to simulate an electromagnetic field in COMSOL software and then import this electromagnetic field into Geant4 to simulate a RAD7 system. I have no experience with COMSOL and of course, don’t know how to import the output electromagnetic fields into Geant4.
Has anyone ever done the same thing? Can you give me experience or an example code?
Thank you in advance.

This is a much more complex problem than you might think. The COMSOL output is in the form of a tetrahedral mesh, with a value of the field(s) at each point in the mesh. In addition, COMSOL generally does its simulation in a “local” coordinate system, so you need some way to map those local coordinates into the world coordinates of your Geant4 simulation (or you use your whole G4 geometry in COMSOL, so the two coordinate systems coincide).

To make use of the COMSOL output in Geant4, you need both a way to read in the mesh information (the coordinates, the values at each coordinate, and the tetrahedra themselves), and you need code to do 3D interpolation.

In the G4CMP package (GitHub - kelseymh/G4CMP: A low energy condensed matter physics package for Geant4), I’ve written classes that support the second half of that (the 3D interpolation) for a plain electric field, but not a magnetic field. You might be able to take what’s there (G4CMPTriLinearInterpolator) and adapt it to your own purposes.

Thank you for your help.