Skip to content

Mstm3

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

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

MSTM3Manager

Source code in yasfpy/benchmark/mstm3.py
22
23
24
25
26
27
28
29
30
31
32
33
def __init__(
    self,
    config_path: str = None,
    binary: str = None,
    input_file: str = "mstm3.inp",
    output_file: str = "mstm3.dat",
):
    self.config = Config(config_path)
    self.binary = binary

    self.input_file = input_file
    self.output_file = output_file

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

__write

Source code in yasfpy/benchmark/mstm3.py
 35
 36
 37
 38
 39
 40
 41
 42
 43
 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
103
104
105
106
107
108
109
110
111
112
113
114
115
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 += "write_sphere_data\n"
    mstm_config += "1\n"
    mstm_config += "calculate_scattering_coefficients\n"
    mstm_config += "1\n"
    # mstm_config += "scattering_coefficient_file\n"
    # mstm_config += "mstm3.coef\n"
    mstm_config += "incident_or_target_frame\n"
    mstm_config += "0\n"
    mstm_config += "min_scattering_angle_deg\n"
    mstm_config += "0.0d0\n"
    mstm_config += "max_scattering_angle_deg\n"
    mstm_config += "180.d0\n"
    mstm_config += "min_scattering_plane_angle_deg\n"
    mstm_config += "0.0d0\n"
    mstm_config += "max_scattering_plane_angle_deg\n"
    mstm_config += "360.0d0\n"
    mstm_config += "delta_scattering_angle_deg\n"
    mstm_config += "30\n"
    mstm_config += "normalize_scattering_matrix\n"
    mstm_config += "0\n"
    mstm_config += "incident_polar_angle_deg\n"
    mstm_config += f"{self.config.config['initial_field']['polar_angle']:e}\n"
    mstm_config += "incident_azimuth_angle_deg\n"
    mstm_config += f"{self.config.config['initial_field']['azimuthal_angle']:e}\n"
    mstm_config += "real_ref_index_scale_factor\n"
    mstm_config += "1.0d0\n"
    mstm_config += "imag_ref_index_scale_factor\n"
    mstm_config += "1.0d0\n"
    mstm_config += "real_chiral_factor\n"
    mstm_config += "0.0d0\n"
    mstm_config += "imag_chiral_factor\n"
    mstm_config += "0.0d0\n"
    mstm_config += "medium_real_chiral_factor\n"
    mstm_config += "0.d0\n"
    mstm_config += "medium_imag_chiral_factor\n"
    mstm_config += "0.d0\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):e}\n"
        mstm_config += "medium_real_ref_index\n"
        mstm_config += f"{self.config.medium_refractive_index[wl_idx].real}\n"
        mstm_config += "medium_imag_ref_index\n"
        mstm_config += f"{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_sizes_and_positions\n"
        for particle_idx in range(self.config.spheres.shape[0]):
            position_str = ",".join(
                [str(i) for i in self.config.spheres[particle_idx, [3, 0, 1, 2]]]
            )
            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"

        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/mstm3.py
117
118
119
120
121
122
def __exec(self, runner: pyperf.Runner = None):
    command = [self.binary, self.input_file]
    if runner is None:
        os.system(" ".join(command))
    else:
        runner.bench_command("mstm3_exec", command)

__parse_output staticmethod

Source code in yasfpy/benchmark/mstm3.py
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
@staticmethod
def __parse_output(results):
    p = re.compile(r"input parameters for run number\s+(\d+)")
    r = p.search(results)
    id = r.group(1)
    id = int(id)

    p = re.compile(r"length, ref. indx. scale factors:\s+(.+?)\s+(.+?)\s+(.+?)")
    r = p.search(results)
    length_scale_factor = r.group(1)
    length_scale_factor = float(length_scale_factor)

    p = re.compile(
        r"sphere host\s+ka\s+x-x\(host\)\s+y-y\(host\)\s+z-z\(host\)\s+Re\(m\)\s+Im\(m\)\s+ Qext\s+Qsca\s+Qabs\s+Qabs\(V\)(.+)unpolarized",
        re.S,
    )
    r = p.search(results)
    sphere_data = r.group(1)
    sphere_data = pd.read_csv(
        StringIO(sphere_data),
        names=[
            "sphere host",
            "ka",
            "x-x(host)",
            "y-y(host)",
            "z-z(host)",
            "Re(m)",
            "Im(m)",
            "Qext",
            "Qsca",
            "Qabs",
            "Qabs(V)",
        ],
        header=None,
        sep="\s+",
    )

    efficiencies = {}

    p = re.compile(
        r"unpolarized total ext, abs, scat efficiencies, w.r.t. xv, and asym. parm\s+(.+)"
    )
    r = p.search(results)
    efficiencies_unp = r.group(1)
    efficiencies_unp = [float(e) for e in efficiencies_unp.split()]
    efficiencies["q_ext_unp"] = efficiencies_unp[0]
    efficiencies["q_abs_unp"] = efficiencies_unp[1]
    efficiencies["q_sca_unp"] = efficiencies_unp[2]

    p = re.compile(r"parallel total ext, abs, scat efficiencies\s+(.+)")
    r = p.search(results)
    efficiencies_par = r.group(1)
    efficiencies_par = [float(e) for e in efficiencies_par.split()]
    efficiencies["q_ext_par"] = efficiencies_par[0]
    efficiencies["q_abs_par"] = efficiencies_par[1]
    efficiencies["q_sca_par"] = efficiencies_par[2]

    p = re.compile(r"perpendicular total ext, abs, scat efficiencies\s+(.+)")
    r = p.search(results)
    efficiencies_per = r.group(1)
    efficiencies_per = [float(e) for e in efficiencies_per.split()]
    efficiencies["q_ext_per"] = efficiencies_per[0]
    efficiencies["q_abs_per"] = efficiencies_per[1]
    efficiencies["q_sca_per"] = efficiencies_per[2]

    p = re.compile(
        r"theta\s+phi\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",
            "phi",
            "11",
            "12",
            "13",
            "14",
            "21",
            "22",
            "23",
            "24",
            "31",
            "32",
            "33",
            "34",
            "41",
            "42",
            "43",
            "44",
        ],
        header=None,
        sep="\s+",
    )

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

__read

Source code in yasfpy/benchmark/mstm3.py
229
230
231
232
233
234
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
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)

    results = []
    for s in sections:
        results.append(MSTM3Manager.__parse_output(s))
    if not results:
        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(results)
    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=[None] * 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),
        ),
        # scattering_matrix=[None] * number_of_runs,
        scattering_matrix={
            key: np.zeros((number_of_angles, number_of_runs))
            for key in scattering_matrix_header
        },
    )
    for r in results:
        idx = r["id"] - 1
        mstm["length_scale_factor"][idx] = r["length_scale_factor"]
        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 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/mstm3.py
289
290
def clean(self):
    os.system(f"rm -f {self.input_file} {self.output_file} mstm3.coef")

run

Source code in yasfpy/benchmark/mstm3.py
292
293
294
295
296
297
298
299
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/mstm3.py
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
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", "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