Commit 2d8bce78 authored by Richard Berger's avatar Richard Berger
Browse files

Refactor PYTHON package and wrapper classes

parent 9a027a01
Loading
Loading
Loading
Loading
+2 −2
Changes for src/.gitignore: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -846,8 +846,8 @@
/pppm_tip4p_cg.h
/prd.cpp
/prd.h
/python.cpp
/python.h
/python_impl.cpp
/python_impl.h
/reader_molfile.cpp
/reader_molfile.h
/reaxc_allocate.cpp
+3 −5
Changes for src/PYTHON/Install.sh: 3 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -26,16 +26,14 @@ action () {
  fi
}

# force rebuild of files with LMP_KOKKOS switch
# also variable so its *.d dependence on changed python_wrapper.h is rebuilt
# force rebuild of files using python header

touch ../python_wrapper.h
touch ../variable.cpp
touch ../python.h

# all package files with no dependencies

for file in *.cpp *.h; do
  test -f ${file} && action $file
  action $file
done

# edit 2 Makefile.package files to include/exclude package info
+9 −11
Changes for src/PYTHON/python_impl.cpp: 9 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -43,10 +43,8 @@ enum{NONE,INT,DOUBLE,STRING,PTR};

/* ---------------------------------------------------------------------- */

Python::Python(LAMMPS *lmp) : Pointers(lmp)
PythonImpl::PythonImpl(LAMMPS *lmp) : Pointers(lmp)
{
  python_exists = 1;

  pyMain = NULL;

  // pfuncs stores interface info for each Python function
@@ -59,7 +57,7 @@ Python::Python(LAMMPS *lmp) : Pointers(lmp)

/* ---------------------------------------------------------------------- */

Python::~Python()
PythonImpl::~PythonImpl()
{
  // clean up
  PyGILState_STATE gstate = PyGILState_Ensure();
@@ -85,7 +83,7 @@ Python::~Python()

/* ---------------------------------------------------------------------- */

void Python::command(int narg, char **arg)
void PythonImpl::command(int narg, char **arg)
{
  if (narg < 2) error->all(FLERR,"Invalid python command");

@@ -244,7 +242,7 @@ void Python::command(int narg, char **arg)

/* ------------------------------------------------------------------ */

void Python::invoke_function(int ifunc, char *result)
void PythonImpl::invoke_function(int ifunc, char *result)
{
  PyGILState_STATE gstate = PyGILState_Ensure();
  PyObject *pValue;
@@ -344,7 +342,7 @@ void Python::invoke_function(int ifunc, char *result)

/* ------------------------------------------------------------------ */

int Python::find(char *name)
int PythonImpl::find(char *name)
{
  for (int i = 0; i < nfunc; i++)
    if (strcmp(name,pfuncs[i].name) == 0) return i;
@@ -353,7 +351,7 @@ int Python::find(char *name)

/* ------------------------------------------------------------------ */

int Python::variable_match(char *name, char *varname, int numeric)
int PythonImpl::variable_match(char *name, char *varname, int numeric)
{
  int ifunc = find(name);
  if (ifunc < 0) return -1;
@@ -365,14 +363,14 @@ int Python::variable_match(char *name, char *varname, int numeric)

/* ------------------------------------------------------------------ */

char *Python::long_string(int ifunc)
char *PythonImpl::long_string(int ifunc)
{
  return pfuncs[ifunc].longstr;
}

/* ------------------------------------------------------------------ */

int Python::create_entry(char *name)
int PythonImpl::create_entry(char *name)
{
  // ifunc = index to entry by name in pfuncs vector, can be old or new
  // free old vectors if overwriting old pfunc
@@ -482,7 +480,7 @@ int Python::create_entry(char *name)

/* ------------------------------------------------------------------ */

void Python::deallocate(int i)
void PythonImpl::deallocate(int i)
{
  delete [] pfuncs[i].itype;
  delete [] pfuncs[i].ivarflag;
+130 −0
Changes for src/PYTHON/python_impl.h: 130 added lines, 0 removed lines.
Original line number Diff line number Diff line
/* -*- c++ -*- ----------------------------------------------------------
   LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
   http://lammps.sandia.gov, Sandia National Laboratories
   Steve Plimpton, sjplimp@sandia.gov

   Copyright (2003) Sandia Corporation.  Under the terms of Contract
   DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
   certain rights in this software.  This software is distributed under
   the GNU General Public License.

   See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */

#ifndef LMP_PYTHON_IMPL_H
#define LMP_PYTHON_IMPL_H

#include "pointers.h"

namespace LAMMPS_NS {

class PythonImpl : protected Pointers, public PythonInterface {
 public:
  bool external_interpreter;

  PythonImpl(class LAMMPS *);
  ~PythonImpl();
  void command(int, char **);
  void invoke_function(int, char *);
  int find(char *);
  int variable_match(char *, char *, int);
  char *long_string(int);

 private:
  int ninput,noutput,length_longstr;
  char **istr;
  char *ostr,*format;
  void *pyMain;

  struct PyFunc {
    char *name;
    int ninput,noutput;
    int *itype,*ivarflag;
    int *ivalue;
    double *dvalue;
    char **svalue;
    int otype;
    char *ovarname;
    char *longstr;
    int length_longstr;
    void *pFunc;
  };

  PyFunc *pfuncs;
  int nfunc;

  int create_entry(char *);
  void deallocate(int);
};

}

#endif

/* ERROR/WARNING messages:

E: Invalid python command

Self-explanatory.  Check the input script syntax and compare to the
documentation for the command.  You can use -echo screen as a
command-line option when running LAMMPS to see the offending line.

E: Python invoke of undefined function

Cannot invoke a function that has not been previously defined.

E: Python variable does not match Python function

This matching is defined by the python-style variable and the python
command.

E: Cannot embed Python when also extending Python with LAMMPS

When running LAMMPS via Python through the LAMMPS library interface
you cannot also user the input script python command.

E: Could not initialize embedded Python

The main module in Python was not accessible.

E: Could not open Python file

The specified file of Python code cannot be opened.  Check that the
path and name are correct.

E: Could not process Python file

The Python code in the specified file was not run successfully by
Python, probably due to errors in the Python code.

E: Could not process Python string

The Python code in the here string was not run successfully by Python,
probably due to errors in the Python code.

E: Could not find Python function

The provided Python code was run successfully, but it not
define a callable function with the required name.

E: Python function is not callable

The provided Python code was run successfully, but it not
define a callable function with the required name.

E: Could not create Python function arguments

This is an internal Python error, possibly because the number
of inputs to the function is too large.

E: Could not evaluate Python function input variable

Self-explanatory.

E: Python function evaluation failed

The Python function did not run successfully and/or did not return a
value (if it is supposed to return a value).  This is probably due to
some error condition in the function.

*/
+8 −0
Changes for src/lammps.cpp: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@
#include "accelerator_kokkos.h"
#include "accelerator_omp.h"
#include "timer.h"
#include "python.h"
#include "memory.h"
#include "version.h"
#include "error.h"
@@ -67,6 +68,7 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator)
  error = new Error(this);
  universe = new Universe(this,communicator);
  output = NULL;
  python = NULL;

  screen = NULL;
  logfile = NULL;
@@ -585,6 +587,7 @@ LAMMPS::~LAMMPS()

  if (world != universe->uworld) MPI_Comm_free(&world);

  delete python;
  delete kokkos;
  delete [] suffix;
  delete [] suffix2;
@@ -639,6 +642,8 @@ void LAMMPS::create()
                              // must be after modify so can create Computes
  update = new Update(this);  // must be after output, force, neighbor
  timer = new Timer(this);

  python = new Python(this);
}

/* ----------------------------------------------------------------------
@@ -759,6 +764,9 @@ void LAMMPS::destroy()

  delete timer;
  timer = NULL;

  delete python;
  python = NULL;
}

/* ----------------------------------------------------------------------
Loading