From 6fbcee59ee0b2d4b6705c106b1c0d537bc804793 Mon Sep 17 00:00:00 2001
From: sjplimp <sjplimp@f3b2605a-c512-4ea7-a41b-209d697bcdaa>
Date: Thu, 6 Aug 2009 22:21:38 +0000
Subject: [PATCH] git-svn-id: svn://svn.icms.temple.edu/lammps-ro/trunk@3009
 f3b2605a-c512-4ea7-a41b-209d697bcdaa

---
 src/atom.cpp    | 21 ++++++++++++++
 src/atom.h      |  2 ++
 src/compute.h   |  1 +
 src/fix.h       |  1 +
 src/library.cpp | 75 ++++++++++++++++++++++++++++++++++++-------------
 src/library.h   | 17 +++++------
 src/memory.cpp  |  6 +++-
 src/modify.cpp  | 36 ++++++++++++++++++++++++
 src/modify.h    |  5 ++++
 9 files changed, 135 insertions(+), 29 deletions(-)

diff --git a/src/atom.cpp b/src/atom.cpp
index 28bee5266e..563548c388 100644
--- a/src/atom.cpp
+++ b/src/atom.cpp
@@ -1369,6 +1369,27 @@ void Atom::update_callback(int ifix)
     if (extra_restart[i] > ifix) extra_restart[i]--;
 }
 
+/* ----------------------------------------------------------------------
+   return a pointer to a named internal variable
+   if don't recognize name, return NULL
+------------------------------------------------------------------------- */
+
+void *Atom::extract(char *name)
+{
+  if (strcmp(name,"natoms") == 0) return (void *) &natoms;
+  if (strcmp(name,"nlocal") == 0) return (void *) &nlocal;
+
+  if (strcmp(name,"id") == 0) return (void *) tag;
+  if (strcmp(name,"type") == 0) return (void *) type;
+  if (strcmp(name,"x") == 0) return (void *) x;
+  if (strcmp(name,"v") == 0) return (void *) v;
+  if (strcmp(name,"f") == 0) return (void *) f;
+  if (strcmp(name,"mass") == 0) return (void *) mass;
+  if (strcmp(name,"rmass") == 0) return (void *) rmass;
+
+  return NULL;
+}
+
 /* ----------------------------------------------------------------------
    return # of bytes of allocated memory
    call to avec sums per-atom vectors
diff --git a/src/atom.h b/src/atom.h
index 8325811bff..33d4668e78 100644
--- a/src/atom.h
+++ b/src/atom.h
@@ -146,6 +146,8 @@ class Atom : protected Pointers {
   void delete_callback(const char *, int);
   void update_callback(int);
 
+  void *extract(char *);
+
   double memory_usage();
   int memcheck(const char *);
 
diff --git a/src/compute.h b/src/compute.h
index effcc9462a..9f37dfdaf9 100644
--- a/src/compute.h
+++ b/src/compute.h
@@ -90,6 +90,7 @@ class Compute : protected Pointers {
   int matchstep(int);
   void clearstep();
 
+  virtual void *extract(char *) {return NULL;}
   virtual double memory_usage() {return 0.0;}
 
  protected:
diff --git a/src/fix.h b/src/fix.h
index 31ea065fbe..f869f9fc26 100644
--- a/src/fix.h
+++ b/src/fix.h
@@ -126,6 +126,7 @@ class Fix : protected Pointers {
 
   virtual int modify_param(int, char **) {return 0;}
 
+  virtual void *extract(char *) {return NULL;}
   virtual double memory_usage() {return 0.0;}
 
  protected:
diff --git a/src/library.cpp b/src/library.cpp
index d341a33725..8a1918c98e 100644
--- a/src/library.cpp
+++ b/src/library.cpp
@@ -15,10 +15,14 @@
 // new LAMMPS-specific functions can be added
 
 #include "mpi.h"
+#include "string.h"
 #include "library.h"
 #include "lammps.h"
 #include "input.h"
 #include "atom.h"
+#include "update.h"
+#include "domain.h"
+#include "modify.h"
 
 using namespace LAMMPS_NS;
 
@@ -29,8 +33,8 @@ using namespace LAMMPS_NS;
 
 void lammps_open(int argc, char **argv, MPI_Comm communicator, void **ptr)
 {
-  LAMMPS *lammps = new LAMMPS(argc,argv,communicator);
-  *ptr = (void *) lammps;
+  LAMMPS *lmp = new LAMMPS(argc,argv,communicator);
+  *ptr = (void *) lmp;
 }
 
 /* ----------------------------------------------------------------------
@@ -39,8 +43,8 @@ void lammps_open(int argc, char **argv, MPI_Comm communicator, void **ptr)
 
 void lammps_close(void *ptr)
 {
-  LAMMPS *lammps = (LAMMPS *) ptr;
-  delete lammps;
+  LAMMPS *lmp = (LAMMPS *) ptr;
+  delete lmp;
 }
 
 /* ----------------------------------------------------------------------
@@ -49,8 +53,8 @@ void lammps_close(void *ptr)
 
 void lammps_file(void *ptr, char *str)
 {
-  LAMMPS *lammps = (LAMMPS *) ptr;
-  lammps->input->file(str);
+  LAMMPS *lmp = (LAMMPS *) ptr;
+  lmp->input->file(str);
 }
 
 /* ----------------------------------------------------------------------
@@ -59,8 +63,8 @@ void lammps_file(void *ptr, char *str)
 
 char *lammps_command(void *ptr, char *str)
 {
-  LAMMPS *lammps = (LAMMPS *) ptr;
-  return lammps->input->one(str);
+  LAMMPS *lmp = (LAMMPS *) ptr;
+  return lmp->input->one(str);
 }
 
 /* ----------------------------------------------------------------------
@@ -68,12 +72,43 @@ char *lammps_command(void *ptr, char *str)
    all must receive LAMMPS pointer as argument
 ------------------------------------------------------------------------- */
 
+/* ----------------------------------------------------------------------
+   extract a pointer to an internal LAMMPS value or data structure
+   category: 0 = general, 1 = fix, 2 = compute
+   id = ID of fix or compute (else not used)
+   name = desired quantity, e.g. x or dt
+   returns a void pointer which the caller can cast to the desired data type
+   returns a NULL if LAMMPS does not recognize the name
+------------------------------------------------------------------------- */
+
+void *lammps_extract(void *ptr, int category, char *id, char *name)
+{
+  LAMMPS *lmp = (LAMMPS *) ptr;
+
+  if (category == 0) {
+    if (strcmp(name,"dt") == 0) return (void *) &lmp->update->dt;
+    if (strcmp(name,"boxxlo") == 0) return (void *) &lmp->domain->boxlo[0];
+    if (strcmp(name,"boxxhi") == 0) return (void *) &lmp->domain->boxhi[0];
+    if (strcmp(name,"boxylo") == 0) return (void *) &lmp->domain->boxlo[1];
+    if (strcmp(name,"boxyhi") == 0) return (void *) &lmp->domain->boxhi[1];
+    if (strcmp(name,"boxzlo") == 0) return (void *) &lmp->domain->boxlo[2];
+    if (strcmp(name,"boxzhi") == 0) return (void *) &lmp->domain->boxhi[2];
+    return NULL;
+  }
+
+  if (category == 1) return lmp->atom->extract(name);
+  if (category == 2) return lmp->modify->extract_fix(id,name);
+  if (category == 3) return lmp->modify->extract_compute(id,name);
+
+  return NULL;
+}
+
 /* ---------------------------------------------------------------------- */
 
 int lammps_get_natoms(void *ptr)
 {
-  LAMMPS *lammps = (LAMMPS *) ptr;
-  int natoms = static_cast<int> (lammps->atom->natoms);
+  LAMMPS *lmp = (LAMMPS *) ptr;
+  int natoms = static_cast<int> (lmp->atom->natoms);
   return natoms;
 }
 
@@ -81,14 +116,14 @@ int lammps_get_natoms(void *ptr)
 
 void lammps_get_coords(void *ptr, double *coords)
 {
-  LAMMPS *lammps = (LAMMPS *) ptr;
-  int natoms = static_cast<int> (lammps->atom->natoms);
+  LAMMPS *lmp = (LAMMPS *) ptr;
+  int natoms = static_cast<int> (lmp->atom->natoms);
   double *copy = new double[3*natoms];
   for (int i = 0; i < 3*natoms; i++) copy[i] = 0.0;
 
-  double **x = lammps->atom->x;
-  int *tag = lammps->atom->tag;
-  int nlocal = lammps->atom->nlocal;
+  double **x = lmp->atom->x;
+  int *tag = lmp->atom->tag;
+  int nlocal = lmp->atom->nlocal;
 
   int id,offset;
   for (int i = 0; i < nlocal; i++) {
@@ -99,7 +134,7 @@ void lammps_get_coords(void *ptr, double *coords)
     copy[offset+2] = x[i][2];
   }
 
-  MPI_Allreduce(copy,coords,3*natoms,MPI_DOUBLE,MPI_SUM,lammps->world);
+  MPI_Allreduce(copy,coords,3*natoms,MPI_DOUBLE,MPI_SUM,lmp->world);
   delete [] copy;
 }
 
@@ -107,14 +142,14 @@ void lammps_get_coords(void *ptr, double *coords)
 
 void lammps_put_coords(void *ptr, double *coords)
 {
-  LAMMPS *lammps = (LAMMPS *) ptr;
-  int natoms = static_cast<int> (lammps->atom->natoms);
+  LAMMPS *lmp = (LAMMPS *) ptr;
+  int natoms = static_cast<int> (lmp->atom->natoms);
 
-  double **x = lammps->atom->x;
+  double **x = lmp->atom->x;
 
   int m,offset;
   for (int i = 0; i < natoms; i++) {
-    if ((m = lammps->atom->map(i+1)) >= 0) {
+    if ((m = lmp->atom->map(i+1)) >= 0) {
       offset = 3*i;
       x[m][0] = coords[offset+0];
       x[m][1] = coords[offset+1];
diff --git a/src/library.h b/src/library.h
index f97f27fdc0..9c21d7a1d2 100644
--- a/src/library.h
+++ b/src/library.h
@@ -24,14 +24,15 @@
 extern "C" {
 #endif
 
-void lammps_open(int, char **, MPI_Comm, void **);  /* start-up LAMMPS */
-void lammps_close(void *);                          /* shut-down LAMMPS */
-void lammps_file(void *, char *);                   /* run an input script */
-char *lammps_command(void *, char *);               /* execute a command */
-
-int lammps_get_natoms(void *);              /* return # of atoms */
-void lammps_get_coords(void *, double *);   /* get atom x from all procs */
-void lammps_put_coords(void *, double *);   /* put atom x on all procs */
+void lammps_open(int, char **, MPI_Comm, void **);
+void lammps_close(void *);
+void lammps_file(void *, char *);
+char *lammps_command(void *, char *);
+
+void *lammps_extract(void *, int, char *, char *);
+int lammps_get_natoms(void *);
+void lammps_get_coords(void *, double *);
+void lammps_put_coords(void *, double *);
 
 #ifdef __cplusplus
 }
diff --git a/src/memory.cpp b/src/memory.cpp
index caf553120a..4e46b87b57 100644
--- a/src/memory.cpp
+++ b/src/memory.cpp
@@ -56,7 +56,11 @@ void Memory::sfree(void *ptr)
 
 void *Memory::srealloc(void *ptr, int n, const char *name)
 {
-  if (n == 0) return NULL;
+  if (n == 0) {
+    sfree(ptr);
+    return NULL;
+  }
+
   ptr = realloc(ptr,n);
   if (ptr == NULL) {
     char str[128];
diff --git a/src/modify.cpp b/src/modify.cpp
index 962536a9f9..3dace9f5d0 100644
--- a/src/modify.cpp
+++ b/src/modify.cpp
@@ -996,6 +996,42 @@ void Modify::list_init_compute()
     if (compute[i]->timeflag) list_timeflag[n_timeflag++] = i;
 }
 
+/* ----------------------------------------------------------------------
+   return a pointer to a named internal variable owned by fix ID
+   if don't recognize ID, return NULL
+------------------------------------------------------------------------- */
+
+void *Modify::extract_fix(char *id, char *name)
+{
+  int ifix = find_fix(id);
+  if (ifix < 0) return NULL;
+
+  if (strcmp(name,"index") == 0) {
+    index_permanent = ifix;
+    return (void *) &index_permanent;
+  }
+
+  return fix[ifix]->extract(name);
+}
+
+/* ----------------------------------------------------------------------
+   return a pointer to a named internal variable owned by compute ID
+   if don't recognize ID, return NULL
+------------------------------------------------------------------------- */
+
+void *Modify::extract_compute(char *id, char *name)
+{
+  int icompute = find_compute(id);
+  if (icompute < 0) return NULL;
+
+  if (strcmp(name,"index") == 0) {
+    index_permanent = icompute;
+    return (void *) &index_permanent;
+  }
+
+  return compute[icompute]->extract(name);
+}
+
 /* ----------------------------------------------------------------------
    return # of bytes of allocated memory from all fixes
 ------------------------------------------------------------------------- */
diff --git a/src/modify.h b/src/modify.h
index a749f010dc..067513ee6d 100644
--- a/src/modify.h
+++ b/src/modify.h
@@ -83,6 +83,9 @@ class Modify : protected Pointers {
   int read_restart(FILE *);
   void restart_deallocate();
 
+  void *extract_fix(char *, char *);
+  void *extract_compute(char *, char *);
+
   double memory_usage();
 
  private:
@@ -111,6 +114,8 @@ class Modify : protected Pointers {
   char **style_restart_peratom;
   int *index_restart_peratom;
 
+  int index_permanent;        // fix/compute index returned to library call
+
   void list_init(int, int &, int *&);
   void list_init_end_of_step(int, int &, int *&);
   void list_init_thermo_energy(int, int &, int *&);
-- 
GitLab