KAHIBARO
Discord Login Register

4.1. Importing GATE

`import opengate`

To start any GATE simulation in Python you must first load the GATE Python package into your script. In OpenGATE this package is called opengate. At the very top of your simulation file you typically write a standard Python import statement:

python
import opengate as gate

The as gate part is optional, but it is widely used and highly recommended because almost all examples and documentation assume that gate is the short name for the package. This makes later code much more readable, for example:

python
sim = gate.Simulation()

instead of

python
sim = opengate.Simulation()

If Python cannot find the opengate package, you will see an ImportError such as:

text
ModuleNotFoundError: No module named 'opengate'

This usually means that GATE is not installed in the current Python environment or that you are using a different environment than the one where you installed it. Make sure you have followed the installation steps, activated the correct environment, and are running Python from there.

You can quickly check that opengate is correctly installed and accessible by opening a Python interpreter and typing:

python
import opengate as gate
print(gate.__version__)

If no error is raised and a version number appears, you are ready to create simulations in that environment.

Creating the simulation object

Once opengate is imported, the first concrete thing you need in any simulation script is a simulation object. This object represents a whole GATE run, including geometry, sources, physics, actors, and output. In OpenGATE the class is called Simulation and you create one instance at the beginning of your script.

A minimal setup looks like this:

python
import opengate as gate
sim = gate.Simulation()

Here sim is your central handle to the entire simulation. You will use it throughout the script to configure all parts of your model. For absolute beginners, it is useful to think of sim as an empty simulation workspace that you will gradually fill with components.

Typically you will also specify at least the random seed and either the number of events or the simulation time. For example:

python
import opengate as gate
sim = gate.Simulation()
sim.random_seed = 123456
sim.number_of_events = 10000

or, for time based runs,

python
sim.run_time = 10 * gate.g4_units.s

The details of units and of how to choose between events and run time are covered in later chapters. At this stage you only need to understand that all global settings are stored as attributes of the simulation object.

The simulation object is also responsible for starting the Monte Carlo run. After you have defined the world, geometry, sources, physics, and actors, you will use a method like:

python
sim.run()

to execute the simulation.

The simulation object is unique for each run.
Create exactly one gate.Simulation() object per script and configure everything through this object before calling sim.run().
Trying to modify geometry or physics after sim.run() has completed will not affect the already finished run.

Throughout the rest of this course, any example that begins with a GATE simulation in Python will follow this basic pattern: import opengate, create the simulation object, then configure and run it.

Views: 14

Comments

Please login to add a comment.

Don't have an account? Register now!