diff -Naur lammps-24Jan10/src/error.cpp lammps-24Jan10-patched/src/error.cpp --- lammps-24Jan10/src/error.cpp 2007-10-03 16:22:30.000000000 +0000 +++ lammps-24Jan10-patched/src/error.cpp 2010-01-30 18:40:17.000000000 +0000 @@ -22,14 +22,20 @@ /* ---------------------------------------------------------------------- */ -Error::Error(LAMMPS *lmp) : Pointers(lmp) {} +Error::Error(LAMMPS *lmp, int library) : Pointers(lmp) { + noexit = library; +} + +Error::Error(LAMMPS *lmp) : Pointers(lmp) { + noexit = 0; +} /* ---------------------------------------------------------------------- called by all procs in universe close all output, screen, and log files in world and universe ------------------------------------------------------------------------- */ -void Error::universe_all(const char *str) +void Error::universe_all(const char *str) throw (LAMMPSexception) { MPI_Barrier(universe->uworld); @@ -38,6 +44,11 @@ if (universe->ulogfile) fprintf(universe->ulogfile,"ERROR: %s\n",str); } + if (noexit) { + last_error_msg = str; + throw LAMMPSexception(str); + } + if (output) delete output; if (universe->nworlds > 1) { if (screen && screen != stdout) fclose(screen); @@ -65,7 +76,7 @@ close all output, screen, and log files in world ------------------------------------------------------------------------- */ -void Error::all(const char *str) +void Error::all(const char *str) throw (LAMMPSexception) { MPI_Barrier(world); @@ -77,6 +88,11 @@ if (logfile) fprintf(logfile,"ERROR: %s\n",str); } + if (noexit) { + last_error_msg = str; + throw LAMMPSexception(str); + } + if (output) delete output; if (screen && screen != stdout) fclose(screen); if (logfile) fclose(logfile); @@ -110,3 +126,23 @@ { if (screen) fprintf(screen,"WARNING: %s\n",str); } + +/* ---------------------------------------------------------------------- + called only when the library interface is used + used to clear the error status before a library call +------------------------------------------------------------------------- */ + +void Error::clear() +{ + last_error_msg = NULL; +} + +/* ---------------------------------------------------------------------- + called only when the library interface is used + returns the last error trapped, if any +------------------------------------------------------------------------- */ + +const char *Error::last_error() +{ + return last_error_msg; +} diff -Naur lammps-24Jan10/src/error.h lammps-24Jan10-patched/src/error.h --- lammps-24Jan10/src/error.h 2010-01-12 01:37:48.000000000 +0000 +++ lammps-24Jan10-patched/src/error.h 2010-01-30 18:40:28.000000000 +0000 @@ -15,19 +15,27 @@ #define LMP_ERROR_H #include "pointers.h" +#include "exception.h" namespace LAMMPS_NS { class Error : protected Pointers { public: + Error(class LAMMPS *, int); Error(class LAMMPS *); - void universe_all(const char *); + void universe_all(const char *) throw (LAMMPSexception); void universe_one(const char *); - void all(const char *); + void all(const char *) throw (LAMMPSexception); void one(const char *); void warning(const char *); + + void clear(); + const char *last_error(); + private: + int noexit; + const char *last_error_msg; }; } diff -Naur lammps-24Jan10/src/exception.cpp lammps-24Jan10-patched/src/exception.cpp --- lammps-24Jan10/src/exception.cpp 1970-01-01 00:00:00.000000000 +0000 +++ lammps-24Jan10-patched/src/exception.cpp 2010-01-30 18:40:17.000000000 +0000 @@ -0,0 +1,15 @@ +#include "exception.h" + +using namespace LAMMPS_NS; + + +LAMMPSexception::LAMMPSexception(const char *msg) : exception() { + message = msg; +} + +const char *LAMMPSexception::what() const throw() +{ + return message; +} + + diff -Naur lammps-24Jan10/src/exception.h lammps-24Jan10-patched/src/exception.h --- lammps-24Jan10/src/exception.h 1970-01-01 00:00:00.000000000 +0000 +++ lammps-24Jan10-patched/src/exception.h 2010-01-30 18:40:28.000000000 +0000 @@ -0,0 +1,21 @@ +#ifndef EXCEPTION_H +#define EXCEPTION_H + +#include + +using namespace std; + +namespace LAMMPS_NS { + +class LAMMPSexception: public exception { + public: + LAMMPSexception(const char *); + + const char* what() const throw(); + + private: + const char *message; +}; + +} +#endif diff -Naur lammps-24Jan10/src/lammps.cpp lammps-24Jan10-patched/src/lammps.cpp --- lammps-24Jan10/src/lammps.cpp 2010-01-14 00:33:02.000000000 +0000 +++ lammps-24Jan10-patched/src/lammps.cpp 2010-01-30 18:40:17.000000000 +0000 @@ -41,8 +41,18 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) { + init(narg, arg, communicator, 0); +} + +LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator, int aslibrary) +{ + init(narg, arg, communicator, aslibrary); +} + +void LAMMPS::init(int narg, char **arg, MPI_Comm communicator, int aslibrary) +{ memory = new Memory(this); - error = new Error(this); + error = new Error(this, aslibrary); universe = new Universe(this,communicator); output = NULL; diff -Naur lammps-24Jan10/src/lammps.h lammps-24Jan10-patched/src/lammps.h --- lammps-24Jan10/src/lammps.h 2010-01-12 01:37:48.000000000 +0000 +++ lammps-24Jan10-patched/src/lammps.h 2010-01-30 18:40:28.000000000 +0000 @@ -44,10 +44,13 @@ FILE *logfile; // logfile LAMMPS(int, char **, MPI_Comm); + LAMMPS(int, char **, MPI_Comm, int aslibrary); ~LAMMPS(); void create(); void init(); void destroy(); + private: + void init(int, char **, MPI_Comm, int aslibrary); }; } diff -Naur lammps-24Jan10/src/library.cpp lammps-24Jan10-patched/src/library.cpp --- lammps-24Jan10/src/library.cpp 2009-08-06 22:25:12.000000000 +0000 +++ lammps-24Jan10-patched/src/library.cpp 2010-01-30 18:40:17.000000000 +0000 @@ -23,17 +23,36 @@ #include "update.h" #include "domain.h" #include "modify.h" +#include "exception.h" +#include "error.h" using namespace LAMMPS_NS; /* ---------------------------------------------------------------------- + return the last error produced by LAMMPS, or NULL. Required by C +------------------------------------------------------------------------- */ + +const char *lammps_last_error(void *ptr) +{ + LAMMPS *lmp = (LAMMPS *) ptr; + return lmp->error->last_error(); +} + + +/* ---------------------------------------------------------------------- create an instance of LAMMPS and return pointer to it pass in command-line args and MPI communicator to run on ------------------------------------------------------------------------- */ void lammps_open(int argc, char **argv, MPI_Comm communicator, void **ptr) { - LAMMPS *lmp = new LAMMPS(argc,argv,communicator); + LAMMPS *lmp; + try { + lmp = new LAMMPS(argc, argv, communicator, 1); + } catch (LAMMPSexception e) { + *ptr = NULL; + return; + } *ptr = (void *) lmp; } @@ -44,7 +63,12 @@ void lammps_close(void *ptr) { LAMMPS *lmp = (LAMMPS *) ptr; - delete lmp; + lmp->error->clear(); + try { + delete lmp; + } catch (LAMMPSexception e) { + return; + } } /* ---------------------------------------------------------------------- @@ -54,7 +78,12 @@ void lammps_file(void *ptr, char *str) { LAMMPS *lmp = (LAMMPS *) ptr; - lmp->input->file(str); + lmp->error->clear(); + try { + lmp->input->file(str); + } catch (LAMMPSexception e) { + return; + } } /* ---------------------------------------------------------------------- @@ -64,7 +93,14 @@ char *lammps_command(void *ptr, char *str) { LAMMPS *lmp = (LAMMPS *) ptr; - return lmp->input->one(str); + char *retval; + lmp->error->clear(); + try { + retval = lmp->input->one(str); + } catch (LAMMPSexception e) { + retval = NULL; + } + return retval; } /* ---------------------------------------------------------------------- diff -Naur lammps-24Jan10/src/library.h lammps-24Jan10-patched/src/library.h --- lammps-24Jan10/src/library.h 2009-08-06 22:21:38.000000000 +0000 +++ lammps-24Jan10-patched/src/library.h 2010-01-30 18:40:28.000000000 +0000 @@ -17,6 +17,7 @@ */ #include "mpi.h" +#include "error.h" /* ifdefs allow this file to be included in a C program */ @@ -24,6 +25,8 @@ extern "C" { #endif +const char *lammps_last_error(void *ptr); + void lammps_open(int, char **, MPI_Comm, void **); void lammps_close(void *); void lammps_file(void *, char *);