Skip to content

Commit

Permalink
Update UI interactions (#779)
Browse files Browse the repository at this point in the history
* Improve selecting distribution name/type interactions

* Simplify existing prob_relative logic

* Fix prob_relative multigrid validation

* Update prob_relative interactions

User values are preserved rather than resetting every time max_level or poisson_solver changes.

* Add init value for first_field_value

* Revert back to correct default name

* Update variable names in exportTemplate.py
  • Loading branch information
proy30 authored Jan 13, 2025
1 parent 337f25b commit 350565b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@

from impactx import distribution

from .. import TrameFunctions, generalFunctions, setup_server, vuetify
from .. import (
DashboardDefaults,
TrameFunctions,
generalFunctions,
setup_server,
vuetify,
)
from .distributionFunctions import DistributionFunctions

server, state, ctrl = setup_server()
Expand Down Expand Up @@ -117,13 +123,18 @@ def distribution_parameters():

@state.change("distribution")
def on_distribution_name_change(distribution, **kwargs):
if distribution == "Thermal":
state.distribution_type = "Quadratic Form"
if distribution == "Thermal" or distribution == "Empty":
state.distribution_type = ""
state.distribution_type_disable = True
state.dirty("distribution_type")
else:
type_list_default = DashboardDefaults.LISTS["distribution_type_list"]
type_default = DashboardDefaults.DISTRIBUTION_PARAMETERS["distribution_type"]

if state.distribution_type not in type_list_default:
state.distribution_type = type_default

state.distribution_type_disable = False
populate_distribution_parameters()


@state.change("distribution_type")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def validate_prob_relative_fields(index, prob_relative_value):

if index == 0:
if poisson_solver == "multigrid":
if prob_relative_value < 3:
if prob_relative_value <= 3:
error_message = "Must be greater than 3."
elif poisson_solver == "fft":
if prob_relative_value <= 1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,32 @@
# -----------------------------------------------------------------------------


def populate_prob_relative_fields(max_level):
num_prob_relative_fields = int(max_level) + 1
def populate_prob_relative_fields():
tot_num_prob_relative_fields = int(state.max_level) + 1
fft_first_field_value = generalFunctions.get_default(
"prob_relative_first_value_fft", "default_values"
)
multigrid_first_field_value = generalFunctions.get_default(
"prob_relative_first_value_multigrid", "default_values"
)

first_field_value = 0
if state.poisson_solver == "fft":
state.prob_relative = [fft_first_field_value] + [0.0] * (
num_prob_relative_fields - 1
)
first_field_value = fft_first_field_value
elif state.poisson_solver == "multigrid":
state.prob_relative = [multigrid_first_field_value] + [0.0] * (
num_prob_relative_fields - 1
)
first_field_value = multigrid_first_field_value

if state.prob_relative:
size = len(state.prob_relative)
num_of_extra_fields = tot_num_prob_relative_fields - size

if size < tot_num_prob_relative_fields:
state.prob_relative.extend([0.0] * (num_of_extra_fields))
elif size > tot_num_prob_relative_fields:
state.prob_relative = state.prob_relative[:tot_num_prob_relative_fields]
else:
state.prob_relative = [0.0] * num_prob_relative_fields
state.prob_relative = [first_field_value] + [0.0] * (
tot_num_prob_relative_fields - 1
)

state.prob_relative_fields = [
{
Expand All @@ -45,7 +52,7 @@ def populate_prob_relative_fields(max_level):
),
"step": generalFunctions.get_default("prob_relative", "steps"),
}
for i in range(num_prob_relative_fields)
for i in range(tot_num_prob_relative_fields)
]


Expand Down Expand Up @@ -83,7 +90,7 @@ def update_blocking_factor_and_n_cell(category, kwargs):
# -----------------------------------------------------------------------------
@state.change("poisson_solver")
def on_poisson_solver_change(poisson_solver, **kwargs):
populate_prob_relative_fields(state.max_level)
populate_prob_relative_fields()
state.dirty("prob_relative_fields")
generalFunctions.update_simulation_validation_status()

Expand All @@ -96,7 +103,7 @@ def on_space_charge_change(space_charge, **kwargs):

@state.change("max_level")
def on_max_level_change(max_level, **kwargs):
populate_prob_relative_fields(max_level)
populate_prob_relative_fields()
generalFunctions.update_simulation_validation_status()


Expand Down
6 changes: 3 additions & 3 deletions src/python/impactx/dashboard/Toolbar/exportTemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ def build_distribution_list():
Generates an instance of distribution inputs
as a string for exporting purposes.
"""
distribution_name = state.selected_distribution
distribution_name = state.distribution
parameters = DistributionFunctions.convert_distribution_parameters_to_valid_type()

indentation = " " * (8 if state.selected_distribution_type == "Twiss" else 4)
indentation = " " * (8 if state.distribution_type == "Twiss" else 4)
distribution_parameters = ",\n".join(
f"{indentation}{key}={value}" for key, value in parameters.items()
)

if state.selected_distribution_type == "Twiss":
if state.distribution_type == "Twiss":
return (
f"distr = distribution.{distribution_name}(\n"
f" **twiss(\n"
Expand Down

0 comments on commit 350565b

Please sign in to comment.