Skip to content

Commit

Permalink
output version numbers if there is a mismatch in SA.
Browse files Browse the repository at this point in the history
  • Loading branch information
hannorein committed Jun 28, 2024
1 parent 198527f commit 9598345
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
14 changes: 11 additions & 3 deletions rebound/simulationarchive.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class Simulationarchive(Structure):
_fields_ = [("_inf", c_void_p),
("_filename", c_char_p),
("version", c_int),
("_reb_version_major", c_int),
("_reb_version_minor", c_int),
("_reb_version_patch", c_int),
("auto_interval", c_double),
("auto_walltime", c_double),
("auto_step", c_uint64),
Expand All @@ -52,7 +55,7 @@ class Simulationarchive(Structure):
("t", POINTER(c_double))
]
def __repr__(self):
return '<{0}.{1} object at {2}, nblobs={3}>'.format(self.__module__, type(self).__name__, hex(id(self)), self.nblobs)
return '<{0}.{1} object at {2}, nblobs={3}, reb_version={4}.{5}.{6}>'.format(self.__module__, type(self).__name__, hex(id(self)), self.nblobs, self._reb_version_major, reb_version_minor, reb_version_patch)

def __init__(self,filename,setup=None, setup_args=(), process_warnings=True, reuse_index=None):
"""
Expand Down Expand Up @@ -91,6 +94,11 @@ def __init__(self,filename,setup=None, setup_args=(), process_warnings=True, reu
raise RuntimeError(message)
else:
# Just a warning
if value==2: # Version warning. Append version used to save SA to message
sa_version = "%d.%d.%d" %(self._reb_version_major, self._reb_version_minor, self._reb_version_patch)
if sa_version != __version__ and sa_version != "0.0.0":
message += " Binary file was saved with REBOUND Version " + sa_version + "."
message += " You are currently using REBOUND Version " + __version__ + "."
if process_warnings:
warnings.warn(message, RuntimeWarning)
if not process_warnings:
Expand All @@ -109,7 +117,7 @@ def __str__(self):
"""
Returns a string with details of this simulationarchive.
"""
return "<rebound.Simulationarchive instance, snapshots={0} >".format(str(len(self)))
return '<{0}.{1} object at {2}, nblobs={3}, reb_version={4}.{5}.{6}>'.format(self.__module__, type(self).__name__, hex(id(self)), self.nblobs, self._reb_version_major, self._reb_version_minor, self._reb_version_patch)

def __getitem__(self, key):
PY3 = sys.version_info[0] == 3
Expand Down Expand Up @@ -352,4 +360,4 @@ def getBezierPaths(self,origin=None):
return verts, codes

from .simulation import Simulation, BINARY_WARNINGS
from . import clibrebound
from . import clibrebound, __version__
7 changes: 5 additions & 2 deletions src/rebound.h
Original file line number Diff line number Diff line change
Expand Up @@ -892,10 +892,13 @@ struct reb_simulationarchive{
FILE* inf; // File pointer (will be kept open)
char* filename; // Filename of open file. This is NULL if this is a memory-mapped file (using fmemopen)
int version; // Simulationarchive version
int reb_version_major; // Major REBOUND Version used to save SA
int reb_version_minor; // Minor REBOUND Version used to save SA
int reb_version_patch; // Patch REBOUND Version used to save SA
double auto_interval; // Interval setting used to create SA (if used)
double auto_walltime; // Walltime setting used to create SA (if used)
uint64_t auto_step; // Steps in-between SA snapshots (if used)
int64_t nblobs; // Total number of snapshots (including initial binary)
uint64_t auto_step; // Steps in-between SA snapshots (if used)
int64_t nblobs; // Total number of snapshots (including initial binary)
uint64_t* offset; // Index of offsets in file (length nblobs)
double* t; // Index of simulation times in file (length nblobs)
};
Expand Down
15 changes: 10 additions & 5 deletions src/simulationarchive.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ void reb_read_simulationarchive_from_stream_with_messages(struct reb_simulationa
struct reb_binary_field field = {0};
sa->version = 0;
double t0 = 0;
int version_major = 0; // will be overwritten unless there is an error
int version_minor = 0;
sa->reb_version_major = 0;
sa->reb_version_minor = 0;
sa->reb_version_patch = 0;
int uses32bitoffsets = 1;
// Cache descriptors
struct reb_binary_field_descriptor fd_header = reb_binary_field_descriptor_for_name("header");
Expand Down Expand Up @@ -163,15 +164,19 @@ void reb_read_simulationarchive_from_stream_with_messages(struct reb_simulationa
if (debug) printf("SA Error. Cannot determine version.\n");
*warnings |= REB_SIMULATION_BINARY_WARNING_CORRUPTFILE;
}else{
char cpatch[64];
char cminor[64];
char cmajor[64];
strncpy(cpatch, readbuf+c3+1, 3);
cminor[4] = '\0';
strncpy(cminor, readbuf+c2+1, c3-c2-1);
cminor[c3-c2-1] = '\0';
strncpy(cmajor, readbuf+c1+1, c2-c1-1);
cmajor[c2-c1-1] = '\0';
version_minor = atoi(cminor);
version_major = atoi(cmajor);
if (version_major <= 3 && version_minor < 18){
sa->reb_version_patch = atoi(cpatch);
sa->reb_version_minor = atoi(cminor);
sa->reb_version_major = atoi(cmajor);
if (sa->reb_version_major <= 3 && sa->reb_version_minor < 18){
uses32bitoffsets = 0; // fallback to 16 bit
}
}
Expand Down

0 comments on commit 9598345

Please sign in to comment.