Alamo
PNG.H
Go to the documentation of this file.
1//
2// Initialize a field using a PNG image. (2D only)
3//
4
5#ifndef IC_PNG_H
6#define IC_PNG_H
7#include <cmath>
8
9#ifndef ALAMO_NOPNG
10#include <stdarg.h>
11#include <stddef.h>
12#include <setjmp.h>
13#include <png.h>
14#endif
15
16#include "IC/IC.H"
17#include "Util/Util.H"
18#include "Util/BMP.H"
19#include "Set/Set.H"
20#include "IO/ParmParse.H"
21#include "Util/PNG.H"
22
23namespace IC
24{
25class PNG : public IC<Set::Scalar>
26{
27public:
28 static constexpr const char* name = "png";
29
30 //enum Type {XYZ, XY, YZ, XZ};
32 enum Channel { R = 0, G = 1, B = 2, A = 3 };
33
34 virtual ~PNG() {}
35
36 PNG(amrex::Vector<amrex::Geometry>& _geom) : IC(_geom) {}
37
38 PNG(amrex::Vector<amrex::Geometry>& _geom, IO::ParmParse& pp, std::string name) : PNG(_geom)
39 {
40 pp_queryclass(name, *this);
41 }
42
43 void Add(const int& lev, Set::Field<Set::Scalar>& a_field, Set::Scalar)
44 {
45
46#ifndef ALAMO_NOPNG
47 Set::Vector DX(geom[lev].CellSize());
48 amrex::Box domain = geom[lev].Domain();
49
50 amrex::IndexType type = a_field[lev]->ixType();
51 domain.convert(type);
52
53 Set::Vector domlo(AMREX_D_DECL(geom[lev].ProbLo()[0], geom[lev].ProbLo()[1], 0.0));
54 Set::Vector domhi(AMREX_D_DECL(geom[lev].ProbHi()[0], geom[lev].ProbHi()[1], 0.0));
55
56 png.setDomain(domlo, domhi);
57
58 for (amrex::MFIter mfi(*a_field[lev], amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi)
59 {
60 amrex::Box bx;
61 if (type == amrex::IndexType::TheNodeType()) bx = mfi.grownnodaltilebox();
62 if (type == amrex::IndexType::TheCellType()) bx = mfi.growntilebox();
63 bx = bx & domain;
64
65 amrex::Array4<Set::Scalar> const& field = a_field[lev]->array(mfi);
66 amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k)
67 {
68 Set::Vector x = Set::Vector::Zero();
69 // NODE
70 if (type == amrex::IndexType::TheNodeType())
71 {
72 x(0) = domlo(0) + ((amrex::Real)(i)) * geom[lev].CellSize()[0];
73 x(1) = domlo(1) + ((amrex::Real)(j)) * geom[lev].CellSize()[1];
74 }
75 else if (type == amrex::IndexType::TheCellType())
76 {
77 x(0) = domlo(0) + ((amrex::Real)(i)+0.5) * geom[lev].CellSize()[0];
78 x(1) = domlo(1) + ((amrex::Real)(j)+0.5) * geom[lev].CellSize()[1];
79 }
80
81
82 std::array<Set::Scalar, 4> val = png(x);
83
84 field(i,j,k,0) = val[channel];
85
86
87 if (field.nComp() > 1) field(i, j, k, 1) = 1.0 - field(i, j, k, 0);
88
89 });
90 }
91 a_field[lev]->FillBoundary();
92#else
93 Util::Abort(INFO,"PNG is disabled");
94#endif
95
96 };
97
98private:
99#ifndef ALAMO_NOPNG
102#endif
103
104public:
105 static void Parse(PNG& value, IO::ParmParse& pp)
106 {
107#ifndef ALAMO_NOPNG
108
109 std::string channel = "g";
110 // Color channel to use (options: r, R, g, G, b, B, a, A)
111 pp_query_validate("channel", channel, {"r","g","b","a","R","G","B","A"});
112 if (channel == "r" || channel == "R") value.channel = Channel::R;
113 else if (channel == "g" || channel == "G") value.channel = Channel::G;
114 else if (channel == "b" || channel == "B") value.channel = Channel::B;
115 else if (channel == "a" || channel == "A") value.channel = Channel::A;
116 else Util::Abort(INFO, "Invalid value for bmp channel - should be r/g/b/a but received '", channel, "'");
117
118
119 pp.queryclass<Util::PNG>(value.png);
120
121#else
122 Util::Abort(INFO,"PNG is disabled");
123#endif
124 }
125};
126}
127#endif
#define pp_query_validate(...)
Definition ParmParse.H:101
#define pp_queryclass(...)
Definition ParmParse.H:107
#define INFO
Definition Util.H:20
amrex::Vector< amrex::Geometry > & geom
Definition IC.H:61
Definition PNG.H:26
static constexpr const char * name
Definition PNG.H:28
PNG(amrex::Vector< amrex::Geometry > &_geom, IO::ParmParse &pp, std::string name)
Definition PNG.H:38
void Add(const int &lev, Set::Field< Set::Scalar > &a_field, Set::Scalar)
Definition PNG.H:43
Channel channel
Definition PNG.H:101
Channel
Definition PNG.H:32
@ G
Definition PNG.H:32
@ A
Definition PNG.H:32
@ R
Definition PNG.H:32
@ B
Definition PNG.H:32
virtual ~PNG()
Definition PNG.H:34
static void Parse(PNG &value, IO::ParmParse &pp)
Definition PNG.H:105
PNG(amrex::Vector< amrex::Geometry > &_geom)
Definition PNG.H:36
Fit
Definition PNG.H:31
@ FitWidth
Definition PNG.H:31
@ Coord
Definition PNG.H:31
@ Stretch
Definition PNG.H:31
@ FitHeight
Definition PNG.H:31
Util::PNG png
Definition PNG.H:100
void queryclass(std::string name, T *value, std::string file="", std::string func="", int line=-1)
Definition ParmParse.H:604
void setDomain(Set::Vector &_lo, Set::Vector &_hi)
Definition PNG.H:126
Initialize a spherical inclusion.
Definition BMP.H:19
amrex::Real Scalar
Definition Base.H:19
Eigen::Matrix< amrex::Real, AMREX_SPACEDIM, 1 > Vector
Definition Base.H:20
void Abort(const char *msg)
Definition Util.cpp:170