Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
update_vlasiator_cfg_variables.py
Go to the documentation of this file.
1import numpy as np
2import sys,os
3import shutil
4
5def updatecfg(infile, verbose=False):
6
7 replacecategories = {
8 'backstream': 'thermal',
9 }
10
11 delete = ['fluxb', 'fluxe', 'populations_rholossvelboundary', 'rholossvelboundary', 'vole']
12
13 replace = {
14 # First deal with some ancient pre-multipop variables
15 'rho': 'populations_vg_rho',
16 'rhov': 'populations_vg_v',
17 'blocks': 'populations_vg_blocks',
18 'ptensor': 'populations_vg_ptensor',
19 'rhobackstream': 'populations_vg_moments_nonthermal',
20 'rhononbackstream': 'populations_vg_moments_thermal',
21 'rhovbackstream': 'populations_vg_moments_nonthermal',
22 'rhovnonbackstream': 'populations_vg_moments_thermal',
23 'ptensorbackstream': 'populations_vg_moments_nonthermal',
24 'ptensornonbackstream': 'populations_vg_moments_thermal',
25 'accsubcycles': 'populations_vg_acceleration_subcycles',
26 'minvalue': 'populations_vg_effectivesparsitythreshold',
27 # fields
28 'b': 'fg_b',
29 'fg_backgroundb': 'fg_b_background',
30 'backgroundb': 'fg_b_background',
31 'perturbedb': 'fg_b_perturbed',
32 'fg_perturbedb': 'fg_b_perturbed',
33 'e': 'fg_e',
34 'rhom': 'vg_rhom',
35 'rhoq': 'vg_rhoq',
36 'v': 'vg_v',
37 # per-population
38 'populations_rho': 'populations_vg_rho',
39 'populations_v': 'populations_vg_v',
40 'populations_blocks': 'populations_vg_blocks',
41 'populations_ptensor': 'populations_vg_ptensor',
42 'populations_moments_nonbackstream': 'populations_vg_moments_thermal',
43 'populations_moments_thermal': 'populations_vg_moments_thermal',
44 'populations_moments_backstream': 'populations_vg_moments_nonthermal',
45 'populations_moments_nonthermal': 'populations_vg_moments_nonthermal',
46 'populations_minvalue': 'populations_vg_effectivesparsitythreshold',
47 'populations_effectivesparsitythreshold': 'populations_vg_effectivesparsitythreshold',
48 'populations_rholossadjust': 'populations_vg_rho_loss_adjust',
49 'populations_rhomlossadjust': 'populations_vg_rho_loss_adjust',
50 'populations_rho_loss_adjust': 'populations_vg_rho_loss_adjust',
51 'populations_energydensity': 'populations_vg_energydensity',
52 'populations_precipitationflux': 'populations_vg_precipitationdifferentialflux',
53 'populations_precipitationdifferentialflux': 'populations_vg_precipitationdifferentialflux',
54 'populations_maxvdt': 'populations_vg_maxdt_acceleration',
55 'populations_maxdt_acceleration': 'populations_vg_maxdt_acceleration',
56 'populations_maxrdt': 'populations_vg_maxdt_translation',
57 'populations_maxdt_translation': 'populations_vg_maxdt_translation',
58 'populations_accsubcycles': 'populations_vg_acceleration_subcycles',
59 'populations_acceleration_subcycles': 'populations_vg_acceleration_subcycles',
60 'velocitysubsteps': 'populations_vg_acceleration_subcycles',
61 # Other variables
62 'maxvdt': 'vg_maxdt_acceleration',
63 'maxrdt': 'vg_maxdt_translation',
64 'maxfieldsdt': 'fg_maxdt_fieldsolver',
65 'fg_maxfieldsdt': 'fg_maxdt_fieldsolver',
66 'mpirank': 'vg_rank',
67 'fsgridrank': 'fg_rank',
68 'lbweight': 'vg_loadbalance_weight',
69 'vg_lbweight': 'vg_loadbalance_weight',
70 'vg_loadbalanceweight': 'vg_loadbalance_weight',
71 'boundarytype': 'vg_boundarytype',
72 'fsgridboundarytype': 'fg_boundarytype',
73 'boundarylayer': 'vg_boundarylayer',
74 'fsgridboundarylayer': 'fg_boundarylayer',
75 'gridcoordinates': 'vg_gridcoordinates',
76 'fsaved': 'vg_f_saved',
77 'vg_fsaved': 'vg_f_saved',
78 'halle': 'fg_e_hall',
79 'fg_halle': 'fg_e_hall',
80 'gradpee': 'vg_e_gradpe',
81 'e_gradpe': 'vg_e_gradpe',
82 'volb': 'vg_b_vol',
83 'vg_volb': 'vg_b_vol',
84 'b_vol': 'vg_b_vol',
85 'bvol': 'vg_b_vol',
86 'vg_bvol': 'vg_b_vol',
87 'fg_volb': 'fg_b_vol',
88 'fg_bvol': 'fg_b_vol',
89 'backgroundvolb': 'vg_b_background_vol',
90 'perturbedvolb': 'vg_b_perturbed_vol',
91 'pressure': 'vg_pressure',
92 'bvolderivs': 'vg_b_vol_derivatives',
93 'derivs': 'vg_b_vol_derivatives',
94 'b_vol_derivs': 'vg_b_vol_derivatives',
95 'b_vol_derivatives': 'vg_b_vol_derivatives',
96 # Does make unlisted (accepted) variables lowercase, as Vlasiator-5 is case-insensitive in output variable names
97 }
98
99 oldinfile = infile+'_old'
100 outfile = infile+'_new'
101
102 inf = open(infile, 'r')
103 outf = open(outfile, 'w')
104
105 for line in inf:
106 sline = line.strip()
107 columns = sline.split()
108 passdirect = True
109 if len(columns)>=3:
110 if ((columns[0] == 'output') or (columns[0] == '#output')) and columns[1] == '=':
111 if columns[2].lower() in replace:
112 genline = columns[0]+' = '+replace[columns[2].lower()]+'\n'
113 outf.write(genline)
114 if verbose: print('Replaced '+line[:-1]+"\n with "+genline[:-1])
115 passdirect = False
116 elif columns[2].lower() in delete:
117 if verbose: print('Removed '+line[:-1])
118 passdirect = False
119 else:
120 if verbose: print('Passed '+line[:-1])
121 pass
122 elif ((columns[0] == 'diagnostic') or (columns[0] == '#diagnostic')) and columns[1] == '=':
123 if columns[2].lower() in replace:
124 genline = columns[0]+' = '+replace[columns[2].lower()]+'\n'
125 if verbose: print('Replaced '+line[:-1]+"\n with "+genline[:-1])
126 outf.write(genline)
127 passdirect = False
128 elif columns[2].lower() in delete:
129 if verbose: print('Removed '+line[:-1])
130 passdirect = False
131 else:
132 if verbose: print('Passed '+line[:-1])
133 pass
134
135 elif len(columns)==1:
136 if columns[0][0]=='[':
137 for category in replacecategories:
138 lencat = len(category)
139 if category == columns[0][-1-lencat:-1]:
140 genline = columns[0][:-1-lencat]+replacecategories[category]+']\n'
141 if verbose: print('Replaced '+line[:-1]+"\n with "+genline[:-1])
142 outf.write(genline)
143 passdirect = False
144
145 if passdirect:
146 outf.write(line)
147
148 inf.close()
149 outf.flush()
150 outf.close()
151
152 # Backup the old config file, and replace it with the new one
153 shutil.copy2(infile,oldinfile)
154 shutil.move(outfile,infile)
155
156
157if __name__== "__main__":
158 if len(sys.argv)!=2:
159 print("Usage: python update_vlasiator_cfg_variables.py full_path_to_config_file.cfg [--verbose]")
160 else:
161 verbose = False
162 infile = sys.argv[1]
163 if not os.path.exists(infile):
164 print("Error locating config file "+infile)
165 exit
166 if len(sys.argv) > 1:
167 if sys.argv=="--verbose":
168 verbose=True
169 updatecfg(infile, verbose=verbose)