def get_schema(sim_type): if sim_type in _SCHEMA_CACHE: return _SCHEMA_CACHE[sim_type] schema = read_json(STATIC_FOLDER.join('json/{}-schema'.format(sim_type))) pkcollections.mapping_merge(schema, SCHEMA_COMMON) pkcollections.mapping_merge( schema, {'feature_config': feature_config.for_sim_type(sim_type)}, ) schema.simulationType = sim_type _SCHEMA_CACHE[sim_type] = schema #TODO(mvk): improve merging common and local schema _merge_dicts(schema.common.dynamicFiles, schema.dynamicFiles) schema.dynamicModules = _files_in_schema(schema.dynamicFiles) for item in [ 'appModes', 'constants', 'cookies', 'enum', 'notifications', 'localRoutes', 'model', 'view' ]: if item not in schema: schema[item] = pkcollections.Dict() _merge_dicts(schema.common[item], schema[item]) _validate_schema(schema) return schema
def _propagation(values): merged = copy.deepcopy(_PROPAGATION) pkcollections.mapping_merge(merged, values) return pkcollections.map_values( merged, lambda v: int(v) if isinstance(v, bool) else v, )
def get_schema(sim_type): if sim_type in _SCHEMA_CACHE: return _SCHEMA_CACHE[sim_type] schema = read_json(STATIC_FOLDER.join('json/{}-schema'.format(sim_type))) pkcollections.mapping_merge(schema, SCHEMA_COMMON) pkcollections.mapping_merge( schema, {'feature_config': feature_config.for_sim_type(sim_type)}, ) schema['simulationType'] = sim_type _SCHEMA_CACHE[sim_type] = schema # merge common models into app models common_models = schema['commonModels'] app_models = schema['model'] for model_Name in common_models: if model_Name not in app_models: app_models[model_Name] = common_models[model_Name] for model_field_name in common_models[model_Name]: if model_field_name not in app_models[model_Name]: app_models[model_Name][model_field_name] = common_models[ model_Name][model_field_name] # merge common enums into app models common_enums = schema['commonEnums'] app_enums = schema['enum'] for enum_Name in common_enums: if enum_Name not in app_enums: app_enums[enum_Name] = common_enums[enum_Name] # merge common views into app views - since these can be deeply nested, for now merge only # the title, basic fields, and top level of advanced fields common_views = schema['commonViews'] app_views = schema['view'] for view_Name in common_views: if view_Name not in app_views: app_views[view_Name] = common_views[view_Name] if 'title' not in app_views[view_Name] and 'title' in common_views[ view_Name]: app_views[view_Name]['title'] = common_views[view_Name]['title'] if 'basic' not in app_views[view_Name] and 'basic' in common_views[ view_Name]: for basic_field in common_views[view_Name]['basic']: if basic_field not in app_views[view_Name]['basic']: app_views[view_Name]['basic'][basic_field] = basic_field if 'advanced' in common_views[view_Name]: for advanced_field in common_views[view_Name]['advanced']: # ignore arrays if isinstance( advanced_field, basestring ) and advanced_field not in app_views[view_Name]['advanced']: app_views[view_Name]['advanced'].append(advanced_field) return schema
def get_schema(sim_type): if sim_type in _SCHEMA_CACHE: return _SCHEMA_CACHE[sim_type] schema = read_json(STATIC_FOLDER.join('json/{}-schema'.format(sim_type))) pkcollections.mapping_merge(schema, SCHEMA_COMMON) pkcollections.mapping_merge( schema, {'feature_config': feature_config.for_sim_type(sim_type)}, ) schema['simulationType'] = sim_type _SCHEMA_CACHE[sim_type] = schema return schema
def get_schema(sim_type): if sim_type in _SCHEMA_CACHE: return _SCHEMA_CACHE[sim_type] schema = read_json( STATIC_FOLDER.join('json/{}-schema'.format(sim_type))) pkcollections.mapping_merge(schema, SCHEMA_COMMON) pkcollections.mapping_merge( schema, {'feature_config': feature_config.for_sim_type(sim_type)}, ) schema['simulationType'] = sim_type _SCHEMA_CACHE[sim_type] = schema return schema
def get_schema(sim_type): if sim_type in _SCHEMA_CACHE: return _SCHEMA_CACHE[sim_type] schema = read_json(STATIC_FOLDER.join('json/{}-schema'.format(sim_type))) pkcollections.mapping_merge(schema, SCHEMA_COMMON) pkcollections.mapping_merge( schema, {'feature_config': feature_config.for_sim_type(sim_type)}, ) schema['simulationType'] = sim_type _SCHEMA_CACHE[sim_type] = schema # merge common local routes into app local routes util.merge_dicts(schema['commonLocalRoutes'], schema['localRoutes'], 2) if 'appModes' not in schema: schema['appModes'] = {} util.merge_dicts(schema['commonAppModes'], schema['appModes'], 1) # merge common models into app models util.merge_dicts(schema['commonModels'], schema['model'], 2) # merge common enums into app models util.merge_dicts(schema['commonEnums'], schema['enum'], 1) # merge common views into app views - since these can be deeply nested, for now merge only # the title, basic fields, and top level of advanced fields common_views = schema['commonViews'] app_views = schema['view'] util.merge_dicts(common_views, app_views, 1) for view_Name in common_views: if 'title' not in app_views[view_Name] and 'title' in common_views[ view_Name]: app_views[view_Name]['title'] = common_views[view_Name]['title'] if 'basic' not in app_views[view_Name] and 'basic' in common_views[ view_Name]: for basic_field in common_views[view_Name]['basic']: if basic_field not in app_views[view_Name]['basic']: app_views[view_Name]['basic'][basic_field] = basic_field if 'advanced' in common_views[view_Name]: for advanced_field in common_views[view_Name]['advanced']: # ignore arrays if isinstance( advanced_field, basestring ) and advanced_field not in app_views[view_Name]['advanced']: app_views[view_Name]['advanced'].append(advanced_field) _validate_schema(schema) return schema
def get_schema(sim_type): """Get the schema for `sim_type` If sim_type is None, it will return the schema for the first sim_type in `feature_config.cfg().sim_types` Args: sim_type (str): must be valid Returns: dict: Shared schem """ t = sirepo.template.assert_sim_type(sim_type) if sim_type is not None \ else list(feature_config.cfg().sim_types)[0] if t in _SCHEMA_CACHE: return _SCHEMA_CACHE[t] schema = read_json(STATIC_FOLDER.join('json/{}-schema'.format(t))) pkcollections.mapping_merge(schema, SCHEMA_COMMON) pkcollections.mapping_merge( schema, PKDict(feature_config=feature_config.for_sim_type(t)), ) schema.feature_config = feature_config.for_sim_type(t) schema.simulationType = t _SCHEMA_CACHE[t] = schema #TODO(mvk): improve merging common and local schema _merge_dicts(schema.common.dynamicFiles, schema.dynamicFiles) schema.dynamicModules = _files_in_schema(schema.dynamicFiles) for item in [ 'appModes', 'constants', 'cookies', 'enum', 'notifications', 'localRoutes', 'model', 'view' ]: if item not in schema: schema[item] = PKDict() _merge_dicts(schema.common[item], schema[item]) _merge_subclasses(schema, item) srschema.validate(schema) return schema
def get_schema(sim_type): if sim_type in _SCHEMA_CACHE: return _SCHEMA_CACHE[sim_type] schema = read_json( STATIC_FOLDER.join('json/{}-schema'.format(sim_type))) pkcollections.mapping_merge(schema, SCHEMA_COMMON) pkcollections.mapping_merge( schema, {'feature_config': feature_config.for_sim_type(sim_type)}, ) schema.simulationType = sim_type _SCHEMA_CACHE[sim_type] = schema #TODO(mvk): improve merging common and local schema _merge_dicts(schema.common.dynamicFiles, schema.dynamicFiles) schema.dynamicModules = _files_in_schema(schema.dynamicFiles) for item in ['appModes', 'constants', 'cookies', 'enum', 'notifications', 'localRoutes', 'model', 'view']: if item not in schema: schema[item] = pkcollections.Dict() _merge_dicts(schema.common[item], schema[item]) _validate_schema(schema) return schema
def test_mapping_merge(): n, order = _random_init() pkcollections.mapping_merge(n, {}) assert list(order) == _keys(n), \ 'mapping_merge of empty dict should do nothing' pkcollections.mapping_merge(n, OrderedMapping()) assert list(order) == _keys(n), \ 'mapping_merge of empty OrderedMapping should do nothing' n2 = OrderedMapping(n) existing = order[0] new = '!' pkcollections.mapping_merge(n, {existing: 3, new: 4}) order += new assert list(order) == _keys(n), \ 'mapping_merge with dict should replace and add' pkcollections.mapping_merge(n2, OrderedMapping(b=3, c=4)) assert order == _keys(n), \ 'mapping_merge with dict should replace and add'
def simulate(params, msg_callback=lambda _: _): """Run a multi-particle (thick) simulation and return results Args: params (OrderedMapping): configuration parameters msg_callback (function): Called at various stages to log output Returns: OrderedMapping: results and params (see code for format) """ p = pkcollections.OrderedMapping() for k in 'simulation_kind', 'wavefront': v = params[k] p[k] = v.value if hasattr(v, 'value') else v pkcollections.mapping_merge( p, srw_params.to_undulator_multi_particle(params.undulator)) p.beam = srw_params.to_beam(params.beam) p.stkF = srw_params.to_wavefront_multi_particle(p.wavefront) p.stkP = srw_params.to_wavefront_multi_particle(p.wavefront) p.ar_prec_f = srw_params.to_flux_precision(params.precision) p.ar_prec_p = srw_params.to_power_precision(params.precision) p.arPrecPar = [1] #General Precision parameters for Trajectory calculation: p.fieldInterpMeth = 4 p.plots = [] msg_callback('Performing trajectory calculation') _trajectory(p) if params.simulation_kind == 'E': msg_callback('Performing Electric Field (spectrum vs photon energy) calculation') msg_callback('Extracting Intensity from calculated Electric Field') srwlib.srwl.CalcStokesUR(p.stkF, p.beam, p.und, p.ar_prec_f) p.plots.append([ uti_plot.uti_plot1d, p.stkF.arS, [p.stkF.mesh.eStart, p.stkF.mesh.eFin, p.stkF.mesh.ne], [ 'Photon Energy [eV]', 'Flux [ph/s/.1%bw]', 'Flux through Finite Aperture', ], ]) elif params.simulation_kind == 'X': msg_callback('Performing Power Density calculation (from field) vs x-coordinate calculation') srwlib.srwl.CalcPowDenSR(p.stkP, p.beam, 0, p.magFldCnt, p.ar_prec_p) msg_callback('Extracting Intensity from calculated Electric Field') p.plotMeshX = [1000*p.stkP.mesh.xStart, 1000*p.stkP.mesh.xFin, p.stkP.mesh.nx] p.powDenVsX = pkarray.new_float([0]*p.stkP.mesh.nx) for i in xrange(p.stkP.mesh.nx): powDenVsX[i] = p.stkP.arS[p.stkP.mesh.nx*int(p.stkP.mesh.ny*0.5) + i] p.plots.append([ uti_plot.uti_plot1d, p.powDenVsX, p.plotMeshX, [ 'Horizontal Position [mm]', 'Power Density [W/mm^2]', 'Power Density\n(horizontal cut at y = 0)', ], ]) elif params.simulation_kind == 'Y': msg_callback('Performing Power Density calculation (from field) vs x-coordinate calculation') srwlib.srwl.CalcPowDenSR(p.stkP, p.beam, 0, p.magFldCnt, p.ar_prec_p) msg_callback('Extracting Intensity from calculated Electric Field') p.plotMeshY = [1000*p.stkP.mesh.yStart, 1000*p.stkP.mesh.yFin, p.stkP.mesh.ny] p.powDenVsY = pkarray.new_float([0]*p.stkP.mesh.ny) for i in xrange(p.stkP.mesh.ny): p.powDenVsY[i] = p.stkP.arS[p.stkP.mesh.ny*int(p.stkP.mesh.nx*0.5) + i] p.plots.append([ uti_plot.uti_plot1d, p.powDenVsY, p.plotMeshY, [ 'Vertical Position [mm]', 'Power Density [W/mm^2]', 'Power Density\n(vertical cut at x = 0)', ], ]) elif params.simulation_kind == 'X_AND_Y': msg_callback('Performing Electric Field (intensity vs x- and y-coordinate) calculation') srwlib.srwl.CalcPowDenSR(p.stkP, p.beam, 0, p.magFldCnt, p.ar_prec_p) msg_callback('Extracting Intensity from calculated Electric Field') p.plotMeshX = [1000*p.stkP.mesh.xStart, 1000*p.stkP.mesh.xFin, p.stkP.mesh.nx] p.plotMeshY = [1000*p.stkP.mesh.yStart, 1000*p.stkP.mesh.yFin, p.stkP.mesh.ny] p.plots.append([ uti_plot.uti_plot2d, p.stkP.arS, p.plotMeshX, p.plotMeshY, [ 'Horizontal Position [mm]', 'Vertical Position [mm]', 'Power Density', ], ]) else: raise AssertionError('{}: invalid simulation_kind'.format(params.simulation_kind)) return p
def simulate(params, msg_callback): """Run a single-particle (thin) simulation and return results Args: params (OrderedMapping): map of parameters msg_callback (function): Called at various stages to log output Returns: OrderedMapping: results and params (see code for format) """ p = pkcollections.OrderedMapping() for k in 'polarization', 'intensity', 'simulation_kind', 'wavefront': v = params[k] p[k] = v.value if hasattr(v, 'value') else v pkcollections.mapping_merge( p, srw_params.to_undulator_single_particle(params.undulator)) p.arPrecPar = srw_params.to_precision_single_particle(params.precision) p.wfrE = srw_params.to_wavefront_single_particle(p.wavefront) p.wfrE.partBeam = srw_params.to_beam(params.beam) p.wfrXY = srw_params.to_wavefront_single_particle(p.wavefront) p.wfrXY.partBeam = srw_params.to_beam(params.beam) p.beam = srw_params.to_beam(params.beam) p.plots = [] skv = p.simulation_kind if params.simulation_kind == 'E': msg_callback('Performing Electric Field (spectrum vs photon energy) calculation') srwlib.srwl.CalcElecFieldSR(p.wfrE, 0, p.magFldCnt, p.arPrecPar) msg_callback('Extracting Intensity from calculated Electric Field') p.arI1 = pkarray.new_float([0]*p.wfrE.mesh.ne) srwlib.srwl.CalcIntFromElecField( p.arI1, p.wfrE, p.polarization, p.intensity, skv, p.wfrE.mesh.eStart, 0, 0) p.plots.append([ uti_plot.uti_plot1d, p.arI1, [p.wfrE.mesh.eStart, p.wfrE.mesh.eFin, p.wfrE.mesh.ne], ['Photon energy, eV','Spectral intensity, ph/s/0.1%BW','Intensity vs photon energy'], ]) elif params.simulation_kind == 'X': msg_callback('Performing Electric Field (intensity vs x-coordinate) calculation') srwlib.srwl.CalcElecFieldSR(p.wfrXY, 0, p.magFldCnt, p.arPrecPar) msg_callback('Extracting Intensity from calculated Electric Field') p.arI1 = pkarray.new_float([0]*p.wfrXY.mesh.nx) srwlib.srwl.CalcIntFromElecField(p.arI1, p.wfrXY, p.polarization, p.intensity, skv, 0, p.wfrXY.mesh.xStart, 0) p.plots.append([ uti_plot.uti_plot1d, p.arI1, [p.wfrXY.mesh.xStart, p.wfrXY.mesh.xFin, p.wfrXY.mesh.nx], ['Horizontal Position [m]','Spectral intensity, ph/s/0.1%BW','Intensity vs x-coordinate'], ]) elif params.simulation_kind == 'Y': msg_callback('Performing Electric Field (intensity vs y-coordinate) calculation') srwlib.srwl.CalcElecFieldSR(p.wfrXY, 0, p.magFldCnt, p.arPrecPar) msg_callback('Extracting Intensity from calculated Electric Field') p.arI1 = pkarray.new_float([0]*p.wfrXY.mesh.ny) srwlib.srwl.CalcIntFromElecField(p.arI1, p.wfrXY, p.polarization, p.intensity, skv, 0, p.wfrXY.mesh.yStart, 0) p.plots.append([ uti_plot.uti_plot1d, p.arI1, [p.wfrXY.mesh.yStart, p.wfrXY.mesh.yFin, p.wfrXY.mesh.ny], ['Vertical Position [m]','Spectral intensity, ph/s/0.1%BW','Intensity vs y-coordinate'], ]) elif params.simulation_kind == 'X_AND_Y': msg_callback('Performing Electric Field (intensity vs x- and y-coordinate) calculation') srwlib.srwl.CalcElecFieldSR(p.wfrXY, 0, p.magFldCnt, p.arPrecPar) msg_callback('Extracting Intensity from calculated Electric Field') p.arI1 = pkarray.new_float([0]*p.wfrXY.mesh.nx*p.wfrXY.mesh.ny) srwlib.srwl.CalcIntFromElecField(p.arI1, p.wfrXY, p.polarization, p.intensity, skv, p.wfrXY.mesh.eStart, p.wfrXY.mesh.xStart, p.wfrXY.mesh.yStart) p.plots.append([ uti_plot.uti_plot2d, p.arI1, [1*p.wfrXY.mesh.xStart, 1*p.wfrXY.mesh.xFin, p.wfrXY.mesh.nx], [1*p.wfrXY.mesh.yStart, 1*p.wfrXY.mesh.yFin, p.wfrXY.mesh.ny], ['Horizontal Position [m]', 'Vertical Position [m]', 'Intensity at ' + str(wfrXY.mesh.eStart) + ' eV'], ]), elif params.simulation_kind == 'E_AND_X': msg_callback('Performing Electric Field (intensity vs energy- and x-coordinate) calculation') srwlib.srwl.CalcElecFieldSR(p.wfrXY, 0, p.magFldCnt, p.arPrecPar) msg_callback('* Extracting Intensity from calculated Electric Field') p.arI1 = pkarray.new_float([0]*p.wfrXY.mesh.ne*p.wfrXY.mesh.nx) srwlib.srwl.CalcIntFromElecField(p.arI1, p.wfrXY, p.polarization, p.intensity, skv, p.wfrXY.mesh.eStart, p.wfrXY.mesh.xStart, p.wfrXY.mesh.yStart) p.plots.append([ uti_plot.uti_plot1d, p.arI1, [1*p.wfrXY.mesh.eStart, 1*p.wfrXY.mesh.eFin, p.wfrXY.mesh.ne], [1*p.wfrXY.mesh.xStart, 1*p.wfrXY.mesh.xFin, p.wfrXY.mesh.nx], ['Energy [eV]', 'Horizontal Position [m]', 'Intensity integrated from ' + str(p.wfrXY.mesh.yStart) + ' to ' + str(p.wfrXY.mesh.yFin) + ' ,m in y-coordinate'], ]) elif params.simulation_kind == 'E_AND_Y': msg_callback('Performing Electric Field (intensity vs energy- and y-coordinate) calculation') srwlib.srwl.CalcElecFieldSR(p.wfrXY, 0, p.magFldCnt, p.arPrecPar) msg_callback('Extracting Intensity from calculated Electric Field') p.arI1 = pkarray.new_float([0]*p.wfrXY.mesh.ne*p.wfrXY.mesh.ny) srwlib.srwl.CalcIntFromElecField(p.arI1, p.wfrXY, p.polarization, p.intensity, skv, p.wfrXY.mesh.eStart, p.wfrXY.mesh.xStart, p.wfrXY.mesh.yStart) p.plots.append([ uti_plot.uti_plot1d, p.arI1, [1*p.wfrXY.mesh.eStart, 1*p.wfrXY.mesh.eFin, p.wfrXY.mesh.ne], [1*p.wfrXY.mesh.yStart, 1*p.wfrXY.mesh.yFin, p.wfrXY.mesh.ny], ['Energy [eV]', 'Vertical Position [m]', 'Intensity integrated from ' + str(p.wfrXY.mesh.xStart) + ' to ' + str(p.wfrXY.mesh.xFin)+ ' ,m in x-coordinate'], ]) else: raise AssertionError('{}: invalid p.simulation_kind'.format(params.simulation_kind)) return p