|
| 1 | +from itertools import chain |
| 2 | +from typing import TypedDict |
| 3 | +from warnings import warn |
| 4 | + |
| 5 | +from boutdata.data import BoutOptionsFile |
| 6 | + |
| 7 | +from .bout_v5_input_file_upgrader import add_parser_general, run_general |
| 8 | + |
| 9 | + |
| 10 | +class Replacement(TypedDict): |
| 11 | + old: str |
| 12 | + new: str |
| 13 | + |
| 14 | + |
| 15 | +REPLACEMENTS = [] |
| 16 | +DELETED = [] |
| 17 | + |
| 18 | +NEW_NAMES: dict[str, str | dict[str, list[str]]] = { |
| 19 | + "collisions": { |
| 20 | + "braginskii_collisions": [ |
| 21 | + "electron_electron", |
| 22 | + "electron_ion", |
| 23 | + "electron_neutral", |
| 24 | + "ion_ion", |
| 25 | + "ion_neutral", |
| 26 | + "neutral_neutral", |
| 27 | + "ei_multiplier", |
| 28 | + "diagnose", |
| 29 | + ], |
| 30 | + "braginskii_friction": ["frictional_heating", "diagnose"], |
| 31 | + "braginskii_heat_exchange": ["diagnose"], |
| 32 | + }, |
| 33 | + "electron_viscosity": "braginskii_electron_viscosity", |
| 34 | + "ion_viscosity": "braginskii_ion_viscosity", |
| 35 | + "thermal_force": "braginskii_thermal_force", |
| 36 | +} |
| 37 | + |
| 38 | +# If name in `components` and type not given then change the name in components and heading; if a multi-replacement, duplicate the section |
| 39 | +# If type is same as name, duplicate section |
| 40 | +# If name in type of any component, change it there (don't duplicate) |
| 41 | + |
| 42 | + |
| 43 | +def split_list_string(list_string: str) -> tuple[list[str], bool]: |
| 44 | + result = [tname.strip() for tname in list_string.split(",")] |
| 45 | + open_paren = result[0][0] == "(" |
| 46 | + close_paren = result[-1][-1] == ")" |
| 47 | + if open_paren != close_paren: |
| 48 | + warn(f'Unmatched parentheses around "{list_string}"') |
| 49 | + if open_paren: |
| 50 | + result[0] = result[0][1:] |
| 51 | + if close_paren: |
| 52 | + result[-1] = result[-1][:-1] |
| 53 | + return result, open_paren and close_paren |
| 54 | + |
| 55 | + |
| 56 | +def rename_simple_component( |
| 57 | + options_file: BoutOptionsFile, section_name: str |
| 58 | +) -> list[str]: |
| 59 | + """Rename a component when its type is the same as its name.""" |
| 60 | + if section_name in NEW_NAMES: |
| 61 | + new_names = NEW_NAMES[section_name] |
| 62 | + if isinstance(new_names, dict): |
| 63 | + old_section = options_file.pop(section_name) |
| 64 | + new_components = [] |
| 65 | + for new_name, configs in new_names.items(): |
| 66 | + new_components.append(new_name) |
| 67 | + new_section = options_file.getSection(new_name) |
| 68 | + for conf in configs: |
| 69 | + if conf in old_section: |
| 70 | + new_section[conf] = old_section[conf] |
| 71 | + return new_components |
| 72 | + else: |
| 73 | + options_file.rename(section_name, new_names) |
| 74 | + return [new_names] |
| 75 | + return [section_name] |
| 76 | + |
| 77 | + |
| 78 | +def update_component_names(options_file: BoutOptionsFile) -> None: |
| 79 | + """Change the names of closure-related components to reflect the refactor""" |
| 80 | + has_collisions = False |
| 81 | + recycling_component = "" |
| 82 | + old_components, has_parens = split_list_string(options_file["hermes:components"]) |
| 83 | + new_components = [] |
| 84 | + for section_name in old_components: |
| 85 | + section = options_file.getSection(section_name) |
| 86 | + # Component type is set explicitly |
| 87 | + if "type" in section: |
| 88 | + old_types, types_have_parens = split_list_string(section["type"]) |
| 89 | + # If component name and type match, treat similarly to if no type were given (see below) |
| 90 | + if len(old_types) == 1 and old_types[0] == section_name: |
| 91 | + has_collisions = has_collisions or section_name == "collisions" |
| 92 | + new_types = rename_simple_component(options_file, section_name) |
| 93 | + for t in new_types: |
| 94 | + options_file[f"{t}:type"] = ( |
| 95 | + ("(" if types_have_parens else "") |
| 96 | + + t |
| 97 | + + (")" if types_have_parens else "") |
| 98 | + ) |
| 99 | + new_components.extend(new_types) |
| 100 | + # Otherwise simply replace any type-names that need changing |
| 101 | + else: |
| 102 | + has_collisions = has_collisions or section_name in old_types |
| 103 | + new_types = list( |
| 104 | + chain.from_iterable( |
| 105 | + [nt] if isinstance((nt := NEW_NAMES.get(t, t)), str) else nt |
| 106 | + for t in old_types |
| 107 | + ) |
| 108 | + ) |
| 109 | + if new_types != old_types: |
| 110 | + section["type"] = ( |
| 111 | + ("(" if types_have_parens else "") |
| 112 | + + ", ".join(new_types) |
| 113 | + + (")" if types_have_parens else "") |
| 114 | + ) |
| 115 | + new_components.append(section_name) |
| 116 | + # Component type is same as component name |
| 117 | + else: |
| 118 | + has_collisions = has_collisions or section_name == "collisions" |
| 119 | + new_types = rename_simple_component(options_file, section_name) |
| 120 | + new_components.extend(new_types) |
| 121 | + if "recycling" in new_types: |
| 122 | + recycling_component = section_name |
| 123 | + # Add braginskii_conduction to the end of the list of components |
| 124 | + if has_collisions: |
| 125 | + new_components.append("braginskii_conduction") |
| 126 | + # Make sure recycling is evaluated after conduction |
| 127 | + if recycling_component != "": |
| 128 | + new_components.remove(recycling_component) |
| 129 | + new_components.append(recycling_component) |
| 130 | + if new_components != old_components: |
| 131 | + options_file["hermes:components"] = ( |
| 132 | + ("(" if has_parens else "") |
| 133 | + + ", ".join(new_components) |
| 134 | + + (")" if has_parens else "") |
| 135 | + ) |
| 136 | + |
| 137 | + |
| 138 | +def run(args) -> None: |
| 139 | + run_general( |
| 140 | + REPLACEMENTS, DELETED, args, additional_modifications=update_component_names |
| 141 | + ) |
| 142 | + |
| 143 | + |
| 144 | +def add_parser(subcommand, default_args, files_args): |
| 145 | + return add_parser_general(subcommand, default_args, files_args, run, "Hermes-3") |
0 commit comments