## Copyright (C) 2013 Marco Vassallo <gedeone-octave@users.sourceforge.net>
##
## 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_rhs (ufl_name)

STRING ="\n\
#include ""@@UFL_NAME@@.h""\n\
#include <fem-fenics/form.h>\n\
#include <fem-fenics/coefficient.h>\n\
#include <fem-fenics/function.h>\n\
#include <fem-fenics/functionspace.h>\n\
\n\
DEFUN_DLD (@@UFL_NAME@@_BilinearForm, 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 (! functionspace_type_loaded)\n\
        {\n\
          functionspace::register_type ();\n\
          functionspace_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 () == functionspace::static_type_id ())\n\
        {\n\
          const functionspace & fspo\n\
            = static_cast<const functionspace&> (args(0).get_rep ());\n\
\n\
          if (! error_state)\n\
            {\n\
              const dolfin::FunctionSpace & V = fspo.get_fsp ();\n\
              @@UFL_NAME@@::BilinearForm a (V, V);\n\
              std::size_t ncoef = a.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 = a.coefficient_number (cf.get_str ());\n\
                      const boost::shared_ptr<const expression> & pexp = cf.get_expr ();\n\
                      a.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 = a.coefficient_number (fun.get_str ());\n\
                      const boost::shared_ptr<const dolfin::Function> & pfun = fun.get_pfun ();\n\
                      a.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 (a);\n\
                }\n\
            }\n\
        }\n\
    }\n\
  return retval;\n\
}";

STRING =  strrep (STRING, "@@UFL_NAME@@", ufl_name);

fid = fopen (sprintf ("%s_BilinearForm.cc", ufl_name), 'w');
if (fid >= 0)
  fputs (fid, STRING);
  output = fclose (fid);
else
  error ("cannot open file");
  output = 1;
endif

endfunction
