From 9c13ee1c65f12896dbe5c0138ef78bc4b00e6b8b Mon Sep 17 00:00:00 2001 From: schnetter <> Date: Wed, 5 Dec 2001 15:27:00 +0000 Subject: Prevent core dump when the specified output coordinate is not on the grid. darcs-hash:20011205152726-07bb3-5c72caf0c73a825d3aacf4249277ecfa06a56ee0.gz --- Carpet/CarpetIOASCII/src/ioascii.cc | 22 +++++++++++++++------- Carpet/CarpetIOASCII/src/ioascii.hh | 5 +++-- 2 files changed, 18 insertions(+), 9 deletions(-) (limited to 'Carpet/CarpetIOASCII/src') diff --git a/Carpet/CarpetIOASCII/src/ioascii.cc b/Carpet/CarpetIOASCII/src/ioascii.cc index d55852b40..b3749a61a 100644 --- a/Carpet/CarpetIOASCII/src/ioascii.cc +++ b/Carpet/CarpetIOASCII/src/ioascii.cc @@ -24,7 +24,7 @@ #include "ioascii.hh" -static const char* rcsid = "$Header: /home/eschnett/C/carpet/Carpet/Carpet/CarpetIOASCII/src/ioascii.cc,v 1.20 2001/11/05 17:53:02 schnetter Exp $"; +static const char* rcsid = "$Header: /home/eschnett/C/carpet/Carpet/Carpet/CarpetIOASCII/src/ioascii.cc,v 1.21 2001/12/05 16:27:26 schnetter Exp $"; @@ -444,7 +444,7 @@ int CarpetIOASCII assert (pcoord); const CCTK_REAL coord = *pcoord; assert (ptype == PARAMETER_REAL); - return CoordToOffset (cgh, dir, coord); + return CoordToOffset (cgh, dir, coord, 0); } // Second choice: explicit index @@ -468,7 +468,7 @@ int CarpetIOASCII assert (pcoord); const CCTK_REAL coord = *pcoord; assert (ptype == PARAMETER_REAL); - return CoordToOffset (cgh, dir, coord); + return CoordToOffset (cgh, dir, coord, 0); } // Fourth choice: explicit global index @@ -483,22 +483,30 @@ int CarpetIOASCII } // Fifth choice: default coordinate - return CoordToOffset (cgh, dir, cfallback); + return CoordToOffset (cgh, dir, cfallback, 0); } template int CarpetIOASCII -::CoordToOffset (const cGH* cgh, int dir, CCTK_REAL coord) +::CoordToOffset (const cGH* cgh, const int dir, const CCTK_REAL coord, + const int ifallback) { + assert (dir>=1 && dir<=dim); + CCTK_REAL lower, upper; CCTK_CoordRange (cgh, &lower, &upper, dir, 0, "cart3d"); const int npoints = cgh->cctk_gsh[dir-1]; const CCTK_REAL rindex = (coord - lower) / (upper - lower) * (npoints-1); - const int cindex = (int)floor(rindex + 0.5 + 1e-6); + int cindex = (int)floor(rindex + 0.5 + 1e-6); + + if (cindex<0 || cindex>=npoints) { + cindex = ifallback; + } + assert (cindex>=0 && cindex int ptype; const char* const* const ppval = (const char* const*)CCTK_ParameterGet (parametername, CCTK_THORNSTRING, - &ptype); + &ptype); assert (ppval); const char* const pval = *ppval; assert (ptype == PARAMETER_STRING); diff --git a/Carpet/CarpetIOASCII/src/ioascii.hh b/Carpet/CarpetIOASCII/src/ioascii.hh index 7bb8f3bb5..d46445fdd 100644 --- a/Carpet/CarpetIOASCII/src/ioascii.hh +++ b/Carpet/CarpetIOASCII/src/ioascii.hh @@ -1,4 +1,4 @@ -// $Header: /home/eschnett/C/carpet/Carpet/Carpet/CarpetIOASCII/src/ioascii.hh,v 1.9 2001/11/05 17:53:03 schnetter Exp $ +// $Header: /home/eschnett/C/carpet/Carpet/Carpet/CarpetIOASCII/src/ioascii.hh,v 1.10 2001/12/05 16:27:27 schnetter Exp $ #include @@ -57,7 +57,8 @@ struct CarpetIOASCII { const char* itempl, const char* iglobal, const char* ctempl, const char* cglobal, CCTK_REAL cfallback); - static int CoordToOffset (const cGH* cgh, int dir, CCTK_REAL coord); + static int CoordToOffset (const cGH* cgh, int dir, CCTK_REAL coord, + int ifallback); static const char* GetStringParameter (const char* parametertemplate, const char* fallback); -- cgit v1.2.3