-
Notifications
You must be signed in to change notification settings - Fork 4
DIST block for PySixDesk #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
f63d65f
b08f2f0
d7930a1
da75a8f
ed93429
c6863cd
dd94ed8
9ab47bd
f0b4c7c
95d6acc
83cf50c
111785a
089f22b
d2ac817
9d54b12
4f2992f
9e603af
d84cbe0
727c70e
4924d18
23e55f9
73449a0
2e6b0ed
151c8bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import re | ||
import logging | ||
|
||
from math import pi | ||
from pathlib import Path | ||
from collections import OrderedDict | ||
from itertools import product | ||
|
@@ -9,7 +10,7 @@ | |
|
||
from . import machineparams | ||
from .constants import PROTON_MASS | ||
from .utils import PYSIXDESK_ABSPATH, merge_dicts | ||
from .utils import PYSIXDESK_ABSPATH, linspace | ||
|
||
|
||
class StudyParams: | ||
|
@@ -46,7 +47,7 @@ def __init__(self, | |
# comment regexp | ||
self._reg_comment = re.compile(r'^(\s?!|\s?/).*', re.MULTILINE) | ||
# placeholder pattern regexp | ||
self._reg = re.compile(r'%([a-zA-Z0-9_]+/?)') | ||
self._reg = re.compile(r'%(?!FILE|%)([a-zA-Z0-9_]+/?)') | ||
self.fort_path = fort_path | ||
self.mask_path = mask_path | ||
# initialize empty calculation queue | ||
|
@@ -85,18 +86,17 @@ def __init__(self, | |
"writebins": 1, | ||
} | ||
self.machine_defaults = machine_defaults | ||
self.defaults = merge_dicts(self.f3_defaults, self.machine_defaults) | ||
self.defaults = {**self.f3_defaults, **self.machine_defaults} | ||
# phasespace params | ||
# TODO: find sensible defaults for the phasespace parameters. | ||
amp = [8, 10, 12] # The amplitude | ||
self.phasespace = {"amp": list(zip(amp, amp[1:])), | ||
"kang": list(range(1, 1 + 1)), | ||
"kmax": 5, | ||
self.phasespace = {"phase_space_var1": [], | ||
"phase_space_var2": [], | ||
} | ||
|
||
self.madx = self.find_patterns(self.mask_path) | ||
self.sixtrack = self.find_patterns(self.fort_path, | ||
mandatory=['chrom_eps', 'CHROM']) | ||
self.sixtrack['dist_type'] = 'None' | ||
|
||
@property | ||
def _sixtrack_only(self): | ||
|
@@ -117,6 +117,10 @@ def oneturn(self): | |
sixtrack['toggle_coll/'] = '/' | ||
return sixtrack | ||
|
||
@staticmethod | ||
def da_angles(start=0, end=pi/2, n=7): | ||
return linspace(start, end, n + 2)[1: -1] # exclusive | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add a more verbose comment, saying that we don't take 0 and pi/2 |
||
|
||
def keys(self): | ||
"""Gets the keys of 'self.madx', 'self.sixtrack' and 'self.phasespace'. | ||
|
||
|
@@ -180,7 +184,7 @@ def find_patterns(self, file_path, keep_none=True, mandatory=None): | |
|
||
@staticmethod | ||
def _combinations_prep(**kwargs): | ||
'''Sanitizes the paramter values. | ||
'''Sanitizes the parameter values. | ||
|
||
Args: | ||
**kwargs: Parameter name = parameter value. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,58 @@ def download_output(filenames, dest, zp=True): | |
shutil.copy(filename, dest) | ||
|
||
|
||
def sandwich(in_file, out_file, path_prefix='', logger=None): | ||
''' | ||
Looks for any patterns matching '^%FILE:.*' in in_file, then replaces the | ||
match with the content of the file following the match. | ||
A path prefix can be specified to look for matched file in another | ||
directory. If the matched file is not found, comment out the pattern. | ||
Example: | ||
contents of in_file: | ||
aaaaaaaaa | ||
%FILE:insert.txt | ||
aaaaaaaaa | ||
contents of insert.txt: | ||
bbbbbbbbb | ||
bbbbbbbbb | ||
writes to out_file: | ||
aaaaaaaaa | ||
bbbbbbbbb | ||
bbbbbbbbb | ||
aaaaaaaaa | ||
TODO: maybe it's best to return the the sandwiched lines and not write the | ||
file, so that more parsing can happen in memory without reopening and | ||
writing files. Also might be best to take as input the contents itself for | ||
the same reason. | ||
''' | ||
|
||
if logger is not None: | ||
display = logger.warning | ||
else: | ||
display = print | ||
|
||
with open(in_file, 'r') as f: | ||
in_lines = f.read() | ||
|
||
reg = re.compile(r'^%FILE:.*', re.MULTILINE) | ||
for m in re.finditer(reg, in_lines): | ||
m_str = m.group() | ||
try: | ||
if path_prefix is None: | ||
path_prefix = '' | ||
fname = os.path.join(path_prefix, m_str.split(':')[1].lstrip()) | ||
with open(fname, 'r') as f: | ||
file_lines = f.read() | ||
in_lines = re.sub(f'{m_str}', file_lines, in_lines) | ||
except FileNotFoundError as e: | ||
display(e) | ||
display(f'Commenting out {m_str} for {out_file}') | ||
in_lines = re.sub(f'{m_str}', f'/{m_str}', in_lines) | ||
|
||
with open(out_file, 'w') as out: | ||
out.write(in_lines) | ||
|
||
|
||
def check_fort3_block(fort3, block): | ||
'''Check the existence of the given block in fort.3''' | ||
|
||
|
@@ -238,15 +290,13 @@ def condor_logger(name): | |
return logger | ||
|
||
|
||
def merge_dicts(x, y): | ||
"""Merges two dicts. | ||
|
||
Returns: | ||
dict: Merged dict | ||
""" | ||
z = x.copy() | ||
z.update(y) | ||
return z | ||
def linspace(a, b, n): | ||
'''Numpyless linear spacing function. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you want a numpyless function for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is my doing, the main reason was simply to not have to add |
||
''' | ||
if n < 2: | ||
return a | ||
diff = (float(b) - a)/(n - 1) | ||
return [diff * i + a for i in range(n)] | ||
|
||
|
||
class ProgressBar(object): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't you want to use really
None
?