From 86fe38fd94358d20b002cebb20dfe7a4e66943cb Mon Sep 17 00:00:00 2001 From: Philipp Edelmann Date: Sun, 23 Jun 2024 13:39:57 -0600 Subject: [PATCH 1/4] simplify radius handling in equation_coefficients class --- post_processing/reference_tools.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/post_processing/reference_tools.py b/post_processing/reference_tools.py index cb55f4fe..7ce8c5c2 100644 --- a/post_processing/reference_tools.py +++ b/post_processing/reference_tools.py @@ -17,8 +17,7 @@ def __init__(self,radius=[], file=None): if (len(radius) != 0): nr = len(radius) self.nr = nr - self.radius = numpy.zeros(nr,dtype='float64') - self.radius[:] = radius[:] + self.radius = numpy.asarray(radius, dtype='float64') self.functions = numpy.zeros((self.nfunc,nr) , dtype='float64' ) self.constants = numpy.zeros(self.nconst , dtype='float64' ) From f9b0d47176c78b6f2169792c9dc2e53d22d8b7ea Mon Sep 17 00:00:00 2001 From: Philipp Edelmann Date: Sun, 23 Jun 2024 13:46:10 -0600 Subject: [PATCH 2/4] raise an error if radius and file are provided to equation_coefficients --- post_processing/reference_tools.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/post_processing/reference_tools.py b/post_processing/reference_tools.py index 7ce8c5c2..813d937a 100644 --- a/post_processing/reference_tools.py +++ b/post_processing/reference_tools.py @@ -15,6 +15,8 @@ class equation_coefficients: def __init__(self,radius=[], file=None): if (len(radius) != 0): + if file is not None: + raise RuntimeError("Cannot provide radius and file at the same time.") nr = len(radius) self.nr = nr self.radius = numpy.asarray(radius, dtype='float64') From ccf0d783d3f203cc356c7d20754c6dbbfe710201 Mon Sep 17 00:00:00 2001 From: Philipp Edelmann Date: Sun, 23 Jun 2024 13:49:13 -0600 Subject: [PATCH 3/4] pythonify if statements in reference_tools --- post_processing/reference_tools.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/post_processing/reference_tools.py b/post_processing/reference_tools.py index 813d937a..8fd34839 100644 --- a/post_processing/reference_tools.py +++ b/post_processing/reference_tools.py @@ -15,6 +15,7 @@ class equation_coefficients: def __init__(self,radius=[], file=None): if (len(radius) != 0): + if len(radius) != 0: if file is not None: raise RuntimeError("Cannot provide radius and file at the same time.") nr = len(radius) @@ -25,8 +26,8 @@ def __init__(self,radius=[], file=None): self.constants = numpy.zeros(self.nconst , dtype='float64' ) self.cset = numpy.zeros(self.nconst , dtype='int32' ) self.fset = numpy.zeros(self.nfunc , dtype='int32' ) - elif (file != None): self.read(filename=file) + elif file is not None: def __getattr__(self, name): if name in self.f_dict: @@ -46,7 +47,7 @@ def __setattr__(self, name, value): def set_function(self,y,f_name): - if (isinstance(f_name,str)): + if isinstance(f_name,str): fi = self.f_dict[f_name] else: fi = f_name @@ -55,7 +56,7 @@ def set_function(self,y,f_name): self.fset[fi-1] = 1 def set_constant(self,c,c_name): - if (isinstance(c_name,str)): + if isinstance(c_name,str): ci = self.c_dict[c_name] else: ci = c_name @@ -137,7 +138,7 @@ def __init__(self, radius, pressure = None, temperature = None, print(' Returning empty data structure.') # Initialize the class attributes - if (passed_rcheck): + if passed_rcheck: self.nr = nr self.radius = radius @@ -148,7 +149,7 @@ def __init__(self, radius, pressure = None, temperature = None, self.set_variable(k,v) def set_variable(self,k,v): - if (k != 'self' and k != 'radius'): + if k != 'self' and k != 'radius': attr_consistent = False try: nv = len(v) @@ -161,11 +162,11 @@ def set_variable(self,k,v): print('The',k,'attribute will not be initialized.') except: - if (v != None): + if v != None: print('Error: ', k, 'must be a numpy array or list.') print('The',k,'attribute will not be initialized.') - if (attr_consistent): + if attr_consistent: setattr(self,k,v) # sets self.k = v def compute_heating_profile(hpars, radius, htype=0, pressure = 0): @@ -187,7 +188,7 @@ def compute_heating_profile(hpars, radius, htype=0, pressure = 0): to have a constant value of unity. htype > 0: Currently there are no other types defined...""" - if (htype == 0): + if htype == 0: nr= len(radius) r0 = hpars[0] width = hpars[1] From 884324b4f9ddf1ae6741b11be0b5fbf818f40b7a Mon Sep 17 00:00:00 2001 From: Philipp Edelmann Date: Sun, 23 Jun 2024 14:07:01 -0600 Subject: [PATCH 4/4] add option to read equation coefficients with wrong nconst Fixes #560. --- post_processing/reference_tools.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/post_processing/reference_tools.py b/post_processing/reference_tools.py index 8fd34839..7c192b10 100644 --- a/post_processing/reference_tools.py +++ b/post_processing/reference_tools.py @@ -13,8 +13,20 @@ class equation_coefficients: 'eta':7, 'd_ln_rho':8, 'd2_ln_rho':9, 'd_ln_T':10, 'd_ln_nu':11, 'd_ln_kappa':12, 'd_ln_eta':13, 'ds_dr':14} - def __init__(self,radius=[], file=None): - if (len(radius) != 0): + def __init__(self, radius=[], file=None, override_nconst=False): + """Reads/writes equation_coefficient and custom reference state files. + + Keyword arguments: + radius: If provided, a new state will be created with these + radii and zero coefficients. + file: If provided, the reference state will be read from the + provided file. + override_nconst: Fixes file reading for equation_coefficients created + between versions that already had 11 constants but + had not updated the version number to 2 yet. + See https://github.com/geodynamics/Rayleigh/issues/560 + for details. + """ if len(radius) != 0: if file is not None: raise RuntimeError("Cannot provide radius and file at the same time.") @@ -26,8 +38,8 @@ def __init__(self,radius=[], file=None): self.constants = numpy.zeros(self.nconst , dtype='float64' ) self.cset = numpy.zeros(self.nconst , dtype='int32' ) self.fset = numpy.zeros(self.nfunc , dtype='int32' ) - self.read(filename=file) elif file is not None: + self.read(filename=file, override_nconst=override_nconst) def __getattr__(self, name): if name in self.f_dict: @@ -82,7 +94,7 @@ def write(self, filename='ecoefs.dat'): self.functions.tofile(fd) fd.close() - def read(self, filename='equation_coefficients'): + def read(self, filename='equation_coefficients', override_nconst=False): class_version = self.version fd = open(filename,'rb') picheck = numpy.fromfile(fd,dtype='int32',count=1)[0] @@ -91,8 +103,14 @@ def read(self, filename='equation_coefficients'): if self.version > 1: self.nconst = numpy.fromfile(fd,dtype='int32', count=1)[0] self.nfunc = numpy.fromfile(fd,dtype='int32', count=1)[0] - else: # if the version is 1, nconst was 10 - self.nconst = 10 + else: + if override_nconst: + # Override for the versions of Rayleigh that already had 11 constants + # but had not updated the version number of the file yet. + self.nconst = 11 + else: + # if the version is 1, nconst was 10 + self.nconst = 10 self.cset = numpy.fromfile(fd,dtype='int32',count=self.nconst) self.fset = numpy.fromfile(fd,dtype='int32',count=self.nfunc) self.constants = numpy.fromfile(fd,dtype='float64',count=self.nconst)