Skip to content

Commit

Permalink
Collective Effects Config in Dashboard (#690)
Browse files Browse the repository at this point in the history
* Added initial space charge UI with basic compatibility with simulation

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Connected max_level and poisson_solver to simulation

* prob_relative simulation functionality added

* Update code structuring

* rename variables

* Moved input cards

* code structure adjustment

* Added space charge documentation

* Added CSR configuration

* Rename 'CSR Configuration' to 'CSR' for UI and code

folder name is still called 'csrConfiguration'

* Connect csr_bins to simulation

* Updated UI look and minor code structure

* Add initial validation for Space Charge

* update simulation code & change Vcombobox to vSelect

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Updated default values

* Format update

* Minor updates

* Added initial space charge UI with basic compatibility with simulation

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* prob_relative simulation functionality added

* Update code structuring

* rename variables

* Moved input cards

* Added space charge documentation

* Add initial validation for Space Charge

* update simulation code & change Vcombobox to vSelect

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Revert "Added IDs to UI components"

This reverts commit 4456665.

* Removed duiplicated UI cards

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Formatting change and remove duplicate code

* Update default n_cell

* particle_shape placed in CSR

* Added blocking_factor to UI, no functionality

* Added blocking_factor defaults

* Added blocking_factor functionality

last step is to connect to simulation

* Update default values for space charge

* Updated prob_relative validation

prob_relative values are required to decrease with increasing index

* Connected blocking_factor to simulation

* Add multigrid-specific numerical options

No validation yet and not yet connected to simulation

* Adjust UI formatting

* Update export_template for space charge and csr

* Updated default for blocking_factor xyz

* Fixed simulation.py after merge

Space charge/csr simulation content did not merge in correctly originally, this fixes it.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update validation for n_cell and blocking_factor

* Updated ui format

* Add csr validation

csr_bins int and positive number

* Update simulation station function

to ensure that users cannot run a simulation when inputs have errors

* connect multigrid advanced settings to simulation

* add space

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
proy30 and pre-commit-ci[bot] authored Nov 22, 2024
1 parent 7e28c8b commit 1c3861f
Show file tree
Hide file tree
Showing 8 changed files with 682 additions and 103 deletions.
66 changes: 66 additions & 0 deletions src/python/impactx/dashboard/Input/csrConfiguration/csrMain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from trame.widgets import vuetify

from ...trame_setup import setup_server
from ..generalFunctions import generalFunctions

server, state, ctrl = setup_server()

# -----------------------------------------------------------------------------
# Default State Variables
# -----------------------------------------------------------------------------

state.csr_bins = 150
state.csr_bins_error_message = ""

# -----------------------------------------------------------------------------
#
# -----------------------------------------------------------------------------


@state.change("csr_bins")
def on_csr_bins_change(csr_bins, **kwargs):
error_message = generalFunctions.validate_against(csr_bins, "int", ["positive"])
state.csr_bins_error_message = error_message
generalFunctions.update_simulation_validation_status()


# -----------------------------------------------------------------------------
# UI
# -----------------------------------------------------------------------------


class csrConfiguration:
@staticmethod
def card():
"""
Creates UI content for CSR.
"""

with vuetify.VCard(v_show="csr", style="width: 170px;"):
with vuetify.VCardTitle("CSR"):
vuetify.VSpacer()
vuetify.VIcon(
"mdi-information",
classes="ml-2",
click=lambda: generalFunctions.documentation("CSR"),
style="color: #00313C;",
)
vuetify.VDivider()
with vuetify.VCardText():
with vuetify.VRow(classes="my-0"):
with vuetify.VCol(classes="py-0"):
vuetify.VSelect(
label="Particle Shape",
v_model=("particle_shape",),
items=([1, 2, 3],),
dense=True,
)
with vuetify.VRow(classes="my-0"):
with vuetify.VCol(classes="py-0"):
vuetify.VTextField(
label="CSR Bins",
v_model=("csr_bins",),
error_messages=("csr_bins_error_message",),
type="number",
dense=True,
)
57 changes: 44 additions & 13 deletions src/python/impactx/dashboard/Input/generalFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ def documentation(section_name):
Opens a tab to the specified section link in the documentation.
:param section_name (str): The name of the documentation section to open.
"""

if section_name == "LatticeElements":
url = "https://impactx.readthedocs.io/en/latest/usage/python.html#lattice-elements"
elif section_name == "BeamDistributions":
url = "https://impactx.readthedocs.io/en/latest/usage/python.html#initial-beam-distributions"
elif section_name == "pythonParameters":
url = "https://impactx.readthedocs.io/en/latest/usage/python.html#general"
else:
url_dict = {
"LatticeElements": "https://impactx.readthedocs.io/en/latest/usage/python.html#lattice-elements",
"BeamDistributions": "https://impactx.readthedocs.io/en/latest/usage/python.html#initial-beam-distributions",
"pythonParameters": "https://impactx.readthedocs.io/en/latest/usage/python.html#general",
"space_charge_documentation": "https://impactx.readthedocs.io/en/latest/usage/parameters.html#space-charge",
"CSR": "https://impactx.readthedocs.io/en/latest/usage/parameters.html#coherent-synchrotron-radiation-csr",
}

url = url_dict.get(section_name)
if url is None:
raise ValueError(f"Invalid section name: {section_name}")

if "WSL_DISTRO_NAME" in os.environ:
Expand Down Expand Up @@ -104,11 +106,11 @@ def validate_against(input_value, value_type, additional_conditions=None):
if errors == [] and additional_conditions:
for condition in additional_conditions:
if condition == "non_zero" and value == 0:
errors.append("Must be non-zero")
if condition == "positive" and value <= 0:
errors.append("Must be positive")
if condition == "negative" and value >= 0:
errors.append("Must be negative")
errors.append("Must be non-zero.")
if condition == "positive" and value < 0:
errors.append("Must be positive.")
if condition == "negative" and value > 0:
errors.append("Must be negative.")

return errors

Expand Down Expand Up @@ -151,6 +153,35 @@ def update_simulation_validation_status():
if state.selectedLatticeList == []:
error_details.append("LatticeListIsEmpty")

# Check for errors in CSR parameters
if state.csr_bins_error_message:
error_details.append(f"CSR Bins: {state.csr_bins_error_message}")

# Check for errors in Space Charge parameters
if state.space_charge:
# n_cell parameters
for direction in ["x", "y", "z"]:
n_cell_error = getattr(state, f"error_message_n_cell_{direction}")
if n_cell_error:
error_details.append(f"n_cell_{direction}: {n_cell_error}")

# Blocking factor parameters
for direction in ["x", "y", "z"]:
blocking_factor_error = getattr(
state, f"error_message_blocking_factor_{direction}"
)
if blocking_factor_error:
error_details.append(
f"blocking_factor_{direction}: {blocking_factor_error}"
)

# Prob Relative Fields
for index, field in enumerate(state.prob_relative_fields):
if field["error_message"]:
error_details.append(
f"prob_relative[{index}]: {field['error_message']}"
)

state.disableRunSimulationButton = bool(error_details)

# -----------------------------------------------------------------------------
Expand Down
19 changes: 13 additions & 6 deletions src/python/impactx/dashboard/Input/inputParameters/inputMain.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,19 @@ def card(self):
)
vuetify.VDivider()
with vuetify.VCardText():
vuetify.VCombobox(
v_model=("particle_shape",),
label="Particle Shape",
items=([1, 2, 3],),
dense=True,
)
with vuetify.VRow(classes="py-2"):
with vuetify.VCol(cols=6, classes="py-0"):
vuetify.VCheckbox(
label="Space Charge",
v_model=("space_charge", False),
dense=True,
)
with vuetify.VCol(cols=6, classes="py-0"):
vuetify.VCheckbox(
label="CSR",
v_model=("csr", False),
dense=True,
)
with vuetify.VRow(classes="my-2"):
with vuetify.VCol(cols=6, classes="py-0"):
vuetify.VTextField(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from ...trame_setup import setup_server
from ..generalFunctions import generalFunctions

server, state, ctrl = setup_server()

# -----------------------------------------------------------------------------
# Functions
# -----------------------------------------------------------------------------


class SpaceChargeFunctions:
@staticmethod
def validate_prob_relative_fields(index, prob_relative_value):
"""
This function checks specific validation requirements
for prob_relative_fields.
:param index: The index of the prob_relative_field modified.
:param prob_relative_value: The numerical value entered by the user.
:return: An error message. An empty string if there is no error.
"""
error_message = ""

try:
prob_relative_value = float(prob_relative_value)
poisson_solver = state.poisson_solver

if index == 0:
if poisson_solver == "multigrid":
if prob_relative_value < 3:
error_message = "Must be greater than 3."
elif poisson_solver == "fft":
if prob_relative_value <= 1:
error_message = "Must be greater than 1."
else:
previous_value = float(state.prob_relative[index - 1])
if prob_relative_value >= previous_value:
error_message = (
f"Must be less than previous value ({previous_value})."
)
else:
if prob_relative_value <= 1:
error_message = "Must be greater than 1."
except ValueError:
error_message = "Must be a float."

return error_message

@staticmethod
def validate_n_cell_and_blocking_factor(direction):
"""
Validation function for n_cell and blocking_factor parameters.
"""
n_cell_value = getattr(state, f"n_cell_{direction}", None)
blocking_factor_value = getattr(state, f"blocking_factor_{direction}", None)

n_cell_errors = generalFunctions.validate_against(n_cell_value, "int")
blocking_factor_errors = generalFunctions.validate_against(
blocking_factor_value, "int", ["non_zero", "positive"]
)

setattr(state, f"error_message_n_cell_{direction}", "; ".join(n_cell_errors))
setattr(
state,
f"error_message_blocking_factor_{direction}",
"; ".join(blocking_factor_errors),
)

if not n_cell_errors and not blocking_factor_errors:
n_cell_value = int(n_cell_value)
blocking_factor_value = int(blocking_factor_value)
if n_cell_value % blocking_factor_value != 0:
setattr(
state,
f"error_message_n_cell_{direction}",
"Must be a multiple of blocking factor.",
)
Loading

0 comments on commit 1c3861f

Please sign in to comment.