added flexibility
This commit is contained in:
@ -53,13 +53,23 @@ def run_sims(
|
||||
subprocess.run(arguments)
|
||||
|
||||
|
||||
def find_config_file(var_params: dict) -> pathlib.Path:
|
||||
def find_config_file(motion: str, distribution: str, var_params: dict) -> pathlib.Path:
|
||||
# TODO handle situation if multiple files fit
|
||||
pattern = re.compile('|'.join(([f'{k}={v:1.6e}' for (k, v) in var_params.items()])).replace('.', '\.').replace('+', '\+'))
|
||||
p_file = None
|
||||
if var_params:
|
||||
var_string = '|'.join(([f'{k}={v:1.6e}' for (k, v) in var_params.items()])).replace('.', '\.').replace('+', '\+')
|
||||
pattern = re.compile(var_string)
|
||||
for p_file in pathlib.Path('.').glob('*_parameter.txt'):
|
||||
if len(re.findall(pattern, str(p_file))) == len(var_params) and re.search(f'{motion}_{distribution}', str(p_file)):
|
||||
return p_file
|
||||
raise ValueError(f'No parameter file found for {motion}, {distribution}, {var_params}')
|
||||
else:
|
||||
for p_file in pathlib.Path('.').glob('*_parameter.txt'):
|
||||
if re.search(f'{motion}_{distribution}', str(p_file)):
|
||||
return p_file
|
||||
raise ValueError(f'No parameter file found for {motion}, {distribution}, {var_params}')
|
||||
|
||||
|
||||
for p_file in pathlib.Path('.').glob('*_parameter.txt'):
|
||||
if len(re.findall(pattern, str(p_file))) == len(var_params):
|
||||
return p_file
|
||||
|
||||
|
||||
def read_parameter_file(path: str | pathlib.Path) -> dict[str, float]:
|
||||
|
@ -24,6 +24,8 @@ def ste_decay(x: np.ndarray, m0: float, t: float, beta: float, finfty: float) ->
|
||||
|
||||
def fit_decay(x: np.ndarray, y: np.ndarray, tevo: np.ndarray, verbose: bool = True) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
|
||||
num_evo = y.shape[1]
|
||||
if num_evo != tevo.size:
|
||||
tevo = np.arange(num_evo)
|
||||
|
||||
tau_fit = np.empty((num_evo, 2))
|
||||
tau_fit[:, 0] = tevo
|
||||
@ -37,7 +39,7 @@ def fit_decay(x: np.ndarray, y: np.ndarray, tevo: np.ndarray, verbose: bool = Tr
|
||||
scaled_y = (y-y[-1, :]) / (y[0, :]-y[-1, :])
|
||||
|
||||
for j in range(num_evo):
|
||||
p0 = [scaled_y[0, 1], x[np.argmin(np.abs(scaled_y[:, j]-np.exp(-1)))], 0.5, 0.1]
|
||||
p0 = [y[0, 0], x[np.argmin(np.abs(scaled_y[:, j]-np.exp(-1)))], 0.8, 0.1]
|
||||
|
||||
try:
|
||||
res = curve_fit(ste_decay, x, y[:, j], p0, bounds=[(0, 0, 0., 0), (np.inf, np.inf, 1, 1)])
|
||||
@ -57,7 +59,12 @@ def fit_decay(x: np.ndarray, y: np.ndarray, tevo: np.ndarray, verbose: bool = Tr
|
||||
return tau_fit, beta_fit, finfty_fit
|
||||
|
||||
|
||||
def fit_and_save_ste(parameter_file: pathlib.Path, plot_decays: bool = True, verbose: bool = True):
|
||||
def fit_and_save_ste(
|
||||
parameter_file: pathlib.Path,
|
||||
prefix: str,
|
||||
plot_decays: bool = True,
|
||||
verbose: bool = True,
|
||||
) -> tuple[str, np.ndarray, np.ndarray, np.ndarray]:
|
||||
# read simulation parameters
|
||||
parameter = read_parameter_file(parameter_file)
|
||||
|
||||
@ -67,36 +74,24 @@ def fit_and_save_ste(parameter_file: pathlib.Path, plot_decays: bool = True, ver
|
||||
# make evolution times
|
||||
tevo = np.linspace(parameter['tevo_start'], parameter['tevo_stop'], num=int(parameter['tevo_steps']))
|
||||
|
||||
raw_data_cc = np.loadtxt(f'coscos_{varied_string}.dat')
|
||||
raw_data_ss = np.loadtxt(f'sinsin_{varied_string}.dat')
|
||||
raw_data = np.loadtxt(f'{prefix}_{varied_string}.dat')
|
||||
|
||||
t_mix = raw_data_cc[:, 0]
|
||||
cc_decay = raw_data_cc[:, 1:]
|
||||
ss_decay = raw_data_ss[:, 1:]
|
||||
t_mix = raw_data[:, 0]
|
||||
decay = raw_data[:, 1:]
|
||||
|
||||
if plot_decays:
|
||||
fig_cc, ax_cc = plt.subplots()
|
||||
ax_cc.set_title('Cos-Cos')
|
||||
ax_cc.semilogx(t_mix, cc_decay, '.')
|
||||
fig, ax = plt.subplots()
|
||||
ax.set_title(prefix)
|
||||
ax.semilogx(t_mix, decay, '.')
|
||||
|
||||
fig_ss, ax_ss = plt.subplots()
|
||||
ax_ss.set_title('Sin-Sin')
|
||||
ax_ss.semilogx(t_mix, ss_decay, '.')
|
||||
fig.show()
|
||||
|
||||
plt.show()
|
||||
print(f'Fit {prefix}')
|
||||
tau, beta, finfty = fit_decay(t_mix, decay, tevo, verbose=verbose)
|
||||
|
||||
print('Fit Cos-Cos')
|
||||
tau_cc, beta_cc, finfty_cc = fit_decay(t_mix, cc_decay, tevo, verbose=verbose)
|
||||
|
||||
np.savetxt(f'tau_cc_{varied_string}.dat', tau_cc)
|
||||
np.savetxt(f'beta_cc_{varied_string}.dat', beta_cc)
|
||||
np.savetxt(f'finfty_cc_{varied_string}.dat', finfty_cc)
|
||||
np.savetxt(f'tau_{prefix}_{varied_string}.dat', tau)
|
||||
np.savetxt(f'beta_{prefix}_{varied_string}.dat', beta)
|
||||
np.savetxt(f'finfty_{prefix}_{varied_string}.dat', finfty)
|
||||
|
||||
print('Fit Sin-Sin')
|
||||
tau_ss, beta_ss, finfty_ss = fit_decay(t_mix, ss_decay, tevo, verbose=verbose)
|
||||
|
||||
np.savetxt(f'tau_ss_{varied_string}.dat', tau_ss)
|
||||
np.savetxt(f'beta_ss_{varied_string}.dat', beta_ss)
|
||||
np.savetxt(f'finfty_ss_{varied_string}.dat', finfty_ss)
|
||||
|
||||
return varied_string, tau_cc, beta_cc, finfty_cc, tau_ss, beta_ss, finfty_ss
|
||||
return varied_string, tau, beta, finfty
|
||||
|
Reference in New Issue
Block a user