Skip to content

Commit e48b5d8

Browse files
authored
Merge pull request #5578 from chillenzer/species-refactoring
PICMI Species handling refactoring
2 parents 2ba49a2 + 0ff04ad commit e48b5d8

File tree

125 files changed

+1035
-8015
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+1035
-8015
lines changed

docs/source/usage/picmi/custom_template.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ The output of the picongpu simulation in the default templates is configured in
7474
$USED_CHARGE_CONSERVATION_FLAGS
7575
7676
77-
{{#species_initmanager.species}}
77+
{{#species}}
7878
--{{{name}}}_macroParticlesCount.period {{{period}}}
7979
8080
--{{{name}}}_energy.period {{{period}}}
@@ -92,7 +92,7 @@ The output of the picongpu simulation in the default templates is configured in
9292
--{{{name}}}_png.slicePoint 0.5
9393
--{{{name}}}_png.folder png_{{{name}}}_{{{axis}}}
9494
{{/png_axis}}
95-
{{/species_initmanager.species}}
95+
{{/species}}
9696
9797
{{/output.auto}}
9898
@@ -148,9 +148,9 @@ Instead of hard coding the output we might want to automatically generate one in
148148
--Cu_energyHistogram.minEnergy 0
149149
--Cu_energyHistogram.maxEnergy 256000
150150
151-
{{#species_initmanager.species}}
151+
{{#species}}
152152
--{{{name}}}_macroParticlesCount.period 1
153-
{{/species_initmanager.species}}
153+
{{/species}}
154154
{{/output.auto}}
155155
156156
Let's go in detail through the above example.
@@ -218,9 +218,9 @@ And of course it will be available as such in the rendering of the template whic
218218
--Cu_energyHistogram.minEnergy 0
219219
--Cu_energyHistogram.maxEnergy 256000
220220
221-
{{#species_initmanager.species}}
221+
{{#species}}
222222
--{{{name}}}_macroParticlesCount.period 1
223-
{{/species_initmanager.species}}
223+
{{/species}}
224224
{{/output.auto}}
225225
226226
.. warning::

lib/python/picongpu/picmi/copy_attributes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ def copy_attributes(
8989
"""
9090
assignments = {
9191
to_name: _value_generator(from_name)
92-
for from_name, _ in inspect.getmembers(from_instance)
92+
for from_name, _ in (
93+
type(from_instance).model_fields.items()
94+
if isinstance(from_instance, BaseModel)
95+
else inspect.getmembers(from_instance)
96+
)
9397
if from_name not in ignore
9498
and not from_name.startswith("_")
9599
and has_attribute(to, to_name := from_name.removeprefix(remove_prefix))

lib/python/picongpu/picmi/diagnostics/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
License: GPLv3+
66
"""
77

8-
from .auto import Auto
98
from .binning import Binning, BinningAxis, BinSpec
109
from .phase_space import PhaseSpace
1110
from .energy_histogram import EnergyHistogram
@@ -19,7 +18,6 @@
1918
from .unit_dimension import UnitDimension
2019

2120
__all__ = [
22-
"Auto",
2321
"BackendConfig",
2422
"OpenPMDConfig",
2523
"Binning",

lib/python/picongpu/picmi/diagnostics/auto.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

lib/python/picongpu/picmi/diagnostics/binning.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88
from pathlib import Path
99
from ..copy_attributes import default_converts_to
1010

11-
import typeguard
1211

1312
from picongpu.picmi.diagnostics.backend_config import OpenPMDConfig
1413

1514
from ...pypicongpu.output.binning import Binning as PyPIConGPUBinning
1615
from ...pypicongpu.output.binning import BinningAxis as PyPIConGPUBinningAxis
1716
from ...pypicongpu.output.binning import BinSpec as PyPIConGPUBinSpec
18-
from ...pypicongpu.species.species import Species as PyPIConGPUSpecies
19-
from ..species import Species as PICMISpecies
17+
from ..species import Species as Species
2018
from .particle_functor import ParticleFunctor as BinningFunctor
2119
from .timestepspec import TimeStepSpec
2220

@@ -30,7 +28,6 @@ def __init__(self, kind, start, stop, nsteps):
3028
self.nsteps = nsteps
3129

3230

33-
@typeguard.typechecked
3431
class BinningAxis:
3532
def __init__(
3633
self,
@@ -53,14 +50,13 @@ def get_as_pypicongpu(self) -> PyPIConGPUBinningAxis:
5350
)
5451

5552

56-
@typeguard.typechecked
5753
class Binning:
5854
def __init__(
5955
self,
6056
name: str,
6157
deposition_functor: BinningFunctor,
6258
axes: list[BinningAxis],
63-
species: PICMISpecies | list[PICMISpecies],
59+
species: Species | list[Species],
6460
period: TimeStepSpec | None = None,
6561
openPMD: dict | None = None,
6662
openPMDExt: str | None = None,
@@ -70,7 +66,7 @@ def __init__(
7066
self.name = name
7167
self.deposition_functor = deposition_functor
7268
self.axes = axes
73-
if isinstance(species, PICMISpecies):
69+
if isinstance(species, Species):
7470
species = [species]
7571
self.species = species
7672
self.period = period or TimeStepSpec[:]
@@ -86,19 +82,14 @@ def result_path(self, prefix_path):
8682

8783
def get_as_pypicongpu(
8884
self,
89-
dict_species_picmi_to_pypicongpu: dict[PICMISpecies, PyPIConGPUSpecies],
9085
time_step_size,
9186
num_steps,
9287
) -> PyPIConGPUBinning:
93-
if len(not_found := [s for s in self.species if s not in dict_species_picmi_to_pypicongpu.keys()]) > 0:
94-
raise ValueError(f"Species {not_found} are not known to Simulation")
95-
pypic_species = list(map(dict_species_picmi_to_pypicongpu.get, self.species))
96-
9788
return PyPIConGPUBinning(
9889
name=self.name,
9990
deposition_functor=self.deposition_functor.get_as_pypicongpu(),
10091
axes=list(map(BinningAxis.get_as_pypicongpu, self.axes)),
101-
species=pypic_species,
92+
species=[s.get_as_pypicongpu() for s in self.species],
10293
period=self.period.get_as_pypicongpu(time_step_size, num_steps),
10394
openPMD=self.openPMD,
10495
openPMDExt=self.openPMDExt,

lib/python/picongpu/picmi/diagnostics/energy_histogram.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
License: GPLv3+
66
"""
77

8-
import typeguard
8+
from pydantic import BaseModel
9+
10+
from picongpu.picmi.copy_attributes import default_converts_to
911

10-
from picongpu.picmi.diagnostics.util import diagnostic_converts_to
1112

1213
from ...pypicongpu.output.energy_histogram import (
1314
EnergyHistogram as PyPIConGPUEnergyHistogram,
1415
)
15-
from ..species import Species as PICMISpecies
16+
from ..species import Species as Species
1617
from .timestepspec import TimeStepSpec
1718

1819

19-
@diagnostic_converts_to(PyPIConGPUEnergyHistogram)
20-
@typeguard.typechecked
21-
class EnergyHistogram:
20+
@default_converts_to(PyPIConGPUEnergyHistogram)
21+
class EnergyHistogram(BaseModel):
2222
"""
2323
Specifies the parameters for the output of Energy Histogram of species such as electrons.
2424
@@ -53,24 +53,17 @@ class EnergyHistogram:
5353
Optional name for the energy histogram plugin.
5454
"""
5555

56-
def check(self, dict_species_picmi_to_pypicongpu, *args, **kwargs):
56+
def check(self, *args, **kwargs):
5757
if self.min_energy >= self.max_energy:
5858
raise ValueError("min_energy must be less than max_energy")
5959
if self.bin_count <= 0:
6060
raise ValueError("bin_count must be > 0")
61-
if self.species not in dict_species_picmi_to_pypicongpu.keys():
62-
raise ValueError(f"Species {self.species} is not known to Simulation")
63-
64-
def __init__(
65-
self,
66-
species: PICMISpecies,
67-
period: TimeStepSpec,
68-
bin_count: int,
69-
min_energy: float,
70-
max_energy: float,
71-
):
72-
self.species = species
73-
self.period = period
74-
self.bin_count = bin_count
75-
self.min_energy = min_energy
76-
self.max_energy = max_energy
61+
62+
species: Species
63+
period: TimeStepSpec
64+
bin_count: int
65+
min_energy: float
66+
max_energy: float
67+
68+
class Config:
69+
arbitrary_types_allowed = True

lib/python/picongpu/picmi/diagnostics/macro_particle_count.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@
55
License: GPLv3+
66
"""
77

8-
import typeguard
8+
from pydantic import BaseModel
99

10-
from picongpu.picmi.diagnostics.util import diagnostic_converts_to
10+
from picongpu.picmi.copy_attributes import default_converts_to
1111

1212
from ...pypicongpu.output.macro_particle_count import (
1313
MacroParticleCount as PyPIConGPUMacroParticleCount,
1414
)
15-
from ...pypicongpu.species.species import Species as PyPIConGPUSpecies
16-
from ..species import Species as PICMISpecies
15+
from ..species import Species as Species
1716
from .timestepspec import TimeStepSpec
1817

1918

20-
@diagnostic_converts_to(PyPIConGPUMacroParticleCount)
21-
@typeguard.typechecked
22-
class MacroParticleCount:
19+
@default_converts_to(PyPIConGPUMacroParticleCount)
20+
class MacroParticleCount(BaseModel):
2321
"""
2422
Specifies the parameters for counting the total number of macro particles of a given species.
2523
@@ -39,13 +37,8 @@ class MacroParticleCount:
3937
Optional name for the macro particle count plugin.
4038
"""
4139

42-
def __init__(self, species: PICMISpecies, period: TimeStepSpec):
43-
self.species = species
44-
self.period = period
40+
species: Species
41+
period: TimeStepSpec
4542

46-
def check(self, dict_species_picmi_to_pypicongpu: dict[PICMISpecies, PyPIConGPUSpecies], *args, **kwargs):
47-
if self.species not in dict_species_picmi_to_pypicongpu.keys():
48-
raise ValueError(f"Species {self.species} is not known to Simulation")
49-
pypicongpu_species = dict_species_picmi_to_pypicongpu.get(self.species)
50-
if pypicongpu_species is None:
51-
raise ValueError(f"Species {self.species} is not mapped to a PyPIConGPUSpecies.")
43+
class Config:
44+
arbitrary_types_allowed = True

lib/python/picongpu/picmi/diagnostics/particle_dump.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from pydantic import BaseModel
1212

13-
from picongpu.picmi.species import Species
13+
from picongpu.picmi.species import Species as Species
1414

1515
from .backend_config import BackendConfig, OpenPMDConfig
1616
from .timestepspec import TimeStepSpec

lib/python/picongpu/picmi/diagnostics/phase_space.py

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@
77

88
from typing import Literal
99

10-
import typeguard
10+
from pydantic import BaseModel
1111

12-
from picongpu.picmi.diagnostics.util import diagnostic_converts_to
12+
from picongpu.picmi.copy_attributes import default_converts_to
1313

1414
from ...pypicongpu.output.phase_space import PhaseSpace as PyPIConGPUPhaseSpace
15-
from ..species import Species as PICMISpecies
15+
from ..species import Species as Species
1616
from .timestepspec import TimeStepSpec
1717

1818

19-
@diagnostic_converts_to(PyPIConGPUPhaseSpace)
20-
@typeguard.typechecked
21-
class PhaseSpace:
19+
@default_converts_to(PyPIConGPUPhaseSpace)
20+
class PhaseSpace(BaseModel):
2221
"""
2322
Specifies the parameters for the output of Phase Space of species such as electrons.
2423
@@ -52,30 +51,16 @@ class PhaseSpace:
5251
Optional name for the phase-space plugin.
5352
"""
5453

55-
def check(self, dict_species_picmi_to_pypicongpu, *args, **kwargs):
54+
species: Species
55+
period: TimeStepSpec
56+
spatial_coordinate: Literal["x", "y", "z"]
57+
momentum_coordinate: Literal["px", "py", "pz"]
58+
min_momentum: float
59+
max_momentum: float
60+
61+
def check(self, *args, **kwargs):
5662
if self.min_momentum >= self.max_momentum:
5763
raise ValueError("min_momentum must be less than max_momentum")
5864

59-
if self.species not in dict_species_picmi_to_pypicongpu.keys():
60-
raise ValueError(f"Species {self.species} is not known to Simulation")
61-
62-
# checks if PICMISpecies instance exists in the dictionary. If yes, it returns the corresponding PyPIConGPUSpecies instance.
63-
# self.species refers to the species attribute of the class PhaseSpace(picmistandard.PICMI_PhaseSpace).
64-
if dict_species_picmi_to_pypicongpu.get(self.species) is None:
65-
raise ValueError(f"Species {self.species} is not mapped to a PyPIConGPUSpecies.")
66-
67-
def __init__(
68-
self,
69-
species: PICMISpecies,
70-
period: TimeStepSpec,
71-
spatial_coordinate: Literal["x", "y", "z"],
72-
momentum_coordinate: Literal["px", "py", "pz"],
73-
min_momentum: float,
74-
max_momentum: float,
75-
):
76-
self.species = species
77-
self.period = period
78-
self.spatial_coordinate = spatial_coordinate
79-
self.momentum_coordinate = momentum_coordinate
80-
self.min_momentum = min_momentum
81-
self.max_momentum = max_momentum
65+
class Config:
66+
arbitrary_types_allowed = True

0 commit comments

Comments
 (0)