From 2723cb9cbb66666df451c5a93bcdbf2537eea0dd Mon Sep 17 00:00:00 2001 From: Thomas Albers Raviola Date: Fri, 12 Jul 2024 14:38:44 +0200 Subject: Add config class --- .gitignore | 1 + schroedinger/schrodinger_solve.py | 52 ++++++++++++++++++++++++++++++++++++++- test/schrodinger.inp | 7 ++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 test/schrodinger.inp diff --git a/.gitignore b/.gitignore index 4bcb6f8..251d000 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ docs/_build .idea/ schroedinger/__pycache__ +notes.org diff --git a/schroedinger/schrodinger_solve.py b/schroedinger/schrodinger_solve.py index 9369e2e..a759c3a 100644 --- a/schroedinger/schrodinger_solve.py +++ b/schroedinger/schrodinger_solve.py @@ -1,5 +1,55 @@ +import numpy as np +from pathlib import Path +import argparse + +class Config: + def __init__(self, path): + current_line = 0 + + # Ensure Path object + if path is not Path: + path = Path(path) + + def next_parameter(fd): + '''Read next parameter, ignoring comments or empty lines''' + content = None + nonlocal current_line + while not content: + str = fd.readline() + current_line += 1 + index = str.find('#') + content = str[0:index].strip() + return content + + with open(path, 'r') as fd: + try: + self.mass = float(next_parameter(fd)) + self.interval = [float(attr) for attr in next_parameter(fd).split()] + self.eig_interval = [int(attr) for attr in next_parameter(fd).split()] + self.interpolation = next_parameter(fd) + + npoints = int(next_parameter(fd)) + self.points = np.zeros((npoints, 2)) + for i in range(npoints): + line = next_parameter(fd) + self.points[i] = np.array([float(comp) for comp in line.split()]) + except: + print('Syntax error in \'{}\' line {}'.format(path.name, current_line)) + + def main(): - pass + parser = argparse.ArgumentParser( + prog='schrodinger_solve', + description='a', + epilog='a') + parser.add_argument('filename') + args = parser.parse_args() + conf = Config(args.filename) + print(conf.mass) + print(conf.interval) + print(conf.eig_interval) + print(conf.interpolation) + print(conf.points) if __name__ == '__main__': main() diff --git a/test/schrodinger.inp b/test/schrodinger.inp new file mode 100644 index 0000000..8c9a16b --- /dev/null +++ b/test/schrodinger.inp @@ -0,0 +1,7 @@ +2.0 # mass +-2.0 2.0 1999 # xMin xMax nPoint +1 5 # first and last eigenvalue to print +linear # interpolation type +2 # nr. of interpolation points and xy declarations +-2.0 0.0 +2.0 0.0 -- cgit v1.2.3