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 PNG(amrex::Vector<amrex::Geometry>& _geom, Unit a_unit, IO::ParmParse& pp, std::string name) : PNG(_geom)
44 {
45 unit = a_unit;
46 pp_queryclass(name, *this);
47 }
48
49 void Add(const int& lev, Set::Field<Set::Scalar>& a_field, Set::Scalar)
50 {
51
52#ifndef ALAMO_NOPNG
53 Set::Vector DX(geom[lev].CellSize());
54 amrex::Box domain = geom[lev].Domain();
55
56 amrex::IndexType type = a_field[lev]->ixType();
57 domain.convert(type);
58
59 Set::Vector domlo(AMREX_D_DECL(geom[lev].ProbLo()[0], geom[lev].ProbLo()[1], 0.0));
60 Set::Vector domhi(AMREX_D_DECL(geom[lev].ProbHi()[0], geom[lev].ProbHi()[1], 0.0));
61
62 png.setDomain(domlo, domhi);
63
64 for (amrex::MFIter mfi(*a_field[lev], amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi)
65 {
66 amrex::Box bx;
67 if (type == amrex::IndexType::TheNodeType()) bx = mfi.grownnodaltilebox();
68 if (type == amrex::IndexType::TheCellType()) bx = mfi.growntilebox();
69 bx = bx & domain;
70
71 amrex::Array4<Set::Scalar> const& field = a_field[lev]->array(mfi);
72 amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k)
73 {
74 Set::Vector x = Set::Vector::Zero();
75 // NODE
76 if (type == amrex::IndexType::TheNodeType())
77 {
78 x(0) = domlo(0) + ((amrex::Real)(i)) * geom[lev].CellSize()[0];
79 x(1) = domlo(1) + ((amrex::Real)(j)) * geom[lev].CellSize()[1];
80 }
81 else if (type == amrex::IndexType::TheCellType())
82 {
83 x(0) = domlo(0) + ((amrex::Real)(i)+0.5) * geom[lev].CellSize()[0];
84 x(1) = domlo(1) + ((amrex::Real)(j)+0.5) * geom[lev].CellSize()[1];
85 }
86
87
88 std::array<Set::Scalar, 4> val = png(x);
89
90 field(i,j,k,0) = val[channel] * unit.normalized_value();
91
92 if (field.nComp() > 1) field(i, j, k, 1) = 1.0 - field(i, j, k, 0);
93
94 });
95 }
96 a_field[lev]->FillBoundary();
97#else
98 Util::IgnoreUnused(lev,a_field);
99 Util::Abort(INFO,"PNG is disabled");
100#endif
101
102 };
103
104private:
105#ifndef ALAMO_NOPNG
109#endif
110
111public:
112 static void Parse(PNG& value, IO::ParmParse& pp)
113 {
114#ifndef ALAMO_NOPNG
115
116 std::string channel = "g";
117 // Color channel to use (options: r, R, g, G, b, B, a, A)
118 pp_query_validate("channel", channel, {"r","g","b","a","R","G","B","A"});
119 if (channel == "r" || channel == "R") value.channel = Channel::R;
120 else if (channel == "g" || channel == "G") value.channel = Channel::G;
121 else if (channel == "b" || channel == "B") value.channel = Channel::B;
122 else if (channel == "a" || channel == "A") value.channel = Channel::A;
123 else Util::Abort(INFO, "Invalid value for bmp channel - should be r/g/b/a but received '", channel, "'");
124
125
126 pp.queryclass<Util::PNG>(value.png);
127
128#else
129 Util::Abort(INFO,"PNG is disabled");
130#endif
131 }
132};
133}
134#endif
#define pp_query_validate(...)
Definition ParmParse.H:103
#define pp_queryclass(...)
Definition ParmParse.H:109
#define INFO
Definition Util.H:21
amrex::Vector< amrex::Geometry > & geom
Definition IC.H:61
Definition PNG.H:26
static constexpr const char * name
Definition PNG.H:28
Unit unit
Definition PNG.H:108
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:49
Channel channel
Definition PNG.H:107
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:112
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:106
PNG(amrex::Vector< amrex::Geometry > &_geom, Unit a_unit, IO::ParmParse &pp, std::string name)
Definition PNG.H:43
void queryclass(std::string name, T *value)
Definition ParmParse.H:934
void setDomain(Set::Vector &_lo, Set::Vector &_hi)
Definition PNG.H:133
Initialize a spherical inclusion.
Definition BMP.H:20
amrex::Real Scalar
Definition Base.H:18
Eigen::Matrix< amrex::Real, AMREX_SPACEDIM, 1 > Vector
Definition Base.H:19
void Abort(const char *msg)
Definition Util.cpp:225
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void IgnoreUnused(const Ts &...)
Definition Util.H:355
Definition Unit.H:20
double normalized_value() const
Definition Unit.H:401
static Unit Less()
Definition Unit.H:187