Skip to content

Commit 354898e

Browse files
committed
added f-strings in polaristools and updated required python version to >=3.6
1 parent 934e2e7 commit 354898e

File tree

10 files changed

+103
-236
lines changed

10 files changed

+103
-236
lines changed

QUICKSTART.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The following packages are required for the installation:
2323

2424
- cmake (preferred), or ninja
2525

26-
- python3 (packages: *numpy*, *setuptools*)
26+
- Python version >= 3.6 (packages: *numpy*, *setuptools*)
2727

2828

2929
## Installation (Linux)

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ The following packages are required for the installation:
2626

2727
- cmake (preferred), or ninja
2828

29-
- python3 (packages: *numpy*, *setuptools*)
29+
- Python version >= 3.6 (packages: *numpy*, *setuptools*)
3030

3131

3232
## Installation (Linux)

manual.pdf

-13 Bytes
Binary file not shown.

quickstart.pdf

501 Bytes
Binary file not shown.

tools/polaris-gen.in

+11-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ from argparse import RawTextHelpFormatter, ArgumentParser
1717

1818
__author__ = "Robert Brauer"
1919
__license__ = "GPL"
20-
__version__ = "3.0"
20+
__version__ = "2.0.0"
2121
__maintainer__ = "Robert Brauer"
2222
__email__ = "[email protected]"
2323
__status__ = "Production"
@@ -136,7 +136,7 @@ class GridRoutines:
136136
from polaris_tools_modules.grid import Cylindrical
137137
grid = Cylindrical(self.model, self.path, self.parse_args)
138138
else:
139-
raise ValueError('Grid type ' + str(self.model.parameter['grid_type']) + ' not known!')
139+
raise ValueError(f'Grid type {self.model.parameter["grid_type"]} not known!')
140140
#: Root node
141141
root = grid.init_root()
142142
#: Temporary tree file
@@ -208,9 +208,9 @@ class GridRoutines:
208208
'tmp_' + self.parse_args.grid_filename)
209209

210210
else:
211-
raise ValueError('The number of gas masses ' +
212-
str(len(self.model.parameter['gas_mass'])) + ' does not fit with the numbers of densities ' +
213-
'get via density_distribution ' + str(np.shape(grid.total_gas_mass)))
211+
raise ValueError(
212+
f'The number of gas masses {len(self.model.parameter["gas_mass"])} '
213+
f'does not fit with the numbers of densities {np.shape(grid.total_gas_mass)}')
214214
else:
215215
# Rename the temporary grid file
216216
os.rename(self.path['model'] + 'tmp_' + self.parse_args.grid_filename,
@@ -219,15 +219,15 @@ class GridRoutines:
219219
if self.model.parameter['gas_mass'] is not None and not isinstance(self.model.parameter['gas_mass'], float):
220220
for i in range(len(self.model.parameter['gas_mass'])):
221221
for j in range(len(self.model.parameter['gas_mass'][i])):
222-
print('--- Total gas mass of density distribution ' + str(i + 1) +
223-
' and region ' + str(j + 1) + ':', '%02e M_sun ' % (grid.total_gas_mass[i][j] /
224-
self.math.const['M_sun']))
222+
print(
223+
f'--- Total gas mass of density distribution {i + 1} '
224+
f'and region {j + 1}: {grid.total_gas_mass[i][j] / self.math.const["M_sun"]:.02e} M_sun')
225225
if self.model.parameter['dust_mass'] is not None and not isinstance(self.model.parameter['dust_mass'], float):
226226
for i in range(len(self.model.parameter['dust_mass'])):
227227
for j in range(len(self.model.parameter['dust_mass'][i])):
228-
print('--- Total dust mass of density distribution ' + str(i + 1) +
229-
' and region ' + str(j + 1) + ':', '%02e M_sun ' % (grid.total_dust_mass[i][j] /
230-
self.math.const['M_sun']))
228+
print(
229+
f'--- Total dust mass of density distribution {i + 1} '
230+
f'and region {j + 1}: {grid.total_dust_mass[i][j] / self.math.const["M_sun"]:.02e} M_sun')
231231

232232
def convert_polaris_grid(self):
233233
"""convert existing ascii grid file to binary grid file or vice versa.

tools/polaris_tools_custom/model.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ def update_parameter(self, extra_parameter):
127127

128128
for i, param in enumerate(extra_parameter[::2]):
129129
if param not in self.custom_parameter_list:
130-
print(' - Warning: invalid parameter: ' + str(param))
130+
print(f' - Warning: invalid parameter: {param}')
131131
continue
132132
try:
133133
self.parameter[param] = float(eval(extra_parameter[2*i+1]))
134134
except:
135135
self.parameter[param] = extra_parameter[2*i+1]
136-
print(' - Info: ' + str(param) + ' updated to ' + str(self.parameter[param]))
136+
print(f' - Info: {param} updated to {self.parameter[param]}')
137137

138138
def gas_density_distribution(self):
139139
"""Calculates the gas density at a given position.

tools/polaris_tools_modules/grid.py

+27-34
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ def __init__(self, model, path, parse_args):
7272
(self.data.dust_size_param() is not None)
7373
if self.data.get_dust_id() is not None:
7474
if self.nr_gas_densities > 1 or self.nr_dust_densities > 1:
75-
raise ValueError(
76-
'Multiple density distributions and dust_id function defined')
75+
raise ValueError('Multiple density distributions and dust_id function defined')
7776
else:
7877
self.data_length += 1
7978

@@ -115,34 +114,36 @@ def check_density_arrays(self):
115114
not isinstance(self.data.get_gas_density_distribution(), (float, int)):
116115
if len(self.data.get_gas_density_distribution()[0]) != \
117116
len(self.model.parameter['gas_mass'][0]):
118-
raise ValueError("gas_density_distribution does not provide the same array than "
119-
"defined in self.parameter['gas_mass']")
117+
raise ValueError(
118+
'gas_density_distribution does not provide the same array than '
119+
'defined in self.parameter[\'gas_mass\']')
120120
elif self.nr_gas_densities > 1:
121121
for i_gas_dens in range(self.nr_gas_densities):
122122
if len(self.data.get_gas_density_distribution()[i_gas_dens]) != \
123123
len(self.model.parameter['gas_mass'][i_gas_dens]):
124-
raise ValueError("gas_density_distribution does not provide the same array than "
125-
"defined in self.parameter['gas_mass']")
124+
raise ValueError(
125+
'gas_density_distribution does not provide the same array than '
126+
'defined in self.parameter[\'gas_mass\']')
126127
except:
127-
raise ValueError(
128-
"the gas_density function and the defined gas_mass do not fit!")
128+
raise ValueError('the gas_density function and the defined gas_mass do not fit!')
129129
try:
130130
if self.nr_dust_densities == 1:
131131
if self.data.get_dust_density_distribution() is not None and \
132132
not isinstance(self.data.get_dust_density_distribution(), (float, int)):
133133
if len(self.data.get_dust_density_distribution()[0]) != \
134134
len(self.model.parameter['dust_mass'][0]):
135-
raise ValueError("dust_density_distribution does not provide the same array than "
136-
"defined in self.parameter['dust_mass']")
135+
raise ValueError(
136+
'dust_density_distribution does not provide the same array than '
137+
'defined in self.parameter[\'dust_mass\']')
137138
elif self.nr_dust_densities > 1:
138139
for i_dust_dens in range(self.nr_dust_densities):
139140
if len(self.data.get_dust_density_distribution()[i_dust_dens]) != \
140141
len(self.model.parameter['dust_mass'][i_dust_dens]):
141-
raise ValueError("dust_density_distribution does not provide the same array than "
142-
"defined in self.parameter['dust_mass']")
142+
raise ValueError(
143+
'dust_density_distribution does not provide the same array than '
144+
'defined in self.parameter[\'dust_mass\']')
143145
except:
144-
raise ValueError(
145-
"the dust_density function and the defined dust_mass do not fit!")
146+
raise ValueError('the dust_density function and the defined dust_mass do not fit!')
146147

147148
def write_header(self, grid_file, grid_type='', num_dens=False, root=None):
148149
"""Writes general header to binary file.
@@ -192,7 +193,7 @@ def write_header(self, grid_file, grid_type='', num_dens=False, root=None):
192193
elif grid_type == 'cylindrical':
193194
grid_id = 40
194195
else:
195-
raise ValueError('Grid type: ' + str(grid_type) + 'is not known!')
196+
raise ValueError(f'Grid type: {grid_type} is not known!')
196197

197198
grid_file.write(struct.pack('H', grid_id))
198199
grid_file.write(struct.pack('H', self.data_length))
@@ -248,8 +249,7 @@ def write_header(self, grid_file, grid_type='', num_dens=False, root=None):
248249
grid_file.write(struct.pack('H', root.parameter['is_leaf']))
249250
grid_file.write(struct.pack('H', root.parameter['level']))
250251
else:
251-
raise ValueError(
252-
'root node has to be defined for writing the grid header!')
252+
raise ValueError('root node has to be defined for writing the grid header!')
253253

254254
def write_node_data(self, grid_file, node, data_type='f', cell_IDs=None, rewrite=False):
255255
"""Write data of each node.
@@ -269,8 +269,7 @@ def write_node_data(self, grid_file, node, data_type='f', cell_IDs=None, rewrite
269269
elif data_type == 'd':
270270
data_type_length = 8
271271
else:
272-
raise ValueError(
273-
'Do not understand the data type ' + data_type + ' in grid.py!')
272+
raise ValueError(f'Do not understand the data type {data_type} in grid.py!')
274273

275274
if rewrite:
276275
grid_file.seek(-(self.data_length * data_type_length), 1)
@@ -347,8 +346,7 @@ def read_write_node_data(self, tmp_file, grid_file, data_type='f'):
347346
elif data_type == 'd':
348347
data_type_length = 8
349348
else:
350-
raise ValueError(
351-
'Do not understand the data type ' + data_type + ' in grid.py!')
349+
raise ValueError(f'Do not understand the data type {data_type} in grid.py!')
352350

353351
# Calculate normalized density
354352
for i_gas_dens in range(self.nr_gas_densities):
@@ -441,7 +439,7 @@ def create_grid(self, grid_file, node, max_tree_level=None, refinement_limit=0.1
441439

442440
percentage = 100 * percentage_count / 64
443441
if percentage - last_percentage > 10:
444-
stdout.write(f'--- Generate cartesian grid: ' + str(int(percentage)) + ' % \r')
442+
stdout.write(f'--- Generate cartesian grid: {int(percentage)} % \r')
445443
stdout.flush()
446444
last_percentage = percentage
447445

@@ -706,8 +704,7 @@ def create_grid(self, grid_file, root):
706704
if sp_param['sf_r'] == 0:
707705
if sp_param['radius_list'][0] != sp_param['inner_radius'] or \
708706
sp_param['radius_list'][-1] != sp_param['outer_radius']:
709-
raise ValueError(
710-
'radius_list does not agree with the inner and outer grid borders!')
707+
raise ValueError('radius_list does not agree with the inner and outer grid borders!')
711708
radius_list = sp_param['radius_list']
712709
sp_param['n_r'] = len(radius_list) - 1
713710
elif sp_param['sf_r'] == 1:
@@ -782,7 +779,7 @@ def create_grid(self, grid_file, root):
782779
for i_t in range(sp_param['n_th']):
783780
percentage = 100 * i_node / nr_cells
784781
if percentage - last_percentage > 10:
785-
stdout.write(f'--- Generate spherical grid: ' + str(int(percentage)) + ' % \r')
782+
stdout.write(f'--- Generate spherical grid: {int(percentage)} % \r')
786783
stdout.flush()
787784
last_percentage = percentage
788785
# Calculate the cell midpoint in spherical coordinates
@@ -984,8 +981,7 @@ def create_grid(self, grid_file, root):
984981
if cy_param['sf_r'] == 0:
985982
if cy_param['radius_list'][0] != cy_param['inner_radius'] or \
986983
cy_param['radius_list'][-1] != cy_param['outer_radius']:
987-
raise ValueError(
988-
'radius_list does not agree with the inner and outer grid borders!')
984+
raise ValueError('radius_list does not agree with the inner and outer grid borders!')
989985
radius_list = cy_param['radius_list']
990986
cy_param['n_r'] = len(radius_list) - 1
991987
elif cy_param['sf_r'] == 1:
@@ -1021,8 +1017,7 @@ def create_grid(self, grid_file, root):
10211017
for i_r in range(cy_param['n_r'])])
10221018
cy_param['n_ph'] = [len(phi_list[0])-1] * cy_param['n_r']
10231019
else:
1024-
raise ValueError(
1025-
'Cell distriution in phi-direction not understood!')
1020+
raise ValueError('Cell distriution in phi-direction not understood!')
10261021
elif cy_param['sf_ph'] == -1:
10271022
phi_list = [self.math.lin_list(0., 2. * np.pi, n_ph)
10281023
for n_ph in cy_param['n_ph']]
@@ -1035,14 +1030,12 @@ def create_grid(self, grid_file, root):
10351030
if len(cy_param['z_list']) > 0:
10361031
if cy_param['z_list'][0] != -cy_param['z_max'] or \
10371032
cy_param['z_list'][-1] != cy_param['z_max']:
1038-
raise ValueError(
1039-
'z_list does not agree with the inner and outer grid borders!')
1033+
raise ValueError('z_list does not agree with the inner and outer grid borders!')
10401034
z_list = np.array([cy_param['z_list']
10411035
for i_r in range(cy_param['n_r'])])
10421036
cy_param['n_z'] = len(z_list[0]) - 1
10431037
else:
1044-
raise ValueError(
1045-
'Cell distribution in z-direction not understood!')
1038+
raise ValueError('Cell distribution in z-direction not understood!')
10461039
elif cy_param['sf_z'] == -1:
10471040
z_max_tmp = [self.model.get_dz(
10481041
radius_list[i_r]) * cy_param['n_z'] / 2. for i_r in range(cy_param['n_r'])]
@@ -1101,7 +1094,7 @@ def create_grid(self, grid_file, root):
11011094
for i_z in range(cy_param['n_z']):
11021095
percentage = 100 * i_node / nr_cells
11031096
if percentage - last_percentage > 10:
1104-
stdout.write('--- Generate cylindrical grid: ' + str(int(percentage)) + ' % \r')
1097+
stdout.write(f'--- Generate cylindrical grid: {int(percentage)} % \r')
11051098
stdout.flush()
11061099
last_percentage = percentage
11071100
# Calculate the cell midpoint in cylindrical coordinates

0 commit comments

Comments
 (0)