LCOV - code coverage report
Current view: top level - src/IC - PSRead.H (source / functions) Coverage Total Hit
Test: coverage_merged.info Lines: 83.1 % 65 54
Test Date: 2025-02-27 04:17:48 Functions: 100.0 % 4 4

            Line data    Source code
       1              : //
       2              : // Fill a domain (region where field=0) with packed spheres (regions where field=1).
       3              : // Sphere locations and radii are determined from an xyzr file.
       4              : //
       5              : 
       6              : #ifndef IC_PSREAD_H_
       7              : #define IC_PSREAD_H_
       8              : 
       9              : #include "Set/Set.H"
      10              : #include "IC/IC.H"
      11              : using namespace std;
      12              : #include <iostream>
      13              : #include <vector>
      14              : #include <algorithm>
      15              : #include <iterator>
      16              : #include "mpi.h"
      17              : #include "IO/ParmParse.H"
      18              : 
      19              : namespace IC
      20              : {
      21              : class PSRead : public IC<Set::Scalar>
      22              : {
      23              : public:
      24              :     static constexpr const char* name = "psread";
      25              : 
      26              :     enum Type
      27              :     {
      28              :         Partition,
      29              :         Values
      30              :     };
      31              : 
      32              :     PSRead(amrex::Vector<amrex::Geometry>& _geom) : IC(_geom) {}
      33              : 
      34            1 :     PSRead(amrex::Vector<amrex::Geometry>& _geom, IO::ParmParse& pp, std::string name) : IC(_geom)
      35              :     {
      36            3 :         pp_queryclass(name, *this);
      37            1 :     }
      38              : 
      39              :     void Define() {
      40              : 
      41              :     };
      42              : 
      43           17 :     void Add(const int& lev, Set::Field<Set::Scalar>& a_phi, Set::Scalar)
      44              :     {
      45           17 :         Set::Vector size = Set::Size(geom[lev]);
      46           17 :         int ncomp = a_phi[lev]->nComp();
      47              : 
      48          308 :         for (amrex::MFIter mfi(*a_phi[lev], amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi)
      49              :         {
      50          291 :             amrex::Box bx;
      51          291 :             amrex::IndexType type = a_phi[lev]->ixType();
      52          582 :             if (type == amrex::IndexType::TheCellType())      bx = mfi.growntilebox();
      53          582 :             else if (type == amrex::IndexType::TheNodeType()) bx = mfi.grownnodaltilebox();
      54            0 :             else Util::Abort(INFO, "Unkonwn index type");
      55              : 
      56          291 :             amrex::Array4<Set::Scalar> const& phi = a_phi[lev]->array(mfi);
      57          291 :             amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k)
      58              :             {
      59        89567 :                 Set::Vector x = Set::Position(i, j, k, geom[lev], type);
      60        89567 :                 Set::Scalar min_grain_id = 0;
      61              : 
      62      6538391 :                 for (unsigned int n = 0; n < X.size(); n++)
      63              :                 {
      64              :                     Set::Scalar d;
      65      6448824 :                     Set::Scalar d1 = std::numeric_limits<Set::Scalar>::infinity();
      66              : 
      67      6448824 :                     d = (x - X[n]).lpNorm<2>();
      68              : 
      69      6448824 :                     if (geom[0].isPeriodic(0))
      70              :                     {
      71            0 :                         d1 = std::min((x - X[n] + size(0) * Set::Vector::Unit(0)).lpNorm<2>(),
      72            0 :                             (x - X[n] - size(0) * Set::Vector::Unit(0)).lpNorm<2>());
      73              :                     }
      74              : #if AMREX_SPACEDIM > 1
      75      6448824 :                     if (geom[0].isPeriodic(1))
      76              :                     {
      77            0 :                         d1 = std::min((x - X[n] + size(1) * Set::Vector::Unit(1)).lpNorm<2>(),
      78            0 :                             (x - X[n] - size(1) * Set::Vector::Unit(1)).lpNorm<2>());
      79              :                     }
      80              : #endif
      81              : #if AMREX_SPACEDIM > 2
      82            0 :                     if (geom[0].isPeriodic(2))
      83              :                     {
      84            0 :                         d1 = std::min((x - X[n] + size(2) * Set::Vector::Unit(2)).lpNorm<2>(),
      85            0 :                             (x - X[n] - size(2) * Set::Vector::Unit(2)).lpNorm<2>());
      86              :                     }
      87              : #endif
      88              : 
      89      6448824 :                     d = std::min(d, d1);
      90              : 
      91      6448824 :                     if (d <= (R[n] + eps))
      92              : 
      93              :                     {
      94        71900 :                         Set::Scalar m = 0.5 * (1 + erf((-d + R[n]) / (eps)));
      95        71900 :                         min_grain_id = min_grain_id + m * (1. - min_grain_id);
      96              :                     }
      97              :                 }
      98              : 
      99        89567 :                 phi(i, j, k, 0) = min_grain_id;
     100        89567 :                 if (invert) phi(i,j,k,0) = 1.0 - min_grain_id;
     101        89567 :                 if (ncomp > 1) phi(i, j, k, 1) = 1.0 - min_grain_id;
     102        89567 :             });
     103           17 :         }
     104           17 :     }
     105              : 
     106            1 :     static void Parse(PSRead& value, IO::ParmParse& pp)
     107              :     {
     108            1 :         std::string filename;
     109            1 :         int verbose = 0;
     110            1 :         pp_query("eps", value.eps); // Diffuseness of the sphere boundary
     111            5 :         pp_query_file("filename", filename); // Location of .xyzr file
     112            1 :         pp_query("verbose", verbose); // Verbosity (used in parser only)
     113            1 :         pp_query("mult",value.mult); // Coordinate multiplier
     114            1 :         pp.query("invert",value.invert); // Coordinate multiplier
     115            2 :         if (pp.contains("x0"))
     116            0 :             pp_queryarr("x0",value.x0); // Coordinate offset
     117            1 :         std::ifstream datafile(filename);
     118            1 :         std::string line;
     119            1 :         if (datafile.is_open())
     120              :         {
     121            1 :             value.X.clear();
     122            1 :             value.R.clear();
     123              : 
     124           73 :             while (getline(datafile, line))
     125              :             {
     126           72 :                 std::istringstream in(line);
     127              : 
     128           72 :                 std::string strx, stry, strz, strR;
     129           72 :                 in >> strx >> stry >> strz >> strR;
     130           72 :                 value.X.push_back(value.x0 + value.mult*Set::Vector(AMREX_D_DECL(std::stod(strx), std::stod(stry), std::stod(strz))));
     131           72 :                 value.R.push_back(value.mult*std::stod(strR));
     132           72 :                 if (verbose > 0)
     133            0 :                     Util::Message(INFO, "x=", value.X.back().transpose(), " r=", value.R.back());
     134           72 :             }
     135            1 :             datafile.close();
     136              :         }
     137              :         else
     138              :         {
     139            0 :             Util::Abort(INFO, "Unable to open file ", filename);
     140              :         }
     141            1 :     }
     142              : 
     143              : private:
     144              :     std::vector<Set::Vector> X;
     145              :     std::vector<Set::Scalar> R;
     146              :     Set::Scalar eps;
     147              :     Set::Scalar mult = 1.0;
     148              :     Set::Vector x0 = Set::Vector::Zero();
     149              :     bool invert = false;
     150              : };
     151              : }
     152              : #endif
        

Generated by: LCOV version 2.0-1