Skip to content

Commit

Permalink
add option to read equation coefficients with wrong nconst
Browse files Browse the repository at this point in the history
Fixes #560.
  • Loading branch information
tukss committed Jun 23, 2024
1 parent ccf0d78 commit 884324b
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions post_processing/reference_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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:
Expand Down Expand Up @@ -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]
Expand All @@ -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)
Expand Down

0 comments on commit 884324b

Please sign in to comment.