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 <string>
27using std::to_string;
28
29#include <iostream>
30using std::cerr;
31using std::endl;
32
33#include <gdal_priv.h>
34#include <gdal_alg.h>
35
36#ifdef _OPENMP
37 #include <omp.h>
38#endif
39
40#include "cme.h"
41#include "line.h"
42#include "cell.h"
43#include "coast.h"
44#include "simulation.h"
45#include "raster_grid.h"
46
47//===============================================================================================================================
49//===============================================================================================================================
51{
52 // Clear all vector coastlines, profiles, and polygons
53 for (int i = 0; i < static_cast<int>(m_pVCoastPolygon.size()); i++)
54 delete m_pVCoastPolygon[i];
55
56 m_pVCoastPolygon.clear();
57 m_VCoast.clear();
58
59 // m_VFloodWaveSetup.clear();
62
63 // Do some every-timestep initialization
64 m_nXMinBoundingBox = INT_MAX;
65 m_nXMaxBoundingBox = INT_MIN;
66 m_nYMinBoundingBox = INT_MAX;
67 m_nYMaxBoundingBox = INT_MIN;
68
73
77
99 m_dThisIterLeftGridUnconsFine = // TODO 067 Suspended fine sediment never decreases i.e. no suspended fine sediment ever leaves the grid. Is this OK?
105
106 for (int n = 0; n < m_nLayers; n++)
107 {
108 m_bConsChangedThisIter[n] = false;
109 m_bUnconsChangedThisIter[n] = false;
110 }
111
112 // Re-calculate the depth of closure, in case deep water wave properties have changed
114
115 int nZeroThickness = 0;
116
125
126 // And go through all cells in the RasterGrid array
127 // Use OpenMP parallel loop with reduction clauses for thread-safe accumulation
128#ifdef _OPENMP
129 #pragma omp parallel for collapse(2) reduction(+:nZeroThickness) \
130 reduction(+:m_dStartIterConsFineAllCells,m_dStartIterConsSandAllCells,m_dStartIterConsCoarseAllCells) \
131 reduction(+:m_dStartIterSuspFineAllCells,m_dStartIterUnconsFineAllCells,m_dStartIterUnconsSandAllCells,m_dStartIterUnconsCoarseAllCells)
132#endif
133
134 for (int nX = 0; nX < m_nXGridSize; nX++)
135 {
136 for (int nY = 0; nY < m_nYGridSize; nY++)
137 {
138 // Re-initialize values for this cell
139 m_pRasterGrid->m_Cell[nX][nY].InitCell();
140
141 if (m_ulIter == 1)
142 {
143 // For the first timestep only, check to see that all cells have some sediment on them
144 double dSedThickness = m_pRasterGrid->m_Cell[nX][nY].dGetTotAllSedThickness();
145
146 if (dSedThickness <= 0)
147 {
148 nZeroThickness++;
149
150 // Note: Logging from parallel regions can cause race conditions, but this is for debugging only
151 // In production, consider collecting problematic cells and logging after the parallel region
153 {
154#ifdef _OPENMP
155 #pragma omp critical(logging)
156#endif
157 LogStream << m_ulIter << ": " << WARN << "total sediment thickness is " << dSedThickness << " at [" << nX << "][" << nY << "] = {" << dGridCentroidXToExtCRSX(nX) << ", " << dGridCentroidYToExtCRSY(nY) << "}" << endl;
158 }
159 }
160
161 // 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
162 m_pRasterGrid->m_Cell[nX][nY].CalcAllLayerElevsAndD50();
163 }
164
165 // 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!)
166 m_dStartIterConsFineAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetTotConsFineThickConsiderNotch();
167 m_dStartIterConsSandAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetTotConsSandThickConsiderNotch();
168 m_dStartIterConsCoarseAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetTotConsCoarseThickConsiderNotch();
169
170 m_dStartIterSuspFineAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetSuspendedSediment();
171 m_dStartIterUnconsFineAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetTotUnconsFine();
172 m_dStartIterUnconsSandAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetTotUnconsSand();
173 m_dStartIterUnconsCoarseAllCells += m_pRasterGrid->m_Cell[nX][nY].dGetTotUnconsCoarse();
174
176 {
177 // 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
178 m_pRasterGrid->m_Cell[nX][nY].SetCellDeepWaterWaveHeight(m_dAllCellsDeepWaterWaveHeight);
179 m_pRasterGrid->m_Cell[nX][nY].SetCellDeepWaterWaveAngle(m_dAllCellsDeepWaterWaveAngle);
180 m_pRasterGrid->m_Cell[nX][nY].SetCellDeepWaterWavePeriod(m_dAllCellsDeepWaterWavePeriod);
181 }
182 }
183 }
184
186 {
187 // Each cell's value for deep water wave height and deep water wave orientation is interpolated from multiple user-supplied values
189
190 if (nRet != RTN_OK)
191 return nRet;
192
193 /* for (int n = 0; n < m_VlDeepWaterWaveValuesAtTimestep.size(); n++)
194 {
195 if (m_ulIter == m_VlDeepWaterWaveValuesAtTimestep[n])
196 {
197 // OK, this timestep we are doing the calculation
198 if (m_VlDeepWaterWaveValuesAtTimestep[n] > 1)
199 {
200 // TODO 036 For every timestep after the first, read in new values before doing the interpolation
201 }
202
203 // Interpolate values each cell's values for deep water height and orientation from user-supplied values
204 int nRet = nInterpolateAllDeepWaterWaveValues();
205 if (nRet != RTN_OK)
206 return nRet;
207 }
208 }*/
209 }
210
211 if (nZeroThickness > 0)
212 {
213 cerr << m_ulIter << ": " << WARN << nZeroThickness << (nZeroThickness > 1 ? " cells" : " cell") << "( out of " << m_nXGridSize * m_nYGridSize << ") have no sediment, is this correct?" << endl;
214 LogStream << m_ulIter << ": " << WARN << nZeroThickness << " cells have no sediment, is this correct?" << endl;
215 }
216
217 // // DEBUG CODE ===========================================================================================================
218 // string strOutFile = m_strOutPath;
219 // strOutFile += "init_deep_water_wave_height_";
220 // strOutFile += to_string(m_ulIter);
221 // strOutFile += ".tif";
222 // GDALDriver* pDriver = GetGDALDriverManager()->GetDriverByName("gtiff");
223 // GDALDataset* pDataSet = pDriver->Create(strOutFile.c_str(), m_nXGridSize, m_nYGridSize, 1, GDT_Float64, m_papszGDALRasterOptions);
224 // pDataSet->SetProjection(m_strGDALBasementDEMProjection.c_str());
225 // pDataSet->SetGeoTransform(m_dGeoTransform);
226 // double* pdRaster = new double[m_ulNumCells];
227 // int nn = 0;
228 // for (int nY = 0; nY < m_nYGridSize; nY++)
229 // {
230 // for (int nX = 0; nX < m_nXGridSize; nX++)
231 // {
232 // // Write this value to the array
233 // pdRaster[nn] = m_pRasterGrid->m_Cell[nX][nY].dGetCellDeepWaterWaveHeight();
234 // nn++;
235 // }
236 // }
237 //
238 // GDALRasterBand* pBand = pDataSet->GetRasterBand(1);
239 // pBand->SetNoDataValue(m_nMissingValue);
240 // int nRet = pBand->RasterIO(GF_Write, 0, 0, m_nXGridSize, m_nYGridSize, pdRaster, m_nXGridSize, m_nYGridSize, GDT_Float64, 0, 0, NULL);
241 //
242 // if (nRet == CE_Failure)
243 // return RTN_ERR_GRIDCREATE;
244 //
245 // GDALClose(pDataSet);
246 // // DEBUG CODE ===========================================================================================================
247
248 // // DEBUG CODE ===========================================================================================================
249 // strOutFile = m_strOutPath;
250 // strOutFile += "init_deep_water_wave_angle_";
251 // strOutFile += to_string(m_ulIter);
252 // strOutFile += ".tif";
253 // pDataSet = pDriver->Create(strOutFile.c_str(), m_nXGridSize, m_nYGridSize, 1, GDT_Float64, m_papszGDALRasterOptions);
254 // pDataSet->SetProjection(m_strGDALBasementDEMProjection.c_str());
255 // pDataSet->SetGeoTransform(m_dGeoTransform);
256 // nn = 0;
257 // for (int nY = 0; nY < m_nYGridSize; nY++)
258 // {
259 // for (int nX = 0; nX < m_nXGridSize; nX++)
260 // {
261 // // Write this value to the array
262 // pdRaster[nn] = m_pRasterGrid->m_Cell[nX][nY].dGetCellDeepWaterWaveAngle();
263 // nn++;
264 // }
265 // }
266 //
267 // pBand = pDataSet->GetRasterBand(1);
268 // pBand->SetNoDataValue(m_nMissingValue);
269 // nRet = pBand->RasterIO(GF_Write, 0, 0, m_nXGridSize, m_nYGridSize, pdRaster, m_nXGridSize, m_nYGridSize, GDT_Float64, 0, 0, NULL);
270 //
271 // if (nRet == CE_Failure)
272 // return RTN_ERR_GRIDCREATE;
273 //
274 // GDALClose(pDataSet);
275 // // DEBUG CODE ===========================================================================================================
276
277 // // DEBUG CODE ===========================================================================================================
278 // strOutFile = m_strOutPath;
279 // strOutFile += "init_water_wave_angle_";
280 // strOutFile += to_string(m_ulIter);
281 // strOutFile += ".tif";
282 // pDataSet = pDriver->Create(strOutFile.c_str(), m_nXGridSize, m_nYGridSize, 1, GDT_Float64, m_papszGDALRasterOptions);
283 // pDataSet->SetProjection(m_strGDALBasementDEMProjection.c_str());
284 // pDataSet->SetGeoTransform(m_dGeoTransform);
285 // nn = 0;
286 // for (int nY = 0; nY < m_nYGridSize; nY++)
287 // {
288 // for (int nX = 0; nX < m_nXGridSize; nX++)
289 // {
290 // // Write this value to the array
291 // pdRaster[nn] = m_pRasterGrid->m_Cell[nX][nY].dGetWaveAngle();
292 // nn++;
293 // }
294 // }
295 //
296 // pBand = pDataSet->GetRasterBand(1);
297 // pBand->SetNoDataValue(m_nMissingValue);
298 // nRet = pBand->RasterIO(GF_Write, 0, 0, m_nXGridSize, m_nYGridSize, pdRaster, m_nXGridSize, m_nYGridSize, GDT_Float64, 0, 0, NULL);
299 //
300 // if (nRet == CE_Failure)
301 // return RTN_ERR_GRIDCREATE;
302 //
303 // GDALClose(pDataSet);
304 // // DEBUG CODE ===========================================================================================================
305
306 // // DEBUG CODE ===========================================================================================================
307 // strOutFile = m_strOutPath;
308 // strOutFile += "init_water_wave_height_";
309 // strOutFile += to_string(m_ulIter);
310 // strOutFile += ".tif";
311 // pDataSet = pDriver->Create(strOutFile.c_str(), m_nXGridSize, m_nYGridSize, 1, GDT_Float64, m_papszGDALRasterOptions);
312 // pDataSet->SetProjection(m_strGDALBasementDEMProjection.c_str());
313 // pDataSet->SetGeoTransform(m_dGeoTransform);
314 // nn = 0;
315 // for (int nY = 0; nY < m_nYGridSize; nY++)
316 // {
317 // for (int nX = 0; nX < m_nXGridSize; nX++)
318 // {
319 // // Write this value to the array
320 // pdRaster[nn] = m_pRasterGrid->m_Cell[nX][nY].dGetWaveHeight();
321 // nn++;
322 // }
323 // }
324 //
325 // pBand = pDataSet->GetRasterBand(1);
326 // pBand->SetNoDataValue(m_nMissingValue);
327 // nRet = pBand->RasterIO(GF_Write, 0, 0, m_nXGridSize, m_nYGridSize, pdRaster, m_nXGridSize, m_nYGridSize, GDT_Float64, 0, 0, NULL);
328 //
329 // if (nRet == CE_Failure)
330 // return RTN_ERR_GRIDCREATE;
331 //
332 // GDALClose(pDataSet);
333 // delete[] pdRaster;
334 // // DEBUG CODE ===========================================================================================================
335
336 return RTN_OK;
337}
Contains CGeomCell definitions.
double m_dThisIterPotentialBeachErosion
Definition simulation.h:890
void CalcDepthOfClosure(void)
Calculate the depth of closure.
Definition utils.cpp:2612
int m_nLogFileDetail
Definition simulation.h:591
double m_dAllCellsDeepWaterWaveHeight
Deep water wave height (m) for all sea cells.
Definition simulation.h:792
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:462
ofstream LogStream
double m_dThisIterBeachErosionCoarse
Definition simulation.h:902
vector< CRWCoast > m_VFloodWaveSetupSurgeRunup
TODO 007 Info needed.
double m_dStartIterUnconsCoarseAllCells
vector< CRWCoast > m_VCoast
The coastline objects.
bool m_bSingleDeepWaterWaveValues
Definition simulation.h:388
double m_dThisIterActualPlatformErosionCoarseCons
Definition simulation.h:886
vector< CGeomCoastPolygon * > m_pVCoastPolygon
int m_nYGridSize
The size of the grid in the y direction.
Definition simulation.h:465
double m_dStartIterSuspFineInPolygons
unsigned long m_ulThisIterNumCoastCells
The number of grid cells which are marked as coast, for this iteration.
Definition simulation.h:633
double m_dThisIterCliffCollapseErosionCoarseUncons
vector< CRWCoast > m_VFloodWaveSetupSurge
TODO 007 Info needed.
unsigned long m_ulThisIterNumActualPlatformErosionCells
Definition simulation.h:641
int m_nXMaxBoundingBox
The maximum x value of the bounding box.
Definition simulation.h:555
double m_dThisIterCliffCollapseErosionFineUncons
Definition simulation.h:994
int m_nLayers
The number of sediment layers.
Definition simulation.h:468
double m_dStartIterConsCoarseAllCells
double m_dStartIterConsSandAllCells
unsigned long m_ulThisIterNumBeachDepositionCells
Definition simulation.h:653
double m_dThisiterUnconsCoarseInput
Depth (m) of coarse unconsolidated sediment added, at this iteration.
double m_dThisIterPotentialPlatformErosion
Definition simulation.h:874
double m_dThisiterUnconsFineInput
Depth (m) of fine unconsolidated sediment added, at this iteration.
bool m_bHaveWaveStationData
Do we have wave station data?
Definition simulation.h:391
double m_dThisIterActualPlatformErosionSandCons
Definition simulation.h:882
double m_dThisIterBeachDepositionCoarse
Definition simulation.h:910
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:70
double m_dAllCellsDeepWaterWaveAngle
Deep water wave angle for all sea cells.
Definition simulation.h:795
double m_dThisIterUnconsCoarseCliffDeposition
double m_dThisIterTotSeaDepth
Total sea depth (m) for this iteration.
Definition simulation.h:870
double m_dThisIterUnconsSandCliffDeposition
int nInterpolateAllDeepWaterWaveValues(void)
If the user supplies multiple deep water wave height and angle values, this routine interplates these...
double m_dThisIterPotentialSedLostBeachErosion
Definition simulation.h:918
double m_dStartIterConsFineAllCells
double m_dThisIterBeachErosionSand
Definition simulation.h:898
double m_dThisIterFineSedimentToSuspension
Definition simulation.h:914
double m_dThisIterBeachErosionFine
Definition simulation.h:894
unsigned long m_ulThisIterNumActualBeachErosionCells
Definition simulation.h:649
int m_nYMaxBoundingBox
The maximum y value of the bounding box.
Definition simulation.h:561
vector< bool > m_bUnconsChangedThisIter
unsigned long m_ulThisIterNumPotentialPlatformErosionCells
Definition simulation.h:637
double m_dStartIterUnconsSandAllCells
double m_dThisIterCliffCollapseErosionFineCons
unsigned long m_ulThisIterNumSeaCells
The number of grid cells which are marked as sea, for this iteration.
Definition simulation.h:630
double m_dStartIterUnconsFineAllCells
int m_nXMinBoundingBox
The minimum x value of the bounding box.
Definition simulation.h:552
unsigned long m_ulThisIterNumPotentialBeachErosionCells
Definition simulation.h:645
double m_dThisIterCliffCollapseErosionSandCons
double m_dThisIterBeachDepositionSand
Definition simulation.h:906
double m_dThisIterLeftGridUnconsCoarse
Definition simulation.h:930
double m_dThisIterCliffCollapseErosionSandUncons
Definition simulation.h:998
unsigned long m_ulIter
The number of the current iteration (time step)
Definition simulation.h:618
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:62
double m_dThisiterUnconsSandInput
Depth (m) of sand unconsolidated sediment added, at this iteration.
double m_dAllCellsDeepWaterWavePeriod
Deep water wave period for all sea cells.
Definition simulation.h:798
double m_dThisIterActualPlatformErosionFineCons
Definition simulation.h:878
double m_dThisIterCliffCollapseErosionCoarseCons
double m_dThisIterLeftGridUnconsSand
Definition simulation.h:926
int nInitGridAndCalcStillWaterLevel(void)
At the beginning of each timestep: clear vector coasts, profiles, and polygons, initialize the raster...
Definition init_grid.cpp:50
double m_dStartIterSuspFineAllCells
vector< bool > m_bConsChangedThisIter
double m_dThisIterLeftGridUnconsFine
Definition simulation.h:922
This file contains global definitions for CoastalME.
int const LOG_FILE_MIDDLE_DETAIL
Definition cme.h:489
string const WARN
Definition cme.h:891
int const RTN_OK
Definition cme.h:692
Contains CRWCoast definitions.
Contains CGeomLine definitions.
Contains CGeomRasterGrid definitions.
Contains CSimulation definitions.