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
10
11/* ==============================================================================================================================
12 This file is part of CoastalME, the Coastal Modelling Environment.
13
14 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.
15
16 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.
17
18 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.
19==============================================================================================================================*/
20#include <assert.h>
21
22#include <climits>
23
24#include <iostream>
25using std::cerr;
26using std::endl;
27
28#ifdef _OPENMP
29#include <omp.h>
30#endif
31
32#include "cme.h"
33#include "cell.h"
34#include "coast.h"
35#include "simulation.h"
36#include "raster_grid.h"
37
38//===============================================================================================================================
40//===============================================================================================================================
42{
43 // Clear all vector coastlines, profiles, and polygons
44 m_VCoast.clear();
45
46 // m_VFloodWaveSetup.clear();
49
50 // Do some every-timestep initialisation
51 m_nXMinBoundingBox = INT_MAX;
52 m_nXMaxBoundingBox = INT_MIN;
53 m_nYMinBoundingBox = INT_MAX;
54 m_nYMaxBoundingBox = INT_MIN;
55
60
64
86 m_dThisIterLeftGridUnconsFine = 0; // TODO 067 Suspended fine sediment never decreases i.e. no suspended fine sediment ever leaves the grid. Is this OK?
92
93 for (int n = 0; n < m_nLayers; n++)
94 {
95 m_bConsChangedThisIter[n] = false;
96 m_bUnconsChangedThisIter[n] = false;
97 }
98
99 // Re-calculate the depth of closure, in case deep water wave properties have changed
101
102 int nZeroThickness = 0;
103
112
113 // And go through all cells in the RasterGrid array
114 // Use OpenMP parallel loop with reduction clauses for thread-safe accumulation
115#ifdef _OPENMP
116#pragma omp parallel for collapse(2) reduction(+ : nZeroThickness) \
117 reduction(+ : m_dStartIterConsFineAllCells, m_dStartIterConsSandAllCells, m_dStartIterConsCoarseAllCells) \
118 reduction(+ : m_dStartIterSuspFineAllCells, m_dStartIterUnconsFineAllCells, m_dStartIterUnconsSandAllCells, m_dStartIterUnconsCoarseAllCells)
119#endif
120
121 for (int nX = 0; nX < m_nXGridSize; nX++)
122 {
123 for (int nY = 0; nY < m_nYGridSize; nY++)
124 {
125 // Re-initialise values for this cell
126 m_pRasterGrid->m_Cell[nX][nY].InitCell();
127
128 if (m_ulIter == 1)
129 {
130 // For the first timestep only, check to see that all cells have some sediment on them
131 double const dSedThickness = m_pRasterGrid->m_Cell[nX][nY].dGetAllSedDepthAllLayers();
132
133 if (dSedThickness <= 0)
134 {
135 nZeroThickness++;
136
137 // Note: Logging from parallel regions can cause race conditions, but this is for debugging only
138 // In production, consider collecting problematic cells and logging after the parallel region
140 {
141#ifdef _OPENMP
142#pragma omp critical(logging)
143#endif
144 LogStream << m_ulIter << ": " << WARN << "total sediment thickness is " << dSedThickness << " at [" << nX << "][" << nY << "] = {" << dGridCentroidXToExtCRSX(nX) << ", " << dGridCentroidYToExtCRSY(nY) << "}" << endl;
145 }
146 }
147
148 // 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
149 m_pRasterGrid->m_Cell[nX][nY].CalcAllLayerElevsAndD50();
150 }
151
152 // 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!)
153 m_dStartIterConsFineAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetConsFineDepthAllLayers();
154 m_dStartIterConsSandAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetConsSandDepthAllLayers();
155 m_dStartIterConsCoarseAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetConsCoarseDepthAllLayers();
156
157 m_dStartIterSuspFineAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetSuspendedSediment();
158 m_dStartIterUnconsFineAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetUnconsFineDepthAllLayers();
159 m_dStartIterUnconsSandAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetUnconsSandDepthAllLayers();
160 m_dStartIterUnconsCoarseAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetUnconsCoarseDepthAllLayers();
161
163 {
164 // 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
165 m_pRasterGrid->m_Cell[nX][nY].SetCellDeepWaterWaveHeight(m_dAllCellsDeepWaterWaveHeight);
166 m_pRasterGrid->m_Cell[nX][nY].SetCellDeepWaterWaveAngle(m_dAllCellsDeepWaterWaveAngle);
167 m_pRasterGrid->m_Cell[nX][nY].SetCellDeepWaterWavePeriod(m_dAllCellsDeepWaterWavePeriod);
168 }
169 }
170 }
171
173 {
174 // Each cell's value for deep water wave height and deep water wave orientation is interpolated from multiple user-supplied values
175 int const nRet = nInterpolateAllDeepWaterWaveValues();
176
177 if (nRet != RTN_OK)
178 return nRet;
179
180 /* for (int n = 0; n < m_VlDeepWaterWaveValuesAtTimestep.size(); n++)
181 {
182 if (m_ulIter == m_VlDeepWaterWaveValuesAtTimestep[n])
183 {
184 // OK, this timestep we are doing the calculation
185 if (m_VlDeepWaterWaveValuesAtTimestep[n] > 1)
186 {
187 // TODO 036 For every timestep after the first, read in new values before doing the interpolation
188 }
189
190 // Interpolate values each cell's values for deep water height and orientation from user-supplied values
191 int nRet = nInterpolateAllDeepWaterWaveValues();
192 if (nRet != RTN_OK)
193 return nRet;
194 }
195 }*/
196 }
197
198 if (nZeroThickness > 0)
199 {
200 cerr << m_ulIter << ": " << WARN << nZeroThickness << (nZeroThickness > 1 ? " cells" : " cell") << "( out of " << m_nXGridSize * m_nYGridSize << ") have no sediment, is this correct?" << endl;
201 LogStream << m_ulIter << ": " << WARN << nZeroThickness << " cells have no sediment, is this correct?" << endl;
202 }
203
204 // // DEBUG CODE ===========================================================================================================
205 // string strOutFile = m_strOutPath;
206 // strOutFile += "init_deep_water_wave_height_";
207 // strOutFile += to_string(m_ulIter);
208 // strOutFile += ".tif";
209 // GDALDriver* pDriver = GetGDALDriverManager()->GetDriverByName("gtiff");
210 // GDALDataset* pDataSet = pDriver->Create(strOutFile.c_str(), m_nXGridSize, m_nYGridSize, 1, GDT_Float64, m_papszGDALRasterOptions);
211 // pDataSet->SetProjection(m_strGDALBasementDEMProjection.c_str());
212 // pDataSet->SetGeoTransform(m_dGeoTransform);
213 // double* pdRaster = new double[m_ulNumCells];
214 // int nn = 0;
215 // for (int nY = 0; nY < m_nYGridSize; nY++)
216 // {
217 // for (int nX = 0; nX < m_nXGridSize; nX++)
218 // {
219 // // Write this value to the array
220 // pdRaster[nn] = m_pRasterGrid->m_Cell[nX][nY].dGetCellDeepWaterWaveHeight();
221 // nn++;
222 // }
223 // }
224 //
225 // GDALRasterBand* pBand = pDataSet->GetRasterBand(1);
226 // pBand->SetNoDataValue(m_nMissingValue);
227 // int nRet = pBand->RasterIO(GF_Write, 0, 0, m_nXGridSize, m_nYGridSize, pdRaster, m_nXGridSize, m_nYGridSize, GDT_Float64, 0, 0, NULL);
228 //
229 // if (nRet == CE_Failure)
230 // return RTN_ERR_GRIDCREATE;
231 //
232 // GDALClose(pDataSet);
233 // // DEBUG CODE ===========================================================================================================
234
235 // // DEBUG CODE ===========================================================================================================
236 // strOutFile = m_strOutPath;
237 // strOutFile += "init_deep_water_wave_angle_";
238 // strOutFile += to_string(m_ulIter);
239 // strOutFile += ".tif";
240 // pDataSet = pDriver->Create(strOutFile.c_str(), m_nXGridSize, m_nYGridSize, 1, GDT_Float64, m_papszGDALRasterOptions);
241 // pDataSet->SetProjection(m_strGDALBasementDEMProjection.c_str());
242 // pDataSet->SetGeoTransform(m_dGeoTransform);
243 // nn = 0;
244 // for (int nY = 0; nY < m_nYGridSize; nY++)
245 // {
246 // for (int nX = 0; nX < m_nXGridSize; nX++)
247 // {
248 // // Write this value to the array
249 // pdRaster[nn] = m_pRasterGrid->m_Cell[nX][nY].dGetCellDeepWaterWaveAngle();
250 // nn++;
251 // }
252 // }
253 //
254 // pBand = pDataSet->GetRasterBand(1);
255 // pBand->SetNoDataValue(m_nMissingValue);
256 // nRet = pBand->RasterIO(GF_Write, 0, 0, m_nXGridSize, m_nYGridSize, pdRaster, m_nXGridSize, m_nYGridSize, GDT_Float64, 0, 0, NULL);
257 //
258 // if (nRet == CE_Failure)
259 // return RTN_ERR_GRIDCREATE;
260 //
261 // GDALClose(pDataSet);
262 // // DEBUG CODE ===========================================================================================================
263
264 // // DEBUG CODE ===========================================================================================================
265 // strOutFile = m_strOutPath;
266 // strOutFile += "init_water_wave_angle_";
267 // strOutFile += to_string(m_ulIter);
268 // strOutFile += ".tif";
269 // pDataSet = pDriver->Create(strOutFile.c_str(), m_nXGridSize, m_nYGridSize, 1, GDT_Float64, m_papszGDALRasterOptions);
270 // pDataSet->SetProjection(m_strGDALBasementDEMProjection.c_str());
271 // pDataSet->SetGeoTransform(m_dGeoTransform);
272 // nn = 0;
273 // for (int nY = 0; nY < m_nYGridSize; nY++)
274 // {
275 // for (int nX = 0; nX < m_nXGridSize; nX++)
276 // {
277 // // Write this value to the array
278 // pdRaster[nn] = m_pRasterGrid->m_Cell[nX][nY].dGetWaveAngle();
279 // nn++;
280 // }
281 // }
282 //
283 // pBand = pDataSet->GetRasterBand(1);
284 // pBand->SetNoDataValue(m_nMissingValue);
285 // nRet = pBand->RasterIO(GF_Write, 0, 0, m_nXGridSize, m_nYGridSize, pdRaster, m_nXGridSize, m_nYGridSize, GDT_Float64, 0, 0, NULL);
286 //
287 // if (nRet == CE_Failure)
288 // return RTN_ERR_GRIDCREATE;
289 //
290 // GDALClose(pDataSet);
291 // // DEBUG CODE ===========================================================================================================
292
293 // // DEBUG CODE ===========================================================================================================
294 // strOutFile = m_strOutPath;
295 // strOutFile += "init_water_wave_height_";
296 // strOutFile += to_string(m_ulIter);
297 // strOutFile += ".tif";
298 // pDataSet = pDriver->Create(strOutFile.c_str(), m_nXGridSize, m_nYGridSize, 1, GDT_Float64, m_papszGDALRasterOptions);
299 // pDataSet->SetProjection(m_strGDALBasementDEMProjection.c_str());
300 // pDataSet->SetGeoTransform(m_dGeoTransform);
301 // nn = 0;
302 // for (int nY = 0; nY < m_nYGridSize; nY++)
303 // {
304 // for (int nX = 0; nX < m_nXGridSize; nX++)
305 // {
306 // // Write this value to the array
307 // pdRaster[nn] = m_pRasterGrid->m_Cell[nX][nY].dGetWaveHeight();
308 // nn++;
309 // }
310 // }
311 //
312 // pBand = pDataSet->GetRasterBand(1);
313 // pBand->SetNoDataValue(m_nMissingValue);
314 // nRet = pBand->RasterIO(GF_Write, 0, 0, m_nXGridSize, m_nYGridSize, pdRaster, m_nXGridSize, m_nYGridSize, GDT_Float64, 0, 0, NULL);
315 //
316 // if (nRet == CE_Failure)
317 // return RTN_ERR_GRIDCREATE;
318 //
319 // GDALClose(pDataSet);
320 // delete[] pdRaster;
321 // // DEBUG CODE ===========================================================================================================
322
323 return RTN_OK;
324}
Contains CGeomCell definitions.
double m_dThisIterPotentialBeachErosion
Total potential beach erosion (all size classes of unconsolidated sediment) for this iteration (depth...
Definition simulation.h:861
void CalcDepthOfClosure(void)
Calculate the depth of closure.
Definition utils.cpp:2760
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:588
double m_dAllCellsDeepWaterWaveHeight
Deep water wave height (m) for all sea cells.
Definition simulation.h:768
int m_nYMinBoundingBox
The minimum y value of the bounding box.
Definition simulation.h:558
CGeomRasterGrid * m_pRasterGrid
Pointer to the raster grid object.
int m_nXGridSize
The size of the grid in the x direction.
Definition simulation.h:477
ofstream LogStream
double m_dThisIterBeachErosionCoarse
Total actual beach erosion (coarse unconsolidated sediment) for this iteration (depth in m)
Definition simulation.h:870
vector< CRWCoast > m_VFloodWaveSetupSurgeRunup
TODO 007 Finish surge and runup stuff.
double m_dStartIterUnconsCoarseAllCells
Depth (m) of coarse unconsolidated sediment at the start of the simulation, all cells (both inside an...
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:387
double m_dThisIterActualPlatformErosionCoarseCons
Total actual platform erosion (coarse consolidated sediment) for this iteration (depth in m)
Definition simulation.h:858
int m_nYGridSize
The size of the grid in the y direction.
Definition simulation.h:480
double m_dStartIterSuspFineInPolygons
Depth (m) of fine suspended sediment at the start of the simulation (only cells in polygons)
unsigned long m_ulThisIterNumCoastCells
The number of grid cells which are marked as coast, for this iteration.
Definition simulation.h:624
double m_dThisIterCliffCollapseErosionCoarseUncons
This-iteration total of coarse unconsolidated sediment produced by cliff collapse (m^3)
Definition simulation.h:954
vector< CRWCoast > m_VFloodWaveSetupSurge
TODO 007 Finish surge and runup stuff.
unsigned long m_ulThisIterNumActualPlatformErosionCells
The number of grid cells on which actual platform erosion occurs, for this iteration.
Definition simulation.h:630
int m_nXMaxBoundingBox
The maximum x value of the bounding box.
Definition simulation.h:555
double m_dThisIterCliffCollapseErosionFineUncons
This-iteration total of fine unconsolidated sediment produced by cliff collapse (m^3)
Definition simulation.h:948
int m_nLayers
The number of sediment layers.
Definition simulation.h:483
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:639
double m_dThisiterUnconsCoarseInput
Depth (m) of coarse unconsolidated sediment added, at this iteration.
Definition simulation.h:999
double m_dThisIterPotentialPlatformErosion
Total potential platform erosion (all size classes of consolidated sediment) for this iteration (dept...
Definition simulation.h:849
double m_dThisiterUnconsFineInput
Depth (m) of fine unconsolidated sediment added, at this iteration.
Definition simulation.h:993
bool m_bHaveWaveStationData
Do we have wave station data?
Definition simulation.h:390
double m_dThisIterActualPlatformErosionSandCons
Total actual platform erosion (sand consolidated sediment) for this iteration (depth in m)
Definition simulation.h:855
double m_dThisIterBeachDepositionCoarse
Total beach deposition (coarse unconsolidated sediment) for this iteration (depth in m)
Definition simulation.h:876
double dGridCentroidYToExtCRSY(int const) const
Given the integer Y-axis ordinate of a cell in the raster grid CRS, returns the external CRS Y-axis o...
Definition gis_utils.cpp:74
double m_dAllCellsDeepWaterWaveAngle
Deep water wave angle for all sea cells.
Definition simulation.h:771
double m_dThisIterUnconsCoarseCliffDeposition
This-iteration total of coarse unconsolidated sediment deposited due to cliff collapse (m^3)
Definition simulation.h:969
double m_dThisIterTotSeaDepth
Total sea depth (m) for this iteration.
Definition simulation.h:846
double m_dThisIterUnconsSandCliffDeposition
This-iteration total of sand unconsolidated sediment deposited due to cliff collapse (m^3)
Definition simulation.h:966
int nInterpolateAllDeepWaterWaveValues(void)
double m_dThisIterPotentialSedLostBeachErosion
Total unconsolidated sediment from beach erosion (all size classes) lost from the grid this iteration...
Definition simulation.h:882
double m_dStartIterConsFineAllCells
Depth (m) of fine consolidated sediment at the start of the simulation, all cells (both inside and ou...
double m_dThisIterBeachErosionSand
Total actual beach erosion (sand unconsolidated sediment) for this iteration (depth in m)
Definition simulation.h:867
double m_dThisIterFineSedimentToSuspension
Total fine unconsolidated sediment in suspension for this iteration (depth in m)
Definition simulation.h:879
double m_dThisIterBeachErosionFine
Total actual beach erosion (fine unconsolidated sediment) for this iteration (depth in m)
Definition simulation.h:864
unsigned long m_ulThisIterNumActualBeachErosionCells
The number of grid cells on which actual beach (unconsolidated sediment) erosion occurs,...
Definition simulation.h:636
int m_nYMaxBoundingBox
The maximum y value of the bounding box.
Definition simulation.h:561
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:627
double m_dStartIterUnconsSandAllCells
Depth (m) of sand unconsolidated sediment at the start of the simulation, all cells (both inside and ...
double m_dThisIterCliffCollapseErosionFineCons
This-iteration total of fine consolidated sediment produced by cliff collapse (m^3)
Definition simulation.h:957
unsigned long m_ulThisIterNumSeaCells
The number of grid cells which are marked as sea, for this iteration.
Definition simulation.h:621
double m_dStartIterUnconsFineAllCells
Depth (m) of fine unconsolidated sediment at the start of the simulation, all cells (both inside and ...
int m_nXMinBoundingBox
The minimum x value of the bounding box.
Definition simulation.h:552
unsigned long m_ulThisIterNumPotentialBeachErosionCells
The number of grid cells on which potential beach (unconsolidated sediment) erosion occurs,...
Definition simulation.h:633
double m_dThisIterCliffCollapseErosionSandCons
This-iteration total of sand consolidated sediment produced by cliff collapse (m^3)
Definition simulation.h:960
double m_dThisIterBeachDepositionSand
Total beach deposition (sand unconsolidated sediment) for this iteration (depth in m)
Definition simulation.h:873
double m_dThisIterLeftGridUnconsCoarse
Total coarse unconsolidated sediment lost from the grid this iteration (depth in m)
Definition simulation.h:891
double m_dThisIterCliffCollapseErosionSandUncons
This-iteration total of sand unconsolidated sediment produced by cliff collapse (m^3)
Definition simulation.h:951
unsigned long m_ulIter
The number of the current iteration (time step)
Definition simulation.h:609
double dGridCentroidXToExtCRSX(int const) const
Given the integer X-axis ordinate of a cell in the raster grid CRS, returns the external CRS X-axis o...
Definition gis_utils.cpp:65
double m_dThisiterUnconsSandInput
Depth (m) of sand unconsolidated sediment added, at this iteration.
Definition simulation.h:996
double m_dAllCellsDeepWaterWavePeriod
Deep water wave period for all sea cells.
Definition simulation.h:774
double m_dThisIterActualPlatformErosionFineCons
Total actual platform erosion (fine consolidated sediment) for this iteration (depth in m)
Definition simulation.h:852
double m_dThisIterCliffCollapseErosionCoarseCons
This-iteration total of coarse consolidated sediment produced by cliff collapse (m^3)
Definition simulation.h:963
double m_dThisIterLeftGridUnconsSand
Total sand unconsolidated sediment lost from the grid this iteration (depth in m)
Definition simulation.h:888
int nInitGridAndCalcStillWaterLevel(void)
At the beginning of each timestep: clear vector coasts, profiles, and polygons, initialise the raster...
Definition init_grid.cpp:41
double m_dStartIterSuspFineAllCells
Depth (m) of fine suspended sediment at the start of the simulation, all cells (both inside and outsi...
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:885
This file contains global definitions for CoastalME.
int const LOG_FILE_MIDDLE_DETAIL
Definition cme.h:393
string const WARN
Definition cme.h:806
int const RTN_OK
Definition cme.h:585
Contains CRWCoast definitions.
Contains CGeomRasterGrid definitions.
Contains CSimulation definitions.