def test_model_defaults(): from sirepo.template import template_common from sirepo import simulation_db res = template_common.model_defaults('trajectoryReport', simulation_db.get_schema('srw')) assert res == { 'notes': '', 'plotAxisY2': 'None', 'timeMomentEstimation': 'auto', 'magneticField': '2', 'initialTimeMoment': 0.0, 'numberOfPoints': 10000, 'plotAxisY': 'X', 'plotAxisX': 'Z', 'finalTimeMoment': 0.0, } model = { 'numberOfPoints': 10, 'finalTimeMoment': 1.0, } template_common.update_model_defaults(model, 'trajectoryReport', simulation_db.get_schema('srw')) assert model['numberOfPoints'] == 10 assert model['finalTimeMoment'] == 1.0 assert model['plotAxisX'] == 'Z'
def _import_elements(lattice, data): name_to_id = {} beamline = data['models']['beamlines'][0] current_id = beamline['id'] for el in lattice.get_elements(): attrs = {} for attr in el.get_double_attributes(): attrs[attr] = el.get_double_attribute(attr) for attr in el.get_string_attributes(): attrs[attr] = el.get_string_attribute(attr) for attr in el.get_vector_attributes(): attrs[attr] = '{' + ','.join( map(str, el.get_vector_attribute(attr))) + '}' model_name = el.get_type().upper() if model_name not in _SCHEMA.model: raise IOError('Unsupported element type: {}'.format(model_name)) m = template_common.model_defaults(model_name, _SCHEMA) if 'l' in attrs: attrs['l'] = float(str(attrs['l'])) if model_name == 'DRIFT' and re.search(r'^auto_drift', el.get_name()): drift_name = _drift_name(attrs['l']) m['name'] = drift_name else: m['name'] = el.get_name().upper() if m['name'] in name_to_id: beamline['items'].append(name_to_id[m['name']]) continue m['type'] = model_name current_id += 1 beamline['items'].append(current_id) m['_id'] = current_id name_to_id[m['name']] = m['_id'] info = _SCHEMA['model'][model_name] for f in info.keys(): if f in attrs: m[f] = attrs[f] for attr in attrs: if attr not in m: if attr not in _IGNORE_ATTRIBUTES: pkdlog('unknown attr: {}: {}'.format(model_name, attr)) data['models']['elements'].append(m) data['models']['elements'] = sorted(data['models']['elements'], key=lambda el: (el['type'], el['name'].lower()))
def _import_elements(lattice, data): name_to_id = {} beamline = data['models']['beamlines'][0] current_id = beamline['id'] for el in lattice.get_elements(): attrs = {} for attr in el.get_double_attributes(): attrs[attr] = el.get_double_attribute(attr) for attr in el.get_string_attributes(): attrs[attr] = el.get_string_attribute(attr) for attr in el.get_vector_attributes(): attrs[attr] = '{' + ','.join(map(str, el.get_vector_attribute(attr))) + '}' model_name = el.get_type().upper() if model_name not in _SCHEMA.model: raise IOError('Unsupported element type: {}'.format(model_name)) m = template_common.model_defaults(model_name, _SCHEMA) if 'l' in attrs: attrs['l'] = float(str(attrs['l'])) if model_name == 'DRIFT' and re.search(r'^auto_drift', el.get_name()): drift_name = _drift_name(attrs['l']) m['name'] = drift_name else: m['name'] = el.get_name().upper() if m['name'] in name_to_id: beamline['items'].append(name_to_id[m['name']]) continue m['type'] = model_name current_id += 1 beamline['items'].append(current_id) m['_id'] = current_id name_to_id[m['name']] = m['_id'] info = _SCHEMA['model'][model_name] for f in info.keys(): if f in attrs: m[f] = attrs[f] for attr in attrs: if attr not in m: if attr not in _IGNORE_ATTRIBUTES: pkdlog('unknown attr: {}: {}'.format(model_name, attr)) data['models']['elements'].append(m)