Skip to content

Dev NewReportExample

Paul Moeller edited this page Dec 20, 2018 · 10 revisions

Adding a New Report to Sirepo

Creating a new Sirepo report requires updating the application schema, html template, and application python template. Below are the steps required:

  • Create the model and view definitions in the application schema file.
  • Add the reportPanel directive to the html template used by the angularjs route.
  • Update the application template python module to add the new model data to existing simulation files.
  • Update the application package_data jinja template to use the new model values.

As an example, we'll add a new report to the SRW application named farFieldPowerDensityReport which has the same fields as the powerDensityReport. This model will always use the "far field" setting when generating the power density report.

Create a new model and view in the application schema, srw-schema.json:

--- a/sirepo/package_data/static/json/srw-schema.json
+++ b/sirepo/package_data/static/json/srw-schema.json
@@ -457,6 +457,16 @@
             "horizontalOffset": ["Horizontal Center Position [mm]", "Float", 0],
             "verticalOffset": ["Vertical Center Position [mm]", "Float", 0]
         },
+        "farFieldPowerDensityReport": {
+            "distanceFromSource": ["Distance From Source [m]", "Float"],
+            "horizontalPosition": ["Horizontal Center Position [mm]", "Float"],
+            "horizontalRange": ["Range of Horizontal Position [mm]", "Float"],
+            "verticalPosition": ["Vertical Center Position [mm]", "Float"],
+            "verticalRange": ["Range of Vertical Position [mm]", "Float"],
+            "precision": ["Relative Precision", "Float"],
+            "horizontalPointCount": ["Number of Points vs Horizontal Position", "Integer"],
+            "verticalPointCount": ["Number of Points vs Vertical Position", "Integer"]
+        },
         "fiber": {
             "title": ["Element Name", "String", "Fiber"],
             "position": ["Nominal Position [m]", "Float"],
@@ -1076,6 +1086,26 @@
                 ]]
             ]
         },
+        "farFieldPowerDensityReport": {
+            "title": "Far Field Power Density Report",
+            "basic": [],
+            "advanced": [
+                "distanceFromSource",
+                [
+                    ["Horizontal", [
+                        "horizontalPosition",
+                        "horizontalRange",
+                        "horizontalPointCount"
+                    ]],
+                    ["Vertical", [
+                        "verticalPosition",
+                        "verticalRange",
+                        "verticalPointCount"
+                    ]]
+                ],
+                "precision"
+            ]
+        },
         "fiber": {
             "title": "Fiber",
             "basic": [],

Add the following to the html template used by the angularjs route. In this case srw-source.html:

--- a/sirepo/package_data/static/html/srw-source.html
+++ b/sirepo/package_data/static/html/srw-source.html
@@ -32,6 +32,9 @@
     <div data-ng-if="source.srwService.isElectronBeam()" class="col-md-6 col-xl-4">
       <div data-report-panel="3d" data-request-priority="3" data-model-name="powerDensityReport" data-panel-title="{{ source.srwService.getReportTitle('powerDensityReport') }}"></div>
     </div>
+    <div data-ng-if="source.srwService.isElectronBeam()" class="col-md-6 col-xl-4">
+      <div data-report-panel="3d" data-model-name="farFieldPowerDensityReport"></div>
+    </div>
     <div data-ng-if="source.srwService.isTabulatedUndulator() || source.srwService.isIdealizedUndulator()" class="clearfix hidden-xl"></div>
     <div data-ng-if="source.srwService.isGaussianBeam() || source.srwService.isElectronBeam()" class="col-md-6 col-xl-4">
       <div data-report-panel="3d" data-request-priority="4" data-model-name="sourceIntensityReport" data-panel-title="{{ source.srwService.getReportTitle('sourceIntensityReport') }}"></div>

Update the sirepo/template module for the application to add the new model to existing simulation data files. In this case we'll copy the appropriate settings from the existing powerDensityReport which already exists in the application data files.

This example doen't require any changes to the application's jinja template. Instead, the model values will get copied into the existing powerDensityReport model prior to generating the python code. Edit the template/srw.py file and make these changes:

--- a/sirepo/template/srw.py
+++ b/sirepo/template/srw.py
@@ -49,6 +49,7 @@ _DATA_FILE_FOR_MODEL = pkcollections.Dict({
     'mirrorReport': {'filename': _MIRROR_OUTPUT_FILE, 'dimension': 3},
     'multiElectronAnimation': {'filename': 'res_int_pr_me.dat', 'dimension': 3},
     'powerDensityReport': {'filename': 'res_pow.dat', 'dimension': 3},
+    'farFieldPowerDensityReport': {'filename': 'res_pow.dat', 'dimension': 3},
     'sourceIntensityReport': {'filename': 'res_int_se.dat', 'dimension': 3},
     'brillianceReport': {'filename': _BRILLIANCE_OUTPUT_FILE, 'dimension': 2},
     'trajectoryReport': {'filename': 'res_trj.dat', 'dimension': 2},
@@ -317,6 +318,9 @@ def fixup_old_data(data):
         if m not in data['models']:
             data['models'][m] = pkcollections.Dict()
         template_common.update_model_defaults(data['models'][m], m, _SCHEMA)
+    if 'farFieldPowerDensityReport' not in data['models']:
+        data['models']['farFieldPowerDensityReport'] = data['models']['powerDensityReport'].copy()
+        del data['models']['farFieldPowerDensityReport']['method']
     for m in data['models']:
         if template_common.is_watchpoint(m):
             template_common.update_model_defaults(data['models'][m], 'watchpointReport', _SCHEMA)
@@ -1411,6 +1415,10 @@ def _generate_parameters_file(data, plot_reports=False, run_dir=None):
                   'sampleFactor', 'samplingMethod', 'verticalPointCount', 'verticalPosition', 'verticalRange']:
             data['models']['simulation'][k] = data['models']['sourceIntensityReport'][k]

+    if data['report'] == 'farFieldPowerDensityReport':
+        data['models']['powerDensityReport'] = data['models'][data['report']].copy()
+        data['models']['powerDensityReport']['method'] = 2
+
     if _is_tabulated_undulator_source(data['models']['simulation']):
         if undulator_type == 'u_i':
             data['models']['tabulatedUndulator']['gap'] = 0.0
@@ -1507,7 +1515,7 @@ def _generate_srw_main(data, plot_reports):
         content.append('v.sm = True')
         if plot_reports:
             content.append("v.sm_pl = 'e'")
-    if (run_all and source_type != 'g') or report == 'powerDensityReport':
+    if (run_all and source_type != 'g') or report == 'powerDensityReport' or report == 'farFieldPowerDensityReport':
         content.append('v.pw = True')
         if plot_reports:
             content.append("v.pw_pl = 'xy'")

On a dev VM, to trigger a schema fixup, change the modified date on the schema-common.json file, in the bash terminal. Then restart the webserver, and on the next request the fixups will be applied to all the application datafiles.

$ touch sirepo/package_data/static/json/schema-common.json

If the model has new fields which aren't already available in the generated python script, add the new values to the sirepo/package_data/parameters.py.jinja jinja template. The model values are flattened into jinja variables, for example the model value powerDensityReport.verticalRange would be referenced as {{ powerDensityReport_verticalRange }} in the jinja template.

Clone this wiki locally