src/viscosity.h

    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
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    
    #include "poisson.h"
    
    struct Viscosity {
      vector u;
      face vector mu;
      scalar rho;
      double dt;
      int nrelax;
      scalar * res;
    };
    
    #if AXI
    # define lambda ((coord){1., 1. + dt/rho[]*(mu.x[] + mu.x[1] + \
    					    mu.y[] + mu.y[0,1])/2./sq(y)})
    #else // not AXI
    # if dimension == 1
    #   define lambda ((coord){1.})
    # elif dimension == 2
    #   define lambda ((coord){1.,1.})
    # elif dimension == 3
    #   define lambda ((coord){1.,1.,1.})
    #endif
    #endif
    
    static void relax_viscosity (scalar * a, scalar * b, int l, void * data)
    {
      struct Viscosity * p = (struct Viscosity *) data;
      (const) face vector mu = p->mu;
      (const) scalar rho = p->rho;
      double dt = p->dt;
      vector u = vector(a[0]), r = vector(b[0]);
    
    #if JACOBI
      vector w[];
    #else
      vector w = u;
    #endif
    
      foreach_level_or_leaf (l) {
        foreach_dimension()
          w.x[] = (dt/rho[]*(2.*mu.x[1]*u.x[1] + 2.*mu.x[]*u.x[-1]
                   #if dimension > 1
    			   + mu.y[0,1]*(u.x[0,1] +
    					(u.y[1,0] + u.y[1,1])/4. -
    					(u.y[-1,0] + u.y[-1,1])/4.)
    			   - mu.y[]*(- u.x[0,-1] +
    				     (u.y[1,-1] + u.y[1,0])/4. -
    				     (u.y[-1,-1] + u.y[-1,0])/4.)
                   #endif
    	       #if dimension > 2
    			   + mu.z[0,0,1]*(u.x[0,0,1] +
    					  (u.z[1,0,0] + u.z[1,0,1])/4. -
    					  (u.z[-1,0,0] + u.z[-1,0,1])/4.)
    			   - mu.z[]*(- u.x[0,0,-1] +
    				     (u.z[1,0,-1] + u.z[1,0,0])/4. -
    				     (u.z[-1,0,-1] + u.z[-1,0,0])/4.)
                   #endif
    			   ) + r.x[]*sq(Delta))/
        (sq(Delta)*lambda.x + dt/rho[]*(2.*mu.x[1] + 2.*mu.x[]
                                        #if dimension > 1
    				      + mu.y[0,1] + mu.y[]
                                        #endif
    			            #if dimension > 2
    				      + mu.z[0,0,1] + mu.z[]
    			            #endif
    			     ));
      }
    
    #if JACOBI
      foreach_level_or_leaf (l)
        foreach_dimension()
          u.x[] = (u.x[] + 2.*w.x[])/3.;
    #endif
    
    #if TRASH
      vector u1[];
      foreach_level_or_leaf (l)
        foreach_dimension()
          u1.x[] = u.x[];
      trash ({u});
      foreach_level_or_leaf (l)
        foreach_dimension()
          u.x[] = u1.x[];
    #endif
    }
    
    static double residual_viscosity (scalar * a, scalar * b, scalar * resl, 
    				  void * data)
    {
      struct Viscosity * p = (struct Viscosity *) data;
      (const) face vector mu = p->mu;
      (const) scalar rho = p->rho;
      double dt = p->dt;
      vector u = vector(a[0]), r = vector(b[0]), res = vector(resl[0]);
      double maxres = 0.;
    #if TREE
      /* conservative coarse/fine discretisation (2nd order) */
      foreach_dimension() {
        face vector taux[];
        foreach_face(x)
          taux.x[] = 2.*mu.x[]*(u.x[] - u.x[-1])/Delta;
        #if dimension > 1
          foreach_face(y)
    	taux.y[] = mu.y[]*(u.x[] - u.x[0,-1] + 
    			   (u.y[1,-1] + u.y[1,0])/4. -
    			   (u.y[-1,-1] + u.y[-1,0])/4.)/Delta;
        #endif
        #if dimension > 2
          foreach_face(z)
    	taux.z[] = mu.z[]*(u.x[] - u.x[0,0,-1] + 
    			   (u.z[1,0,-1] + u.z[1,0,0])/4. -
    			   (u.z[-1,0,-1] + u.z[-1,0,0])/4.)/Delta;
        #endif
        boundary_flux ({taux});
        foreach (reduction(max:maxres)) {
          double d = 0.;
          foreach_dimension()
    	d += taux.x[1] - taux.x[];
          res.x[] = r.x[] - lambda.x*u.x[] + dt/rho[]*d/Delta;
          if (fabs (res.x[]) > maxres)
    	maxres = fabs (res.x[]);
        }
      }
      boundary (resl);
    #else
      /* "naive" discretisation (only 1st order on trees) */
      foreach (reduction(max:maxres))
        foreach_dimension() {
          res.x[] = r.x[] - lambda.x*u.x[] +
            dt/rho[]*(2.*mu.x[1,0]*(u.x[1] - u.x[])
    		  - 2.*mu.x[]*(u.x[] - u.x[-1])
            #if dimension > 1
    		  + mu.y[0,1]*(u.x[0,1] - u.x[] +
    			       (u.y[1,0] + u.y[1,1])/4. -
    			       (u.y[-1,0] + u.y[-1,1])/4.)
    		  - mu.y[]*(u.x[] - u.x[0,-1] +
    			    (u.y[1,-1] + u.y[1,0])/4. -
    			    (u.y[-1,-1] + u.y[-1,0])/4.)
    	#endif
            #if dimension > 2
    		  + mu.z[0,0,1]*(u.x[0,0,1] - u.x[] +
    				 (u.z[1,0,0] + u.z[1,0,1])/4. -
    				 (u.z[-1,0,0] + u.z[-1,0,1])/4.)
    		  - mu.z[]*(u.x[] - u.x[0,0,-1] +
    			    (u.z[1,0,-1] + u.z[1,0,0])/4. -
    			    (u.z[-1,0,-1] + u.z[-1,0,0])/4.)
    	#endif
    		  )/sq(Delta);
          if (fabs (res.x[]) > maxres)
    	maxres = fabs (res.x[]);
        }
    #endif
      return maxres;
    }
    
    #undef lambda
    
    trace
    mgstats viscosity (struct Viscosity p)
    {
      vector u = p.u, r[];
      foreach()
        foreach_dimension()
          r.x[] = u.x[];
    
      face vector mu = p.mu;
      scalar rho = p.rho;
      restriction ({mu,rho});
    
      return mg_solve ((scalar *){u}, (scalar *){r},
    		   residual_viscosity, relax_viscosity, &p, p.nrelax, p.res);
    }
    
    trace
    mgstats viscosity_explicit (struct Viscosity p)
    {
      vector u = p.u, r[];
      mgstats mg = {0};
      mg.resb = residual_viscosity ((scalar *){u}, (scalar *){u}, (scalar *){r}, &p);
      foreach()
        foreach_dimension()
          u.x[] += r.x[];
      boundary ((scalar *){u});
      return mg;
    }