Commit 90ff54c4 authored by Richard Berger's avatar Richard Berger
Browse files

Ensure all library functions capture exceptions

parent 04eadb63
Loading
Loading
Loading
Loading
+317 −231
Original line number Original line Diff line number Diff line
@@ -38,16 +38,67 @@


using namespace LAMMPS_NS;
using namespace LAMMPS_NS;


/* ----------------------------------------------------------------------
   Utility macros for optional code path which captures all exceptions
   and stores the last error message. These assume there is a variable lmp
   which is a pointer to the current LAMMPS instance.

   Usage:

   BEGIN_CAPTURE
   {
     // code paths which might throw exception
     ...
   }
   END_CAPTURE
------------------------------------------------------------------------- */

#ifdef LAMMPS_EXCEPTIONS
#define BEGIN_CAPTURE \
  Error * error = lmp->error; \
  try

#define END_CAPTURE \
  catch(LAMMPSAbortException & ae) { \
    int nprocs = 0; \
    MPI_Comm_size(ae.universe, &nprocs ); \
    \
    if (nprocs > 1) { \
      error->set_last_error(ae.message.c_str(), ERROR_ABORT); \
    } else { \
      error->set_last_error(ae.message.c_str(), ERROR_NORMAL); \
    } \
  } catch(LAMMPSException & e) { \
    error->set_last_error(e.message.c_str(), ERROR_NORMAL); \
  }
#else
#define BEGIN_CAPTURE
#define END_CAPTURE
#endif


/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
   create an instance of LAMMPS and return pointer to it
   create an instance of LAMMPS and return pointer to it
   pass in command-line args and MPI communicator to run on
   pass in command-line args and MPI communicator to run on
------------------------------------------------------------------------- */
------------------------------------------------------------------------- */


void lammps_open(int argc, char **argv, MPI_Comm communicator, void **ptr)
void lammps_open(int argc, char **argv, MPI_Comm communicator, void **ptr)
{
#ifdef LAMMPS_EXCEPTIONS
  try
  {
  {
    LAMMPS *lmp = new LAMMPS(argc,argv,communicator);
    LAMMPS *lmp = new LAMMPS(argc,argv,communicator);
    *ptr = (void *) lmp;
    *ptr = (void *) lmp;
  }
  }
  catch(LAMMPSException & e) {
    fprintf(stderr, "LAMMPS Exception: %s", e.message.c_str());
    *ptr = (void*) NULL;
  }
#else
  LAMMPS *lmp = new LAMMPS(argc,argv,communicator);
  *ptr = (void *) lmp;
#endif
}


/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
   create an instance of LAMMPS and return pointer to it
   create an instance of LAMMPS and return pointer to it
@@ -68,9 +119,21 @@ void lammps_open_no_mpi(int argc, char **argv, void **ptr)


  MPI_Comm communicator = MPI_COMM_WORLD;
  MPI_Comm communicator = MPI_COMM_WORLD;


#ifdef LAMMPS_EXCEPTIONS
  try
  {
    LAMMPS *lmp = new LAMMPS(argc,argv,communicator);
    LAMMPS *lmp = new LAMMPS(argc,argv,communicator);
    *ptr = (void *) lmp;
    *ptr = (void *) lmp;
  }
  }
  catch(LAMMPSException & e) {
    fprintf(stderr, "LAMMPS Exception: %s", e.message.c_str());
    *ptr = (void*) NULL;
  }
#else
  LAMMPS *lmp = new LAMMPS(argc,argv,communicator);
  *ptr = (void *) lmp;
#endif
}


/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
   destruct an instance of LAMMPS
   destruct an instance of LAMMPS
@@ -99,8 +162,13 @@ int lammps_version(void *ptr)
void lammps_file(void *ptr, char *str)
void lammps_file(void *ptr, char *str)
{
{
  LAMMPS *lmp = (LAMMPS *) ptr;
  LAMMPS *lmp = (LAMMPS *) ptr;

  BEGIN_CAPTURE
  {
    lmp->input->file(str);
    lmp->input->file(str);
  }
  }
  END_CAPTURE
}


/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
   process a single input command in str
   process a single input command in str
@@ -109,29 +177,15 @@ void lammps_file(void *ptr, char *str)
char *lammps_command(void *ptr, char *str)
char *lammps_command(void *ptr, char *str)
{
{
  LAMMPS *lmp = (LAMMPS *) ptr;
  LAMMPS *lmp = (LAMMPS *) ptr;
  char * result = NULL;


#ifdef LAMMPS_EXCEPTIONS
  BEGIN_CAPTURE
  Error * error = lmp->error;
  {

    result = lmp->input->one(str);
  try {
    return lmp->input->one(str);
  } catch(LAMMPSAbortException & ae) {
    int nprocs = 0;
    MPI_Comm_size(ae.universe, &nprocs );

    if (nprocs > 1) {
      error->set_last_error(ae.message.c_str(), ERROR_ABORT);
    } else {
      error->set_last_error(ae.message.c_str(), ERROR_NORMAL);
    }
    return NULL;
  } catch(LAMMPSException & e) {
    error->set_last_error(e.message.c_str(), ERROR_NORMAL);
    return NULL;
  }
  }
#else
  END_CAPTURE
  return lmp->input->one(str);

#endif
  return result;
}
}


/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
@@ -230,6 +284,8 @@ void *lammps_extract_compute(void *ptr, char *id, int style, int type)
{
{
  LAMMPS *lmp = (LAMMPS *) ptr;
  LAMMPS *lmp = (LAMMPS *) ptr;


  BEGIN_CAPTURE
  {
    int icompute = lmp->modify->find_compute(id);
    int icompute = lmp->modify->find_compute(id);
    if (icompute < 0) return NULL;
    if (icompute < 0) return NULL;
    Compute *compute = lmp->modify->compute[icompute];
    Compute *compute = lmp->modify->compute[icompute];
@@ -282,6 +338,8 @@ void *lammps_extract_compute(void *ptr, char *id, int style, int type)
        return (void *) compute->array_local;
        return (void *) compute->array_local;
      }
      }
    }
    }
  }
  END_CAPTURE


  return NULL;
  return NULL;
}
}
@@ -315,6 +373,8 @@ void *lammps_extract_fix(void *ptr, char *id, int style, int type,
{
{
  LAMMPS *lmp = (LAMMPS *) ptr;
  LAMMPS *lmp = (LAMMPS *) ptr;


  BEGIN_CAPTURE
  {
    int ifix = lmp->modify->find_fix(id);
    int ifix = lmp->modify->find_fix(id);
    if (ifix < 0) return NULL;
    if (ifix < 0) return NULL;
    Fix *fix = lmp->modify->fix[ifix];
    Fix *fix = lmp->modify->fix[ifix];
@@ -349,6 +409,8 @@ void *lammps_extract_fix(void *ptr, char *id, int style, int type,
      if (type == 1) return (void *) fix->vector_local;
      if (type == 1) return (void *) fix->vector_local;
      if (type == 2) return (void *) fix->array_local;
      if (type == 2) return (void *) fix->array_local;
    }
    }
  }
  END_CAPTURE


  return NULL;
  return NULL;
}
}
@@ -384,6 +446,8 @@ void *lammps_extract_variable(void *ptr, char *name, char *group)
{
{
  LAMMPS *lmp = (LAMMPS *) ptr;
  LAMMPS *lmp = (LAMMPS *) ptr;


  BEGIN_CAPTURE
  {
    int ivar = lmp->input->variable->find(name);
    int ivar = lmp->input->variable->find(name);
    if (ivar < 0) return NULL;
    if (ivar < 0) return NULL;


@@ -401,6 +465,8 @@ void *lammps_extract_variable(void *ptr, char *name, char *group)
      lmp->input->variable->compute_atom(ivar,igroup,vector,1,0);
      lmp->input->variable->compute_atom(ivar,igroup,vector,1,0);
      return (void *) vector;
      return (void *) vector;
    }
    }
  }
  END_CAPTURE


  return NULL;
  return NULL;
}
}
@@ -414,7 +480,14 @@ void *lammps_extract_variable(void *ptr, char *name, char *group)
int lammps_set_variable(void *ptr, char *name, char *str)
int lammps_set_variable(void *ptr, char *name, char *str)
{
{
  LAMMPS *lmp = (LAMMPS *) ptr;
  LAMMPS *lmp = (LAMMPS *) ptr;
  int err = lmp->input->variable->set_string(name,str);
  int err = -1;

  BEGIN_CAPTURE
  {
    err = lmp->input->variable->set_string(name,str);
  }
  END_CAPTURE

  return err;
  return err;
}
}


@@ -429,9 +502,14 @@ int lammps_set_variable(void *ptr, char *name, char *str)
double lammps_get_thermo(void *ptr, char *name)
double lammps_get_thermo(void *ptr, char *name)
{
{
  LAMMPS *lmp = (LAMMPS *) ptr;
  LAMMPS *lmp = (LAMMPS *) ptr;
  double dval;
  double dval = 0.0;


  BEGIN_CAPTURE
  {
    lmp->output->thermo->evaluate_keyword(name,&dval);
    lmp->output->thermo->evaluate_keyword(name,&dval);
  }
  END_CAPTURE

  return dval;
  return dval;
}
}


@@ -464,6 +542,8 @@ void lammps_gather_atoms(void *ptr, char *name,
{
{
  LAMMPS *lmp = (LAMMPS *) ptr;
  LAMMPS *lmp = (LAMMPS *) ptr;


  BEGIN_CAPTURE
  {
    // error if tags are not defined or not consecutive
    // error if tags are not defined or not consecutive


    int flag = 0;
    int flag = 0;
@@ -538,6 +618,8 @@ void lammps_gather_atoms(void *ptr, char *name,
      lmp->memory->destroy(copy);
      lmp->memory->destroy(copy);
    }
    }
  }
  }
  END_CAPTURE
}


/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------
   scatter the named atom-based entity across all processors
   scatter the named atom-based entity across all processors
@@ -553,6 +635,8 @@ void lammps_scatter_atoms(void *ptr, char *name,
{
{
  LAMMPS *lmp = (LAMMPS *) ptr;
  LAMMPS *lmp = (LAMMPS *) ptr;


  BEGIN_CAPTURE
  {
    // error if tags are not defined or not consecutive or no atom map
    // error if tags are not defined or not consecutive or no atom map


    int flag = 0;
    int flag = 0;
@@ -615,6 +699,8 @@ void lammps_scatter_atoms(void *ptr, char *name,
      }
      }
    }
    }
  }
  }
  END_CAPTURE
}


#ifdef LAMMPS_EXCEPTIONS
#ifdef LAMMPS_EXCEPTIONS
/* ----------------------------------------------------------------------
/* ----------------------------------------------------------------------