1## Copyright (C) 2006,2007,2008,2009,2010  Carlo de Falco, Massimiliano Culpo
2##
3## This file is part of:
4##     BIM - Diffusion Advection Reaction PDE Solver
5##
6##  BIM is free software; you can redistribute it and/or modify
7##  it under the terms of the GNU General Public License as published by
8##  the Free Software Foundation; either version 2 of the License, or
9##  (at your option) any later version.
10##
11##  BIM is distributed in the hope that it will be useful,
12##  but WITHOUT ANY WARRANTY; without even the implied warranty of
13##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14##  GNU General Public License for more details.
15##
16##  You should have received a copy of the GNU General Public License
17##  along with BIM; If not, see <http://www.gnu.org/licenses/>.
18##
19##  author: Carlo de Falco     <cdf _AT_ users.sourceforge.net>
20##  author: Massimiliano Culpo <culpo _AT_ users.sourceforge.net>
21
22## -*- texinfo -*-
23##
24## @deftypefn {Function File} {[@var{gx}, @var{gy}, @var{gz}]} = @
25## bim3c_pde_gradient(@var{mesh},@var{u})
26##
27## Compute the gradient of the piecewise linear conforming scalar
28## function @var{u}.
29##
30## @seealso{bim3c_global_flux}
31## @end deftypefn
32
33function [gx, gy, gz] = bim3c_pde_gradient (mesh, u)
34
35  ## Check input
36  if (nargin != 2)
37    error("bim3c_pde_gradient: wrong number of input parameters.");
38  elseif (! (isstruct (mesh) && isfield (mesh,"p")) &&
39	   isfield (mesh, "t") && isfield(mesh, "e"))
40    error ("bim3c_pde_gradient: first input is not a valid mesh structure.");
41  endif
42
43  nnodes = columns (mesh.p);
44
45  if (numel (u) != nnodes)
46    error ("bim3c_pde_gradient: length(u) != nnodes.");
47  endif
48
49  gx   = sum (squeeze (mesh.shg(1,:,:)) .* u(mesh.t(1:4,:)), 1);
50  gy   = sum (squeeze (mesh.shg(2,:,:)) .* u(mesh.t(1:4,:)), 1);
51  gz   = sum (squeeze (mesh.shg(3,:,:)) .* u(mesh.t(1:4,:)), 1);
52
53endfunction
54