changeset 260:1e2a9be8083a

Support template dolfin::MeshFunction
author Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
date Wed, 06 Aug 2014 19:52:03 +0200
parents 598c5e9e0a9e
children f22588ae37af
files INDEX inst/import_meshfunction_type.m inst/private/check_mf_files.m inst/private/generate_mf_constructor.m inst/private/generate_mf_header.m inst/private/generate_mf_makefile.m inst/private/generate_mf_save.m
diffstat 7 files changed, 427 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/INDEX	Wed Aug 06 16:48:25 2014 +0200
+++ b/INDEX	Wed Aug 06 19:52:03 2014 +0200
@@ -36,3 +36,5 @@
 MPI
   barrier
   is_master_node
+Miscellaneous
+  import_meshfunction_type
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/inst/import_meshfunction_type.m	Wed Aug 06 19:52:03 2014 +0200
@@ -0,0 +1,62 @@
+## Copyright (C) 2014 Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
+##
+## This program is free software; you can redistribute it and/or modify it under
+## the terms of the GNU General Public License as published by the Free Software
+## Foundation; either version 3 of the License, or (at your option) any later
+## version.
+##
+## This program is distributed in the hope that it will be useful, but WITHOUT
+## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+## details.
+##
+## You should have received a copy of the GNU General Public License along with
+## this program; if not, see <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {} import_meshfunction_type (@var{typename})
+## Add a wrapper for dolfin::MeshFunction <@var{typename}>
+## @end deftypefn
+
+function import_meshfunction_type (typename)
+
+  if (is_master_node ())
+    if (nargin != 1)
+      error ("import_meshfunction_type: wrong number of input parameters.");
+    elseif (! ischar (typename))
+      error ("import_meshfunction_type: first argument is not a valid string");
+    endif
+
+    # Ensure that the typename does not contain whitespace
+    typename = strtrim (typename);
+    typename = strjoin (strsplit (typename), "");
+
+    if (! check_mf_files (typename))
+      n = length (mfilename ("fullpath")) - length (mfilename());
+      path = strtrunc(mfilename ("fullpath"), n);
+
+      private = fullfile (path, "include/");
+      output = generate_mf_header (typename);
+      output += generate_mf_constructor (typename);
+      output += generate_mf_save (typename);
+      output += generate_mf_makefile (typename, private);
+      if (output != 0)
+        error ("compilation failed");
+      else
+        [output, textfile] = system (["make -f Makefile_", typename, " all"]);
+        if (output != 0)
+          display (text);
+          error ("compilation failed");
+        endif
+        [output, textfile] = system (["make -f Makefile_", typename, " clean"]);
+        if (output != 0)
+          display (text);
+          error ("compilation failed");
+        endif
+      endif
+    endif
+  endif
+
+  barrier ();
+
+endfunction
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/inst/private/check_mf_files.m	Wed Aug 06 19:52:03 2014 +0200
@@ -0,0 +1,32 @@
+## Copyright (C) 2014 Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
+##
+## This program is free software; you can redistribute it and/or modify it under
+## the terms of the GNU General Public License as published by the Free Software
+## Foundation; either version 3 of the License, or (at your option) any later
+## version.
+##
+## This program is distributed in the hope that it will be useful, but WITHOUT
+## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+## details.
+##
+## You should have received a copy of the GNU General Public License along with
+## this program; if not, see <http://www.gnu.org/licenses/>.
+
+
+## -*- texinfo -*-
+## function for internal usage only
+## @end deftypefn
+
+function found = check_mf_files (typename)
+  if (! ischar (typename))
+    error ("check_mf_files: invalid argument");
+  endif
+
+  [~, err, ~] = stat (["MeshFunction_", typename, ".oct"]);
+  read = (err == 0);
+  [~, err, ~] = stat (["@meshfunction_", typename, "/save.oct"]);
+  write = (err == 0);
+
+  found = read && write;
+endfunction
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/inst/private/generate_mf_constructor.m	Wed Aug 06 19:52:03 2014 +0200
@@ -0,0 +1,90 @@
+## Copyright (C) 2014 Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
+##
+## This program is free software; you can redistribute it and/or modify it under
+## the terms of the GNU General Public License as published by the Free Software
+## Foundation; either version 3 of the License, or (at your option) any later
+## version.
+##
+## This program is distributed in the hope that it will be useful, but WITHOUT
+## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+## details.
+##
+## You should have received a copy of the GNU General Public License along with
+## this program; if not, see <http://www.gnu.org/licenses/>.
+
+
+## -*- texinfo -*-
+## function for internal usage only
+## @end deftypefn
+
+function output = generate_mf_constructor (typename)
+
+STRING ="\n\
+#include <dolfin.h>\n\
+#include <fem-fenics/mesh.h>\n\
+#include ""meshfunction_@@TYPENAME@@.h""\n\
+#include <fem-fenics/dolfin_compat.h>\n\
+\n\
+DEFUN_DLD (MeshFunction_@@TYPENAME@@, args, nargout, ""MF = MeshFunction_@@TYPENAME@@ (MESH, FILENAME)"")\n\
+{\n\
+\n\
+  int nargin = args.length ();\n\
+  octave_value retval;\n\
+\n\
+  if (nargin < 2 || nargin > 2 || nargout > 1)\n\
+    print_usage ();\n\
+  else\n\
+    {\n\
+      if (! mesh_type_loaded)\n\
+        {\n\
+          mesh::register_type ();\n\
+          mesh_type_loaded = true;\n\
+          mlock ();\n\
+        }\n\
+\n\
+      if (! meshfunction_@@TYPENAME@@_type_loaded)\n\
+        {\n\
+          meshfunction_@@TYPENAME@@::register_type ();\n\
+          meshfunction_@@TYPENAME@@_type_loaded = true;\n\
+          mlock ();\n\
+        }\n\
+\n\
+      if (args(0).type_id () == mesh::static_type_id ()\n\
+          && args(1).is_string ())\n\
+        {\n\
+          std::string filename = args(1).string_value ();\n\
+          mesh const & msh_arg =\n\
+            static_cast<mesh const &> (args(0).get_rep ());\n\
+\n\
+          if (!error_state)\n\
+            {\n\
+              SHARED_PTR <dolfin::Mesh const> const &\n\
+                pmsh = msh_arg.get_pmsh ();\n\
+              filename += "".xdmf"";\n\
+\n\
+              try\n\
+                { retval = new meshfunction_@@TYPENAME@@ (pmsh, filename); }\n\
+              catch (std::runtime_error &)\n\
+                { error (""error reading file""); }\n\
+            }\n\
+        }\n\
+      else\n\
+        error (""invalid input arguments"");\n\
+    }\n\
+\n\
+  return retval;\n\
+}";
+
+STRING =  strrep (STRING, "@@TYPENAME@@", typename);
+
+fid = fopen (["MeshFunction_", typename, ".cc"], "w");
+if (is_valid_file_id (fid))
+  fputs (fid, STRING);
+  output = fclose (fid);
+else
+  error ("cannot open file");
+  output = 1;
+endif
+
+endfunction
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/inst/private/generate_mf_header.m	Wed Aug 06 19:52:03 2014 +0200
@@ -0,0 +1,90 @@
+## Copyright (C) 2014 Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
+##
+## This program is free software; you can redistribute it and/or modify it under
+## the terms of the GNU General Public License as published by the Free Software
+## Foundation; either version 3 of the License, or (at your option) any later
+## version.
+##
+## This program is distributed in the hope that it will be useful, but WITHOUT
+## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+## details.
+##
+## You should have received a copy of the GNU General Public License along with
+## this program; if not, see <http://www.gnu.org/licenses/>.
+
+
+## -*- texinfo -*-
+## function for internal usage only
+## @end deftypefn
+
+function output = generate_mf_header (typename)
+
+STRING ="\n\
+#ifndef _MESHFUNCTION_@@TYPENAME@@_OCTAVE_\n\
+#define _MESHFUNCTION_@@TYPENAME@@_OCTAVE_\n\
+\n\
+#include <dolfin.h>\n\
+#include <octave/oct.h>\n\
+#include <fem-fenics/dolfin_compat.h>\n\
+\n\
+class meshfunction_@@TYPENAME@@ : public octave_base_value\n\
+{\n\
+  public:\n\
+\n\
+  meshfunction_@@TYPENAME@@ (void)\n\
+    : octave_base_value () {}\n\
+\n\
+  meshfunction_@@TYPENAME@@ (dolfin::MeshFunction <@@TYPENAME@@> const & _mf)\n\
+    : octave_base_value (), pmf (new dolfin::MeshFunction <@@TYPENAME@@> (_mf)) {}\n\
+\n\
+  meshfunction_@@TYPENAME@@ (SHARED_PTR <dolfin::Mesh const> mesh,\n\
+                             std::string const & filename)\n\
+    : octave_base_value (),\n\
+      pmf (new dolfin::MeshFunction <@@TYPENAME@@> (mesh, filename)) {}\n\
+\n\
+  bool\n\
+  is_defined (void) const\n\
+    { return true; }\n\
+\n\
+  void\n\
+  print (std::ostream& os, bool pr_as_read_syntax = false) const\n\
+    { os << ""MeshFunction <@@TYPENAME@@>: ""\n\
+         << get_pmf ()->str (false) << std::endl; }\n\
+\n\
+  dolfin::MeshFunction <@@TYPENAME@@> const &\n\
+  get_mf (void) const\n\
+    { return *pmf; }\n\
+\n\
+  SHARED_PTR <dolfin::MeshFunction <@@TYPENAME@@> const> const &\n\
+  get_pmf (void) const\n\
+    { return pmf; }\n\
+\n\
+  private:\n\
+\n\
+  SHARED_PTR <dolfin::MeshFunction <@@TYPENAME@@> const> pmf;\n\
+\n\
+  DECLARE_OCTAVE_ALLOCATOR;\n\
+  DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA;\n\
+};\n\
+\n\
+static bool meshfunction_@@TYPENAME@@_type_loaded = false;\n\
+\n\
+DEFINE_OCTAVE_ALLOCATOR (meshfunction_@@TYPENAME@@);\n\
+DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (meshfunction_@@TYPENAME@@,\n\
+""meshfunction_@@TYPENAME@@"", ""meshfunction_@@TYPENAME@@"");\n\
+\n\
+#endif";
+
+STRING =  strrep (STRING, "@@TYPENAME@@", typename);
+
+fid = fopen (["meshfunction_", typename, ".h"], "w");
+if (is_valid_file_id (fid))
+  fputs (fid, STRING);
+  output = fclose (fid);
+else
+  error ("cannot open file");
+  output = 1;
+endif
+
+endfunction
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/inst/private/generate_mf_makefile.m	Wed Aug 06 19:52:03 2014 +0200
@@ -0,0 +1,68 @@
+## Copyright (C) 2014 Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
+##
+## This program is free software; you can redistribute it and/or modify it under
+## the terms of the GNU General Public License as published by the Free Software
+## Foundation; either version 3 of the License, or (at your option) any later
+## version.
+##
+## This program is distributed in the hope that it will be useful, but WITHOUT
+## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+## details.
+##
+## You should have received a copy of the GNU General Public License along with
+## this program; if not, see <http://www.gnu.org/licenses/>.
+
+
+## -*- texinfo -*-
+## function for internal usage only
+## @end deftypefn
+
+function output = generate_mf_makefile (typename, path)
+
+STRING ="\n\
+DIR = @@PATH@@\n\
+CPPFLAGS+=@@FF_CPPFLAGS@@ -g\n\
+LIBS+=@@FF_LIBS@@\n\
+MKOCTFILE = mkoctfile\n\
+FFC = ffc\n\
+\n\
+OCTFILES = MeshFunction_@@TYPENAME@@.oct save_@@TYPENAME@@.oct\n\
+\n\
+all : $(OCTFILES)\n\
+\n\
+MeshFunction_@@TYPENAME@@.oct: MeshFunction_@@TYPENAME@@.cc \
+                               meshfunction_@@TYPENAME@@.h\n\
+	CPPFLAGS='$(CPPFLAGS)' $(MKOCTFILE) $< -o $@ -I$(DIR) -I. $(LDFLAGS) $(LIBS)\n\
+\n\
+save_@@TYPENAME@@.oct: save_@@TYPENAME@@.cc meshfunction_@@TYPENAME@@.h mkmfdir\n\
+	CPPFLAGS='$(CPPFLAGS)' $(MKOCTFILE) $< -o @meshfunction_@@TYPENAME@@/save.oct\
+ -I$(DIR) -I. $(LDFLAGS) $(LIBS)\n\
+\n\
+mkmfdir:\n\
+	mkdir -p @meshfunction_@@TYPENAME@@\n\
+\n\
+.PHONY: clean\n\
+\n\
+clean:\n\
+	$(RM) meshfunction_@@TYPENAME@@.h save_@@TYPENAME@@.o\n\
+	$(RM) MeshFunction_@@TYPENAME@@.o MeshFunction_@@TYPENAME@@.cc\n\
+	$(RM) save_@@TYPENAME@@.cc\n\
+	$(RM) Makefile_@@TYPENAME@@\n\
+";
+
+STRING = strrep (STRING, "@@TYPENAME@@", typename);
+STRING = strrep (STRING, "@@PATH@@", path);
+STRING = strrep (STRING, "@@FF_CPPFLAGS@@", get_vars ("CPPFLAGS"));
+STRING = strrep (STRING, "@@FF_LIBS@@", get_vars ("LIBS"));
+
+fid = fopen (["Makefile_", typename], "w");
+if (is_valid_file_id (fid))
+  fputs (fid, STRING);
+  output = fclose (fid);
+else
+  error ("cannot open file");
+  output = 1;
+endif
+
+endfunction
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/inst/private/generate_mf_save.m	Wed Aug 06 19:52:03 2014 +0200
@@ -0,0 +1,83 @@
+## Copyright (C) 2014 Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
+##
+## This program is free software; you can redistribute it and/or modify it under
+## the terms of the GNU General Public License as published by the Free Software
+## Foundation; either version 3 of the License, or (at your option) any later
+## version.
+##
+## This program is distributed in the hope that it will be useful, but WITHOUT
+## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+## details.
+##
+## You should have received a copy of the GNU General Public License along with
+## this program; if not, see <http://www.gnu.org/licenses/>.
+
+
+## -*- texinfo -*-
+## function for internal usage only
+## @end deftypefn
+
+function output = generate_mf_save (typename)
+
+STRING ="\n\
+#include ""meshfunction_@@TYPENAME@@.h""\n\
+\n\
+DEFUN_DLD (save, args, nargout, ""save (MESHFUNCTION, FILENAME)"")\n\
+{\n\
+  int nargin = args.length ();\n\
+  octave_value retval;\n\
+\n\
+  if (nargin < 2 || nargin > 2 || nargout > 1)\n\
+    print_usage ();\n\
+  else\n\
+    {\n\
+      if (! meshfunction_@@TYPENAME@@_type_loaded)\n\
+        {\n\
+          meshfunction_@@TYPENAME@@::register_type ();\n\
+          meshfunction_@@TYPENAME@@_type_loaded = true;\n\
+          mlock ();\n\
+        }\n\
+\n\
+      if (args(0).type_id () == meshfunction_@@TYPENAME@@::static_type_id ()\n\
+          && args(1).is_string ())\n\
+        {\n\
+          meshfunction_@@TYPENAME@@ const & mf_arg =\n\
+            static_cast<meshfunction_@@TYPENAME@@ const &>\n\
+            (args(0).get_rep ());\n\
+          std::string str = args(1).string_value ();\n\
+\n\
+          if (!error_state)\n\
+            {\n\
+              dolfin::MeshFunction <@@TYPENAME@@> const &\n\
+                mf = mf_arg.get_mf ();\n\
+              str += "".xdmf"";\n\
+              try\n\
+                {\n\
+                  dolfin::File file (str);\n\
+                  file << mf;\n\
+                }\n\
+              catch (std::runtime_error &)\n\
+                { error (""error saving meshfunction@@TYPENAME@@""); }\n\
+              retval = 0;\n\
+            }\n\
+        }\n\
+      else\n\
+        error (""invalid input arguments"");\n\
+    }\n\
+\n\
+  return retval;\n\
+}";
+
+STRING =  strrep (STRING, "@@TYPENAME@@", typename);
+
+fid = fopen (["save_", typename, ".cc"], "w");
+if (is_valid_file_id (fid))
+  fputs (fid, STRING);
+  output = fclose (fid);
+else
+  error ("cannot open file");
+  output = 1;
+endif
+
+endfunction