///
/// This file is part of Rheolef.
///
/// Copyright (C) 2000-2018 Pierre Saramito <Pierre.Saramito@imag.fr>
///
/// Rheolef 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.
///
/// Rheolef 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 Rheolef; if not, write to the Free Software
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
/// 
/// =========================================================================
//! @examplefile dirichlet_hdg_average.icc The Poisson problem by the hybrid discontinuous Galerkin method -- local averaging function
field dirichlet_hdg_average (field uh, field lambda_h) {
  size_t k = uh.get_space().degree();
  size_t d = uh.get_geo().dimension();
  space Zh (uh.get_geo(), "P0");
  trial zeta(Zh); test xi(Zh);
  integrate_option iopt;
  iopt.invert = true;
  if (k >= 1) {
    form inv_mz = integrate (zeta*xi, iopt);
    iopt.set_order(2*k+2);
    field lh = integrate (uh*xi, iopt);
    return inv_mz*lh;
  }
  const space& Mh = lambda_h.get_space();
  trial lambda (Mh); test mu (Mh);
  form inv_ms = integrate("sides", lambda*mu, iopt);
  field inv_sh = inv_ms*field(Mh,1);
  field lh = integrate (on_local_sides(inv_sh*lambda_h*xi));
  return (1./(d+1))*lh;
}
