diff --git a/src/scripts/model_maker.py b/src/scripts/model_maker.py index ebd8592419e3991d1ec282bdd2f903c248bba747..4475a826aafa9a2cf053ac9c73ce94c59a7c7996 100755 --- a/src/scripts/model_maker.py +++ b/src/scripts/model_maker.py @@ -69,63 +69,69 @@ def main(): # \return result: `int` An exit code (0 if successful). def interpolate_constants(sconf): result = 0 - for i in range(sconf['configurations']): - for j in range(sconf['nshl'][i]): - file_idx = sconf['dielec_id'][i][j] - dielec_path = Path(sconf['dielec_path'], sconf['dielec_file'][int(file_idx) - 1]) - file_name = str(dielec_path) - dielec_file = open(file_name, 'r') - wavelengths = [] - rpart = [] - ipart = [] - str_line = dielec_file.readline() - while (str_line != ""): - if (not str_line.startswith('#')): - split_line = str_line.split(',') - if (len(split_line) == 3): - wavelengths.append(float(split_line[0])) - rpart.append(float(split_line[1])) - ipart.append(float(split_line[2])) + err_arg = "" + try: + for i in range(sconf['configurations']): + for j in range(sconf['nshl'][i]): + file_idx = sconf['dielec_id'][i][j] + dielec_path = Path(sconf['dielec_path'], sconf['dielec_file'][int(file_idx) - 1]) + err_arg = str(dielec_path) + file_name = err_arg + dielec_file = open(file_name, 'r') + wavelengths = [] + rpart = [] + ipart = [] str_line = dielec_file.readline() - dielec_file.close() - wi = 0 - x0 = 0.0 - x1 = 0.0 - ry0 = 0.0 - iy0 = 0.0 - ry1 = 0.0 - iy1 = 0.0 - for dci in range(sconf['nxi']): - w = sconf['vec_xi'][dci] - while (w > x1): - x0 = wavelengths[wi] - ry0 = rpart[wi] - iy0 = ipart[wi] - if (wi == len(wavelengths)): - print("ERROR: file %s does not cover requested wavelengths!"%file_name) - return 1 - wi += 1 - x1 = wavelengths[wi] - ry1 = rpart[wi] - iy1 = ipart[wi] - if (wi > 0): - x0 = wavelengths[wi - 1] - ry0 = rpart[wi - 1] - iy0 = ipart[wi - 1] - dx = w - x0 - dry = (ry1 - ry0) / (x1 - x0) * dx - diy = (iy1 - iy0) / (x1 - x0) * dx - ry = ry0 + dry - iy = iy0 + diy - sconf['rdc0'][j][i][dci] = ry - sconf['idc0'][j][i][dci] = iy - else: - if (wavelengths[wi] == w): - sconf['rdc0'][j][i][dci] = rpart[0] - sconf['idc0'][j][i][dci] = ipart[0] + while (str_line != ""): + if (not str_line.startswith('#')): + split_line = str_line.split(',') + if (len(split_line) == 3): + wavelengths.append(float(split_line[0])) + rpart.append(float(split_line[1])) + ipart.append(float(split_line[2])) + str_line = dielec_file.readline() + dielec_file.close() + wi = 0 + x0 = 0.0 + x1 = 0.0 + ry0 = 0.0 + iy0 = 0.0 + ry1 = 0.0 + iy1 = 0.0 + for dci in range(sconf['nxi']): + w = sconf['vec_xi'][dci] + while (w > x1): + x0 = wavelengths[wi] + ry0 = rpart[wi] + iy0 = ipart[wi] + if (wi == len(wavelengths)): + print("ERROR: file %s does not cover requested wavelengths!"%file_name) + return 1 + wi += 1 + x1 = wavelengths[wi] + ry1 = rpart[wi] + iy1 = ipart[wi] + if (wi > 0): + x0 = wavelengths[wi - 1] + ry0 = rpart[wi - 1] + iy0 = ipart[wi - 1] + dx = w - x0 + dry = (ry1 - ry0) / (x1 - x0) * dx + diy = (iy1 - iy0) / (x1 - x0) * dx + ry = ry0 + dry + iy = iy0 + diy + sconf['rdc0'][j][i][dci] = ry + sconf['idc0'][j][i][dci] = iy else: - print("ERROR: file %s does not cover requested wavelengths!"%file_name) - return 2 + if (wavelengths[wi] == w): + sconf['rdc0'][j][i][dci] = rpart[0] + sconf['idc0'][j][i][dci] = ipart[0] + else: + print("ERROR: file %s does not cover requested wavelengths!"%file_name) + return 2 + except FileNotFoundError as ex: + print("ERROR: file not found %s!"%err_arg) + return 3 return result ## \brief Create tha calculation configuration structure from YAML input. @@ -262,12 +268,16 @@ def load_model(model_file): [0.0 for k in range(sconf['nxi'])] for j in range(sconf['configurations']) ] for i in range(max_layers) ] - interpolate_constants(sconf) + check = interpolate_constants(sconf) + if (check != 0): + return (None, None) else: # sconf[idfc] != 0 and scaling on wavelength print("ERROR: for wavelength scaling, optical constants must be tabulated!") return (None, None) elif (model['material_settings']['match_mode'] == "GRID"): - match_grid(sconf) + check = match_grid(sconf) + if (check != 0): + return(None, None) else: print("ERROR: %s is not a recognized match mode!"%(model['material_settings']['match_mode'])) return (None, None) @@ -466,72 +476,78 @@ def match_grid(sconf): max_layers = 0 nxi = 0 sconf['vec_xi'] = [] - for i in range(sconf['configurations']): - layers = sconf['nshl'][i] - if (sconf['application'] == "INCLUSION" and i == 0): - layers += 1 - for j in range(layers): - file_idx = sconf['dielec_id'][i][j] - dielec_path = Path(sconf['dielec_path'], sconf['dielec_file'][int(file_idx) - 1]) - file_name = str(dielec_path) - dielec_file = open(file_name, 'r') - wavelengths = [] - rpart = [] - ipart = [] - str_line = dielec_file.readline() - while (str_line != ""): - if (not str_line.startswith('#')): - split_line = str_line.split(',') - if (len(split_line) == 3): - wavelengths.append(float(split_line[0])) - rpart.append(float(split_line[1])) - ipart.append(float(split_line[2])) + err_arg = "" + try: + for i in range(sconf['configurations']): + layers = sconf['nshl'][i] + if (sconf['application'] == "INCLUSION" and i == 0): + layers += 1 + for j in range(layers): + file_idx = sconf['dielec_id'][i][j] + dielec_path = Path(sconf['dielec_path'], sconf['dielec_file'][int(file_idx) - 1]) + err_arg = str(dielec_path) + file_name = err_arg + dielec_file = open(file_name, 'r') + wavelengths = [] + rpart = [] + ipart = [] str_line = dielec_file.readline() - dielec_file.close() - if (max_layers == 0): - # This is executed only once - max_layers = max(sconf['nshl']) - if (sconf['application'] == "INCLUSION" and max_layers < sconf['nshl'][0] + 1): - max_layers = sconf['nshl'][0] + 1 - w_start = sconf['xi_start'] - w_end = sconf['xi_end'] - for wi in range(len(wavelengths)): - w = wavelengths[wi] - if (w >= w_start and w <= w_end): - sconf['vec_xi'].append(w) - nxi += 1 - sconf['rdc0'] = [ - [ + while (str_line != ""): + if (not str_line.startswith('#')): + split_line = str_line.split(',') + if (len(split_line) == 3): + wavelengths.append(float(split_line[0])) + rpart.append(float(split_line[1])) + ipart.append(float(split_line[2])) + str_line = dielec_file.readline() + dielec_file.close() + if (max_layers == 0): + # This is executed only once + max_layers = max(sconf['nshl']) + if (sconf['application'] == "INCLUSION" and max_layers < sconf['nshl'][0] + 1): + max_layers = sconf['nshl'][0] + 1 + w_start = sconf['xi_start'] + w_end = sconf['xi_end'] + for wi in range(len(wavelengths)): + w = wavelengths[wi] + if (w >= w_start and w <= w_end): + sconf['vec_xi'].append(w) + nxi += 1 + sconf['rdc0'] = [ [ - 0.0 for dk in range(nxi) - ] for dj in range(sconf['configurations']) - ] for di in range(max_layers) - ] - sconf['idc0'] = [ - [ + [ + 0.0 for dk in range(nxi) + ] for dj in range(sconf['configurations']) + ] for di in range(max_layers) + ] + sconf['idc0'] = [ [ - 0.0 for dk in range(nxi) - ] for dj in range(sconf['configurations']) - ] for di in range(max_layers) - ] - sconf['nxi'] = nxi - # This is executed for all layers in all configurations - wi = 0 - x = wavelengths[wi] - ry = rpart[wi] - iy = ipart[wi] - for dci in range(sconf['nxi']): - w = sconf['vec_xi'][dci] - while (w > x): - x = wavelengths[wi] - ry = rpart[wi] - iy = ipart[wi] - if (wi == len(wavelengths)): - print("ERROR: file %s does not cover requested wavelengths!"%file_name) - return 1 - wi += 1 - sconf['rdc0'][j][i][dci] = ry - sconf['idc0'][j][i][dci] = iy + [ + 0.0 for dk in range(nxi) + ] for dj in range(sconf['configurations']) + ] for di in range(max_layers) + ] + sconf['nxi'] = nxi + # This is executed for all layers in all configurations + wi = 0 + x = wavelengths[wi] + ry = rpart[wi] + iy = ipart[wi] + for dci in range(sconf['nxi']): + w = sconf['vec_xi'][dci] + while (w > x): + x = wavelengths[wi] + ry = rpart[wi] + iy = ipart[wi] + if (wi == len(wavelengths)): + print("ERROR: file %s does not cover requested wavelengths!"%file_name) + return 1 + wi += 1 + sconf['rdc0'][j][i][dci] = ry + sconf['idc0'][j][i][dci] = iy + except FileNotFoundError as ex: + print("ERROR: file not found %s!"%err_arg) + return 3 return result ## \brief Parse the command line arguments.