Skip to content

Mstm4

mstm = MSTM4Manager('./config.json', './MSTM/code/mstm', parallel=4) module-attribute

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

MSTM4Manager

Source code in yasfpy/benchmark/mstm4.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def __init__(
    self,
    config_path: str = None,
    binary: str = None,
    input_file: str = "mstm4.inp",
    output_file: str = "mstm4.dat",
    parallel: int = 1,
):
    self.config = Config(config_path)
    self.binary = binary

    self.input_file = input_file
    self.output_file = output_file

    self.parallel = parallel

config = Config(config_path) instance-attribute

binary: str = binary class-attribute instance-attribute

input_file: str = input_file class-attribute instance-attribute

output_file: str = output_file class-attribute instance-attribute

parallel: int = parallel class-attribute instance-attribute

__write

Source code in yasfpy/benchmark/mstm4.py
 44
 45
 46
 47
 48
 49
 50
 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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def __write(
    self,
    fixed_lmax: bool = True,
    save: bool = True,
) -> None:
    mstm_config = "output_file\n"
    mstm_config += f"{self.output_file}\n"
    mstm_config += "print_sphere_data\n"
    mstm_config += "t\n"
    mstm_config += "calculate_scattering_matrix\n"
    mstm_config += "t\n"
    mstm_config += "incident_frame\n"
    mstm_config += "f\n"
    mstm_config += "scattering_map_model\n"
    mstm_config += "0\n"
    mstm_config += "scattering_map_increment\n"
    mstm_config += "30.d0\n"
    mstm_config += "incident_beta_deg\n"
    mstm_config += f"{self.config.config['initial_field']['polar_angle']:e}\n"
    mstm_config += "incident_alpha_deg\n"
    mstm_config += f"{self.config.config['initial_field']['azimuthal_angle']:e}\n"

    if fixed_lmax:
        mstm_config += "mie_epsilon\n"
        mstm_config += f"-{int(self.config.config['numerics']['lmax'])}\n"

    ref_idx = np.take(
        self.config.refractive_index_interpolated,
        self.config.spheres[:, 4].astype(int),
        axis=0,
    )
    for wl_idx, wl in enumerate(self.config.wavelength):
        mstm_config += "length_scale_factor\n"
        mstm_config += f"{2 * np.pi / (wl * self.config.wavelength_scale / self.config.particles_scale)}\n"
        mstm_config += "layer_ref_index\n"
        mstm_config += f"({self.config.medium_refractive_index[wl_idx].real}, {self.config.medium_refractive_index[wl_idx].imag})\n"
        mstm_config += "number_spheres\n"
        mstm_config += f"{self.config.spheres.shape[0]}\n"

        mstm_config += "sphere_data\n"
        for particle_idx in range(self.config.spheres.shape[0]):
            position_str = ",".join(
                [str(i) for i in self.config.spheres[particle_idx, :-1]]
            )
            ref_idx_str = f"({ref_idx[particle_idx, wl_idx].real}, {ref_idx[particle_idx, wl_idx].imag})"
            mstm_config += f"{position_str},{ref_idx_str}\n"

        mstm_config += "end_of_sphere_data\n"

        if wl_idx + 1 == self.config.wavelength.size:
            mstm_config += "end_of_options"
        else:
            mstm_config += "new_run\n"

    if save:
        with open(self.input_file, "w") as fh:
            fh.write(mstm_config)

    return mstm_config

__exec

Source code in yasfpy/benchmark/mstm4.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def __exec(self, runner: pyperf.Runner = None):
    if self.parallel == 1:
        command = [self.binary, self.input_file]
    elif self.parallel % 4 == 0:
        command = [
            "mpiexec",
            "-n",
            str(self.parallel),
            self.binary,
            self.input_file,
        ]
    else:
        raise Exception("parallel parameter needs to be divisble by 4!")

    if runner is None:
        os.system(" ".join(command))
    else:
        runner.bench_command(f"mstm4_exec_{self.parallel}", command)

__parse_input staticmethod

Source code in yasfpy/benchmark/mstm4.py
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
@staticmethod
def __parse_input(input):
    # Get id of run
    p = re.compile("\s+input variables for run\s+(\d+)")
    r = p.search(input)
    id = r.group(1)
    id = int(id)

    # lengths scale factor
    p = re.compile("\s+length, ref index scale factors\s+(.+?)\s+(.+?)\s+(.+?)")
    r = p.search(input)
    length_scale_factor = r.group(1)
    length_scale_factor = float(length_scale_factor)

    return dict(
        id=id,
        length_scale_factor=length_scale_factor,
    )

__parse_output staticmethod

Source code in yasfpy/benchmark/mstm4.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
@staticmethod
def __parse_output(results):
    # Get id of run
    p = re.compile(r"\s+calculation results for run\s+(\d+)")
    r = p.search(results)
    id = r.group(1)
    id = int(id)

    p = re.compile(r"sphere\s+Qext\s+Qabs\s+Qvabs\s+(.+)total", re.S)
    r = p.search(results)
    sphere_data = r.group(1)
    sphere_data = pd.read_csv(
        StringIO(sphere_data),
        names=["sphere", "Qext", "Qabs", "Qvabs"],
        header=None,
        sep="\s+",
    )

    p = re.compile(
        r"total extinction, absorption, scattering efficiencies \(unpol, par, perp incidence\)\s+(.+)"
    )
    r = p.search(results)
    efficiencies = r.group(1)
    efficiencies = [float(e) for e in efficiencies.split()]
    efficiencies = dict(
        q_ext_unp=efficiencies[0],
        q_abs_unp=efficiencies[1],
        q_sca_unp=efficiencies[2],
        q_ext_par=efficiencies[3],
        q_abs_par=efficiencies[4],
        q_sca_par=efficiencies[5],
        q_ext_per=efficiencies[6],
        q_abs_per=efficiencies[7],
        q_sca_per=efficiencies[8],
    )

    p = re.compile(
        r"down and up hemispherical scattering efficiencies \(unpol, par, perp\)\s+(.+)"
    )
    r = p.search(results)
    efficiencies_hem = r.group(1)
    efficiencies_hem = [float(e) for e in efficiencies_hem.split()]
    efficiencies_hem = dict(
        q_hem_u_unp=efficiencies_hem[0],
        q_hem_d_unp=efficiencies_hem[1],
        q_hem_u_par=efficiencies_hem[2],
        q_hem_d_par=efficiencies_hem[3],
        q_hem_u_per=efficiencies_hem[4],
        q_hem_d_per=efficiencies_hem[5],
    )

    p = re.compile(
        r"theta\s+11\s+12\s+13\s+14\s+21\s+22\s+23\s+24\s+31\s+32\s+33\s+34\s+41\s+42\s+43\s+44\s+(.+)",
        re.S,
    )
    r = p.search(results)
    scattering_matrix = r.group(1)
    scattering_matrix = pd.read_csv(
        StringIO(scattering_matrix),
        names=[
            "theta",
            "11",
            "12",
            "13",
            "14",
            "21",
            "22",
            "23",
            "24",
            "31",
            "32",
            "33",
            "34",
            "41",
            "42",
            "43",
            "44",
        ],
        header=None,
        sep="\s+",
    )

    return dict(
        id=id,
        sphere_data=sphere_data,
        efficiencies=efficiencies,
        efficiencies_hem=efficiencies_hem,
        scattering_matrix=scattering_matrix,
    )

__read

Source code in yasfpy/benchmark/mstm4.py
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
def __read(self):
    output = None
    with open(self.output_file, "r") as fh:
        output = fh.read()

    if output is None:
        raise Exception("Could not read file")

    section_divider = "****************************************************\n"
    sections = output.split(section_divider)
    sections = [
        s
        for s in sections
        if ("calculation results for run" in s) or ("input variables for run " in s)
    ]

    inputs = []
    results = []
    for s in sections:
        if "input variables for run " in s:
            inputs.append(MSTM4Manager.__parse_input(s))
        elif "calculation results for run" in s:
            results.append(MSTM4Manager.__parse_output(s))
    if (not results) or (not inputs):
        raise Exception("No results found in the file!")
    number_of_particles = results[0]["sphere_data"].shape[0]
    number_of_angles = results[0]["scattering_matrix"].shape[0]
    number_of_runs = len(inputs)
    sphere_data_header = list(results[0]["sphere_data"])
    scattering_matrix_header = list(results[0]["scattering_matrix"])

    mstm = dict(
        length_scale_factor=np.zeros(number_of_runs),
        sphere_data={
            key: np.zeros((number_of_particles, number_of_runs))
            for key in sphere_data_header
        },
        efficiencies=dict(
            q_ext_unp=np.zeros(number_of_runs),
            q_abs_unp=np.zeros(number_of_runs),
            q_sca_unp=np.zeros(number_of_runs),
            q_ext_par=np.zeros(number_of_runs),
            q_abs_par=np.zeros(number_of_runs),
            q_sca_par=np.zeros(number_of_runs),
            q_ext_per=np.zeros(number_of_runs),
            q_abs_per=np.zeros(number_of_runs),
            q_sca_per=np.zeros(number_of_runs),
        ),
        efficiencies_hem=dict(
            q_hem_u_unp=np.zeros(number_of_runs),
            q_hem_d_unp=np.zeros(number_of_runs),
            q_hem_u_par=np.zeros(number_of_runs),
            q_hem_d_par=np.zeros(number_of_runs),
            q_hem_u_per=np.zeros(number_of_runs),
            q_hem_d_per=np.zeros(number_of_runs),
        ),
        scattering_matrix={
            key: np.zeros((number_of_angles, number_of_runs))
            for key in scattering_matrix_header
        },
    )

    for i, r in zip(inputs, results):
        # inputs
        idx = i["id"] - 1
        mstm["length_scale_factor"][idx] = i["length_scale_factor"]

        # results
        idx = r["id"] - 1
        for key in sphere_data_header:
            mstm["sphere_data"][key][:, idx] = r["sphere_data"][key].to_numpy()
        for efficiency_type, value in r["efficiencies"].items():
            mstm["efficiencies"][efficiency_type][idx] = value
        for efficiency_type, value in r["efficiencies_hem"].items():
            mstm["efficiencies_hem"][efficiency_type][idx] = value
        for key in scattering_matrix_header:
            mstm["scattering_matrix"][key][:, idx] = r["scattering_matrix"][
                key
            ].to_numpy()

    self.output = mstm

clean

Source code in yasfpy/benchmark/mstm4.py
317
318
def clean(self):
    os.system(f"rm -f {self.input_file} {self.output_file} temp_pos.dat")

run

Source code in yasfpy/benchmark/mstm4.py
320
321
322
323
324
325
326
327
def run(self, runner: pyperf.Runner = None, cleanup: bool = True):
    self.__write()
    self.__exec(runner)
    if runner is None:
        self.__read()

    if cleanup:
        self.clean()

export

Source code in yasfpy/benchmark/mstm4.py
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
def export(self, file: str = None, cleanup: bool = False):
    if file is None:
        raise Exception("Please provide a filename")
    self.run(cleanup=False)
    mstm = copy.deepcopy(self.output)

    mstm["length_scale_factor"] = mstm["length_scale_factor"].tolist()
    for tag in [
        "sphere_data",
        "efficiencies",
        "efficiencies_hem",
        "scattering_matrix",
    ]:
        for key, value in mstm[tag].items():
            mstm[tag][key] = value.tolist()

    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