CoastalME (Coastal Modelling Environment)
Simulates the long-term behaviour of complex coastlines
Loading...
Searching...
No Matches
init_grid.cpp
Go to the documentation of this file.
1
12
13/* ==============================================================================================================================
14
15 This file is part of CoastalME, the Coastal Modelling Environment.
16
17 CoastalME is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23==============================================================================================================================*/
24#include <assert.h>
25
26#include <cstdio>
27#include <climits>
28
29// #include <string>
30// using std::to_string;
31
32#include <iostream>
33using std::cerr;
34using std::endl;
35
36// #include <gdal_priv.h>
37// #include <gdal_alg.h>
38
39#ifdef _OPENMP
40#include <omp.h>
41#endif
42
43#include "cme.h"
44// #include "line.h"
45#include "cell.h"
46#include "coast.h"
47#include "simulation.h"
48#include "raster_grid.h"
49
50//===============================================================================================================================
52//===============================================================================================================================
54{
55 // Clear all vector coastlines, profiles, and polygons
56 for (int i = 0; i < static_cast<int>(m_pVCoastPolygon.size()); i++)
57 delete m_pVCoastPolygon[i];
58
59 m_pVCoastPolygon.clear();
60 m_VCoast.clear();
61
62 // m_VFloodWaveSetup.clear();
65
66 // Do some every-timestep initialization
67 m_nXMinBoundingBox = INT_MAX;
68 m_nXMaxBoundingBox = INT_MIN;
69 m_nYMinBoundingBox = INT_MAX;
70 m_nYMaxBoundingBox = INT_MIN;
71
76
80
102 m_dThisIterLeftGridUnconsFine = // TODO 067 Suspended fine sediment never decreases i.e. no suspended fine sediment ever leaves the grid. Is this OK?
108
109 for (int n = 0; n < m_nLayers; n++)
110 {
111 m_bConsChangedThisIter[n] = false;
112 m_bUnconsChangedThisIter[n] = false;
113 }
114
115 // Re-calculate the depth of closure, in case deep water wave properties have changed
117
118 int nZeroThickness = 0;
119
128
129 // And go through all cells in the RasterGrid array
130 // Use OpenMP parallel loop with reduction clauses for thread-safe accumulation
131#ifdef _OPENMP
132#pragma omp parallel for collapse(2) reduction(+ : nZeroThickness) \
133 reduction(+ : m_dStartIterConsFineAllCells, m_dStartIterConsSandAllCells, m_dStartIterConsCoarseAllCells) \
134 reduction(+ : m_dStartIterSuspFineAllCells, m_dStartIterUnconsFineAllCells, m_dStartIterUnconsSandAllCells, m_dStartIterUnconsCoarseAllCells)
135#endif
136
137 for (int nX = 0; nX < m_nXGridSize; nX++)
138 {
139 for (int nY = 0; nY < m_nYGridSize; nY++)
140 {
141 // Re-initialize values for this cell
142 m_pRasterGrid->m_Cell[nX][nY].InitCell();
143
144 if (m_ulIter == 1)
145 {
146 // For the first timestep only, check to see that all cells have some sediment on them
147 double const dSedThickness = m_pRasterGrid->m_Cell[nX][nY].dGetTotAllSedThickness();
148
149 if (dSedThickness <= 0)
150 {
151 nZeroThickness++;
152
153 // Note: Logging from parallel regions can cause race conditions, but this is for debugging only
154 // In production, consider collecting problematic cells and logging after the parallel region
156 {
157#ifdef _OPENMP
158#pragma omp critical(logging)
159#endif
160 LogStream << m_ulIter << ": " << WARN << "total sediment thickness is " << dSedThickness << " at [" << nX << "][" << nY << "] = {" << dGridCentroidXToExtCRSX(nX) << ", " << dGridCentroidYToExtCRSY(nY) << "}" << endl;
161 }
162 }
163
164 // For the first timestep only, calculate the elevation of all this cell's layers. During the rest of the simulation, each cell's elevation is re-calculated just after any change occurs on that cell
165 m_pRasterGrid->m_Cell[nX][nY].CalcAllLayerElevsAndD50();
166 }
167
168 // Note that these totals include sediment which is both within and outside the polygons (because we have not yet defined polygons for this iteration, duh!)
169 m_dStartIterConsFineAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetTotConsFineThickConsiderNotch();
170 m_dStartIterConsSandAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetTotConsSandThickConsiderNotch();
171 m_dStartIterConsCoarseAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetTotConsCoarseThickConsiderNotch();
172
173 m_dStartIterSuspFineAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetSuspendedSediment();
174 m_dStartIterUnconsFineAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetTotUnconsFine();
175 m_dStartIterUnconsSandAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetTotUnconsSand();
176 m_dStartIterUnconsCoarseAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetTotUnconsCoarse();
177
179 {
180 // If we have just a single measurement for deep water waves (either given by the user, or from a single wave station) then set all cells, even dry land cells, to the same value for deep water wave height, deep water wave orientation, and deep water period
181 m_pRasterGrid->m_Cell[nX][nY].SetCellDeepWaterWaveHeight(m_dAllCellsDeepWaterWaveHeight);
182 m_pRasterGrid->m_Cell[nX][nY].SetCellDeepWaterWaveAngle(m_dAllCellsDeepWaterWaveAngle);
183 m_pRasterGrid->m_Cell[nX][nY].SetCellDeepWaterWavePeriod(m_dAllCellsDeepWaterWavePeriod);
184 }
185 }
186 }
187
189 {
190 // Each cell's value for deep water wave height and deep water wave orientation is interpolated from multiple user-supplied values
191 int const nRet = nInterpolateAllDeepWaterWaveValues();
192
193 if (nRet != RTN_OK)
194 return nRet;
195
196 /* for (int n = 0; n < m_VlDeepWaterWaveValuesAtTimestep.size(); n++)
197 {
198 if (m_ulIter == m_VlDeepWaterWaveValuesAtTimestep[n])
199 {
200 // OK, this timestep we are doing the calculation
201 if (m_VlDeepWaterWaveValuesAtTimestep[n] > 1)
202 {
203 // TODO 036 For every timestep after the first, read in new values before doing the interpolation
204 }
205
206 // Interpolate values each cell's values for deep water height and orientation from user-supplied values
207 int nRet = nInterpolateAllDeepWaterWaveValues();
208 if (nRet != RTN_OK)
209 return nRet;
210 }
211 }*/
212 }
213
214 if (nZeroThickness > 0)
215 {
216 cerr << m_ulIter << ": " << WARN << nZeroThickness << (nZeroThickness > 1 ? " cells" : " cell") << "( out of " << m_nXGridSize * m_nYGridSize << ") have no sediment, is this correct?" << endl;
217 LogStream << m_ulIter << ": " << WARN << nZeroThickness << " cells have no sediment, is this correct?" << endl;
218 }
219
220 // // DEBUG CODE ===========================================================================================================
221 // string strOutFile = m_strOutPath;
222 // strOutFile += "init_deep_water_wave_height_";
223 // strOutFile += to_string(m_ulIter);
224 // strOutFile += ".tif";
225 // GDALDriver* pDriver = GetGDALDriverManager()->GetDriverByName("gtiff");
226 // GDALDataset* pDataSet = pDriver->Create(strOutFile.c_str(), m_nXGridSize, m_nYGridSize, 1, GDT_Float64, m_papszGDALRasterOptions);
227 // pDataSet->SetProjection(m_strGDALBasementDEMProjection.c_str());
228 // pDataSet->SetGeoTransform(m_dGeoTransform);
229 // double* pdRaster = new double[m_ulNumCells];
230 // int nn = 0;
231 // for (int nY = 0; nY < m_nYGridSize; nY++)
232 // {
233 // for (int nX = 0; nX < m_nXGridSize; nX++)
234 // {
235 // // Write this value to the array
236 // pdRaster[nn] = m_pRasterGrid->m_Cell[nX][nY].dGetCellDeepWaterWaveHeight();
237 // nn++;
238 // }
239 // }
240 //
241 // GDALRasterBand* pBand = pDataSet->GetRasterBand(1);
242 // pBand->SetNoDataValue(m_nMissingValue);
243 // int nRet = pBand->RasterIO(GF_Write, 0, 0, m_nXGridSize, m_nYGridSize, pdRaster, m_nXGridSize, m_nYGridSize, GDT_Float64, 0, 0, NULL);
244 //
245 // if (nRet == CE_Failure)
246 // return RTN_ERR_GRIDCREATE;
247 //
248 // GDALClose(pDataSet);
249 // // DEBUG CODE ===========================================================================================================
250
251 // // DEBUG CODE ===========================================================================================================
252 // strOutFile = m_strOutPath;
253 // strOutFile += "init_deep_water_wave_angle_";
254 // strOutFile += to_string(m_ulIter);
255 // strOutFile += ".tif";
256 // pDataSet = pDriver->Create(strOutFile.c_str(), m_nXGridSize, m_nYGridSize, 1, GDT_Float64, m_papszGDALRasterOptions);
257 // pDataSet->SetProjection(m_strGDALBasementDEMProjection.c_str());
258 // pDataSet->SetGeoTransform(m_dGeoTransform);
259 // nn = 0;
260 // for (int nY = 0; nY < m_nYGridSize; nY++)
261 // {
262 // for (int nX = 0; nX < m_nXGridSize; nX++)
263 // {
264 // // Write this value to the array
265 // pdRaster[nn] = m_pRasterGrid->m_Cell[nX][nY].dGetCellDeepWaterWaveAngle();
266 // nn++;
267 // }
268 // }
269 //
270 // pBand = pDataSet->GetRasterBand(1);
271 // pBand->SetNoDataValue(m_nMissingValue);
272 // nRet = pBand->RasterIO(GF_Write, 0, 0, m_nXGridSize, m_nYGridSize, pdRaster, m_nXGridSize, m_nYGridSize, GDT_Float64, 0, 0, NULL);
273 //
274 // if (nRet == CE_Failure)
275 // return RTN_ERR_GRIDCREATE;
276 //
277 // GDALClose(pDataSet);
278 // // DEBUG CODE ===========================================================================================================
279
280 // // DEBUG CODE ===========================================================================================================
281 // strOutFile = m_strOutPath;
282 // strOutFile += "init_water_wave_angle_";
283 // strOutFile += to_string(m_ulIter);
284 // strOutFile += ".tif";
285 // pDataSet = pDriver->Create(strOutFile.c_str(), m_nXGridSize, m_nYGridSize, 1, GDT_Float64, m_papszGDALRasterOptions);
286 // pDataSet->SetProjection(m_strGDALBasementDEMProjection.c_str());
287 // pDataSet->SetGeoTransform(m_dGeoTransform);
288 // nn = 0;
289 // for (int nY = 0; nY < m_nYGridSize; nY++)
290 // {
291 // for (int nX = 0; nX < m_nXGridSize; nX++)
292 // {
293 // // Write this value to the array
294 // pdRaster[nn] = m_pRasterGrid->m_Cell[nX][nY].dGetWaveAngle();
295 // nn++;
296 // }
297 // }
298 //
299 // pBand = pDataSet->GetRasterBand(1);
300 // pBand->SetNoDataValue(m_nMissingValue);
301 // nRet = pBand->RasterIO(GF_Write, 0, 0, m_nXGridSize, m_nYGridSize, pdRaster, m_nXGridSize, m_nYGridSize, GDT_Float64, 0, 0, NULL);
302 //
303 // if (nRet == CE_Failure)
304 // return RTN_ERR_GRIDCREATE;
305 //
306 // GDALClose(pDataSet);
307 // // DEBUG CODE ===========================================================================================================
308
309 // // DEBUG CODE ===========================================================================================================
310 // strOutFile = m_strOutPath;
311 // strOutFile += "init_water_wave_height_";
312 // strOutFile += to_string(m_ulIter);
313 // strOutFile += ".tif";
314 // pDataSet = pDriver->Create(strOutFile.c_str(), m_nXGridSize, m_nYGridSize, 1, GDT_Float64, m_papszGDALRasterOptions);
315 // pDataSet->SetProjection(m_strGDALBasementDEMProjection.c_str());
316 // pDataSet->SetGeoTransform(m_dGeoTransform);
317 // nn = 0;
318 // for (int nY = 0; nY < m_nYGridSize; nY++)
319 // {
320 // for (int nX = 0; nX < m_nXGridSize; nX++)
321 // {
322 // // Write this value to the array
323 // pdRaster[nn] = m_pRasterGrid->m_Cell[nX][nY].dGetWaveHeight();
324 // nn++;
325 // }
326 // }
327 //
328 // pBand = pDataSet->GetRasterBand(1);
329 // pBand->SetNoDataValue(m_nMissingValue);
330 // nRet = pBand->RasterIO(GF_Write, 0, 0, m_nXGridSize, m_nYGridSize, pdRaster, m_nXGridSize, m_nYGridSize, GDT_Float64, 0, 0, NULL);
331 //
332 // if (nRet == CE_Failure)
333 // return RTN_ERR_GRIDCREATE;
334 //
335 // GDALClose(pDataSet);
336 // delete[] pdRaster;
337 // // DEBUG CODE ===========================================================================================================
338
339 return RTN_OK;
340}
Contains CGeomCell definitions.
double m_dThisIterPotentialBeachErosion
Total potential beach erosion (all size classes of unconsolidated sediment) for this iteration (depth...
Definition simulation.h:844
void CalcDepthOfClosure(void)
Calculate the depth of closure.
Definition utils.cpp:2625
int m_nLogFileDetail
The level of detail in the log file output. Can be LOG_FILE_LOW_DETAIL, LOG_FILE_MIDDLE_DETAIL,...
Definition simulation.h:574
double m_dAllCellsDeepWaterWaveHeight
Deep water wave height (m) for all sea cells.
Definition simulation.h:754
int m_nYMinBoundingBox
The minimum y value of the bounding box.
Definition simulation.h:544
CGeomRasterGrid * m_pRasterGrid
Pointer to the raster grid object.
int m_nXGridSize
The size of the grid in the x direction.
Definition simulation.h:457
ofstream LogStream
double m_dThisIterBeachErosionCoarse
Total actual beach erosion (coarse unconsolidated sediment) for this iteration (depth in m)
Definition simulation.h:853
vector< CRWCoast > m_VFloodWaveSetupSurgeRunup
TODO 007 Info needed.
double m_dStartIterUnconsCoarseAllCells
Depth (m) of coarse unconsolidated sediment at the start of the simulation, all cells (both inside an...
Definition simulation.h:994
vector< CRWCoast > m_VCoast
The coastline objects.
bool m_bSingleDeepWaterWaveValues
Do we have just a point source for (i.e. only a single measurement of) deep water wave values.
Definition simulation.h:385
double m_dThisIterActualPlatformErosionCoarseCons
Total actual platform erosion (coarse consolidated sediment) for this iteration (depth in m)
Definition simulation.h:841
vector< CGeomCoastPolygon * > m_pVCoastPolygon
Pointers to coast polygon objects, in down-coast sequence TODO 044 Will need to use global polygon ID...
int m_nYGridSize
The size of the grid in the y direction.
Definition simulation.h:460
double m_dStartIterSuspFineInPolygons
Depth (m) of fine suspended sediment at the start of the simulation (only cells in polygons)
Definition simulation.h:985
unsigned long m_ulThisIterNumCoastCells
The number of grid cells which are marked as coast, for this iteration.
Definition simulation.h:613
double m_dThisIterCliffCollapseErosionCoarseUncons
This-iteration total of coarse unconsolidated sediment produced by cliff collapse (m^3)
Definition simulation.h:934
vector< CRWCoast > m_VFloodWaveSetupSurge
TODO 007 Info needed.
unsigned long m_ulThisIterNumActualPlatformErosionCells
The number of grid cells on which actual platform erosion occurs, for this iteration.
Definition simulation.h:619
int m_nXMaxBoundingBox
The maximum x value of the bounding box.
Definition simulation.h:541
double m_dThisIterCliffCollapseErosionFineUncons
This-iteration total of fine unconsolidated sediment produced by cliff collapse (m^3)
Definition simulation.h:928
int m_nLayers
The number of sediment layers.
Definition simulation.h:463
double m_dStartIterConsCoarseAllCells
Depth (m) of coarse consolidated sediment at the start of the simulation, all cells (both inside and ...
double m_dStartIterConsSandAllCells
Depth (m) of sand consolidated sediment at the start of the simulation, all cells (both inside and ou...
unsigned long m_ulThisIterNumBeachDepositionCells
The number of grid cells on which beach (unconsolidated sediment) deposition occurs,...
Definition simulation.h:628
double m_dThisiterUnconsCoarseInput
Depth (m) of coarse unconsolidated sediment added, at this iteration.
Definition simulation.h:979
double m_dThisIterPotentialPlatformErosion
Total potential platform erosion (all size classes of consolidated sediment) for this iteration (dept...
Definition simulation.h:832
double m_dThisiterUnconsFineInput
Depth (m) of fine unconsolidated sediment added, at this iteration.
Definition simulation.h:973
bool m_bHaveWaveStationData
Do we have wave station data?
Definition simulation.h:388
double m_dThisIterActualPlatformErosionSandCons
Total actual platform erosion (sand consolidated sediment) for this iteration (depth in m)
Definition simulation.h:838
double m_dThisIterBeachDepositionCoarse
Total beach deposition (coarse unconsolidated sediment) for this iteration (depth in m)
Definition simulation.h:859
double dGridCentroidYToExtCRSY(int const) const
Definition gis_utils.cpp:98
double m_dAllCellsDeepWaterWaveAngle
Deep water wave angle for all sea cells.
Definition simulation.h:757
double m_dThisIterUnconsCoarseCliffDeposition
This-iteration total of coarse unconsolidated sediment deposited due to cliff collapse (m^3)
Definition simulation.h:949
double m_dThisIterTotSeaDepth
Total sea depth (m) for this iteration.
Definition simulation.h:829
double m_dThisIterUnconsSandCliffDeposition
This-iteration total of sand unconsolidated sediment deposited due to cliff collapse (m^3)
Definition simulation.h:946
int nInterpolateAllDeepWaterWaveValues(void)
If the user supplies multiple deep water wave height and angle values, this routine interplates these...
double m_dThisIterPotentialSedLostBeachErosion
Total unconsolidated sediment from beach erosion (all size classes) lost from the grid this iteration...
Definition simulation.h:865
double m_dStartIterConsFineAllCells
Depth (m) of fine consolidated sediment at the start of the simulation, all cells (both inside and ou...
Definition simulation.h:997
double m_dThisIterBeachErosionSand
Total actual beach erosion (sand unconsolidated sediment) for this iteration (depth in m)
Definition simulation.h:850
double m_dThisIterFineSedimentToSuspension
Total fine unconsolidated sediment in suspension for this iteration (depth in m)
Definition simulation.h:862
double m_dThisIterBeachErosionFine
Total actual beach erosion (fine unconsolidated sediment) for this iteration (depth in m)
Definition simulation.h:847
unsigned long m_ulThisIterNumActualBeachErosionCells
The number of grid cells on which actual beach (unconsolidated sediment) erosion occurs,...
Definition simulation.h:625
int m_nYMaxBoundingBox
The maximum y value of the bounding box.
Definition simulation.h:547
vector< bool > m_bUnconsChangedThisIter
One element per layer: has the consolidated sediment of this layer been changed during this iteration...
unsigned long m_ulThisIterNumPotentialPlatformErosionCells
The number of grid cells on which potential platform erosion occurs, for this iteration.
Definition simulation.h:616
double m_dStartIterUnconsSandAllCells
Depth (m) of sand unconsolidated sediment at the start of the simulation, all cells (both inside and ...
Definition simulation.h:991
double m_dThisIterCliffCollapseErosionFineCons
This-iteration total of fine consolidated sediment produced by cliff collapse (m^3)
Definition simulation.h:937
unsigned long m_ulThisIterNumSeaCells
The number of grid cells which are marked as sea, for this iteration.
Definition simulation.h:610
double m_dStartIterUnconsFineAllCells
Depth (m) of fine unconsolidated sediment at the start of the simulation, all cells (both inside and ...
Definition simulation.h:988
int m_nXMinBoundingBox
The minimum x value of the bounding box.
Definition simulation.h:538
unsigned long m_ulThisIterNumPotentialBeachErosionCells
The number of grid cells on which potential beach (unconsolidated sediment) erosion occurs,...
Definition simulation.h:622
double m_dThisIterCliffCollapseErosionSandCons
This-iteration total of sand consolidated sediment produced by cliff collapse (m^3)
Definition simulation.h:940
double m_dThisIterBeachDepositionSand
Total beach deposition (sand unconsolidated sediment) for this iteration (depth in m)
Definition simulation.h:856
double m_dThisIterLeftGridUnconsCoarse
Total coarse unconsolidated sediment lost from the grid this iteration (depth in m)
Definition simulation.h:874
double m_dThisIterCliffCollapseErosionSandUncons
This-iteration total of sand unconsolidated sediment produced by cliff collapse (m^3)
Definition simulation.h:931
unsigned long m_ulIter
The number of the current iteration (time step)
Definition simulation.h:598
double dGridCentroidXToExtCRSX(int const) const
Definition gis_utils.cpp:87
double m_dThisiterUnconsSandInput
Depth (m) of sand unconsolidated sediment added, at this iteration.
Definition simulation.h:976
double m_dAllCellsDeepWaterWavePeriod
Deep water wave period for all sea cells.
Definition simulation.h:760
double m_dThisIterActualPlatformErosionFineCons
Total actual platform erosion (fine consolidated sediment) for this iteration (depth in m)
Definition simulation.h:835
double m_dThisIterCliffCollapseErosionCoarseCons
This-iteration total of coarse consolidated sediment produced by cliff collapse (m^3)
Definition simulation.h:943
double m_dThisIterLeftGridUnconsSand
Total sand unconsolidated sediment lost from the grid this iteration (depth in m)
Definition simulation.h:871
int nInitGridAndCalcStillWaterLevel(void)
At the beginning of each timestep: clear vector coasts, profiles, and polygons, initialize the raster...
Definition init_grid.cpp:53
double m_dStartIterSuspFineAllCells
Depth (m) of fine suspended sediment at the start of the simulation, all cells (both inside and outsi...
Definition simulation.h:982
vector< bool > m_bConsChangedThisIter
One element per layer: has the consolidated sediment of this layer been changed during this iteration...
double m_dThisIterLeftGridUnconsFine
Total fine unconsolidated sediment lost from the grid this iteration (depth in m)
Definition simulation.h:868
This file contains global definitions for CoastalME.
int const LOG_FILE_MIDDLE_DETAIL
Definition cme.h:491
string const WARN
Definition cme.h:893
int const RTN_OK
Definition cme.h:694
Contains CRWCoast definitions.
Contains CGeomRasterGrid definitions.
Contains CSimulation definitions.