Skip to content

Use logging instead of print#199

Open
max-models wants to merge 44 commits intodevelfrom
use-logging-instead-of-print
Open

Use logging instead of print#199
max-models wants to merge 44 commits intodevelfrom
use-logging-instead-of-print

Conversation

@max-models
Copy link
Copy Markdown
Member

@max-models max-models commented Mar 8, 2026

Based on this tutorial: https://www.youtube.com/watch?v=9L77QExPmI0

  • Add logging config
  • Allow for overriding the logging level as an arg into Simulation
  • Add a rank filter so only rank == 0 actually logs
  • Replace all prints with logging

Here are the logging levels:

image

I would suggest that the cases where we have

if verbose:
    print(message)

could be replaced by logger.debug(message).

Then we can set the default logging to INFO and if you wish to change it, you just set it to DEBUG and then all the verbose logging messages will be show.

Also, another nice thing is that we can add this rank filter so that instead of doing:

if self.rank == 0:
    print(message)

we can just do logger.debug(message) and by default only rank == 0 logs. We can also add more handlers to which this filter does not apply, which might be the case for certain types of debugging, where you might want to log from every rank.

@max-models max-models requested a review from spossann March 18, 2026 22:21
@max-models max-models marked this pull request as ready for review March 20, 2026 15:31
Copy link
Copy Markdown
Member

@spossann spossann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the following launch file I get unexpected output en_f and logging that I cannot get rid of. I set the logging info with a string, but with the requested int it didn't work either.

# -----------------------------
# Description of the simulation
# -----------------------------
# Please fill in a verbal description of the simulation. 
# It will be printed at the beginning of the simulation and can be used to keep track of the different runs.

name = "Default Vlasov"
description = """
This is the default simulation for the model Vlasov. 
It is meant to be a template for users to set up their own simulations with this model. 
It contains all the necessary components of a Struphy simulation, including the model, 
the environment options, the time stepping options, the geometry, the equilibrium, 
the grid, the Derham options, and the initial conditions. 
Users can modify this file to set up their own simulations with different parameters and initial conditions.
"""

# ------------------
# Import Struphy API
# ------------------

from struphy import (
    BaseUnits,
    DerhamOptions,
    EnvironmentOptions,
    FieldsBackground,
    Simulation,
    Time,
    domains,
    equils,
    grids,
    perturbations,
)

# For particles:
from struphy import (
    BinningPlot,
    BoundaryParameters,
    KernelDensityPlot,
    LoadingParameters,
    WeightsParameters,
    maxwellians,
)

# ---------------------
# Instance of the model
# ---------------------

from struphy.models import Vlasov
model = Vlasov()

# List all species and set their physical properties (charge and mass number, etc.)
model.kinetic_ions.set_species_properties()

# List all variables and decide whether to save their data
model.kinetic_ions.var.save_data = True

# --------------------------
# Instance of the simulation
# --------------------------

# Environment options
env = EnvironmentOptions()

# Units
base_units = BaseUnits()

# Time stepping
time_opts = Time(dt=0.2, Tend=10.0)

# Geometry
a1 = 0.00001
a2 = 5.0
Lz = 20.0
domain = domains.HollowCylinder(a1=a1, a2=a2, Lz=Lz)

# Fluid equilibrium (can be used as part of initial conditions)
B0x = 0.0
B0y = 0.0
B0z = 1.0
equil = equils.HomogenSlab(B0x=B0x, B0y=B0y, B0z=B0z)

# Grid
grid = grids.TensorProductGrid()

# Derham options
spl_kind = (False, True, True)
derham_opts = DerhamOptions(spl_kind=spl_kind)

# Simulation object
sim = Simulation(
    model=model,
    name=name,
    description=description,
    params_path=__file__,
    env=env,
    base_units=base_units,
    time_opts=time_opts,
    domain=domain,
    equil=equil,
    grid=grid,
    derham_opts=derham_opts,
    logging_level="WARNING",
)

# -------------------
# Particle parameters
# -------------------

loading_params = LoadingParameters(Np=20, seed=1234)
weights_params = WeightsParameters()
boundary_params = BoundaryParameters(bc=("remove", "periodic", "periodic"))
model.kinetic_ions.set_markers(
    loading_params=loading_params, weights_params=weights_params, boundary_params=boundary_params
)
model.kinetic_ions.set_sorting_boxes()
model.kinetic_ions.set_save_data(n_markers=1.0)

# propagator options
model.propagators.push_vxb.options = model.propagators.push_vxb.Options(algo="analytic")
model.propagators.push_eta.options = model.propagators.push_eta.Options()

# initial conditions (background + perturbation)
perturbation = None
background = maxwellians.Maxwellian3D(n=(1.0, perturbation))

model.kinetic_ions.var.add_background(background)

if __name__ == "__main__":
    sim.run(verbose=True)

@max-models
Copy link
Copy Markdown
Member Author

For the following launch file I get unexpected output en_f and logging that I cannot get rid of. I set the logging info with a string, but with the requested int it didn't work either.

# -----------------------------
# Description of the simulation
# -----------------------------
# Please fill in a verbal description of the simulation. 
# It will be printed at the beginning of the simulation and can be used to keep track of the different runs.

name = "Default Vlasov"
description = """
This is the default simulation for the model Vlasov. 
It is meant to be a template for users to set up their own simulations with this model. 
It contains all the necessary components of a Struphy simulation, including the model, 
the environment options, the time stepping options, the geometry, the equilibrium, 
the grid, the Derham options, and the initial conditions. 
Users can modify this file to set up their own simulations with different parameters and initial conditions.
"""

# ------------------
# Import Struphy API
# ------------------

from struphy import (
    BaseUnits,
    DerhamOptions,
    EnvironmentOptions,
    FieldsBackground,
    Simulation,
    Time,
    domains,
    equils,
    grids,
    perturbations,
)

# For particles:
from struphy import (
    BinningPlot,
    BoundaryParameters,
    KernelDensityPlot,
    LoadingParameters,
    WeightsParameters,
    maxwellians,
)

# ---------------------
# Instance of the model
# ---------------------

from struphy.models import Vlasov
model = Vlasov()

# List all species and set their physical properties (charge and mass number, etc.)
model.kinetic_ions.set_species_properties()

# List all variables and decide whether to save their data
model.kinetic_ions.var.save_data = True

# --------------------------
# Instance of the simulation
# --------------------------

# Environment options
env = EnvironmentOptions()

# Units
base_units = BaseUnits()

# Time stepping
time_opts = Time(dt=0.2, Tend=10.0)

# Geometry
a1 = 0.00001
a2 = 5.0
Lz = 20.0
domain = domains.HollowCylinder(a1=a1, a2=a2, Lz=Lz)

# Fluid equilibrium (can be used as part of initial conditions)
B0x = 0.0
B0y = 0.0
B0z = 1.0
equil = equils.HomogenSlab(B0x=B0x, B0y=B0y, B0z=B0z)

# Grid
grid = grids.TensorProductGrid()

# Derham options
spl_kind = (False, True, True)
derham_opts = DerhamOptions(spl_kind=spl_kind)

# Simulation object
sim = Simulation(
    model=model,
    name=name,
    description=description,
    params_path=__file__,
    env=env,
    base_units=base_units,
    time_opts=time_opts,
    domain=domain,
    equil=equil,
    grid=grid,
    derham_opts=derham_opts,
    logging_level="WARNING",
)

# -------------------
# Particle parameters
# -------------------

loading_params = LoadingParameters(Np=20, seed=1234)
weights_params = WeightsParameters()
boundary_params = BoundaryParameters(bc=("remove", "periodic", "periodic"))
model.kinetic_ions.set_markers(
    loading_params=loading_params, weights_params=weights_params, boundary_params=boundary_params
)
model.kinetic_ions.set_sorting_boxes()
model.kinetic_ions.set_save_data(n_markers=1.0)

# propagator options
model.propagators.push_vxb.options = model.propagators.push_vxb.Options(algo="analytic")
model.propagators.push_eta.options = model.propagators.push_eta.Options()

# initial conditions (background + perturbation)
perturbation = None
background = maxwellians.Maxwellian3D(n=(1.0, perturbation))

model.kinetic_ions.var.add_background(background)

if __name__ == "__main__":
    sim.run(verbose=True)

This is the same error as in the tutorials test, not sure what causes it.

@spossann
Copy link
Copy Markdown
Member

I fixed the tutorials error with the last two commits, this is sg else.

@max-models
Copy link
Copy Markdown
Member Author

These lines should be rewritten using just a string argument:

logger.info(f"rank {rank} | starts:", [component.starts for component in x1_PSY])

should be:

logger.info(f"rank {rank} | starts: {[component.starts for component in x1_PSY]}")

error here: https://github.com/struphy-hub/struphy/actions/runs/23428915045/job/68150111653?pr=199

@max-models
Copy link
Copy Markdown
Member Author

"rank {rank} | starts:", [component.starts for component in x1_PSY]

Fixed in 7d25987

@max-models
Copy link
Copy Markdown
Member Author

Seems like the logging is now working @spossann, but I think we still need to think through the setup before merging.

It's not clear to me where to call this setup_logger function, if it is needed at all.

@spossann
Copy link
Copy Markdown
Member

spossann commented Apr 2, 2026

Seems like the logging is now working @spossann, but I think we still need to think through the setup before merging.

It's not clear to me where to call this setup_logger function, if it is needed at all.

Ok good! I played around a bit, see here: #219

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants