LCOV - code coverage report
Current view: top level - src/Integrator - AllenCahn.H (source / functions) Hit Total Coverage
Test: coverage_merged.info Lines: 57 60 95.0 %
Date: 2024-11-18 05:28:54 Functions: 8 8 100.0 %

          Line data    Source code
       1             : //
       2             : // This is a simple implementation of an Allen-Cahn equation governed by
       3             : // 
       4             : // .. math::
       5             : //
       6             : //    \frac{\partial\alpha}{\partial t} = 
       7             : //               - L \Big(\frac{\lambda}{\epsilon}(2.0\alpha 
       8             : //                        - 6\alpha^2 + 4\alpha^3) + \epsilon\,\kappa\,\Delta \alpha \Big)
       9             : // 
      10             : // where :math:`\alpha(x,t)` is the order parameter.
      11             : //
      12             : 
      13             : #ifndef INTEGRATOR_ALLENCAHN_H // Include guards
      14             : #define INTEGRATOR_ALLENCAHN_H // 
      15             : 
      16             : // Standard library includes
      17             : #include <iostream>
      18             : #include <fstream>
      19             : #include <iomanip>
      20             : 
      21             : // AMReX Includes
      22             : #include "AMReX.H"
      23             : #include "AMReX_ParallelDescriptor.H"
      24             : #include "AMReX_ParmParse.H"
      25             : 
      26             : // Alamo Includes
      27             : #include "IO/ParmParse.H"
      28             : #include "Integrator/Integrator.H"
      29             : #include "BC/Constant.H"
      30             : #include "IC/IC.H"
      31             : #include "IC/Sphere.H"
      32             : #include "IC/Constant.H"
      33             : #include "IC/Expression.H"
      34             : #include "IC/BMP.H"
      35             : #include "IC/PNG.H"
      36             : #include "IC/PSRead.H"
      37             : #include "Numeric/Stencil.H"
      38             : #include "IC/Random.H"
      39             : 
      40             : namespace Integrator
      41             : {
      42             : class AllenCahn : virtual public Integrator
      43             : {
      44             : public:
      45             :     // Empty constructor
      46           2 :     AllenCahn() : Integrator()
      47           2 :     {}
      48             : 
      49             :     // Constructor that triggers parse
      50           2 :     AllenCahn(IO::ParmParse& pp) : AllenCahn()
      51             :     {
      52           2 :         Parse(*this, pp);
      53           2 :     }
      54             : 
      55             :     // The Parse function initializes the AllenCahn object using
      56             :     // a parser, pp. 
      57             :     // Note that this is a static function, which means it does not have
      58             :     // direct access to member variables. Instead, it initializes the variables
      59             :     // inside the argument, "value", and so all references to member items are
      60             :     // prefixed by "value."
      61           2 :     static void Parse(AllenCahn& value, IO::ParmParse& pp)
      62             :     {
      63             :         // Criterion for mesh refinement [0.01]
      64           2 :         pp_query_default("refinement_threshold", value.refinement_threshold, 0.01);
      65             : 
      66             :         // Value for :math:`L` (mobility)
      67           2 :         pp_query_default("ch.L",value.ch.L, 1.0);
      68             :         // Value for :math:`\epsilon` (diffuse boundary width)
      69           2 :         pp_query_default("ch.eps",value.ch.eps, 0.1);
      70             :         // Value for :math:`\kappa` (Interface energy parameter)
      71           2 :         pp_query_default("ch.grad",value.ch.grad, 1.0);
      72             :         // Value for :math:`\lambda` (Chemical potential coefficient)
      73           2 :         pp_query_default("ch.chempot",value.ch.chempot, 1.0);
      74             : 
      75           2 :         std::string type = "sphere";
      76             :         // Initial condition type ([sphere], constant, expression, bmp, random)
      77           2 :         pp_query_validate("alpha.ic.type", type, {"sphere","constant","expression","bmp","random","psread"});
      78           2 :         if (type == "sphere")          value.ic = new IC::Sphere(value.geom, pp, "alpha.ic.sphere");
      79           2 :         else if (type == "constant")   value.ic = new IC::Constant(value.geom, pp, "alpha.ic.constant");
      80           2 :         else if (type == "expression") value.ic = new IC::Expression(value.geom, pp, "alpha.ic.expression");
      81           2 :         else if (type == "bmp")        value.ic = new IC::BMP(value.geom, pp, "alpha.ic.bmp");
      82           0 :         else if (type == "random")     value.ic = new IC::Random(value.geom, pp, "alpha.ic.random");
      83           0 :         else if (type == "psread")     value.ic = new IC::PSRead(value.geom, pp, "alpha.ic.psread");
      84           0 :         else  Util::Abort(INFO, "Invalid ic.type ", type);
      85             : 
      86             :         // Use a constant BC object for temperature
      87           2 :         value.bc = new BC::Constant(1);
      88             :         // :ref:`BC::Constant` parameters 
      89           2 :         pp_queryclass("alpha.bc", *static_cast<BC::Constant*>(value.bc));
      90             : 
      91             :         // Register the temperature and old temperature fields.
      92             :         // alpha_mf and alpha_old_mf are defined near the bottom of this Header file.
      93           2 :         value.RegisterNewFab(value.alpha_mf, value.bc, value.number_of_components, value.number_of_ghost_cells, "alpha", true);
      94           2 :         value.RegisterNewFab(value.alpha_old_mf, value.bc, value.number_of_components, value.number_of_ghost_cells, "alpha_old", false);
      95           2 :     }
      96             : 
      97             : protected:
      98             : 
      99             :     // Use the ic object to initialize the order parameter
     100           6 :     void Initialize(int lev)
     101             :     {
     102           6 :         ic->Initialize(lev, alpha_mf);
     103           6 :         ic->Initialize(lev, alpha_old_mf);
     104           6 :     }
     105             : 
     106             :     // Integrate the Allen Cahn equation
     107        7700 :     void Advance(int lev, Set::Scalar /*time*/, Set::Scalar dt)
     108             :     {
     109        7700 :         std::swap(*alpha_mf[lev], *alpha_old_mf[lev]);
     110             : 
     111        7700 :         const Set::Scalar *DX = this->geom[lev].CellSize();
     112             :         // Evolve alpha
     113       15400 :         for (amrex::MFIter mfi(*alpha_mf[lev], true); mfi.isValid(); ++mfi)
     114             :         {
     115        7700 :             const amrex::Box& bx = mfi.tilebox();
     116        7700 :             amrex::Array4<Set::Scalar> const& alpha_new = (*alpha_mf[lev]).array(mfi);
     117        7700 :             amrex::Array4<const Set::Scalar> const& alpha = (*alpha_old_mf[lev]).array(mfi);
     118             : 
     119        7700 :             amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k)
     120             :             {
     121   218414000 :                 Set::Scalar driving_force = 0.0;
     122             :                 // Chemical potential
     123   436828000 :                 Set::Scalar alpha_2 = alpha(i, j, k) * alpha(i, j, k);
     124   218414000 :                 Set::Scalar alpha_3 = alpha_2 * alpha(i, j, k);
     125   218414000 :                 driving_force += (ch.chempot / ch.eps) * (2.0 * alpha(i, j, k) - 6.0 * alpha_2 + 4.0 * alpha_3);
     126             :                 // Gradient
     127   218414000 :                 Set::Scalar alpha_lap = Numeric::Laplacian(alpha, i, j, k, 0, DX);
     128   218414000 :                 driving_force -= ch.eps * ch.grad * alpha_lap;
     129             :                 // Update
     130   436828000 :                 alpha_new(i, j, k) =
     131   436828000 :                     alpha(i, j, k) - ch.L * dt * driving_force;
     132   218414000 :             });
     133             :         }
     134        7700 :     }
     135             : 
     136             :     // Tag cells for mesh refinement based on temperature gradient
     137        2208 :     void TagCellsForRefinement(int lev, amrex::TagBoxArray& a_tags, Set::Scalar /*time*/, int /*ngrow*/)
     138             :     {
     139             :         // Get cell dimensions as done above.
     140        2208 :         const Set::Scalar* DX = geom[lev].CellSize();
     141             :         // Calculate the diagonal.
     142        2208 :         Set::Scalar dr = sqrt(AMREX_D_TERM(DX[0] * DX[0], +DX[1] * DX[1], +DX[2] * DX[2]));
     143             : 
     144             :         // Iterate over the patches on this level
     145        4416 :         for (amrex::MFIter mfi(*alpha_mf[lev], amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi)
     146             :         {
     147             :             // Get the box and handles as done above.
     148        2208 :             const amrex::Box& bx = mfi.tilebox();
     149        2208 :             amrex::Array4<char>         const& tags = a_tags.array(mfi);
     150        2208 :             amrex::Array4<Set::Scalar>  const& alpha = (*alpha_mf[lev]).array(mfi);
     151             : 
     152             :             // Iterate over the grid as done above.
     153        2208 :             amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k)
     154             :             {
     155             :                 // Calculate the temperature gradient.
     156    21927900 :                 Set::Vector grad = Numeric::Gradient(alpha, i, j, k, 0, DX);
     157             : 
     158             :                 // Is the gradient * cell_size too big? If so, then
     159             :                 // mark this cell as needing refinement.
     160    21927900 :                 if (grad.lpNorm<2>() * dr > refinement_threshold)
     161    32841400 :                     tags(i, j, k) = amrex::TagBox::SET;
     162    21927900 :             });
     163             :         }
     164        2208 :     }
     165             : 
     166             : protected:
     167             :     Set::Field<Set::Scalar> alpha_mf;         // Temperature field variable (current timestep)
     168             :     Set::Field<Set::Scalar> alpha_old_mf;     // Temperature field variable (previous timestep)
     169             : 
     170             :     struct {
     171             :         Set::Scalar L = NAN;
     172             :         Set::Scalar eps = NAN;
     173             :         Set::Scalar grad = NAN;
     174             :         Set::Scalar chempot = NAN;
     175             :     } ch;
     176             : 
     177             : private:
     178             :     int number_of_components = 1;            // Number of components
     179             :     int number_of_ghost_cells = 2;           // Number of ghost cells
     180             : 
     181             :     Set::Scalar refinement_threshold = NAN; // Criterion for cell refinement
     182             : 
     183             :     IC::IC* ic;                              // Object used to initialize temperature field
     184             :     BC::BC<Set::Scalar>* bc;                 // Object used to update temp field boundary ghost cells
     185             : };
     186             : } // namespace Integrator
     187             : #endif

Generated by: LCOV version 1.14