Skip to content

Commit

Permalink
Modification of the reference data (= experiment data) reading section.
Browse files Browse the repository at this point in the history
- Additional options: "reference_first_line", "reference_last_line". The former specifies the first line to read the reference data, and the latter specifies the last line to read the reference data.
- Changed the reading of reference data from the np.loadtxt function to using the open, read, readlines, and split functions.
  • Loading branch information
H-Iwamoto-research committed Sep 26, 2023
1 parent 3755365 commit 5e1a921
Showing 1 changed file with 54 additions and 20 deletions.
74 changes: 54 additions & 20 deletions src/py2dmat/solver/sim_trhepd_rheed_mb_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,33 +486,34 @@ def __init__(self, info, d_timer):

# solver.reference
info_ref = info_s.get("reference", {})
reference_path = info_ref.get("path", "experiment.txt")

v = info_ref.setdefault("first", 1)
v = info_ref.setdefault("reference_first_line", 1)
if not (isinstance(v, int) and v >= 0):
raise exception.InputError(
"ERROR: reference_first_line should be non-negative integer"
)
firstline = v

v = info_ref.setdefault("last", 56)
if not (isinstance(v, int) and v >= firstline):
raise exception.InputError(
"ERROR: reference_last_line < reference_first_line"
)
# None is dummy value
# If "reference_last_line" is not specified in the toml file,
# the last line of the reference file is used for the R-factor calculation.
v = info_ref.setdefault("reference_last_line", None)
if v is None:
reference_are_read_to_final_line = True
else:
reference_are_read_to_final_line = False
if not (isinstance(v, int) and v >= firstline):
raise exception.InputError(
"ERROR: reference_last_line < reference_first_line"
)
lastline = v


# Read experiment data
if self.mpirank == 0:
data_e = np.loadtxt(reference_path)
else:
data_e = None
data_experiment = self.mpicomm.bcast(data_e,root=0)

self.beam_number_experiment = data_experiment.shape[1]
reference_path = info_ref.get("path", "experiment.txt")
data_experiment = self.read_experiment(
reference_path, firstline, lastline,
reference_are_read_to_final_line )
self.angle_number_experiment = data_experiment.shape[0]

self.beam_number_exp_raw = data_experiment.shape[1]

v = info_ref.get("exp_number", None)

if v == None :
Expand All @@ -525,7 +526,7 @@ def __init__(self, info, d_timer):
"ERROR: 'exp_number' must be a list type."
)

if max(v) > self.beam_number_experiment:
if max(v) > self.beam_number_exp_raw :
raise exception.InputError(
"ERROR: The 'exp_number' setting is wrong."
)
Expand All @@ -537,7 +538,9 @@ def __init__(self, info, d_timer):
)

self.exp_number = v
self.beam_number_experiment = len(self.exp_number)
number_ex = self.exp_number

sum_experiment = 0
for i in number_ex:
sum_experiment += sum(data_experiment[::,i])
Expand Down Expand Up @@ -601,7 +604,38 @@ def __init__(self, info, d_timer):
]
for p in self.reference_normalized:
self.all_reference_normalized.append(p)


def read_experiment(self, ref_path, first, last, read_to_final_line):
# Read experiment data
if self.mpirank == 0:
file_input = open(ref_path, "r")
Elines = file_input.readlines()

firstline = first
if read_to_final_line :
lastline = len(Elines)
else:
lastline = last

n_exp_row = lastline-firstline+1

# get value from data
for row_index in range(n_exp_row) :
line_index = firstline + row_index - 1
line = Elines[line_index]
data = line.split()
# first loop
if row_index == 0:
n_exp_column = len(data)
data_e = np.zeros((n_exp_row, n_exp_column)) #init value

for column_index in range(n_exp_column):
data_e[row_index, column_index] = float(data[column_index])
else:
data_e = None
data_exp = self.mpicomm.bcast(data_e,root=0)
return data_exp

def prepare(self, fitted_x_list):
self.fitted_x_list = fitted_x_list

Expand Down

0 comments on commit 5e1a921

Please sign in to comment.