[32a023]: obsolete / fem_rhs.cc Maximize Restore History

Download this file

fem_rhs.cc    147 lines (126 with data), 5.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
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/>.
*/
#include "Laplace.h"
#include <dolfin.h>
#include "functionspace.h"
#include "boundarycondition.h"
#include "coefficient.h"
DEFUN_DLD (fem_rhs, args, , "-*- texinfo -*-\n\
@deftypefn {Function File} {[@var{bc}]} = \
fem_rhs (@var{Functional Space}, @var{Coefficient}, \
@var{Boundary Condition}) \n\
The input parameters are\n\
@itemize @bullet \n\
@item @var{Functional Space} is the fem-fenics functional space where\
the bilinear form is defined\n\
@item @var{Boundary Condition} contains the value of the essential bc that\
we want to apply to the Bilinear Form\n\
@item @var{Coefficient} is a variable of type coefficient which contains\
the value of the coefficient for the bilinear form\n\
@end itemize\n\
The output @var{A} is a sparse matrix which represents the bilinear form\n\
@seealso{fem_init_mesh, fem_fs}\n\
@end deftypefn")
{
int nargin = args.length ();
octave_value retval;
if (nargin < 1)
print_usage ();
else
{
if (! functionspace_type_loaded)
{
functionspace::register_type ();
functionspace_type_loaded = true;
mlock ();
}
if (args(0).type_id () == functionspace::static_type_id ())
{
const functionspace & fspo
= static_cast<const functionspace&> (args(0).get_rep ());
if (! error_state)
{
const dolfin::FunctionSpace V = fspo.get_fsp ();
Laplace::BilinearForm a (V, V);
std::size_t ncoef = a.num_coefficients (), nc = 0;
if (! coefficient_type_loaded)
{
coefficient::register_type ();
coefficient_type_loaded = true;
mlock ();
}
for (std::size_t i = 1; i < nargin; ++i)
{
if (args(i).type_id () == coefficient::static_type_id ())
{
const coefficient & cf
= static_cast <const coefficient&> (args(i).get_rep ());
std::size_t n = a.coefficient_number (cf.get_str ());
const boost::shared_ptr<const expression> & pexp = cf.get_expr ();
a.set_coefficient (n, pexp);
++nc;
}
}
if (nc != ncoef)
error ("Wrong number of coefficient");
else
{
dolfin::Matrix A;
dolfin::assemble (A, a);
if (! boundarycondition_type_loaded)
{
boundarycondition::register_type ();
boundarycondition_type_loaded = true;
mlock ();
}
for (std::size_t i = 1; i < nargin; ++i)
{
if (args(i).type_id () == boundarycondition::static_type_id ())
{
const boundarycondition & bc
= static_cast<const boundarycondition&> (args(i).get_rep ());
const std::vector<boost::shared_ptr <const dolfin::DirichletBC> > & pbc
= bc.get_bc ();
for (std::size_t j = 0; j < pbc.size (); ++j)
pbc[j]->apply(A);
}
}
int nr = A.size (0), nc = A.size (1);
// nz shoud be estimated in a better way
int nz = nr * nc;
SparseMatrix sm (nr, nc, nz);
int ii = 0;
sm.cidx (0) = 0;
for (int j = 0; j < nc; ++j)
{
for (int i = 0; i < nr; i++)
{
double tmp = A(i, j);
if (tmp != 0.)
{
sm.data(ii) = tmp;
sm.ridx(ii) = i;
ii++;
}
}
sm.cidx(j+1) = ii;
}
sm.maybe_compress ();
retval = sm;
}
}
}
}
return retval;
}