sandbox/ecipriano/src/gradients.h

    Dirichlet boundary condition

    This function returns the gradient of scalar s, normal to the embedded boundary defined by cs, of unit normal vector n (normalised using the Euclidean norm, not the box norm) and of centroid p. The Dirichlet boundary condition bc is imposed on the embedded boundary.

    The calculation follows Johansen and Colella, 1998 and is summarised in the figure below (see also Figure 4 of Johansen and Colella and Figure 2 of Schwartz et al, 2006 for the 3D implementation). For interface gradients in VOF simulations, we emply the VOF-biased scheme proposed by Bothe and Fleckenstein, 2013.

    Third-order normal gradient scheme

    For degenerate cases, a non-zero value of coef is returned and coef*s[] must be added to the value returned to obtain the gradient.

    #define quadratic(x,a1,a2,a3) \
      (((a1)*((x) - 1.) + (a3)*((x) + 1.))*(x)/2. - (a2)*((x) - 1.)*((x) + 1.))
    
    typedef struct {
      double d[2], v[2], coef;
    } dstats;
    
    foreach_dimension()
    static inline double vof_dirichlet_gradient_x (Point point, scalar s,
                 scalar cs, face vector fs,
                 coord n, coord p, double bc,
                 double * coef, bool vof = false, dstats * stat = NULL)
    {
      foreach_dimension()
        n.x = - n.x;
      double d[2], v[2] = {nodata,nodata};
      bool defined = true;
      foreach_dimension()
        if (defined && !fs.x[(n.x > 0.)])
          defined = false;
      if (defined)
        for (int l = 0; l <= 1; l++) {
          int i = (l + 1)*sign(n.x);
          d[l] = (i - p.x)/n.x;
          double y1 = p.y + d[l]*n.y;
          int j = y1 > 0.5 ? 1 : y1 < -0.5 ? -1 : 0;
          y1 -= j;
    #if dimension == 2
          if (fs.x[i + (i < 0),j] && fs.y[i,j] && fs.y[i,j+1] &&
              cs[i,j-1] && cs[i,j] && cs[i,j+1])
            v[l] = quadratic (y1, (s[i,j-1]), (s[i,j]), (s[i,j+1]));
    #else // dimension == 3
          double z = p.z + d[l]*n.z;
          int k = z > 0.5 ? 1 : z < -0.5 ? -1 : 0;
          z -= k;
          bool defined = fs.x[i + (i < 0),j,k];
          for (int m = -1; m <= 1 && defined; m++)
            if (!fs.y[i,j,k+m] || !fs.y[i,j+1,k+m] ||
                !fs.z[i,j+m,k] || !fs.z[i,j+m,k+1] ||
                !cs[i,j+m,k-1] || !cs[i,j+m,k] || !cs[i,j+m,k+1])
              defined = false;
          if (defined)
            // bi-quadratic interpolation
            v[l] =
              quadratic (z,
                   quadratic (y1,
                  (s[i,j-1,k-1]), (s[i,j,k-1]), (s[i,j+1,k-1])),
                   quadratic (y1,
                  (s[i,j-1,k]),   (s[i,j,k]),   (s[i,j+1,k])),
                   quadratic (y1,
                  (s[i,j-1,k+1]), (s[i,j,k+1]), (s[i,j+1,k+1])));
    #endif // dimension == 3
          else
            break;
        }
    
      if (stat) {
        stat->d[0] = d[0], stat->d[1] = d[1];
        stat->v[0] = v[0], stat->v[1] = v[1];
      }
    
      if (v[0] == nodata) {

    This is a degenerate case, we use the boundary value and the cell-center value to define the gradient.

        d[0] = max(1e-3, fabs(p.x/n.x));
        *coef = - 1./(d[0]*Delta);
        return bc/(d[0]*Delta);
      }

    For non-degenerate cases, the gradient is obtained using either second- or third-order estimates.

      *coef = 0.;
      if (v[1] != nodata && vof) // vof-biased scheme
        return (cs[]*(bc - v[0])/d[0] + (1. - cs[])*(bc - v[1])/d[1])/Delta;
      else if (v[1] != nodata) // third-order gradient
        return (d[1]*(bc - v[0])/d[0] - d[0]*(bc - v[1])/d[1])/((d[1] - d[0])*Delta);
      else
        return (bc - v[0])/(d[0]*Delta); // second-order gradient
    }

    vof_dirichlet_gradient(): embed interface gradients

    double vof_dirichlet_gradient (Point point, scalar s, scalar cs, face vector fs,
             coord n, coord p, double bc, double * coef,
             bool vof = false, dstats * stat = NULL)
    {
    #if dimension == 2
      foreach_dimension()
        if (fabs(n.x) >= fabs(n.y))
          return vof_dirichlet_gradient_x (point, s, cs, fs, n, p, bc, coef, vof, stat);
    #else // dimension == 3
      if (fabs(n.x) >= fabs(n.y)) {
        if (fabs(n.x) >= fabs(n.z))
          return vof_dirichlet_gradient_x (point, s, cs, fs, n, p, bc, coef, vof, stat);
      }
      else if (fabs(n.y) >= fabs(n.z))
        return vof_dirichlet_gradient_y (point, s, cs, fs, n, p, bc, coef, vof, stat);
      return vof_dirichlet_gradient_z (point, s, cs, fs, n, p, bc, coef, vof, stat);
    #endif // dimension == 3
      return nodata;
    }

    plic_gradient(): VOF interface gradients

    The interface gradients for VOF-tracers can be computed using the following function, provided the volume and surface fractions cs and fs which define the region where the tracers s is defined, and the value of the field at the interface (bc). In degenerate cases the gradient is zero and the degenerate coefficient is stored in stat->coef.

    Note that, inside foreach loops, all the arguments must be given explicitly: omitting the optional ones makes qcc pass NULL for the face vector fs in the stencil functions.

    double plic_gradient (Point point, scalar s, scalar cs, face vector fs,
        double bc, bool vof = true, dstats * stat = NULL)
    {
      coord m = vof ?
        interface_normal (point, cs) :
        facet_normal (point, cs, fs), p;
      double alpha = plane_alpha (cs[], m);
      plane_area_center (m, alpha, &p);
      normalize (&m);
      double coef = 0.;
      double grad = vof_dirichlet_gradient (point, s, cs, fs, m, p, bc, &coef, vof, stat);
      if (stat)
        stat->coef = coef;
      return (grad == nodata || coef != 0.) ? 0. : grad;
    }

    plic_navier(): Navier/Robin interface values

    This function returns the interface value s_b which satisfies the Navier/Robin condition s_b + \lambda \partial_n s|_b = expr with \mathbf{n} pointing out of the region where cs = 1 (as for the domain boundaries). The gradient computed by plic_gradient() is linear in the interface value, \partial_n s|_b = a s_b + b, so two evaluations give a and b, and s_b = \frac{expr - \lambda b}{1 + \lambda a} In degenerate cases we use the gradient -coef\,(s_b - s[]) of embed_flux().

    double plic_navier (Point point, scalar s, scalar cs, face vector fs,
        double expr, double lambda, bool vof = true)
    {
      dstats stat;
      double b = plic_gradient (point, s, cs, fs, 0., vof, &stat);
      double a = plic_gradient (point, s, cs, fs, 1., vof, &stat) - b;
      if (stat.coef)
        a = - stat.coef, b = stat.coef*s[];
      return (expr - lambda*b)/(1. + lambda*a);
    }

    On embedded boundaries the Navier condition is turned into a Dirichlet condition with value given by plic_navier(). On the domain boundaries we use the default navier() condition.

    #if EMBED
    macro2
    double navier (double expr, double lambda, Point point = point,
        scalar s = _s, bool * data = data)
    {
      return data ? embed_area_center (point, &x, &y, &z),
        *((bool *)data) = true,
        plic_navier (point, s, cs, fs, expr, lambda, false) :
        (Delta*expr + s[]*(lambda - Delta/2.))/(lambda + Delta/2.);
    }
    
    macro2
    double navier_homogeneous (double expr, double lambda, Point point = point,
        scalar s = _s, bool * data = data)
    {
      return data ? embed_area_center (point, &x, &y, &z),
        *((bool *)data) = true,
        plic_navier (point, s, cs, fs, 0., lambda, false) :
        s[]*(lambda - Delta/2.)/(lambda + Delta/2.);
    }
    #endif

    References

    [bothe2013]

    Dieter Bothe and Stefan Fleckenstein. A volume-of-fluid-based method for mass transfer processes at fluid particles. Chemical Engineering Science, 101:283–302, 2013.

    [schwartz2006]

    Peter Schwartz, Michael Barad, Phillip Colella, and Terry Ligocki. A cartesian grid embedded boundary method for the heat equation and poisson’s equation in three dimensions. Journal of Computational Physics, 211(2):531–550, 2006. [ .pdf ]

    [johansen1998]

    Hans Johansen and Phillip Colella. A cartesian grid embedded boundary method for poisson’s equation on irregular domains. Journal of Computational Physics, 147(1):60–85, 1998. [ .pdf ]