Peano 4
Loading...
Searching...
No Matches
NetCDFWriter.cpp
Go to the documentation of this file.
1#include "NetCDFWriter.hpp"
3 const char *bathymetry_file_path,
4 unsigned int i_nX, unsigned int i_nY,
5 double cell_size,
6 double *b,
7 double area_of_interest_coord_0, double area_of_interest_coord_1,
8 double area_of_interest_coord_2, double area_of_interest_coord_3,
9 double end_time,
10 double start_time,
11 unsigned int flush):
12 l_nX_(i_nX),
13 l_nY_(i_nY)
14{
15 //create a netCDF-file, an existing file will be replaced
16 //check if the netCDF-file creation constructor succeeded.
17 if (nc_create(output_file_name, NC_NETCDF4, &file_id_) != NC_NOERR)
18 {
19 std::cout << "Output NetCDF file could not be created!" << std::endl;
20 return;
21 }
22
23 //dimensions
24 int dimension_time, dimension_x, dimension_y;
25 nc_def_dim(file_id_, "time", NC_UNLIMITED, &dimension_time);
26 nc_def_dim(file_id_, "x", i_nX, &dimension_x);
27 nc_def_dim(file_id_, "y", i_nY, &dimension_y);
28
29 int varid_x, varid_y;
30
31 nc_def_var(file_id_, "time", NC_DOUBLE, 1, &dimension_time, &time_id_);
32 nc_put_attr_text(time_id_, "long_name", "Time");
33 nc_put_attr_text(time_id_, "units", "seconds since simulation start");
34
35 nc_def_var(file_id_, "x", NC_DOUBLE, 1, &dimension_x, &varid_x);
36 nc_def_var(file_id_, "y", NC_DOUBLE, 1, &dimension_y, &varid_y);
37
38 int dimensions[] = {dimension_time, dimension_y, dimension_x};
39 nc_def_var(file_id_, "h", NC_DOUBLE, 3, dimensions, &varid_h_);
40 nc_def_var(file_id_, "hu", NC_DOUBLE, 3, dimensions, &varid_hu_);
41 nc_def_var(file_id_, "hv", NC_DOUBLE, 3, dimensions, &varid_hv_);
42 nc_def_var(file_id_, "b", NC_DOUBLE, 2, &dimensions[1], &varid_b_);
43
44 nc_put_attr_text(NC_GLOBAL, "Conventions", "CF-1.5");
45 nc_put_attr_text(NC_GLOBAL, "title", "Computed Adjoint Solution");
46 nc_put_attr_text(NC_GLOBAL, "history", "ExaHyPE SWE Application");
47 nc_put_attr_text(NC_GLOBAL, "institution", "Technische Universitaet Muenchen, School of Computation, Information and Technology, Chair of Scientific Computing");
48 nc_put_attr_text(NC_GLOBAL, "source", bathymetry_file_path);
49 nc_put_attr_text(NC_GLOBAL, "references", "exahype.org");
50
51 nc_put_att_double(file_id_, NC_GLOBAL, "end_time", NC_DOUBLE, 1, &end_time);
52 nc_put_att_double(file_id_, NC_GLOBAL, "start_time", NC_DOUBLE, 1, &start_time);
53 double area_of_interest[4] = {area_of_interest_coord_0, area_of_interest_coord_1, area_of_interest_coord_2, area_of_interest_coord_3};
54 nc_put_att_double(file_id_, NC_GLOBAL, "area_of_interest", NC_DOUBLE, 4, area_of_interest);
55
56 //setup grid size
57 double gridPosition = 0;
58 for (size_t i = 0; i < l_nX_; i++)
59 {
60 nc_put_var1_double(file_id_, varid_x, &i, &gridPosition);
61 gridPosition += 1.0;
62 }
63
64 gridPosition = 0;
65 for (size_t j = 0; j < l_nY_; j++)
66 {
67
68 nc_put_var1_double(file_id_, varid_y, &j, &gridPosition);
69 gridPosition += 1.0;
70 }
71
72 size_t start[] = {0, 0};
73 size_t count[] = {l_nY_, 1};
74 for(unsigned int i = 1; i < l_nX_ + 1; i++)
75 {
76 start[1] = i - 1;
77 if(nc_put_vara_double(file_id_, varid_b_, start, count, &b[i * (l_nY_ + 2) + 1]) != NC_NOERR)
78 {
79 std::cout << "Bathymetry could not be written to netCDF output file!" << std::endl;
80 return;
81 }
82 }
83}
84
89
90void applications::exahype2::swe::adjoint::NetCDFWriter::writeTimeStep(double *h, double *hu, double *hv, double t, size_t checkpoint)
91{
92 size_t start[] = {checkpoint, 0, 0};
93 size_t count[] = {1, l_nY_, 1};
94 for(unsigned int i = 1; i < l_nX_ + 1; i++)
95 {
96 start[2] = i - 1;
97 if(nc_put_vara_double(file_id_, varid_h_, start, count, &h[i * (l_nY_ + 2) + 1]) != NC_NOERR)
98 {
99 std::cout << "h could not be written to output for timestep: " << t << "!" << std::endl;
100 return;
101 }
102 if(nc_put_vara_double(file_id_, varid_hu_, start, count, &hu[i * (l_nY_ + 2) + 1]) != NC_NOERR)
103 {
104 std::cout << "hu could not be written to output for timestep: " << t << "!" << std::endl;
105 return;
106 }
107 if(nc_put_vara_double(file_id_, varid_hv_, start, count, &hv[i * (l_nY_ + 2) + 1]) != NC_NOERR)
108 {
109 std::cout << "hv could not be written to output for timestep: " << t << "!" << std::endl;
110 return;
111 }
112 }
113 nc_put_var1_double(file_id_, time_id_, &checkpoint, &t);
114
115 if(flush > 0 && checkpoint % flush == 0)
116 nc_sync(file_id_);
117}
And from this we can write down f$ nabla phi_i nabla phi_i dx but since we are constructing matrix let s investigate the f$ j
NetCDFWriter(const char *output_file_name, const char *bathymetry_file_path, unsigned int i_nX, unsigned int i_nY, double cell_size, double *b, double area_of_interest_coord_0, double area_of_interest_coord_1, double area_of_interest_coord_2, double area_of_interest_coord_3, double end_time, double start_time=0, unsigned int flush=0)
Create a netCdf-file Any existing file will be replaced.
void writeTimeStep(double *h, double *hu, double *hv, double t, size_t checkpoint)
void nc_put_attr_text(int varid, const char *name, const char *value)