Skip to content

Commit 0706476

Browse files
implement new specification (#7)
1 parent 666db67 commit 0706476

File tree

6 files changed

+249
-51
lines changed

6 files changed

+249
-51
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ dist/
1515
sdist/
1616
wheels/
1717
*.egg
18-
*.egg-info/
18+
*.egg-info/

README.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ in form of a [JSON Schema](https://json-schema.org).
1414
A LatticeJSON file for a FODO lattice:
1515
```json
1616
{
17-
"name": "FODO lattice",
17+
"name": "FODO_RING",
1818
"description": "This is the simplest possible strong focusing lattice.",
1919
"elements": {
2020
"D1": {"type": "Drift", "length": 0.55},
21-
"Q1": {"type": "Quad", "length": 0.2, "k1": 1.2},
22-
"Q2": {"type": "Quad", "length": 0.4, "k1": -1.2},
23-
"B1": {"type": "Bend", "length": 1.5, "angle": 0.392701, "e1": 0.1963505, "e2": 0.1963505}
21+
"Q1": {"type": "Quadrupole", "length": 0.2, "k1": 1.2},
22+
"Q2": {"type": "Quadrupole", "length": 0.4, "k1": -1.2},
23+
"B1": {"type": "Dipole", "length": 1.5, "angle": 0.392701, "e1": 0.1963505, "e2": 0.1963505}
2424
},
25-
"cells": {
26-
"fodo": ["Q1", "D1", "B1", "D1", "Q2", "D1", "B1", "D1", "Q1"]
25+
"sub_lattices": {
26+
"FODO": ["Q1", "D1", "B1", "D1", "Q2", "D1", "B1", "D1", "Q1"]
2727
},
2828

29-
"main_cell": ["fodo", "fodo", "fodo", "fodo", "fodo", "fodo", "fodo", "fodo"]
29+
"lattice": ["FODO", "FODO", "FODO", "FODO", "FODO", "FODO", "FODO", "FODO"]
3030
}
3131
```
3232

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

48+
4849
Validate a LatticeJSON file:
4950
```sh
5051
latticejson validate /path/to/lattice
@@ -55,6 +56,19 @@ Convert a LatticeJSON file into the elegant lattice format:
5556
latticejson convert json elegant /path/to/lattice
5657
```
5758

59+
To activate Bash completion add
60+
61+
```
62+
eval "$(_LATTICEJSON_COMPLETE=source latticejson)"
63+
```
64+
65+
to your `.bashrc`. Or, create an activation script with:
66+
67+
68+
```
69+
_LATTICEJSON_COMPLETE=source latticejson > latticejson-complete.sh
70+
```
71+
5872
## License
5973
[GNU General Public License v3.0](https://github.com/andreasfelix/latticejson/blob/master/LICENSE)
6074

latticejson/convert.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ def convert_file(file_path, input_format, output_format):
1717

1818
JSON_TO_ELEGANT = {
1919
'Drift': 'DRIF',
20-
'Bend': 'CSBEND',
21-
'Quad': 'KQUAD',
22-
'Sext': 'KSEXT',
23-
'Cell': 'LINE',
24-
'main_cell': 'RING',
20+
'Dipole': 'CSBEND',
21+
'Quadrupole': 'KQUAD',
22+
'Sextupole': 'KSEXT',
23+
'Lattice': 'LINE',
2524
'length': 'L',
2625
'angle': 'ANGLE',
2726
'e1': 'e1',
@@ -37,34 +36,36 @@ def convert_file(file_path, input_format, output_format):
3736

3837

3938
def convert_json_to_elegant(lattice_dict):
40-
elements_dict = lattice_dict['elements']
41-
cells_dict = lattice_dict['cells']
39+
elements = lattice_dict['elements']
40+
sub_lattices = lattice_dict['sub_lattices']
4241

4342
elements_string = []
44-
for name, element in elements_dict.items():
43+
for name, element in elements.items():
4544
attributes = ', '.join(f'{JSON_TO_ELEGANT[key]}={value}' for key, value in element.items() if key != 'type')
4645
type_ = JSON_TO_ELEGANT[element['type']]
4746
elements_string.append(ELEGANT_ELEMENT_TEMPLATE(name=name, type=type_, attributes=attributes))
4847

49-
ordered_cells = order_cells(cells_dict)
50-
cells_string = [ELEGANT_CELL_TEMPLATE(name=name, objects=', '.join(cells_dict[name])) for name in ordered_cells]
51-
cells_string.append(ELEGANT_CELL_TEMPLATE(name=lattice_dict['name'], objects=', '.join(lattice_dict['main_cell'])))
52-
return '\n'.join(elements_string + cells_string)
48+
ordered_lattices = order_lattices(sub_lattices)
49+
lattices_string = [
50+
ELEGANT_CELL_TEMPLATE(name=name, objects=', '.join(sub_lattices[name])) for name in ordered_lattices
51+
]
52+
lattices_string.append(ELEGANT_CELL_TEMPLATE(name=lattice_dict['name'], objects=', '.join(lattice_dict['lattice'])))
53+
return '\n'.join(elements_string + lattices_string)
5354

5455

55-
def order_cells(cells_dict: Dict[str, List[str]]):
56+
def order_lattices(cells_dict: Dict[str, List[str]]):
5657
cells_dict_copy = cells_dict.copy()
5758
ordered_cells = []
5859

59-
def _order_cells(name, cell: List[str]):
60-
for cell_name in cell:
61-
if cell_name in cells_dict_copy:
62-
_order_cells(cell_name, cells_dict_copy[cell_name])
60+
def _order_lattices(name, cell: List[str]):
61+
for lattice_name in cell:
62+
if lattice_name in cells_dict_copy:
63+
_order_lattices(lattice_name, cells_dict_copy[lattice_name])
6364

6465
ordered_cells.append(name)
6566
cells_dict_copy.pop(name)
6667

6768
for name, cell in cells_dict.items():
68-
_order_cells(name, cell)
69+
_order_lattices(name, cell)
6970

7071
return ordered_cells

latticejson/schema.json

Lines changed: 199 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"$id": "",
2+
"$id": "https://github.com/andreasfelix/latticejson/blob/master/latticejson/schema.json",
33
"$schema": "http://json-schema.org/draft-07/schema#",
44
"title": "JSON lattice file format",
55
"description": "Defines the magnetic lattice format",
66
"additionalProperties": false,
77
"required": [
88
"name",
9-
"main_cell",
9+
"lattice",
1010
"elements"
1111
],
1212
"type": "object",
@@ -19,43 +19,54 @@
1919
"type": "string",
2020
"description": "A brief description of the lattice"
2121
},
22-
"main_cell": {
22+
"lattice": {
2323
"type": "array",
2424
"items": {
2525
"type": "string"
2626
}
2727
},
28-
"cells": {
28+
"sub_lattices": {
2929
"type": "object",
3030
"patternProperties": {
3131
"^.*$": {
32-
"$ref": "#/definitions/cell"
32+
"$ref": "#/definitions/Lattice"
3333
}
3434
},
3535
"additionalProperties": false
3636
},
3737
"elements": {
3838
"type": "object",
39-
"items": {
40-
"anyOf": [
41-
{
42-
"$ref": "#/definitions/cell"
43-
},
44-
{
45-
"$ref": "#/definitions/element"
46-
}
47-
]
39+
"patternProperties": {
40+
"^.*$": {
41+
"oneOf": [
42+
{
43+
"$ref": "#/definitions/Drift"
44+
},
45+
{
46+
"$ref": "#/definitions/Dipole"
47+
},
48+
{
49+
"$ref": "#/definitions/Quadrupole"
50+
},
51+
{
52+
"$ref": "#/definitions/Sextupole"
53+
},
54+
{
55+
"$ref": "#/definitions/Octupole"
56+
}
57+
]
58+
}
4859
}
4960
}
5061
},
5162
"definitions": {
52-
"cell": {
63+
"Lattice": {
5364
"type": "array",
5465
"items": {
5566
"type": "string"
5667
}
5768
},
58-
"element": {
69+
"Element": {
5970
"type": "object",
6071
"required": [
6172
"type",
@@ -66,10 +77,182 @@
6677
"type": "string",
6778
"description": "Type of the element."
6879
},
80+
"description": {
81+
"type": "string",
82+
"description": "Description of the element."
83+
},
6984
"length": {
7085
"type": "number",
7186
"minimum": 0,
7287
"description": "The length of the element."
88+
},
89+
"dx": {
90+
"type": "number",
91+
"description": "Horizontal misalignment"
92+
},
93+
"dy": {
94+
"type": "number",
95+
"description": "Vertical misalignment"
96+
},
97+
"ds": {
98+
"type": "number",
99+
"description": "Longitudinal misalignment"
100+
},
101+
"tilt": {
102+
"type": "number",
103+
"description": "Rotation about the longitudinal axis"
104+
}
105+
}
106+
},
107+
"Drift": {
108+
"allOf": [
109+
{
110+
"$ref": "#/definitions/Element"
111+
}
112+
],
113+
"additionalProperties": false,
114+
"properties": {
115+
"type": {
116+
"const": "Drift"
117+
},
118+
"description": {},
119+
"length": {},
120+
"dx": {},
121+
"dy": {},
122+
"ds": {},
123+
"tilt": {}
124+
}
125+
},
126+
"Dipole": {
127+
"allOf": [
128+
{
129+
"$ref": "#/definitions/Element"
130+
}
131+
],
132+
"additionalProperties": false,
133+
"properties": {
134+
"type": {
135+
"const": "Dipole"
136+
},
137+
"description": {},
138+
"length": {},
139+
"dx": {},
140+
"dy": {},
141+
"ds": {},
142+
"tilt": {},
143+
"angle": {
144+
"type": "number",
145+
"description": "Deflection angle"
146+
},
147+
"radius": {
148+
"type": "number",
149+
"description": "Radius of curvature"
150+
},
151+
"ps_value": {
152+
"type": "number",
153+
"description": "Power supply value."
154+
},
155+
"e1": {
156+
"type": "number"
157+
},
158+
"e2": {
159+
"type": "number"
160+
},
161+
"h1": {
162+
"type": "number"
163+
},
164+
"h2": {
165+
"type": "number"
166+
},
167+
"conversion_factor_ps_value": {
168+
"type": "number"
169+
}
170+
}
171+
},
172+
"Quadrupole": {
173+
"allOf": [
174+
{
175+
"$ref": "#/definitions/Element"
176+
}
177+
],
178+
"properties": {
179+
"type": {
180+
"const": "Quadrupole"
181+
},
182+
"additionalProperties": false,
183+
"description": {},
184+
"length": {},
185+
"dx": {},
186+
"dy": {},
187+
"ds": {},
188+
"tilt": {},
189+
"k1": {
190+
"type": "number",
191+
"description": "Geometric quadrupole strength"
192+
},
193+
"ps_value": {
194+
"type": "number"
195+
},
196+
"conversion_factor_ps_value": {
197+
"type": "number"
198+
}
199+
}
200+
},
201+
"Sextupole": {
202+
"allOf": [
203+
{
204+
"$ref": "#/definitions/Element"
205+
}
206+
],
207+
"properties": {
208+
"type": {
209+
"const": "Sextupole"
210+
},
211+
"additionalProperties": false,
212+
"description": {},
213+
"length": {},
214+
"dx": {},
215+
"dy": {},
216+
"ds": {},
217+
"tilt": {},
218+
"k2": {
219+
"type": "number",
220+
"description": "Geometric sextupole strength"
221+
},
222+
"ps_value": {
223+
"type": "number"
224+
},
225+
"conversion_factor_ps_value": {
226+
"type": "number"
227+
}
228+
}
229+
},
230+
"Octupole": {
231+
"allOf": [
232+
{
233+
"$ref": "#/definitions/Element"
234+
}
235+
],
236+
"properties": {
237+
"type": {
238+
"const": "Octupole"
239+
},
240+
"additionalProperties": false,
241+
"description": {},
242+
"length": {},
243+
"dx": {},
244+
"dy": {},
245+
"ds": {},
246+
"tilt": {},
247+
"k3": {
248+
"type": "number",
249+
"description": "Geometric octupole strength"
250+
},
251+
"ps_value": {
252+
"type": "number"
253+
},
254+
"conversion_factor_ps_value": {
255+
"type": "number"
73256
}
74257
}
75258
}

0 commit comments

Comments
 (0)