Skip to content

comparison

Python tool for comparing MFILE and outputting differences. The tool does not work for MFiles that are not the result of a full PROCESS run (ie if an error or exception occured).

DICT_DESCRIPTIONS = get_dicts()['DICT_DESCRIPTIONS'] module-attribute

DEFAULT_COMPARE_PARAMS = ['rmajor', 'rminor', 'aspect', 'kappa', 'kappa95', 'triang', 'triang95', 'f_nd_impurity_electrons(01', 'f_nd_impurity_electrons(02', 'f_nd_impurity_electrons(03', 'f_nd_impurity_electrons(04', 'f_nd_impurity_electrons(05', 'f_nd_impurity_electrons(06', 'f_nd_impurity_electrons(07', 'f_nd_impurity_electrons(08', 'f_nd_impurity_electrons(09', 'f_nd_impurity_electrons(10', 'f_nd_impurity_electrons(11', 'f_nd_impurity_electrons(12', 'f_nd_impurity_electrons(13', 'f_nd_impurity_electrons(14', 'a_plasma_surface', 'vol_plasma', 'n_tf_coils', 'dr_shld_inboard', 'dr_shld_outboard', 'dr_blkt_inboard', 'dr_blkt_outboard', 'p_fusion_total_mw', 'plasma_current_MA', 'b_plasma_toroidal_on_axis', 'q95', 'beta_poloidal_vol_avg', 'temp_plasma_electron_vol_avg_kev', 'nd_plasma_electrons_vol_avg', 'hfact', 'vs_cs_pf_total_pulse', 'b_plasma_toroidal_on_axis', 'b_tf_inboard_peak_with_ripple', 'tmarg', 'iooic', 'sig_tf_case', 'sig_tf_wp', 'sig_tf_case_max', 'sig_tf_wp_max', 'p_plant_electric_gross_mw', 'p_coolant_pump_elec_total_mw', 'p_plant_electric_net_mw', 'pflux_fw_neutron_mw', 'f_nd_alpha_electron', 'p_plasma_inner_rad_mw', 'p_plasma_rad_mw', 'p_blkt_nuclear_heat_total_mw', 'p_shld_nuclear_heat_mw', 'p_plasma_separatrix_mw', 'p_hcd_primary_extra_heat_mw', 'f_c_plasma_bootstrap', 'f_c_plasma_auxiliary', 'f_c_plasma_inductive', 'gamnb', 'e_beam_kev', 'p_plasma_loss_mw'] module-attribute

BASELINE_LIST = ['procver', 'time', 'username', 'tagno', 'commsg', 'ifail', 'rmajor', 'rminor', 'aspect', 'kappa', 'kappa95', 'triang', 'triang95', 'a_plasma_surface', 'vol_plasma', 'n_tf_coils', 'p_fusion_total_mw', 'plasma_current_MA', 'b_plasma_toroidal_on_axis', 'q95', 'beta_total_vol_avg', 'normalised_thermal_beta', 'beta_norm_total', 'thermal_beta', 'thermal_poloidal_beta', 'temp_plasma_electron_vol_avg_kev', 'temp_plasma_electron_on_axis_kev', 'nd_plasma_electrons_vol_avg', 'nd_plasma_electron_on_axis', 'dnla_gw', 'temp_plasma_separatrix_kev', 'nd_plasma_separatrix_electron', 'temp_plasma_pedestal_kev', 'nd_plasma_pedestal_electron', 'n_charge_plasma_effective_vol_avg', 'nd_plasma_impurities_vol_avg', 't_energy_confinement', 'hfact', 'tauelaw', 'f_nd_alpha_electron', 'pflux_fw_neutron_mw', 'p_plasma_inner_rad_mw', 'p_plasma_sync_mw', 'p_plasma_rad_mw', 'p_blkt_nuclear_heat_total_mw', 'p_shld_nuclear_heat_mw', 'p_plasma_separatrix_mw', 'life_div_fpy', 'p_plant_primary_heat_mw', 'dr_bore', 'dr_cs', 'dr_cs_precomp', 'dr_cs_tf_gap', 'dr_tf_inboard', 'deltf', 'dr_shld_thermal_inboard', 'dr_shld_thermal_outboard', 'dz_shld_thermal', 'dr_shld_vv_gap_inboard', 'dr_vv_inboard', 'dr_vv_outboard', 'dz_vv_upper', 'dz_vv_lower', 'dr_shld_inboard', 'dr_shld_blkt_gap', 'dr_blkt_inboard', 'dr_fw_inboard', 'dr_fw_plasma_gap_inboard', 'dr_fw_plasma_gap_outboard', 'dr_fw_outboard', 'dr_blkt_outboard', 'dr_shld_outboard', 'dr_shld_vv_gap_outboard', 'dr_tf_shld_gap', 'dr_tf_outboard', 'eta_turbine', 'p_plant_electric_gross_mw', 'p_plant_electric_net_mw', 'p_hcd_injected_total_mw', 'p_hcd_primary_extra_heat_mw', 'f_c_plasma_bootstrap', 'f_c_plasma_auxiliary', 'f_c_plasma_inductive', 'gamnb', 'e_beam_kev', 'p_plasma_loss_mw', 'p_plasma_separatrix_mw', 'vssoft', 'vs_cs_pf_total_pulse', 't_plant_pulse_burn', 'b_tf_inboard_peak_symmetric', 'iooic', 'tmarg', 'tftmp', 'qtarget', 'qtargetcomplete', 'totalpowerlost'] module-attribute

BLANKET_COMPARE_PARAMS = ['dr_blkt_inboard', 'dr_blkt_outboard', 'p_fusion_total_mw', 'p_blkt_nuclear_heat_total_mw', 'p_fw_nuclear_heat_total_mw', 'p_tf_nuclear_heat_mw', 'p_shld_nuclear_heat_mw', 'p_div_nuclear_heat_total_mw', 'tbr', 'f_blkt_li6_enrichment', 'a_fw_total', 'f_p_blkt_multiplication'] module-attribute

GENERIC_LIST = ['rmajor', 'rminor', 'aspect', 'kappa', 'kappa95', 'triang', 'triang95', 'p_fusion_total_mw', 'plasma_current_MA', 'b_plasma_toroidal_on_axis', 'q95', 'beta_total_vol_avg', 'temp_plasma_electron_vol_avg_kev', 'nd_plasma_electrons_vol_avg', 'p_hcd_injected_total_mw', 'p_plant_electric_net_mw', 'pflux_fw_neutron_mw', 'pflux_fw_rad_mw', 'f_nd_alpha_electron', 'p_plasma_inner_rad_mw', 'p_plasma_rad_mw', 'f_c_plasma_bootstrap', 'pdivmax_over_rmajor', 'f_nd_impurity_electrons(14', 'eta_turbine', 'capcost', 'coe', 'dr_bore', 'dr_cs', 'dr_cs_precomp', 'dr_cs_tf_gap', 'dr_tf_inboard', 'dr_tf_shld_gap', 'dr_shld_thermal_inboard', 'dr_shld_thermal_outboard', 'dz_shld_thermal', 'dr_shld_vv_gap_inboard', 'dr_vv_inboard', 'dr_shld_inboard', 'dr_shld_blkt_gap', 'dr_blkt_inboard', 'dr_fw_inboard', 'dr_fw_plasma_gap_inboard', 'dr_fw_plasma_gap_outboard', 'dr_fw_outboard', 'dr_blkt_outboard', 'dr_shld_outboard', 'dr_vv_outboard', 'dr_shld_vv_gap_outboard', 'dr_tf_shld_gap', 'dr_tf_outboard', 'dz_xpoint_divertor', 'dz_divertor', 'dz_vv_lower', 'dz_shld_lower', 'dz_shld_vv_gap'] module-attribute

comparison_dict = {'defaults': DEFAULT_COMPARE_PARAMS, 'baseline': BASELINE_LIST, 'blanket': BLANKET_COMPARE_PARAMS, 'generic': GENERIC_LIST} module-attribute

BColors

Source code in process/core/io/mfile/comparison.py
265
266
267
268
269
270
271
class BColors:
    HEADER = "\033[95m"
    OKBLUE = "\033[94m"
    OKGREEN = "\033[92m"
    WARNING = "\033[93m"
    FAIL = "\033[91m"
    ENDC = "\033[0m"

HEADER = '\x1b[95m' class-attribute instance-attribute

OKBLUE = '\x1b[94m' class-attribute instance-attribute

OKGREEN = '\x1b[92m' class-attribute instance-attribute

WARNING = '\x1b[93m' class-attribute instance-attribute

FAIL = '\x1b[91m' class-attribute instance-attribute

ENDC = '\x1b[0m' class-attribute instance-attribute

compare_mfiles(files, comparison, acc=5, save=False, verbose=False)

Main function for comparing MFILEs

Parameters:

Name Type Description Default
arg

List of arguments

required
Source code in process/core/io/mfile/comparison.py
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
316
317
318
319
320
321
322
323
324
325
326
327
328
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
def compare_mfiles(
    files: Iterable[Path],
    comparison: Literal["defaults", "baseline", "blanket", "generic", "all"],
    acc: float = 5,
    save: bool = False,
    verbose: bool = False,
):
    """Main function for comparing MFILEs

    Parameters
    ----------
    arg :
        List of arguments
    """
    print_counter = 0
    n = 2
    mfile_list = []
    for item in files:
        mfile = MFile(filename=item)
        if mfile.get("error_status", scan=-1) == 3:
            raise RuntimeError(
                f"{item} is an MFile from a PROCESS run that did not converge"
                " and instead results from an error during the run"
            )

        mfile_list.append(mfile)

    var_list, missing_vars, diff_list, within_list = [], [], [], []

    key_list = mfile_list[0].data.keys()
    for var in key_list:
        store = True
        for f in mfile_list:
            if var not in f.data:
                store = False
                missing_vars.append(var)

        if store:
            var_list.append(var)

    if comparison != "all":
        var_list = comparison_dict[comparison]

    dts = get_dicts()
    for v in var_list:
        if "normres" in v:
            continue

        values = np.zeros(n)

        if v not in dts["DICT_VAR_TYPE"]:
            try:
                eval(mfile_list[0].get(v, scan=-1))
            except NameError:
                pass
            except TypeError:
                for m in range(len(mfile_list)):
                    values[m] = mfile_list[m].get(v, scan=-1)
            except SyntaxError:
                pass

        elif (
            dts["DICT_VAR_TYPE"][v] == "real_variable"
            or dts["DICT_VAR_TYPE"][v] == "int_variable"
        ):
            for m in range(len(mfile_list)):
                values[m] = mfile_list[m].data[v].get_scan(-1)

        norm_vals = []
        if values[0] != 0 and isfinite(values[0]):
            norm_vals = values / values[0]

        if len(norm_vals) >= 1:
            key = v.strip(".").strip(" ")
            des = dts["DICT_DESCRIPTIONS"].get(key, "-")
            a: list[bool] = norm_vals >= 1.0 + acc / 100.0
            b: list[bool] = norm_vals <= 1.0 - acc / 100.0
            rounded = round((norm_vals[1] - 1) * 100.0, 2)
            vals = f"{values[0]}\t{values[1]}"
            if a[1] or b[1]:
                diff_list.append(v)
                rv = f"{BColors.FAIL}{rounded} %"
            else:
                within_list.append(v)
                rv = f"{rounded} %"

            wline = "\t".join([v, f"{des}\t" if a[1] else des, vals, f"{rounded} %"])
            if a[1] or b[1] or verbose:
                print("\t".join([f"{BColors.ENDC}{v}", des, vals, rv]))
                print_counter += 1
                if save:
                    with open("comp.txt", "a") as ofile:
                        ofile.write(wline + "\n")

    print(BColors.ENDC)
    if comparison == "baseline" and acc >= 10.0:
        if print_counter == 0:
            sys.exit(0)
        else:
            sys.exit(f"Differences in baseline output by more than {acc}%")