-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdodo.py
188 lines (154 loc) · 6.56 KB
/
dodo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from operator import itemgetter
from pathlib import Path
import tomlkit
base_dir = Path(__file__).parent
config = tomlkit.loads((base_dir / "config.toml").read_text())
data_dir = base_dir / str(config["DATA_DIR"])
data_dir.mkdir(exist_ok=True)
results_dir = base_dir / str(config["RESULTS_DIR"])
results_dir.mkdir(exist_ok=True)
info_files = list(data_dir.rglob("info.toml"))
lattices_by_namespace = {
path.parent.stem: tomlkit.loads(path.read_text()) for path in info_files
}
lattices_all = []
lattices_by_simulation = {}
for namespace, lattices in lattices_by_namespace.items():
for name, lattice in lattices.items():
lattice["namespace"] = namespace
lattice["name"] = name
lattices_all.append(lattice)
for simulation in lattice["simulations"]:
try:
lattices_by_simulation[simulation].append(lattice)
except KeyError:
lattices_by_simulation[simulation] = [lattice]
def task_convert_lattices():
"Convert lattice files into different formats"
from actions.convert_lattices import __file__ as python_file
from actions.convert_lattices import action
for lattice in lattices_all:
namespace, name = itemgetter("namespace", "name")(lattice)
source_dir = data_dir / namespace
# TODO: is there a better way?
source = next(source_dir.glob(f"{name}.*"))
target_base = results_dir / namespace / name / name
target_base.parent.mkdir(parents=True, exist_ok=True)
targets = [
target_base.with_suffix(".json"),
target_base.with_suffix(".lte"),
target_base.with_suffix(".madx"),
]
yield {
"name": source,
"actions": [(action, [source, targets])],
"targets": targets,
"file_dep": [python_file, source],
"clean": True,
}
def task_index_json():
"Create global index.json from all info.toml files"
import json
def action(path, data):
with path.open("w") as file:
json.dump(data, file)
index_path = results_dir / "index.json"
yield {
"name": "index.json",
"actions": [(action, (index_path, lattices_all))],
"targets": [index_path],
"file_dep": info_files,
}
def task_index_json_per_lattice():
"Similar to task_index.json, but info file per lattice."
import json
def save_data(path, data):
path.write_text(json.dumps(data))
for lattice in lattices_all:
namespace, name = itemgetter("namespace", "name")(lattice)
info_file = data_dir / namespace / "info.toml"
target = results_dir / namespace / name / "index.json"
target.parent.mkdir(parents=True, exist_ok=True)
yield {
"name": f"{namespace}/{name}",
"actions": [(save_data, (target, lattice))],
"targets": [target],
"file_dep": [info_file],
"clean": True,
}
def task_lattice_info():
from actions.lattice_info import __file__ as python_file
from actions.lattice_info import action, targets
for lattice in lattices_all:
namespace, name = itemgetter("namespace", "name")(lattice)
output_dir = results_dir / namespace / name
lattice_path = (results_dir / namespace / name / name).with_suffix(".json")
yield {
"name": f"{namespace}/{name}",
"actions": [(action, (lattice, lattice_path, output_dir))],
"targets": [output_dir / path for path in targets],
"file_dep": [python_file, lattice_path],
"clean": True,
}
def task_apace_summary():
"Generate lattice summaries using apace"
from actions.twiss_apace import __file__ as python_file
from actions.twiss_apace import action, targets
for lattice in lattices_by_simulation["apace"]:
namespace, name = itemgetter("namespace", "name")(lattice)
lattice_path = (results_dir / namespace / name / name).with_suffix(".json")
output_dir = results_dir / namespace / name / "apace"
# TODO: what does clean do??
yield {
"name": f"{namespace}/{name}",
"actions": [(action, (lattice, lattice_path, output_dir))],
"targets": [output_dir / path for path in targets],
"file_dep": [python_file, lattice_path],
"clean": True,
}
def task_elegant_twiss_simulation():
from actions import config_dir
from actions.twiss_elegant import simulation_elegant_dir
for lattice in lattices_by_simulation["elegant"]:
namespace, name, energy = itemgetter("namespace", "name", "energy")(lattice)
run_file = config_dir / "twiss.ele"
lattice_path = (results_dir / namespace / name / name).with_suffix(".lte")
target = (simulation_elegant_dir / namespace / name).with_suffix(".twi")
target.parent.mkdir(parents=True, exist_ok=True)
yield {
"name": f"{namespace}/{name}",
"actions": [
f"elegant {run_file} -macro=energy={energy},lattice={lattice_path},filename={target} > /dev/null"
],
"targets": [target],
"file_dep": [run_file, lattice_path],
"clean": True,
}
def task_elegant_summary():
from actions.twiss_elegant import __file__ as python_file
from actions.twiss_elegant import action, simulation_elegant_dir, targets
for lattice in lattices_by_simulation["elegant"]:
namespace, name = itemgetter("namespace", "name")(lattice)
output_dir = results_dir / namespace / name / "elegant"
twi_data_path = (simulation_elegant_dir / namespace / name).with_suffix(".twi")
yield {
"name": f"{namespace}/{name}",
"actions": [(action, (twi_data_path, output_dir))],
"targets": [output_dir / path for path in targets],
"file_dep": [python_file, twi_data_path],
"clean": True,
}
def task_madx_summary():
from actions.twiss_madx import __file__ as python_file
from actions.twiss_madx import action, targets
for lattice in lattices_by_simulation["madx"]:
namespace, name = itemgetter("namespace", "name")(lattice)
lattice_path = (results_dir / namespace / name / name).with_suffix(".madx")
output_dir = results_dir / namespace / name / "madx"
yield {
"name": f"{namespace}/{name}",
"actions": [(action, (lattice, lattice_path, output_dir))],
"targets": [output_dir / path for path in targets],
"file_dep": [python_file, lattice_path],
"clean": True,
}