Skip to content

Commit

Permalink
finished netCdf reader and added bathymetry reading support
Browse files Browse the repository at this point in the history
  • Loading branch information
xLPMG committed Nov 24, 2023
1 parent 37947a2 commit 8ea336b
Show file tree
Hide file tree
Showing 12 changed files with 50,381 additions and 132 deletions.
10 changes: 5 additions & 5 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"solver": "fwave",
"nx": 50,
"ny": 50,
"simulationSizeX": 100,
"simulationSizeY": 100,
"nx": 10,
"ny": 10,
"simulationSizeX": 10,
"simulationSizeY": 10,
"setup":"CIRCULARDAMBREAK2D",
"bathymetry":"resources/bathymetry/singleWall2d_100x100.csv",
"bathymetry":"resources/netCdfTest.nc",
"hasBoundaryL": false,
"hasBoundaryR": false,
"hasBoundaryU": false,
Expand Down
17 changes: 8 additions & 9 deletions config.log
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
file /Users/richardhofmann/Dropbox/Mac/Desktop/programming/uni/tsunami/tsunami_lab/SConstruct,line 36:
file /Users/lpmg/Desktop/Uni/Sem5/Tsunami/tsunami_lab/SConstruct,line 36:
Configure(confdir = .sconf_temp)
scons: Configure: Checking for C library netcdf...
scons: Configure: ".sconf_temp/conftest_292a485a87739177f630f5f17109eb40_0.c" is up to date.
scons: Configure: ".sconf_temp/conftest_1ddd6b9627e69bc88093239e56660785_0.c" is up to date.
scons: Configure: The original builder output was:
|.sconf_temp/conftest_292a485a87739177f630f5f17109eb40_0.c <-
|.sconf_temp/conftest_1ddd6b9627e69bc88093239e56660785_0.c <-
| |
| |
| |#include "netcdf.h"
| |
| |int
| |main() {
| |int main(void) {
| |
| |return 0;
| |}
| |
|
scons: Configure: ".sconf_temp/conftest_292a485a87739177f630f5f17109eb40_0.o" is up to date.
scons: Configure: ".sconf_temp/conftest_1ddd6b9627e69bc88093239e56660785_0.o" is up to date.
scons: Configure: The original builder output was:
|gcc -o .sconf_temp/conftest_292a485a87739177f630f5f17109eb40_0.o -c .sconf_temp/conftest_292a485a87739177f630f5f17109eb40_0.c
|gcc -o .sconf_temp/conftest_1ddd6b9627e69bc88093239e56660785_0.o -c .sconf_temp/conftest_1ddd6b9627e69bc88093239e56660785_0.c
|
scons: Configure: ".sconf_temp/conftest_292a485a87739177f630f5f17109eb40_0_6914dc1c61d7fa91c0e58be35491f622" is up to date.
scons: Configure: ".sconf_temp/conftest_1ddd6b9627e69bc88093239e56660785_0_b592ebdd36f6be32a5b2ea6e1bd2921e" is up to date.
scons: Configure: The original builder output was:
|gcc -o .sconf_temp/conftest_292a485a87739177f630f5f17109eb40_0_6914dc1c61d7fa91c0e58be35491f622 .sconf_temp/conftest_292a485a87739177f630f5f17109eb40_0.o -lnetcdf
|gcc -o .sconf_temp/conftest_1ddd6b9627e69bc88093239e56660785_0_b592ebdd36f6be32a5b2ea6e1bd2921e .sconf_temp/conftest_1ddd6b9627e69bc88093239e56660785_0.o -lnetcdf
|
scons: Configure: (cached) yes

Expand Down
41 changes: 41 additions & 0 deletions resources/netcdfTest.cdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
netcdf tsunamiNetCdf {
dimensions:
x = 10 ;
y = 10 ;
variables:
float x(x) ;
x:units = "meters" ;
x:axis = "X" ;
float y(y) ;
y:units = "meters" ;
y:axis = "Y" ;
float bathymetry(x, y) ;
bathymetry:units = "meters" ;
data:

x = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ;

y = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ;

bathymetry =
// bathymetry(0, 0-9)
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
// bathymetry(1, 0-9)
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
// bathymetry(2, 0-9)
-3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
// bathymetry(3, 0-9)
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
// bathymetry(4, 0-9)
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
// bathymetry(5, 0-9)
-1, -1, 23, -1, -1, -1, -1, -1, -1, -1,
// bathymetry(6, 0-9)
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
// bathymetry(7, 0-9)
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
// bathymetry(8, 0-9)
-1, -1, -1, -1, -1, -1, 99, -1, -1, -1,
// bathymetry(9, 0-9)
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1 ;
}
Binary file added resources/netcdfTest.nc
Binary file not shown.
Binary file added solution.nc
Binary file not shown.
2 changes: 1 addition & 1 deletion src/io/BathymetryLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tsunami_lab::io::BathymetryLoader::~BathymetryLoader()

void tsunami_lab::io::BathymetryLoader::loadBathymetry(const std::string &i_file)
{
std::cout << "Loading bathymetry from file: " << i_file << std::endl;
std::cout << "Loading bathymetry from csv file: " << i_file << std::endl;
if (!std::filesystem::exists(i_file))
{
std::cerr << "Error: File not found!" << std::endl;
Expand Down
155 changes: 79 additions & 76 deletions src/io/NetCdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <iostream>
#include <netcdf.h>


void tsunami_lab::io::NetCdf::checkNcErr(tsunami_lab::t_idx i_err)
{
if (i_err)
Expand All @@ -20,36 +19,36 @@ void tsunami_lab::io::NetCdf::checkNcErr(tsunami_lab::t_idx i_err)
}
}

tsunami_lab::io::NetCdf::NetCdf(t_idx i_nx,
const t_idx i_ny,
t_idx i_stride,
t_real const *i_b)
tsunami_lab::io::NetCdf::NetCdf(const char *path,
t_idx i_nx,
t_idx i_ny,
t_idx i_stride)
{

m_nx = i_nx;
m_ny = i_ny;
m_stride = i_stride;

m_err = nc_create("tsunamiNetCdf.nc", // path
NC_CLOBBER, // cmode
&m_ncId); // ncidp
if (path == nullptr)
return;

m_err = nc_create(path, // path
NC_CLOBBER, // cmode
&m_ncId); // ncidp
checkNcErr(m_err);

t_real *m_bathymetryData = new t_real[i_nx * i_ny];
t_real *m_x = new t_real[i_nx];
t_real *m_y = new t_real[i_ny];

// loop for bathymetry
int i = 0;
// set x and y
for (std::size_t l_ix = 0; l_ix < i_nx; l_ix++)
{
for (std::size_t l_iy = 0; l_iy < i_ny; l_iy++)
{
m_bathymetryData[i++] = i_b[l_ix + l_iy * i_stride];
m_y[l_iy] = l_iy;
}
m_x[l_ix] = l_ix;
}
for (std::size_t l_iy = 0; l_iy < i_ny; l_iy++)
{
m_y[l_iy] = l_iy;
}

// define dimensions
int m_dimIds[3];
Expand Down Expand Up @@ -141,7 +140,8 @@ tsunami_lab::io::NetCdf::NetCdf(t_idx i_nx,

// assign attributes
m_err = nc_put_att_text(m_ncId, m_varTId, "units",
strlen("seconds"), "seconds");
strlen("seconds since the earthquake event"),
"seconds since the earthquake event");
checkNcErr(m_err);

m_err = nc_put_att_text(m_ncId, m_varXId, "units",
Expand All @@ -164,17 +164,11 @@ tsunami_lab::io::NetCdf::NetCdf(t_idx i_nx,
m_err = nc_put_att_text(m_ncId, m_varTHId, "units",
strlen("meters"), "meters");
checkNcErr(m_err);
m_err = nc_put_att_text(m_ncId, m_varHuId, "units",
strlen("Newton * seconds"), "meters");
checkNcErr(m_err);
m_err = nc_put_att_text(m_ncId, m_varHvId, "units",
strlen("Newton * seconds"), "meters");
checkNcErr(m_err);

m_err = nc_enddef(m_ncId); // ncid
checkNcErr(m_err);

// write data
// write data
m_err = nc_put_var_float(m_ncId, // ncid
m_varXId, // varid
&m_x[0]); // op
Expand All @@ -184,13 +178,6 @@ tsunami_lab::io::NetCdf::NetCdf(t_idx i_nx,
&m_y[0]); // op
checkNcErr(m_err);

m_err = nc_put_var_float(m_ncId, // ncid
m_varBId, // varid
m_bathymetryData); // op
checkNcErr(m_err);


delete[] m_bathymetryData;
delete[] m_x;
delete[] m_y;
}
Expand Down Expand Up @@ -226,6 +213,28 @@ void tsunami_lab::io::NetCdf::write(t_real const *i_h,
l_i++;
}
}

// write bathymetry on first call
if (m_timeStepCount == 0)
{
t_real *l_b = new t_real[m_nx * m_ny];
for (t_idx l_x = 0; l_x < m_nx; l_x++)
{
for (t_idx l_y = 0; l_y < m_ny; l_y++)
{
l_b[l_i] = i_b[l_x + l_y * m_stride];
l_i++;
}
}

m_err = nc_put_var_float(m_ncId,
m_varBId,
l_b);
checkNcErr(m_err);
delete[] l_b;
}

//write values
m_err = nc_put_var1_float(m_ncId,
m_varTId,
&m_timeStepCount,
Expand All @@ -238,6 +247,7 @@ void tsunami_lab::io::NetCdf::write(t_real const *i_h,
count,
l_h);
checkNcErr(m_err);

m_err = nc_put_vara_float(m_ncId,
m_varTHId,
start,
Expand Down Expand Up @@ -267,55 +277,48 @@ void tsunami_lab::io::NetCdf::write(t_real const *i_h,
delete[] l_hv;
}

void tsunami_lab::io::NetCdf::read(const char* l_filename,
t_real * i_b,
t_real * i_d){
t_real x_in[m_nx], y_in[m_ny];
int x_varid, y_varid, b_varid;

t_real x_in[m_nx], y_in[m_ny];

int *l_data = new int[m_nx * m_ny];

m_err = nc_open(l_filename, NC_NOWRITE, &m_ncIdRead);
checkNcErr(m_err);

m_err = nc_inq_varid(m_ncIdRead, "x", &x_varid);
checkNcErr(m_err);
m_err = nc_inq_varid(m_ncIdRead, "y", &y_varid);
checkNcErr(m_err);

/* Read the coordinate variable data. */
m_err = nc_get_var_float(m_ncIdRead, x_varid, &x_in[0]);
checkNcErr(m_err);
m_err = nc_get_var_float(m_ncIdRead, y_varid, &y_in[0]);
checkNcErr(m_err);
tsunami_lab::t_real *tsunami_lab::io::NetCdf::read(const char *i_file,
const char *i_var,
t_idx &o_nx,
t_idx &o_ny)
{
std::cout << "Loading "<< i_var << " from .nc file: " << i_file << std::endl;

if(i_b == nullptr){

/* Get the varids of the pressure and temperature netCDF
* variables. */
m_err = nc_inq_varid(m_ncIdRead, "bathymetry", &b_varid);
checkNcErr(m_err);
int l_ncIdRead, l_varXIdRead, l_varYIdRead, l_varDataIdRead = 0;

//read data
m_err = nc_get_var_float(m_ncIdRead, b_varid, i_b);
checkNcErr(m_err);
m_err = nc_open(i_file, NC_NOWRITE, &l_ncIdRead);
checkNcErr(m_err);


m_err = nc_close(m_ncIdRead);
checkNcErr(m_err);
} else{
/* Get the varids of the pressure and temperature netCDF
* variables. */
m_err = nc_inq_varid(m_ncIdRead, "displacement", &b_varid);
checkNcErr(m_err);
// get dimension ids
m_err = nc_inq_dimid(l_ncIdRead, "x", &l_varXIdRead);
checkNcErr(m_err);
m_err = nc_inq_dimid(l_ncIdRead, "y", &l_varYIdRead);
checkNcErr(m_err);
// read dimension size
m_err = nc_inq_dimlen(l_ncIdRead, l_varXIdRead, &o_nx);
checkNcErr(m_err);
m_err = nc_inq_dimlen(l_ncIdRead, l_varYIdRead, &o_ny);
checkNcErr(m_err);
// get var id of desired variable
m_err = nc_inq_varid(l_ncIdRead, i_var, &l_varDataIdRead);
checkNcErr(m_err);
// read data
t_real l_data[o_nx][o_ny];
m_err = nc_get_var_float(l_ncIdRead, l_varDataIdRead, &l_data[0][0]);
checkNcErr(m_err);

//read data
m_err = nc_get_var_float(m_ncIdRead, d_varid, i_d);
checkNcErr(m_err);
m_err = nc_close(l_ncIdRead);
checkNcErr(m_err);

m_err = nc_close(m_ncIdRead);
checkNcErr(m_err);
// convert to strided array
t_real *l_stridedArray = new t_real[o_nx * o_ny];
for (std::size_t l_ix = 0; l_ix < o_nx; l_ix++)
{
for (std::size_t l_iy = 0; l_iy < o_ny; l_iy++)
{
l_stridedArray[l_ix + l_iy * o_nx] = l_data[l_ix][l_iy];
}
}
std::cout << "Done loading "<< i_var << std::endl;
return l_stridedArray;
}
Loading

0 comments on commit 8ea336b

Please sign in to comment.