changeset 109:50753c3cb0b6

New function for dealing with form of rank 0. * fem_create_functional: user visible function for the creation of a DLD function which create a Functional from a given .ufl file * generate_fun.cc: writes on the fly the code * generate_makefile: compile also the new function
author gedeone-octave <marcovass89@hotmail.it>
date Thu, 22 Aug 2013 19:01:44 +0200
parents 5cbc7341ded5
children 75780f7dc9f4
files inst/fem_create_functional.m inst/generate_fun.m inst/generate_makefile.m
diffstat 3 files changed, 168 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/inst/fem_create_functional.m	Thu Aug 22 19:01:44 2013 +0200
@@ -0,0 +1,55 @@
+## Copyright (C) 2013 Marco Vassallo
+
+## 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 2 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} {} = fem_ffc (myproblem.ufl)
+## This function take as input the name of the .ufl file where 
+## the Variational Problem is defined.
+## @example
+## 
+## @end example
+## @seealso{} 
+## @end deftypefn
+
+function fem_create_functional (var_prob)
+
+  if nargin != 1
+    error ("fem_create_functional: wrong number of input parameters.");
+  elseif ! ischar (var_prob)
+    error ("fem_create_functional: first argument is not a valid string");
+  endif
+
+  n = length (mfilename ("fullpath")) - length (mfilename());
+  path = strtrunc(mfilename ("fullpath"), n);
+
+  private = fullfile (path, "private/");
+  output = generate_fun (var_prob);
+  output += generate_makefile (var_prob, private);
+  if output != 0
+    error ("Compilation failed");
+  else
+    [output, textfile] = system (sprintf ("make -f Makefile_%s fun", var_prob));
+    if output != 0
+      display (text);
+      error ("Compilation failed");
+    endif
+    [output, textfile] = system (sprintf ("make -f Makefile_%s clean", var_prob));
+    if output != 0
+      display (text);
+      error ("Compilation failed");
+    endif
+  endif
+endfunction
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/inst/generate_fun.m	Thu Aug 22 19:01:44 2013 +0200
@@ -0,0 +1,107 @@
+
+function output = generate_fun (ufl_name)
+
+STRING ="\n\
+#include ""@@UFL_NAME@@.h""\n\
+#include ""mesh.h""\n\
+#include ""form.h""\n\
+#include ""coefficient.h""\n\
+#include ""function.h""\n\
+\n\
+DEFUN_DLD (@@UFL_NAME@@_Functional, args, , ""A = fem_rhs_@@UFL_NAME@@ (FUNCTIONAL SPACE, COEFF)"")\n\
+{\n\
+\n\
+  int nargin = args.length ();\n\
+  octave_value retval;\n\
+\n\
+  if (nargin < 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\
+      if (! form_type_loaded)\n\
+        {\n\
+          form::register_type ();\n\
+          form_type_loaded = true;\n\
+          mlock ();\n\
+        }\n\
+\n\
+      if (args(0).type_id () == mesh::static_type_id ())\n\
+        {\n\
+          const mesh & msho\n\
+            = static_cast<const mesh&> (args(0).get_rep ());\n\
+\n\
+          if (! error_state)\n\
+            {\n\
+              const dolfin::Mesh & mesh = msho.get_msh ();\n\
+              @@UFL_NAME@@::Functional M (mesh);\n\
+              std::size_t ncoef = M.num_coefficients (), nc = 0;\n\
+\n\
+              if (! coefficient_type_loaded)\n\
+                {\n\
+                  coefficient::register_type ();\n\
+                  coefficient_type_loaded = true;\n\
+                  mlock ();\n\
+                }\n\
+\n\
+              if (! function_type_loaded)\n\
+                {\n\
+                  function::register_type ();\n\
+                  function_type_loaded = true;\n\
+                  mlock ();\n\
+                }\n\
+\n\
+              for (std::size_t i = 1; i < nargin; ++i)\n\
+                {\n\
+                  if (args(i).type_id () == coefficient::static_type_id ())\n\
+                    {\n\
+                      const coefficient & cf\n\
+                        = static_cast <const coefficient&> (args(i).get_rep ());\n\
+\n\
+                      std::size_t n = M.coefficient_number (cf.get_str ());\n\
+                      const boost::shared_ptr<const expression> & pexp = cf.get_expr ();\n\
+                      M.set_coefficient (n, pexp);\n\
+                      ++nc;\n\
+                    }\n\
+\n\
+                  if (args(i).type_id () == function::static_type_id ())\n\
+                    {\n\
+                      const function & fun\n\
+                        = static_cast <const function&> (args(i).get_rep ());\n\
+\n\
+                      std::size_t n = M.coefficient_number (fun.get_str ());\n\
+                      const boost::shared_ptr<const dolfin::Function> & pfun = fun.get_pfun ();\n\
+                      M.set_coefficient (n, pfun);\n\
+                      ++nc;\n\
+                    }\n\
+                 }\n\
+\n\
+              if (nc != ncoef)\n\
+                error (""Wrong number of coefficient"");\n\
+              else\n\
+                {\n\
+                  retval = new form (M);\n\
+                }\n\
+            }\n\
+        }\n\
+    }\n\
+  return retval;\n\
+}";
+
+STRING =  strrep (STRING, "@@UFL_NAME@@", ufl_name);
+
+fid = fopen (sprintf ("%s_Functional.cc", ufl_name), 'w');
+if (fid >= 0)
+  fputs (fid, STRING);
+  output = fclose (fid);
+else
+  error ("cannot open file");
+  output = 1;
+endif
+
+endfunction
--- a/inst/generate_makefile.m	Thu Aug 22 18:58:55 2013 +0200
+++ b/inst/generate_makefile.m	Thu Aug 22 19:01:44 2013 +0200
@@ -8,12 +8,13 @@
 MKOCTFILE = mkoctfile\n\
 FFC = ffc\n\
 \n\
-OCTFILES = @@UFL_NAME@@_FunctionSpace.oct @@UFL_NAME@@_BilinearForm.oct @@UFL_NAME@@_LinearForm.oct\n\
+OCTFILES = @@UFL_NAME@@_FunctionSpace.oct @@UFL_NAME@@_BilinearForm.oct @@UFL_NAME@@_LinearForm.oct @@UFL_NAME@@_Functional.oct\n\
 \n\
 all : $(OCTFILES)\n\
 fs : @@UFL_NAME@@_FunctionSpace.oct\n\
 rhs : @@UFL_NAME@@_BilinearForm.oct\n\
 lhs : @@UFL_NAME@@_LinearForm.oct\n\
+fun : @@UFL_NAME@@_Functional.oct\n\
 \n\
 @@UFL_NAME@@_FunctionSpace.oct: @@UFL_NAME@@.h @@UFL_NAME@@_FunctionSpace.cc\n\
 	$(MKOCTFILE) @@UFL_NAME@@_FunctionSpace.cc -I$(DIR) -I. $(LDFLAGS) $(CPPFLAGS) $(LIBS)\n\
@@ -24,6 +25,9 @@
 @@UFL_NAME@@_LinearForm.oct: @@UFL_NAME@@.h @@UFL_NAME@@_LinearForm.cc\n\
 	$(MKOCTFILE) @@UFL_NAME@@_LinearForm.cc -I$(DIR) -I. $(LDFLAGS) $(CPPFLAGS) $(LDFLAGS) $(LIBS)\n\
 \n\
+@@UFL_NAME@@_Functional.oct: @@UFL_NAME@@.h @@UFL_NAME@@_Functional.cc\n\
+	$(MKOCTFILE) @@UFL_NAME@@_Functional.cc -I$(DIR) -I. $(LDFLAGS) $(CPPFLAGS) $(LDFLAGS) $(LIBS)\n\
+\n\
 @@UFL_NAME@@.h: @@UFL_NAME@@.ufl\n\
 	$(FFC) -l dolfin @@UFL_NAME@@.ufl\n\
 \n\
@@ -33,6 +37,7 @@
 	 rm -f @@UFL_NAME@@_FunctionSpace.o @@UFL_NAME@@_FunctionSpace.cc @@UFL_NAME@@.h\n\
 	 rm -f @@UFL_NAME@@_BilinearForm.o @@UFL_NAME@@_BilinearForm.cc\n\
 	 rm -f @@UFL_NAME@@_LinearForm.o @@UFL_NAME@@_LinearForm.cc\n\
+	 rm -f @@UFL_NAME@@_Functional.o @@UFL_NAME@@_Functional.cc\n\
 	 rm -f Makefile_@@UFL_NAME@@\n\
 ";