def get_rdts(madx: Madx, order: int = 4, file: Union[Path, str] = None) -> tfs.TfsDataFrame: """ INITIAL IMPLEMENTATION CREDITS GO TO JOSCHUA DILLY (@JoschD). Calculate the RDTs via PTC_TWISS. Args: madx (cpymad.madx.Madx): an instanciated cpymad Madx object. order (int): maximum derivative order (only 0, 1 or 2 implemented in PTC). Defaults to `2`. file (Union[Path, str]): path to output file. Default `None` Returns: A TfsDataframe with results. """ logger.info(f"Entering PTC to calculate RDTs up to order {order}") madx.ptc_create_universe() logger.trace("Creating PTC layout") madx.ptc_create_layout(model=3, method=4, nst=3, exact=True) # madx.ptc_create_layout(model=3, method=6, nst=1) # from Michi logger.trace("Incorporating MAD-X alignment errors") madx.ptc_align() # use madx alignment errors # madx.ptc_setswitch(fringe=True) # include fringe effects logger.debug("Executing PTC Twiss") madx.ptc_twiss(icase=6, no=order, normal=True, trackrdts=True) madx.ptc_end() logger.debug("Extracting results to TfsDataFrame") dframe = tfs.TfsDataFrame(madx.table.twissrdt.dframe()) dframe.columns = dframe.columns.str.upper() dframe.NAME = dframe.NAME.str.upper() if file: logger.debug(f"Exporting results to disk at '{Path(file).absolute()}'") tfs.write(file, dframe) return dframe