REMORA
Regional Modeling of Oceans Refined Adaptively
Loading...
Searching...
No Matches
REMORA_NCTimeSeriesRiver.cpp
Go to the documentation of this file.
2#include "REMORA_NCFile.H"
3
4#include "AMReX_ParallelDescriptor.H"
5
6#include <string>
7
8#ifdef REMORA_USE_NETCDF
9
10/**
11 * @param[in ] a_file_names vector of file name(s) to read from
12 * @param[in ] a_field_name name of field to read in
13 * @param[in ] a_time_name name of time variable in NetCDF file
14 * @param[in ] a_nz number of vertical levels in domain
15 * @param[in ] a_use_vert_integ whether the data in the file is vertically integrated
16 * @param[in ] a_is_transport whether the field is the river transport rather than a tracer
17 */
18NCTimeSeriesRiver::NCTimeSeriesRiver (const amrex::Vector<std::string>& a_file_names, const std::string a_field_name,
19 const std::string a_time_name,
20 const int a_nz, const int a_use_vert_integ,
21 const int a_is_transport) {
22 file_names.assign(a_file_names.begin(), a_file_names.end());
25 nz = a_nz;
28}
29
31 // open file
32 amrex::Print() << "Loading " << field_name << " from rivers NetCDF file(s)" << std::endl;
33
34 // The time field can have any number of names, depending on the field.
35 // If not specified in input file (time_name.empty()) then set it by default
36 if (time_name.empty())
37 {
38 time_name = "river_time";
39 }
40
41 for (int ifile = 0; ifile < file_names.size(); ++ifile) {
42 const std::string& file_name = file_names[ifile];
43
44 // Check units of time stamps; should be days
45 std::string unit_str = ReadNetCDFVarAttrStr(file_name, time_name, "units"); // works on proc 0
46 if (amrex::ParallelDescriptor::IOProcessor())
47 {
48 if (unit_str.find("days") == std::string::npos) {
49 amrex::Print() << "Units of river_time given as: " << unit_str << std::endl;
50 amrex::Abort("Units must be in days.");
51 }
52 }
53
54 // get times and put in array
56 amrex::Vector<RARRAY> array_ts(1);
57 ReadNetCDFFile(file_name, {time_name}, array_ts); // filled only on proc 0
58 if (amrex::ParallelDescriptor::IOProcessor())
59 {
60 int ntimes_io = array_ts[0].get_vshape()[0];
61 for (int nt(0); nt < ntimes_io; nt++)
62 {
63 // Convert river time from days to seconds
64 river_times.push_back((*(array_ts[0].get_data() + nt)) * amrex::Real(60.0) * amrex::Real(60.0) * amrex::Real(24.0));
65 file_for_time.push_back(ifile);
66 file_itime_offset.push_back(nt);
67 }
68 }
69 }
70 int ntimes = river_times.size();
71 // Only do checks on IO processors since river_times isn't populated on other ranks yet
72 if (amrex::ParallelDescriptor::IOProcessor()) {
73 AMREX_ASSERT(std::is_sorted(river_times.begin(), river_times.end()));
74 if (ntimes <= 1) {
75 amrex::Error("River data must be given at at least two times");
76 }
77 }
78 int ioproc = amrex::ParallelDescriptor::IOProcessorNumber();
79 amrex::ParallelDescriptor::Bcast(&ntimes,1,ioproc);
80 if (!(amrex::ParallelDescriptor::IOProcessor())) {
81 river_times.resize(ntimes);
82 file_for_time.resize(ntimes);
84 }
85 amrex::ParallelDescriptor::Bcast(river_times.data(), river_times.size(), ioproc);
86 amrex::ParallelDescriptor::Bcast(file_for_time.data(), file_for_time.size(), ioproc);
87 amrex::ParallelDescriptor::Bcast(file_itime_offset.data(), file_itime_offset.size(), ioproc);
88
90 if (amrex::ParallelDescriptor::IOProcessor())
91 {
92 std::vector<MPI_Offset> shape = ncf.var(field_name).shape();
93 if (shape.size() == 2) {
94 has_z = 0;
95 nriv = shape[1];
96 } else if (shape.size() == 3) {
97 has_z = 1;
98 nriv = shape[2];
99 } else {
100 amrex::Abort("River field shape not 2 or 3");
101 }
102 }
103 amrex::ParallelDescriptor::Bcast(&has_z, 1, ioproc);
104 amrex::ParallelDescriptor::Bcast(&nriv, 1, ioproc);
105
106 // river_Vshape distributes the total transport in the vertical (ROMS Qsrc = Qbar*Qshape),
107 // so it applies only to the transport. A tracer given as (river_time, river) is a
108 // concentration, and is used unscaled at every level.
109 if (!has_z && !is_transport) {
110 amrex::Print() << "Warning: " << field_name << " has no s_rho dimension in "
111 << file_names[0] << "; the same value will be used at every "
112 << "vertical level. ROMS expects river tracers to be given as "
113 << "(river_time, s_rho, river)." << std::endl;
114 }
115
116 amrex::Box vshape_box(amrex::IntVect(0,0,0),amrex::IntVect(nriv,0,nz));
117 if (!has_z && !use_vert_integ && is_transport) {
118 amrex::Vector<amrex::FArrayBox*> NC_fabs;
119 amrex::Vector<std::string> NC_names;
120 amrex::Vector<enum NC_Data_Dims_Type> NC_dim_types;
121 amrex::Print() << "Reading in river_Vshape from " << file_names[0] << std::endl;
122
123 fab_vshape = new amrex::FArrayBox();
124 NC_fabs.push_back(fab_vshape); NC_names.push_back("river_Vshape");
127 }
128
129 nzbox = (use_vert_integ) ? 1 : nz;
130 amrex::Box riv_box(amrex::IntVect(0,0,0),amrex::IntVect(nriv,0,nzbox));
131#ifdef AMREX_USE_GPU
132 // It's possible there should be a different arena
133 fab_before = new amrex::FArrayBox(riv_box,1,amrex::The_Pinned_Arena());
134 fab_after = new amrex::FArrayBox(riv_box,1,amrex::The_Pinned_Arena());
135 fab_interp = new amrex::FArrayBox(riv_box,1,amrex::The_Pinned_Arena());
136#else
137 fab_before = new amrex::FArrayBox(riv_box,1);
138 fab_after = new amrex::FArrayBox(riv_box,1);
139 fab_interp = new amrex::FArrayBox(riv_box,1);
140#endif
141
142 // dummy initialization
143 i_time_before = -100;
144}
145
147 // Figure out time index:
151 for (int nt=0; nt < river_times.size()-1; nt++) {
152 if ((river_times[nt] <= time) and (river_times[nt+1] >= time)) {
153 i_time_before = nt;
155 time_after = river_times[nt+1];
156 break;
157 }
158 }
159
160 int i_time_after = i_time_before + 1;
161 if (i_time_before_old + 1 == i_time_before) {
162 // swap data vectors so we only have to read in one MultiFab
163 std::swap(fab_before, fab_after);
165 } else if (i_time_before_old != i_time_before) {
168 }
169
170 amrex::Real dt = time_after - time_before;
171 amrex::Real time_before_copy = time_before;
172
173 amrex::Box fab_domain(amrex::IntVect(0,0,0), amrex::IntVect(nriv-1,0,nzbox-1));
174 auto interp_array = fab_interp->array();
175 auto before_array = fab_before->array();
176 auto after_array = fab_after->array();
177 amrex::ParallelFor(fab_domain, [=] AMREX_GPU_DEVICE (int r, int , int k) {
178 interp_array(r,0,k) = before_array(r,0,k) + (time - time_before_copy) * (after_array(r,0,k) - before_array(r,0,k)) / dt;
179 });
180}
181
182void NCTimeSeriesRiver::read_in_at_time (amrex::FArrayBox* fab_dat, int itime) {
183 amrex::FArrayBox NC_fab;
184 amrex::Vector<amrex::FArrayBox*> NC_fabs;
185 amrex::Vector<std::string> NC_names;
186 amrex::Vector<enum NC_Data_Dims_Type> NC_dim_types;
187 // actual dims don't really matter here; only lower is used in call
188
189 const std::string& file_name = file_names[file_for_time[itime]];
191
192 amrex::Print() << "Reading in " << field_name << " at time index " << itime
193 << " from " << file_name << std::endl;
194
195 NC_fabs.push_back(&NC_fab) ; NC_names.push_back(field_name);
196
197 if (has_z) {
199 } else {
201 }
202
203 amrex::Box riv_domain(amrex::IntVect(0,0,0), amrex::IntVect(nriv-1,0,nz-1));
204 amrex::Box fab_domain(amrex::IntVect(0,0,0), amrex::IntVect(nriv-1,0,nzbox-1));
205
207
208 auto dat_array = fab_dat->array();
209 auto tmp_array = NC_fabs[0]->const_array();
210 if (has_z || use_vert_integ) {
211 amrex::ParallelFor(fab_domain, [=] AMREX_GPU_DEVICE (int r, int , int k) {
212 dat_array(r,0,k) = tmp_array(r,0,k);
213 });
214 } else if (is_transport) {
215 // Distribute the vertically integrated transport over the levels
216 auto array_vshape = fab_vshape->const_array();
217 amrex::ParallelFor(fab_domain, [=] AMREX_GPU_DEVICE (int r, int , int k) {
218 dat_array(r,0,k) = tmp_array(r,0,0) * array_vshape(r,0,k);
219 });
220 } else {
221 // Concentration given only as (river_time, river); use it at every level
222 amrex::ParallelFor(fab_domain, [=] AMREX_GPU_DEVICE (int r, int , int k) {
223 dat_array(r,0,k) = tmp_array(r,0,0);
224 });
225 }
226}
227#endif // REMORA_USE_NETCDF
mf_h setVal(geomdata.ProbHi(2))
void ReadNetCDFFile(const std::string &fname, amrex::Vector< std::string > names, amrex::Vector< NDArray< DType > > &arrays, bool one_time=false, int fill_time=0)
Read in data from netcdf file and save to data arrays.
std::string ReadNetCDFVarAttrStr(const std::string &fname, const std::string &var_name, const std::string &attr_name)
Helper function for reading a single variable attribute.
NCTimeSeriesRiver(const amrex::Vector< std::string > &a_file_names, const std::string a_field_name, const std::string a_time_name, const int a_nz, const int a_use_vert_integ=0, const int a_is_transport=0)
int nriv
Number of rivers.
amrex::Real time_before
Time in ocean_time immediately before the last time interpolated to.
int nzbox
How many vertical cells there are in the data fabs.
void update_interpolated_to_time(amrex::Real time)
Calculate.
amrex::Vector< int > file_itime_offset
Offset to access a particular time within its file.
amrex::Vector< std::string > file_names
File names to read from.
std::string time_name
Field name for time series in netcdf file.
amrex::Vector< amrex::Real > river_times
Time points in netcdf file.
std::string field_name
Field name in netcdf file.
amrex::FArrayBox * fab_vshape
Vshape data if needed.
amrex::FArrayBox * fab_after
int i_time_before
Time index immediately before the last time interpolated to.
void read_in_at_time(amrex::FArrayBox *vec, int itime)
amrex::FArrayBox * fab_before
FABs to pointers of river data.
int nz
Number of vertical points.
amrex::Vector< int > file_for_time
File index to access a particular time.
amrex::FArrayBox * fab_interp
Container for interpolated data; Only used if save_interpolated == true.
int has_z
Whether the field is specified in the z-dimension.
amrex::Real time_after
Time in ocean_time immediately after the last time interpolated to.
static NCFile open(const std::string &name, const int cmode=NC_NOWRITE, MPI_Comm comm=MPI_COMM_WORLD, MPI_Info info=MPI_INFO_NULL)
Open an existing file.
NDArray is the datatype designed to hold any data, including scalars, multidimensional arrays,...