def init_from_args(cls, args): parser = cls._get_arg_parser() options, rest_args = parser.parse_known_args(args) instance = cls() instance.nat_tune_x = options.nat_tune_x instance.nat_tune_y = options.nat_tune_y if options.acd and options.adt: raise AcceleratorDefinitionError( "Select only one excitation type.") if options.acd: instance.excitation = LhcExcitationMode.ACD elif options.adt: instance.excitation = LhcExcitationMode.ADT else: instance.excitation = LhcExcitationMode.FREE if options.acd or options.adt: instance.drv_tune_x = options.drv_tune_x instance.drv_tune_y = options.drv_tune_y instance.dpp = options.dpp instance.energy = options.energy instance.optics_file = options.optics instance.fullresponse = options.fullresponse instance.xing = options.xing instance.verify_object() return instance, rest_args
def get_sequence_file(cls): try: return _get_file_for_year(cls.YEAR, "main.seq") except AttributeError: raise AcceleratorDefinitionError( "The accelerator definition is incomplete, mode " + "has to be specified (--lhcmode option missing?).")
def load_main_seq_madx(cls): try: return _get_call_main_for_year(cls.YEAR) except AttributeError: raise AcceleratorDefinitionError( "The accelerator definition is incomplete, mode " + "has to be specified (--lhcmode option missing?).")
def init_from_model_dir(cls, model_dir): # prints only for debugging print("Creating accelerator instance from model dir") instance = cls() instance.modelpath = model_dir if os.path.isfile(model_dir): model_dir = os.path.dirname(model_dir) instance.model_tfs = tfs_pandas.read_tfs(os.path.join(model_dir, "twiss.dat")).set_index("NAME") print("model path =", os.path.join(model_dir, "twiss.dat")) try: model_best_knowledge_path = os.path.join(model_dir, "twiss_best_knowledge.dat") if os.path.isfile(model_best_knowledge_path): instance.model_best_knowledge = tfs_pandas.read_tfs(model_best_knowledge_path).set_index("NAME") except IOError: instance.model_best_knowledge = None elements_path = os.path.join(model_dir, "twiss_elements.dat") if os.path.isfile(elements_path): instance.elements = tfs_pandas.read_tfs(elements_path).set_index("NAME") else: raise AcceleratorDefinitionError("Elements twiss not found") elements_path = os.path.join(model_dir, "twiss_elements_centre.dat") if os.path.isfile(elements_path): instance.elements_centre = tfs_pandas.read_tfs(elements_path).set_index("NAME") else: instance.elements_centre = instance.elements instance.nat_tune_x = float(instance.model_tfs.headers["Q1"]) instance.nat_tune_y = float(instance.model_tfs.headers["Q2"]) return instance
def verify_object(self): # TODO: Maybe more checks? try: self.get_beam() except AttributeError: raise AcceleratorDefinitionError( "The accelerator definition is incomplete, beam " + "has to be specified (--beam option missing?).") if self.optics_file is None: raise AcceleratorDefinitionError( "The accelerator definition is incomplete, optics " "file has not been specified.") if self.excitation is None: raise AcceleratorDefinitionError("Excitation mode not set.") if self.xing is None: raise AcceleratorDefinitionError("Crossing on or off not set.") if (self.excitation == LhcExcitationMode.ACD or self.excitation == LhcExcitationMode.ADT): if self.drv_tune_x is None or self.drv_tune_y is None: raise AcceleratorDefinitionError("Driven tunes not set.")
def verify_object(self): # TODO: Maybe more checks? try: self.get_beam() except AttributeError: raise AcceleratorDefinitionError( "The accelerator definition is incomplete, beam " "has to be specified (--beam option missing?).") if self.model_dir is None: # is the class is used to create full response? if self.optics_file is None: raise AcceleratorDefinitionError( "The accelerator definition is incomplete, optics " "file or model directory has not been specified.") if self.xing is None: raise AcceleratorDefinitionError("Crossing on or off not set.") if self.excitation is None: raise AcceleratorDefinitionError("Excitation mode not set.") if (self.excitation == AccExcitationMode.ACD or self.excitation == AccExcitationMode.ADT): if self.drv_tune_x is None or self.drv_tune_y is None: raise AcceleratorDefinitionError("Driven tunes not set.") if self.optics_file is not None and not os.path.exists( self.optics_file): raise AcceleratorDefinitionError( "Optics file '{:s}' does not exist.".format(self.optics_file))
def get_basic_seq_job(self): """ Return string for madx job of correting model """ with open(self.get_basic_seq_tmpl(), "r") as template: madx_template = template.read() try: replace_dict = { "LIB": self.MACROS_NAME, "MAIN_SEQ": self.load_main_seq_madx(), "OPTICS_PATH": self.optics_file, "CROSSING_ON": "1" if self.xing else "0", "NUM_BEAM": self.get_beam(), "DPP": self.dpp, "QMX": self.nat_tune_x, "QMY": self.nat_tune_y, } except AttributeError: raise AcceleratorDefinitionError( "The accelerator definition is incomplete. " + "Needs to be an accelator instance. Also: --lhcmode or --beam option missing?" ) return madx_template % replace_dict
def verify_object(self): try: self.get_beam() except AttributeError: raise AcceleratorDefinitionError( "The accelerator definition is incomplete, beam " "has to be specified (--beam option missing?).") if self.optics_file is None: raise AcceleratorDefinitionError( "The accelerator definition is incomplete, optics " "file has not been specified.") if self.xing is None: raise AcceleratorDefinitionError("Crossing on or off not set.") if self.label is None: raise AcceleratorDefinitionError("Segment label not set.") if self.start is None: raise AcceleratorDefinitionError("Segment start not set.") if self.end is None: raise AcceleratorDefinitionError("Segment end not set.")
def get_multi_dpp_job(self, dpp_list): """ Return madx job to create twisses (models) with dpps from dpp_list """ with open(self.get_nominal_multidpp_tmpl()) as textfile: madx_template = textfile.read() try: output_path = self.model_dir use_acd = "1" if (self.excitation == AccExcitationMode.ACD) else "0" use_adt = "1" if (self.excitation == AccExcitationMode.ADT) else "0" crossing_on = "1" if self.xing else "0" beam = self.get_beam() replace_dict = { "LIB": self.MACROS_NAME, "MAIN_SEQ": self.load_main_seq_madx(), "OPTICS_PATH": self.optics_file, "NUM_BEAM": beam, "PATH": output_path, "QMX": self.nat_tune_x, "QMY": self.nat_tune_y, "USE_ACD": use_acd, "USE_ADT": use_adt, "CROSSING_ON": crossing_on, "QX": "", "QY": "", "QDX": "", "QDY": "", "DPP": "", "DPP_ELEMS": "", "DPP_AC": "", "DPP_ADT": "", } if (self.excitation in (AccExcitationMode.ACD, AccExcitationMode.ADT)): replace_dict["QX"] = self.nat_tune_x replace_dict["QY"] = self.nat_tune_y replace_dict["QDX"] = self.drv_tune_x replace_dict["QDY"] = self.drv_tune_y except AttributeError: raise AcceleratorDefinitionError( "The accelerator definition is incomplete. " + "Needs to be an accelator instance. Also: --lhcmode or --beam option missing?" ) # add different dpp twiss-command lines twisses_tmpl = "twiss, chrom, sequence=LHCB{beam:d}, deltap={dpp:f}, file='{twiss:s}';\n" for dpp in dpp_list: replace_dict["DPP"] += twisses_tmpl.format( beam=beam, dpp=dpp, twiss=os.path.join(output_path, "twiss_{:f}.dat".format(dpp))) replace_dict["DPP_ELEMS"] += twisses_tmpl.format( beam=beam, dpp=dpp, twiss=os.path.join(output_path, "twiss_{:f}_elements.dat".format(dpp))) replace_dict["DPP_AC"] += twisses_tmpl.format( beam=beam, dpp=dpp, twiss=os.path.join(output_path, "twiss_{:f}_ac.dat".format(dpp))) replace_dict["DPP_ADT"] += twisses_tmpl.format( beam=beam, dpp=dpp, twiss=os.path.join(output_path, "twiss_{:f}_adt.dat".format(dpp))) return madx_template % replace_dict
def init_from_model_dir(self, model_dir): LOGGER.debug("Creating accelerator instance from model dir") self.model_dir = model_dir LOGGER.debug(" model path = " + os.path.join(model_dir, "twiss.dat")) try: self._model = tfs_pandas.read_tfs(os.path.join( model_dir, "twiss.dat"), index="NAME") except IOError: self._model = tfs_pandas.read_tfs(os.path.join( model_dir, "twiss_elements.dat"), index="NAME") bpm_index = [ idx for idx in self._model.index.values if idx.startswith("B") ] self._model = self._model.loc[bpm_index, :] self.nat_tune_x = float(self._model.headers["Q1"]) self.nat_tune_y = float(self._model.headers["Q2"]) # Excitations ##################################### self._model_driven = None self.drv_tune_x = None self.drv_tune_y = None self._excitation = AccExcitationMode.FREE ac_filename = os.path.join(model_dir, "twiss_ac.dat") adt_filename = os.path.join(model_dir, "twiss_adt.dat") if os.path.isfile(ac_filename): self._model_driven = tfs_pandas.read_tfs(ac_filename, index="NAME") self._excitation = AccExcitationMode.ACD if os.path.isfile(adt_filename): if self._excitation == AccExcitationMode.ACD: raise AcceleratorDefinitionError( "ADT as well as ACD models provided." "Please choose only one.") self._model_driven = tfs_pandas.read_tfs(adt_filename, index="NAME") self._excitation = AccExcitationMode.ADT if not self._excitation == AccExcitationMode.FREE: self.drv_tune_x = float(self.get_driven_tfs().headers["Q1"]) self.drv_tune_y = float(self.get_driven_tfs().headers["Q2"]) # Best Knowledge ##################################### self._model_best_knowledge = None best_knowledge_path = os.path.join(model_dir, "twiss_best_knowledge.dat") if os.path.isfile(best_knowledge_path): self._model_best_knowledge = tfs_pandas.read_tfs( best_knowledge_path, index="NAME") # Elements ##################################### elements_path = os.path.join(model_dir, "twiss_elements.dat") if os.path.isfile(elements_path): self._elements = tfs_pandas.read_tfs(elements_path, index="NAME") else: raise AcceleratorDefinitionError("Elements twiss not found") center_path = os.path.join(model_dir, "twiss_elements_centre.dat") if os.path.isfile(center_path): self._elements_centre = tfs_pandas.read_tfs(center_path, index="NAME") else: self._elements_centre = self._elements # Optics File ######################################### self.optics_file = None opticsfilepath = os.path.join(self.model_dir, "modifiers.madx") if os.path.exists(opticsfilepath): self.optics_file = opticsfilepath # Error Def ##################################### self._errordefspath = None errordefspath = os.path.join(self.model_dir, "error_deff.txt") if os.path.exists(errordefspath): self._errordefspath = errordefspath else: # until we have a proper file name convention errordefspath = os.path.join(self.model_dir, "error_deffs.txt") if os.path.exists(errordefspath): self._errordefspath = errordefspath
def __init__(self, *args, **kwargs): # for reasons of import-order and class creation, decoration was not possible parser = EntryPoint(self.get_instance_parameters(), strict=True) opt = parser.parse(*args, **kwargs) if opt.model_dir: self.init_from_model_dir(opt.model_dir) self.energy = None self.dpp = 0.0 self.xing = None if opt.nat_tune_x is not None: raise AcceleratorDefinitionError( "Argument 'nat_tune_x' not allowed when loading from model directory." ) if opt.nat_tune_y is not None: raise AcceleratorDefinitionError( "Argument 'nat_tune_y' not allowed when loading from model directory." ) if opt.drv_tune_x is not None: raise AcceleratorDefinitionError( "Argument 'drv_tune_x' not allowed when loading from model directory." ) if opt.drv_tune_y is not None: raise AcceleratorDefinitionError( "Argument 'drv_tune_y' not allowed when loading from model directory." ) else: if opt.nat_tune_x is None: raise AcceleratorDefinitionError( "Argument 'nat_tune_x' is required.") if opt.nat_tune_y is None: raise AcceleratorDefinitionError( "Argument 'nat_tune_y' is required.") self.nat_tune_x = opt.nat_tune_x self.nat_tune_y = opt.nat_tune_y self.drv_tune_x = None self.drv_tune_y = None self._excitation = AccExcitationMode.FREE if opt.acd or opt.adt: if opt.acd and opt.adt: raise AcceleratorDefinitionError( "Select only one excitation type.") if opt.drv_tune_x is None: raise AcceleratorDefinitionError( "Argument 'drv_tune_x' is required.") if opt.drv_tune_y is None: raise AcceleratorDefinitionError( "Argument 'drv_tune_x' is required.") self.drv_tune_x = opt.drv_tune_x self.drv_tune_y = opt.drv_tune_y if opt.acd: self._excitation = AccExcitationMode.ACD elif opt.adt: self._excitation = AccExcitationMode.ADT # optional with default self.dpp = opt.dpp self.fullresponse = opt.fullresponse # optional no default self.energy = opt.get("energy", None) self.xing = opt.get("xing", None) self.optics_file = opt.get("optics", None) # for GetLLM self.model_dir = None self._model = None self._model_driven = None self._model_best_knowledge = None self._elements = None self._elements_centre = None self._errordefspath = None self.verify_object()
def verify_object(self): # TODO: Maybe more checks? if self.optics_file is None: raise AcceleratorDefinitionError( "The accelerator definition is incomplete, optics " "file has not been specified." )