Skip to content

Adda

DEFAULT_BIN_PATH = '' module-attribute

DEFAULT_CONF_PATH = '' module-attribute

adda = ADDAManager('./config.json', './MSTM3/src/mstm') module-attribute

runner = pyperf.Runner(values=1, processes=5, warmups=1) module-attribute

ADDAManager

Source code in yasfpy/benchmark/adda.py
11
12
13
14
15
16
17
18
19
def __init__(
    self,
    config_path: str = DEFAULT_CONF_PATH,
    binary: str = DEFAULT_BIN_PATH,
    output_file: str = "wow",
):
    self.config = Config(config_path)
    self.binary = binary
    self.output_file = output_file

config = Config(config_path) instance-attribute

binary = binary instance-attribute

output_file = output_file instance-attribute

__exec

Source code in yasfpy/benchmark/adda.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def __exec(self, runner: pyperf.Runner = None):
    # command = [self.binary, self.input_args]
    if self.config.spheres.shape[0] > 1:
        print(
            "WARNING! MORE THAN 1 SPHERE DETECTED IN CONFIG! USING ONLY THE FIRST SPHERE!"
        )

    diameter = (
        self.config.spheres[0, 3] * 2
    )  # TODO: THIS ALWAYS NEEDS TO BE IN NANOMETERS
    for wi, wavelength in enumerate(self.config.wavelength):
        command = [
            self.binary,
            f"-size",
            f"{diameter}",
            f"-lambda",
            f"{wavelength}",
            f"-m",
            f"{self.config.medium_refractive_index[wi].real}",
            f"{self.config.medium_refractive_index[wi].imag}",
            "-asym",
            f"-dir",
            f"run_sphere_r{diameter}_l{wavelength}",
        ]
    if runner is None:
        os.system(" ".join(command))
    else:
        # TODO: this needs to take into account all wavelengths
        runner.bench_command("mstm3_exec", command)

__read

Source code in yasfpy/benchmark/adda.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def __read(self):
    q_exts: list[float] = []
    q_scats: list[float] = []
    gs: list[float] = []
    c_exts: list[float] = []
    c_scats: list[float] = []
    diameter = self.config.spheres[0, 3] * 2
    for wavelength in self.config.wavelength:
        with open(f"run_sphere_r{diameter}_l{wavelength}/CrossSec-Y", "r") as f:
            data = f.readlines()
        for line in data:
            if len(re.findall("^g\t=", line)) > 0:
                g = float(line.split("=")[-1].strip().split(",")[-1].strip(")"))
                gs.append(g)
            if len(re.findall("Qsca\t", line)) > 0:
                q_sca = float(line.split("=")[-1].strip())
                q_scats.append(q_sca)
            if len(re.findall("Qext\t", line)) > 0:
                q_ext = float(line.split("=")[-1].strip())
                q_exts.append(q_ext)
            if len(re.findall("Csca\t", line)) > 0:
                c_sca = float(line.split("=")[-1].strip())
                c_scats.append(c_sca)
            if len(re.findall("Cext\t", line)) > 0:
                c_ext = float(line.split("=")[-1].strip())
                c_exts.append(c_ext)
    adda = {
        "cross_sections": {"C_scat": c_scats, "C_ext": c_ext},
        "efficiencies": {
            "Q_scat": q_scats,
            "Q_ext": q_exts,
        },
        "asymmetry": {"g": gs},
    }
    self.output = adda

clean

Source code in yasfpy/benchmark/adda.py
87
88
89
90
91
92
def clean(self):
    # TODO: clean folders
    res_dirs = [i for i in os.listdir() if len(re.findall("run_sphere", i)) > 0]
    for dir in res_dirs:
        shutil.rmtree(dir)
    pass

run

Source code in yasfpy/benchmark/adda.py
 94
 95
 96
 97
 98
 99
100
def run(self, runner: pyperf.Runner = None, cleanup: bool = True):
    self.__exec(runner)
    if runner is None:
        self.__read()

    if cleanup:
        self.clean()

export

Source code in yasfpy/benchmark/adda.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def export(self, file: str = None, cleanup: bool = False):
    if file is None:
        raise Exception("Please provide a filename")
    self.run(cleanup=False)
    adda = copy.deepcopy(self.output)

    match file.split(".")[-1]:
        case "json":
            with open(file, "w") as outfile:
                json.dump(self.output, outfile)
        case "yaml" | "yml":
            with open(file, "w") as outfile:
                yaml.dump(self.output, outfile, default_flow_style=False)
        case "bz2":
            with bz2.BZ2File(file, "w") as outfile:
                _pickle.dump(self.output, outfile)
        case _:
            raise Exception(
                "The provided config file needs to be a json or yaml file!"
            )

    if cleanup:
        self.clean()

Comments