diff --git a/src/Makefile.am b/src/Makefile.am index 0eba1530737b270206329c33d14dee30c5a27500..dcddfb5f2b0a085f9a7543e0a87000f0998ccdeb 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,21 +2,26 @@ SUBDIRS = check_PROGRAMS = \ + test-python \ test-gradient-method +test_python_SOURCES = \ + test-python.cc + test_gradient_method_SOURCES = \ test-gradient-method.cc \ mynonlinearity.hh \ nicefunction.hh \ samplefunctional.hh -TESTS= test-gradient-method +TESTS= test-python test-gradient-method AM_CXXFLAGS = -Wall -Wextra -Wno-unused-parameter AM_CPPFLAGS = \ $(DUNE_CPPFLAGS) \ - $(ALUGRID_CPPFLAGS) + $(ALUGRID_CPPFLAGS) \ + $(PYTHON_CPPFLAGS) # The libraries have to be given in reverse order (most basic libraries # last). Also, due to some misunderstanding, a lot of libraries include the @@ -24,10 +29,12 @@ AM_CPPFLAGS = \ # here as well. LDADD = \ $(DUNE_LDFLAGS) $(DUNE_LIBS) \ - $(ALUGRID_LDFLAGS) $(ALUGRID_LIBS) + $(ALUGRID_LDFLAGS) $(ALUGRID_LIBS) \ + $(PYTHON_LIBS) AM_LDFLAGS = \ $(ALUGRID_LDFLAGS) \ - $(DUNE_LDFLAGS) + $(DUNE_LDFLAGS) \ + $(PYTHON_LDFLAGS) # don't follow the full GNU-standard # we need automake 1.5 diff --git a/src/nicefunction.py b/src/nicefunction.py new file mode 100644 index 0000000000000000000000000000000000000000..f4c4bbe6f4320603711716182e33ca7a951b239d --- /dev/null +++ b/src/nicefunction.py @@ -0,0 +1,8 @@ +class sampleFunction: + def __call__(self, x): + if x < 1: + return x + else: + return 2*x - 1 + +Functions = {'sampleFunction' : sampleFunction()} diff --git a/src/test-python.cc b/src/test-python.cc new file mode 100644 index 0000000000000000000000000000000000000000..28a3658ad010657eac694df84c2bf8822c296f10 --- /dev/null +++ b/src/test-python.cc @@ -0,0 +1,47 @@ +/* -*- mode:c++; mode:semantic -*- */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifndef HAVE_PYTHON +#error Python is necessary +#else +#define EMBEDDED_PYTHON 1 +#endif + +#include <Python.h> + +#include <dune/common/exceptions.hh> +#include <dune/common/stdstreams.hh> + +#include <dune/fufem/dunepython.hh> +#include <dune/fufem/sharedpointermap.hh> + +int main() { + typedef Dune::VirtualFunction<double, double> Function; + typedef SharedPointerMap<std::string, Function> FunctionMap; + FunctionMap functions; + + try { + Python::start(); + + Python::run("import sys"); + Python::run( + "sys.path.append('../../src')"); // FIXME: use path to source here + + Python::Reference module = Python::import("nicefunction"); + Python::Reference funcRef = module.get("Functions"); + funcRef.toC<FunctionMap::Base>(functions); + + double const in(2.0); + Function const &foo = functions.get("sampleFunction"); + double out; + foo.evaluate(in, out); + + Python::stop(); + } + catch (Dune::Exception &e) { + Dune::derr << "Dune reported error: " << e << std::endl; + } +}