Skip to content

Commit

Permalink
Add defaults.py, Custom Step Values, and fix code in dashboard (#772)
Browse files Browse the repository at this point in the history
* Added missing init files

* Added custom steps increment for various inputs

Functionality to add  custom step for all inputs except lattice is set.

* Centralize parameter units in PARAMETER_UNITS dictionary

* Centralize parameter default values in PARAMETER_VALUES dictionary

* Add more defaults and rename default variables

* fix bug1

Users could not run a simulation because the 'name' parameter in lattice element was always stuck with an error message

* Fix matplotlib errors

* Re-arrange defaults.py and update corresponding code
  • Loading branch information
proy30 authored Jan 4, 2025
1 parent e9d4df8 commit 56d7307
Show file tree
Hide file tree
Showing 11 changed files with 263 additions and 53 deletions.
8 changes: 5 additions & 3 deletions src/python/impactx/dashboard/Analyze/plotsMain.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def load_dataTable_data():
state.filtered_data = []
state.all_data = []
state.all_headers = []
state.phase_space_png = None

# -----------------------------------------------------------------------------
# Functions to update table/plot
Expand Down Expand Up @@ -134,14 +135,13 @@ def update_plot():
state.show_table = True
elif state.active_plot == "Phase Space Plots":
state.show_table = False
ctrl.matplotlib_figure_update(state.simulation_data)


def run_simulation_impactX():
buf = io.StringIO()

with pipes(stdout=buf, stderr=buf):
state.simulation_data = run_simulation()
run_simulation()

buf.seek(0)
lines = [line.strip() for line in buf.getvalue().splitlines()]
Expand Down Expand Up @@ -232,7 +232,9 @@ def plot():
vuetify.VDivider()
with vuetify.VTabsItems(v_model="active_tab"):
with vuetify.VTabItem():
vuetify.VImg(v_if=("image_data",), src=("image_data",))
vuetify.VImg(
v_if=("phase_space_png",), src=("phase_space_png",)
)

with vuetify.VTabItem():
with vuetify.VContainer(
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Default State Variables
# -----------------------------------------------------------------------------

state.csr_bins = 150
state.csr_bins = generalFunctions.get_default("csr_bins", "default_values")
state.csr_bins_error_message = ""

# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -63,4 +63,6 @@ def card():
error_messages=("csr_bins_error_message",),
type="number",
dense=True,
step=generalFunctions.get_default("csr_bins", "steps"),
__properties=["step"],
)
91 changes: 91 additions & 0 deletions src/python/impactx/dashboard/Input/defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
class DashboardDefaults:
"""
Defaults for input parameters in the ImpactX dashboard.
"""

# -------------------------------------------------------------------------
# Inputs by section
# -------------------------------------------------------------------------

INPUT_PARAMETERS = {
"space_charge": False,
"csr": False,
"charge_qe": -1,
"mass_MeV": 0.51099895,
"npart": 1000,
"kin_energy": 2e3,
"kin_energy_unit": "MeV",
"bunch_charge_C": 1e-9,
}

DISTRIBUTION = {
"selected_distribution": "Waterbag",
"selected_distribution_type": "Twiss",
}

LATTICE = {
"selected_lattice_list": [],
"selected_lattice": None,
}

SPACE_CHARGE = {
"dynamic_size": False,
"poisson_solver": "fft",
"particle_shape": 2,
"max_level": 0,
"n_cell": 32,
"blocking_factor": 16,
"prob_relative_first_value_fft": 1.1,
"prob_relative_first_value_multigrid": 3.1,
"mlmg_relative_tolerance": 1.0e-7,
"mlmg_absolute_tolerance": 0,
"mlmg_verbosity": 1,
"mlmg_max_iters": 100,
}

CSR = {
"particle_shape": 2,
"csr_bins": 150,
}

LISTS = {
"kin_energy_unit_list": ["meV", "eV", "keV", "MeV", "GeV", "TeV"],
"distribution_type_list": ["Twiss", "Quadratic"],
"poisson_solver_list": ["fft", "multigrid"],
"particle_shape_list": [1, 2, 3],
"max_level_list": [0, 1, 2, 3, 4],
}

# -------------------------------------------------------------------------
# Main
# -------------------------------------------------------------------------

DEFAULT_VALUES = {
**INPUT_PARAMETERS,
**DISTRIBUTION,
**LATTICE,
**SPACE_CHARGE,
**CSR,
**LISTS,
}

# If a parameter is not included in the dictionary, default step amount is 1.
STEPS = {
"mass_MeV": 0.1,
"bunch_charge_C": 1e-11,
"prob_relative": 0.1,
"mlmg_relative_tolerance": 1e-12,
"mlmg_absolute_tolerance": 1e-12,
"beta": 0.1,
"emitt": 1e-7,
"alpha": 0.1,
}

UNITS = {
"charge_qe": "qe",
"mass_MeV": "MeV",
"bunch_charge_C": "C",
"mlmg_absolute_tolerance": "V/m",
"beta": "m",
"emitt": "m",
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@
# Defaults
# -----------------------------------------------------------------------------

state.selectedDistribution = "Waterbag"
state.selectedDistributionType = "Twiss"
state.selectedDistribution = generalFunctions.get_default(
"selected_distribution", "default_values"
)
state.selectedDistributionType = generalFunctions.get_default(
"selected_distribution_type", "default_values"
)
state.selectedDistributionParameters = []
state.distributionTypeDisabled = False

Expand Down Expand Up @@ -63,9 +67,10 @@ def populate_distribution_parameters(selectedDistribution):
"parameter_error_message": generalFunctions.validate_against(
param.default if param.default != param.empty else None, "float"
),
"parameter_units": "m"
"parameter_units": generalFunctions.get_default(param.name, "units")
if "beta" in param.name or "emitt" in param.name
else "",
"parameter_step": generalFunctions.get_default(param.name, "steps"),
}
for param in sig.parameters.values()
]
Expand All @@ -88,6 +93,7 @@ def populate_distribution_parameters(selectedDistribution):
"parameter_units": "m"
if "beta" in parameter[0] or "emitt" in parameter[0]
else "",
"parameter_step": generalFunctions.get_default(parameter[0], "steps"),
}
for parameter in selectedDistributionParameters
]
Expand Down Expand Up @@ -206,7 +212,11 @@ def card():
vuetify.VSelect(
v_model=("selectedDistributionType",),
label="Type",
items=(["Twiss", "Quadratic Form"],),
items=(
generalFunctions.get_default(
"distribution_type_list", "default_values"
),
),
dense=True,
disabled=("distributionTypeDisabled",),
)
Expand All @@ -231,5 +241,7 @@ def card():
"parameter.parameter_error_message",
),
type="number",
step=("parameter.parameter_step",),
__properties=["step"],
dense=True,
)
17 changes: 16 additions & 1 deletion src/python/impactx/dashboard/Input/generalFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import webbrowser

from ..trame_setup import setup_server
from .defaults import DashboardDefaults

server, state, ctrl = setup_server()

Expand Down Expand Up @@ -45,6 +46,17 @@ def documentation(section_name):
else:
webbrowser.open_new_tab(url)

@staticmethod
def get_default(parameter, type):
parameter_type_dictionary = getattr(DashboardDefaults, f"{type.upper()}", None)
parameter_default = parameter_type_dictionary.get(parameter)

if parameter_default is not None:
return parameter_default

parameter_name_base = parameter.partition("_")[0]
return parameter_type_dictionary.get(parameter_name_base)

# -----------------------------------------------------------------------------
# Validation functions
# -----------------------------------------------------------------------------
Expand All @@ -71,12 +83,15 @@ def validate_against(input_value, value_type, additional_conditions=None):
Validates the input value against the desired type and additional conditions.
:param input_value: The value to validate.
:param value_type: The desired type ('int', 'float', 'str').
:param conditions: A list of additional conditions to validate.
:param additional_conditions: A list of additional conditions to validate.
:return: A list of error messages. An empty list if there are no errors.
"""
errors = []
value = None

if input_value == "None":
return errors

# value_type checking
if value_type == "int":
if input_value is None:
Expand Down
58 changes: 39 additions & 19 deletions src/python/impactx/dashboard/Input/inputParameters/inputMain.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,23 @@ class InputParameters:
"""

def __init__(self):
state.particle_shape = 2
state.npart = 1000
state.kin_energy = 2.0e3
state.particle_shape = generalFunctions.get_default(
"particle_shape", "default_values"
)
state.npart = generalFunctions.get_default("npart", "default_values")
state.kin_energy = generalFunctions.get_default("kin_energy", "default_values")
state.kin_energy_MeV = state.kin_energy
state.bunch_charge_C = 1.0e-9
state.kin_energy_unit = "MeV"
state.old_kin_energy_unit = "MeV"
state.charge_qe = -1
state.mass_MeV = 0.510998950
state.bunch_charge_C = generalFunctions.get_default(
"bunch_charge_C", "default_values"
)
state.kin_energy_unit = generalFunctions.get_default(
"kin_energy_unit", "default_values"
)
state.old_kin_energy_unit = generalFunctions.get_default(
"kin_energy_unit", "default_values"
)
state.charge_qe = generalFunctions.get_default("charge_qe", "default_values")
state.mass_MeV = generalFunctions.get_default("mass_MeV", "default_values")

state.npart_validation = []
state.kin_energy_validation = []
Expand Down Expand Up @@ -114,8 +122,10 @@ def card(self):
vuetify.VTextField(
label="Ref. Particle Charge",
v_model=("charge_qe",),
suffix="qe",
suffix=generalFunctions.get_default("charge_qe", "units"),
type="number",
step=generalFunctions.get_default("charge_qe", "steps"),
__properties=["step"],
dense=True,
error_messages=("charge_qe_validation",),
change=(
Expand All @@ -127,8 +137,10 @@ def card(self):
vuetify.VTextField(
label="Ref. Particle Mass",
v_model=("mass_MeV",),
suffix="MeV",
suffix=generalFunctions.get_default("mass_MeV", "units"),
type="number",
step=generalFunctions.get_default("mass_MeV", "steps"),
__properties=["step"],
dense=True,
error_messages=("mass_MeV_validation",),
change=(
Expand All @@ -147,6 +159,8 @@ def card(self):
"[$event, 'int','npart','npart_validation']",
),
type="number",
step=generalFunctions.get_default("npart", "steps"),
__properties=["step"],
dense=True,
)
with vuetify.VRow(classes="my-2"):
Expand All @@ -160,19 +174,25 @@ def card(self):
"[$event, 'float','kin_energy','kin_energy_validation']",
),
type="number",
step=generalFunctions.get_default("kin_energy", "steps"),
__properties=["step"],
dense=True,
classes="mr-2",
)
with vuetify.VCol(cols=4, classes="py-0"):
vuetify.VSelect(
v_model=("kin_energy_unit",),
label="Unit",
items=(["meV", "eV", "keV", "MeV", "GeV", "TeV"],),
items=(
generalFunctions.get_default(
"kin_energy_unit_list", "default_values"
),
),
change=(ctrl.kin_energy_unit_change, "[$event]"),
dense=True,
)
with vuetify.VRow(classes="my-2"):
with vuetify.VCol(cols=8, classes="py-0"):
with vuetify.VCol(cols=12, classes="py-0"):
vuetify.VTextField(
label="Bunch Charge",
v_model=("bunch_charge_C",),
Expand All @@ -182,12 +202,12 @@ def card(self):
"[$event, 'float','bunch_charge_C','bunch_charge_C_validation']",
),
type="number",
step=generalFunctions.get_default(
"bunch_charge_C", "steps"
),
__properties=["step"],
dense=True,
)
with vuetify.VCol(cols=4, classes="py-0"):
vuetify.VTextField(
label="Unit",
value="C",
dense=True,
disabled=True,
suffix=generalFunctions.get_default(
"bunch_charge_C", "units"
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
# Default
# -----------------------------------------------------------------------------

state.selectedLattice = None
state.selectedLattice = generalFunctions.get_default("lattice", "default_values")
state.selectedLatticeList = []
state.nsliceDefaultValue = None
state.nsliceDefaultValue = generalFunctions.get_default("n_slice", "default_values")

# -----------------------------------------------------------------------------
# Main Functions
Expand Down Expand Up @@ -429,6 +429,11 @@ def dialog_lattice_settings():
ctrl.nsliceDefaultChange,
"['nslice', $event]",
),
type="number",
step=generalFunctions.get_default(
"nslice", "steps"
),
__properties=["step"],
placeholder="Value",
dense=True,
outlined=True,
Expand Down
Empty file.
Loading

0 comments on commit 56d7307

Please sign in to comment.