forked from pylhc/Beta-Beat.src
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_fullresponse_pandas.py
142 lines (125 loc) · 4.86 KB
/
generate_fullresponse_pandas.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
"""
Provides a response generation wrapper.
The response matrices can be either created by response_madx or analytically via TwissResponse.
:author: Joschua Dillys
"""
import cPickle as pickle
import os
from correction.fullresponse import response_madx
from correction.fullresponse import response_twiss
from global_correct_iterative import DEFAULT_ARGS
from model import manager
from utils import logging_tools
from utils.entrypoint import EntryPointParameters, entrypoint
LOG = logging_tools.get_logger(__name__)
DEFAULT_PATTERNS = {
"job_content": "%JOB_CONTENT%", # used in lhc_model_creator, sequence_evaluation
"twiss_columns": "%TWISS_COLUMNS%", # used in lhc_model_creator, sequence_evaluation
"element_pattern": "%ELEMENT_PATTERN%", # used in lhc_model_creator, sequence_evaluation
}
def get_params():
params = EntryPointParameters()
params.add_parameter(
flags="--creator",
help="Create either with madx or analytically from twiss file.",
name="creator",
type=str,
choices=("madx", "twiss"),
default="madx",
)
params.add_parameter(
flags="--variables",
help="List of the variables classes to use.",
name="variable_categories",
default=DEFAULT_ARGS["variables"],
nargs="+",
)
params.add_parameter(
flags=["-m", "--model_dir"],
help="Path to the model directory.",
name="model_dir",
required=True,
type=str
)
params.add_parameter(
flags="--optics_file",
help=("Path to the optics file to use. If not present will default to "
"model_path/modifiers.madx, if such a file exists."),
name="optics_file",
)
params.add_parameter(
flags=["-o", "--outfile"],
help="Name of fullresponse file.",
name="outfile_path",
required=True,
type=str
)
params.add_parameter(
flags=["-k", "--deltak"],
help="Delta K1L to be applied to quads for sensitivity matrix (madx-only).",
default=0.00002,
name="delta_k",
type=float
)
params.add_parameter(
flags="--optics_params",
help="List of parameters to correct upon (e.g. BBX BBY; twiss-only).",
name="optics_params",
type=str,
nargs="+",
)
params.add_parameter(
flags="--debug",
help="Print debug information.",
name="debug",
action="store_true",
)
return params
@entrypoint(get_params())
def create_response(opt, other_opt):
""" Entry point for creating pandas-based response matrices.
The response matrices can be either created by response_madx or TwissResponse.
Keyword Args:
Required
model_dir (str): Path to the model directory.
**Flags**: ['-m', '--model_dir']
outfile_path (str): Name of fullresponse file.
**Flags**: ['-o', '--outfile']
Optional
creator (str): Create either with madx or analytically from twiss file.
**Flags**: --creator
**Choices**: ('madx', 'twiss')
**Default**: ``madx``
debug: Print debug information.
**Flags**: --debug
**Action**: ``store_true``
delta_k (float): Delta K1L to be applied to quads for sensitivity matrix (madx-only).
**Flags**: ['-k', '--deltak']
**Default**: ``2e-05``
optics_params (str): List of parameters to correct upon (e.g. BBX BBY; twiss-only).
**Flags**: --optics_params
variable_categories: List of the variables classes to use.
**Flags**: --variables
**Default**: ``['MQM', 'MQT', 'MQTL', 'MQY']``
"""
with logging_tools.DebugMode(active=opt.debug,
log_file=os.path.join(opt.model_dir, "generate_fullresponse.log")):
LOG.info("Creating response.")
accel_cls = manager.get_accel_class(other_opt)
accel_inst = accel_cls(model_dir=opt.model_dir)
if opt.optics_file is not None:
accel_inst.optics_file = opt.optics_file
if opt.creator == "madx":
fullresponse = response_madx.generate_fullresponse(
accel_inst, opt.variable_categories, delta_k=opt.delta_k
)
elif opt.creator == "twiss":
fullresponse = response_twiss.create_response(
accel_inst, opt.variable_categories, opt.optics_params
)
LOG.debug("Saving Response into file '{:s}'".format(opt.outfile_path))
with open(opt.outfile_path, 'wb') as dump_file:
pickle.Pickler(dump_file, -1).dump(fullresponse)
# Script Mode ################################################################
if __name__ == "__main__":
create_response()