ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_ReadFromERFBdy.cpp File Reference
#include "ERF_ReadFromERFBdy.H"
#include <AMReX.H>
#include <AMReX_VisMF.H>
#include <AMReX_ParallelDescriptor.H>
#include <AMReX_Print.H>
#include <fstream>
#include <sstream>
Include dependency graph for ERF_ReadFromERFBdy.cpp:

Functions

double read_times_from_erfbdy (const std::string &bdy_file_name, const Box &domain, int &ntimes, int &nvars, int &real_width, Vector< double > &bdy_times, double &start_bdy_time, double &final_bdy_time)
 
void read_from_erfbdy (int itime, const std::string &bdy_file_name, Vector< Vector< FArrayBox >> &bdy_data_xlo, Vector< Vector< FArrayBox >> &bdy_data_xhi, Vector< Vector< FArrayBox >> &bdy_data_ylo, Vector< Vector< FArrayBox >> &bdy_data_yhi, int nvars, int)
 

Function Documentation

◆ read_from_erfbdy()

void read_from_erfbdy ( int  itime,
const std::string &  bdy_file_name,
Vector< Vector< FArrayBox >> &  bdy_data_xlo,
Vector< Vector< FArrayBox >> &  bdy_data_xhi,
Vector< Vector< FArrayBox >> &  bdy_data_ylo,
Vector< Vector< FArrayBox >> &  bdy_data_yhi,
int  nvars,
int   
)
91 {
92  Print() << "Reading ERF boundary data for time index " << itime << std::endl;
93  std::string time_dir = bdy_file_name + "/Time_" + Concatenate("", itime, 6);
94 
95  Arena* Arena_Used = The_Arena();
96 #ifdef AMREX_USE_GPU
97  Arena_Used = The_Pinned_Arena();
98 #endif
99 
100  if (bdy_data_xlo[itime].empty()) {
101  bdy_data_xlo[itime].resize(nvars);
102  bdy_data_xhi[itime].resize(nvars);
103  bdy_data_ylo[itime].resize(nvars);
104  bdy_data_yhi[itime].resize(nvars);
105  }
106 
107  for (int ivar = 0; ivar < nvars; ++ivar)
108  {
109  { // X-low boundary
110  std::string filename = time_dir + "/BdyData_xlo_var" + std::to_string(ivar);
111  std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary);
112 
113  // A default-constructed FArrayBox allocates from The_Arena(), which is DEVICE
114  // memory unless amrex.the_arena_is_managed=true. readFrom() then htod_memcpy's
115  // into it and the copy<RunOn::Host> below host-reads that device pointer.
116  FArrayBox tmp_fab(amrex::The_Pinned_Arena());
117 
118  tmp_fab.readFrom(ifs);
119  bdy_data_xlo[itime][ivar].resize(tmp_fab.box(), tmp_fab.nComp(), Arena_Used);
120  bdy_data_xlo[itime][ivar].template copy<RunOn::Host>(tmp_fab, 0, 0, tmp_fab.nComp());
121  ifs.close();
122  }
123 
124  { // X-high boundary
125  std::string filename = time_dir + "/BdyData_xhi_var" + std::to_string(ivar);
126  std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary);
127 
128  // A default-constructed FArrayBox allocates from The_Arena(), which is DEVICE
129  // memory unless amrex.the_arena_is_managed=true. readFrom() then htod_memcpy's
130  // into it and the copy<RunOn::Host> below host-reads that device pointer.
131  FArrayBox tmp_fab(amrex::The_Pinned_Arena());
132 
133  tmp_fab.readFrom(ifs);
134  bdy_data_xhi[itime][ivar].resize(tmp_fab.box(), tmp_fab.nComp(), Arena_Used);
135  bdy_data_xhi[itime][ivar].template copy<RunOn::Host>(tmp_fab, 0, 0, tmp_fab.nComp());
136  ifs.close();
137  }
138 
139  { // Y-low boundary
140  std::string filename = time_dir + "/BdyData_ylo_var" + std::to_string(ivar);
141  std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary);
142 
143  // A default-constructed FArrayBox allocates from The_Arena(), which is DEVICE
144  // memory unless amrex.the_arena_is_managed=true. readFrom() then htod_memcpy's
145  // into it and the copy<RunOn::Host> below host-reads that device pointer.
146  FArrayBox tmp_fab(amrex::The_Pinned_Arena());
147 
148  tmp_fab.readFrom(ifs);
149  bdy_data_ylo[itime][ivar].resize(tmp_fab.box(), tmp_fab.nComp(), Arena_Used);
150  bdy_data_ylo[itime][ivar].template copy<RunOn::Host>(tmp_fab, 0, 0, tmp_fab.nComp());
151  ifs.close();
152  }
153 
154  { // Y-high boundary
155  std::string filename = time_dir + "/BdyData_yhi_var" + std::to_string(ivar);
156  std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary);
157 
158  // A default-constructed FArrayBox allocates from The_Arena(), which is DEVICE
159  // memory unless amrex.the_arena_is_managed=true. readFrom() then htod_memcpy's
160  // into it and the copy<RunOn::Host> below host-reads that device pointer.
161  FArrayBox tmp_fab(amrex::The_Pinned_Arena());
162 
163  tmp_fab.readFrom(ifs);
164  bdy_data_yhi[itime][ivar].resize(tmp_fab.box(), tmp_fab.nComp(), Arena_Used);
165  bdy_data_yhi[itime][ivar].template copy<RunOn::Host>(tmp_fab, 0, 0, tmp_fab.nComp());
166  ifs.close();
167  }
168  }
169 
170  // Barrier to ensure all reads complete.
171  ParallelDescriptor::Barrier();
172 }
@ nvars
Definition: ERF_DataStruct.H:194
Arena * Arena_Used
Definition: ERF_Morrison_Advance_F.H:23

Referenced by ERF::ReadCheckpointFile(), and ERF::timeStep().

Here is the caller graph for this function:

◆ read_times_from_erfbdy()

double read_times_from_erfbdy ( const std::string &  bdy_file_name,
const Box &  domain,
int &  ntimes,
int &  nvars,
int &  real_width,
Vector< double > &  bdy_times,
double &  start_bdy_time,
double &  final_bdy_time 
)
23 {
24  std::string HeaderFileName = bdy_file_name + "/Header";
25 
26  // Read header file.
27  std::ifstream HeaderFile;
28  HeaderFile.open(HeaderFileName.c_str(), std::ifstream::in);
29 
30  if (!HeaderFile.good()) {
31  amrex::FileOpenFailed(HeaderFileName);
32  }
33 
34  std::string line;
35 
36  // Read title line.
37  std::getline(HeaderFile, line);
38 
39  // Read metadata.
40  HeaderFile >> ntimes;
41  HeaderFile >> nvars;
42  HeaderFile >> real_width;
43 
44  // Read boundary times.
45  bdy_times.resize(ntimes);
46  for (int i = 0; i < ntimes; ++i) {
47  HeaderFile >> bdy_times[i];
48  }
49 
50  // Read the domain box of the run that wrote this file.
51  int sml[3], big[3];
52  HeaderFile >> sml[0] >> sml[1] >> sml[2];
53  HeaderFile >> big[0] >> big[1] >> big[2];
54 
55  HeaderFile.close();
56 
57  // The boundary data in this file were built for the domain recorded in its header,
58  // so refuse to use them for any other domain rather than reading boxes that don't fit.
59  const Box bdy_domain(IntVect(AMREX_D_DECL(sml[0],sml[1],sml[2])),
60  IntVect(AMREX_D_DECL(big[0],big[1],big[2])));
61  if (bdy_domain != domain) {
62  std::ostringstream msg;
63  msg << "The boundary file " << bdy_file_name << " was written for the domain "
64  << bdy_domain << " but this run has the domain " << domain
65  << " -- please remove or rename " << bdy_file_name
66  << ", or name a different file with erf.erfbdy_file";
67  amrex::Abort(msg.str());
68  }
69 
70  // Ensure the file holds at least two times.
71  AMREX_ALWAYS_ASSERT(ntimes >= 2);
72 
73  // Set the first and last boundary times.
74  start_bdy_time = bdy_times[0];
75  final_bdy_time = bdy_times[ntimes - 1];
76 
77  // Calculate the interval between boundary times.
78  double bdy_time_interval = bdy_times[1] - bdy_times[0];
79 
80  return bdy_time_interval;
81 }
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)

Referenced by ERF::ReadCheckpointFile().

Here is the call graph for this function:
Here is the caller graph for this function: