-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added export functionality for user inputs in dashboard (#719)
- Loading branch information
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from ..Input.distributionParameters.distributionMain import parameter_input_checker | ||
from ..Input.latticeConfiguration.latticeMain import parameter_input_checker_for_lattice | ||
from ..trame_setup import setup_server | ||
|
||
server, state, ctrl = setup_server() | ||
|
||
# ----------------------------------------------------------------------------- | ||
# Helper Functions | ||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
def build_distribution_list(): | ||
""" | ||
Generates an instance of distribution inputs | ||
as a string for exporting purposes. | ||
""" | ||
distribution_name = state.selectedDistribution | ||
parameters = parameter_input_checker() | ||
|
||
distribution_parameters = ",\n ".join( | ||
f"{key}={value}" for key, value in parameters.items() | ||
) | ||
|
||
return ( | ||
f"distr = distribution.{distribution_name}(\n {distribution_parameters},\n)" | ||
) | ||
|
||
|
||
def build_lattice_list(): | ||
""" | ||
Generates a string representation of lattice element | ||
inputs for export purposes. | ||
""" | ||
|
||
lattice_elements = ",\n ".join( | ||
f'elements.{element["name"]}(' | ||
+ ", ".join( | ||
f"{key}={value}" | ||
for key, value in parameter_input_checker_for_lattice(element).items() | ||
) | ||
+ ")" | ||
for element in state.selectedLatticeList | ||
) | ||
|
||
return f"lattice_configuration = [\n {lattice_elements}\n]" | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
# Trame setup | ||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
def input_file(): | ||
""" | ||
This function creates the template to export | ||
dashboard user inputs into a python script. | ||
""" | ||
script = f""" | ||
from impactx import ImpactX, distribution, elements | ||
sim = ImpactX() | ||
sim.particle_shape = {state.particle_shape} | ||
sim.space_charge = False | ||
sim.csr = False | ||
sim.slice_step_diagnostics = True | ||
sim.init_grids() | ||
# Initialize particle beam | ||
kin_energy_MeV = {state.kin_energy_MeV} | ||
bunch_charge_C = {state.bunch_charge_C} | ||
npart = {state.npart} | ||
# Reference particle | ||
ref = sim.particle_container().ref_particle() | ||
ref.set_charge_qe(-1.0).set_mass_MeV(0.510998950).set_kin_energy_MeV(kin_energy_MeV) | ||
{build_distribution_list()} | ||
sim.add_particles(bunch_charge_C, distr, npart) | ||
{build_lattice_list()} | ||
sim.lattice.extend(lattice_configuration) | ||
# Simulate | ||
sim.evolve() | ||
sim.finalize() | ||
""" | ||
|
||
return script |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters