Skip to content

Commit

Permalink
implement new specification (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-andreas authored Nov 26, 2019
1 parent 666db67 commit 0706476
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ dist/
sdist/
wheels/
*.egg
*.egg-info/
*.egg-info/
28 changes: 21 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ in form of a [JSON Schema](https://json-schema.org).
A LatticeJSON file for a FODO lattice:
```json
{
"name": "FODO lattice",
"name": "FODO_RING",
"description": "This is the simplest possible strong focusing lattice.",
"elements": {
"D1": {"type": "Drift", "length": 0.55},
"Q1": {"type": "Quad", "length": 0.2, "k1": 1.2},
"Q2": {"type": "Quad", "length": 0.4, "k1": -1.2},
"B1": {"type": "Bend", "length": 1.5, "angle": 0.392701, "e1": 0.1963505, "e2": 0.1963505}
"Q1": {"type": "Quadrupole", "length": 0.2, "k1": 1.2},
"Q2": {"type": "Quadrupole", "length": 0.4, "k1": -1.2},
"B1": {"type": "Dipole", "length": 1.5, "angle": 0.392701, "e1": 0.1963505, "e2": 0.1963505}
},
"cells": {
"fodo": ["Q1", "D1", "B1", "D1", "Q2", "D1", "B1", "D1", "Q1"]
"sub_lattices": {
"FODO": ["Q1", "D1", "B1", "D1", "Q2", "D1", "B1", "D1", "Q1"]
},

"main_cell": ["fodo", "fodo", "fodo", "fodo", "fodo", "fodo", "fodo", "fodo"]
"lattice": ["FODO", "FODO", "FODO", "FODO", "FODO", "FODO", "FODO", "FODO"]
}
```

Expand All @@ -45,6 +45,7 @@ You can install and update it using pip or pipenv:
pip install -U latticejson
```


Validate a LatticeJSON file:
```sh
latticejson validate /path/to/lattice
Expand All @@ -55,6 +56,19 @@ Convert a LatticeJSON file into the elegant lattice format:
latticejson convert json elegant /path/to/lattice
```

To activate Bash completion add

```
eval "$(_LATTICEJSON_COMPLETE=source latticejson)"
```

to your `.bashrc`. Or, create an activation script with:


```
_LATTICEJSON_COMPLETE=source latticejson > latticejson-complete.sh
```

## License
[GNU General Public License v3.0](https://github.com/andreasfelix/latticejson/blob/master/LICENSE)

Expand Down
37 changes: 19 additions & 18 deletions latticejson/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ def convert_file(file_path, input_format, output_format):

JSON_TO_ELEGANT = {
'Drift': 'DRIF',
'Bend': 'CSBEND',
'Quad': 'KQUAD',
'Sext': 'KSEXT',
'Cell': 'LINE',
'main_cell': 'RING',
'Dipole': 'CSBEND',
'Quadrupole': 'KQUAD',
'Sextupole': 'KSEXT',
'Lattice': 'LINE',
'length': 'L',
'angle': 'ANGLE',
'e1': 'e1',
Expand All @@ -37,34 +36,36 @@ def convert_file(file_path, input_format, output_format):


def convert_json_to_elegant(lattice_dict):
elements_dict = lattice_dict['elements']
cells_dict = lattice_dict['cells']
elements = lattice_dict['elements']
sub_lattices = lattice_dict['sub_lattices']

elements_string = []
for name, element in elements_dict.items():
for name, element in elements.items():
attributes = ', '.join(f'{JSON_TO_ELEGANT[key]}={value}' for key, value in element.items() if key != 'type')
type_ = JSON_TO_ELEGANT[element['type']]
elements_string.append(ELEGANT_ELEMENT_TEMPLATE(name=name, type=type_, attributes=attributes))

ordered_cells = order_cells(cells_dict)
cells_string = [ELEGANT_CELL_TEMPLATE(name=name, objects=', '.join(cells_dict[name])) for name in ordered_cells]
cells_string.append(ELEGANT_CELL_TEMPLATE(name=lattice_dict['name'], objects=', '.join(lattice_dict['main_cell'])))
return '\n'.join(elements_string + cells_string)
ordered_lattices = order_lattices(sub_lattices)
lattices_string = [
ELEGANT_CELL_TEMPLATE(name=name, objects=', '.join(sub_lattices[name])) for name in ordered_lattices
]
lattices_string.append(ELEGANT_CELL_TEMPLATE(name=lattice_dict['name'], objects=', '.join(lattice_dict['lattice'])))
return '\n'.join(elements_string + lattices_string)


def order_cells(cells_dict: Dict[str, List[str]]):
def order_lattices(cells_dict: Dict[str, List[str]]):
cells_dict_copy = cells_dict.copy()
ordered_cells = []

def _order_cells(name, cell: List[str]):
for cell_name in cell:
if cell_name in cells_dict_copy:
_order_cells(cell_name, cells_dict_copy[cell_name])
def _order_lattices(name, cell: List[str]):
for lattice_name in cell:
if lattice_name in cells_dict_copy:
_order_lattices(lattice_name, cells_dict_copy[lattice_name])

ordered_cells.append(name)
cells_dict_copy.pop(name)

for name, cell in cells_dict.items():
_order_cells(name, cell)
_order_lattices(name, cell)

return ordered_cells
215 changes: 199 additions & 16 deletions latticejson/schema.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"$id": "",
"$id": "https://github.com/andreasfelix/latticejson/blob/master/latticejson/schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "JSON lattice file format",
"description": "Defines the magnetic lattice format",
"additionalProperties": false,
"required": [
"name",
"main_cell",
"lattice",
"elements"
],
"type": "object",
Expand All @@ -19,43 +19,54 @@
"type": "string",
"description": "A brief description of the lattice"
},
"main_cell": {
"lattice": {
"type": "array",
"items": {
"type": "string"
}
},
"cells": {
"sub_lattices": {
"type": "object",
"patternProperties": {
"^.*$": {
"$ref": "#/definitions/cell"
"$ref": "#/definitions/Lattice"
}
},
"additionalProperties": false
},
"elements": {
"type": "object",
"items": {
"anyOf": [
{
"$ref": "#/definitions/cell"
},
{
"$ref": "#/definitions/element"
}
]
"patternProperties": {
"^.*$": {
"oneOf": [
{
"$ref": "#/definitions/Drift"
},
{
"$ref": "#/definitions/Dipole"
},
{
"$ref": "#/definitions/Quadrupole"
},
{
"$ref": "#/definitions/Sextupole"
},
{
"$ref": "#/definitions/Octupole"
}
]
}
}
}
},
"definitions": {
"cell": {
"Lattice": {
"type": "array",
"items": {
"type": "string"
}
},
"element": {
"Element": {
"type": "object",
"required": [
"type",
Expand All @@ -66,10 +77,182 @@
"type": "string",
"description": "Type of the element."
},
"description": {
"type": "string",
"description": "Description of the element."
},
"length": {
"type": "number",
"minimum": 0,
"description": "The length of the element."
},
"dx": {
"type": "number",
"description": "Horizontal misalignment"
},
"dy": {
"type": "number",
"description": "Vertical misalignment"
},
"ds": {
"type": "number",
"description": "Longitudinal misalignment"
},
"tilt": {
"type": "number",
"description": "Rotation about the longitudinal axis"
}
}
},
"Drift": {
"allOf": [
{
"$ref": "#/definitions/Element"
}
],
"additionalProperties": false,
"properties": {
"type": {
"const": "Drift"
},
"description": {},
"length": {},
"dx": {},
"dy": {},
"ds": {},
"tilt": {}
}
},
"Dipole": {
"allOf": [
{
"$ref": "#/definitions/Element"
}
],
"additionalProperties": false,
"properties": {
"type": {
"const": "Dipole"
},
"description": {},
"length": {},
"dx": {},
"dy": {},
"ds": {},
"tilt": {},
"angle": {
"type": "number",
"description": "Deflection angle"
},
"radius": {
"type": "number",
"description": "Radius of curvature"
},
"ps_value": {
"type": "number",
"description": "Power supply value."
},
"e1": {
"type": "number"
},
"e2": {
"type": "number"
},
"h1": {
"type": "number"
},
"h2": {
"type": "number"
},
"conversion_factor_ps_value": {
"type": "number"
}
}
},
"Quadrupole": {
"allOf": [
{
"$ref": "#/definitions/Element"
}
],
"properties": {
"type": {
"const": "Quadrupole"
},
"additionalProperties": false,
"description": {},
"length": {},
"dx": {},
"dy": {},
"ds": {},
"tilt": {},
"k1": {
"type": "number",
"description": "Geometric quadrupole strength"
},
"ps_value": {
"type": "number"
},
"conversion_factor_ps_value": {
"type": "number"
}
}
},
"Sextupole": {
"allOf": [
{
"$ref": "#/definitions/Element"
}
],
"properties": {
"type": {
"const": "Sextupole"
},
"additionalProperties": false,
"description": {},
"length": {},
"dx": {},
"dy": {},
"ds": {},
"tilt": {},
"k2": {
"type": "number",
"description": "Geometric sextupole strength"
},
"ps_value": {
"type": "number"
},
"conversion_factor_ps_value": {
"type": "number"
}
}
},
"Octupole": {
"allOf": [
{
"$ref": "#/definitions/Element"
}
],
"properties": {
"type": {
"const": "Octupole"
},
"additionalProperties": false,
"description": {},
"length": {},
"dx": {},
"dy": {},
"ds": {},
"tilt": {},
"k3": {
"type": "number",
"description": "Geometric octupole strength"
},
"ps_value": {
"type": "number"
},
"conversion_factor_ps_value": {
"type": "number"
}
}
}
Expand Down
Loading

0 comments on commit 0706476

Please sign in to comment.