-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLSDRasterModel.cpp
More file actions
8110 lines (7055 loc) · 260 KB
/
LSDRasterModel.cpp
File metadata and controls
8110 lines (7055 loc) · 260 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// LSDRasterModel.cpp
// cpp file for the LSDRasterModel object
// LSD stands for Land Surface Dynamics
// This object provides an environment for landscape evolution modelling, which can then
// be integrated with the topographic analysis tools to efficiently analyse model runs.
//
// The landscape evolution model uses implicit methods to provide stability with
// relatively long timesteps. Fluvial erosion is solved following Braun and Willet (2013)
// using the fastscape algorithm, whilst hillslope sediment transport is modelled as a
// non-linear diffusive sediment flux, following the implicit scheme developed for
// MuDDPile.
//
// The aim is to have two complimentary models:
// i) a simple coupled hillslope-channel model in which large scale landscape dynamics
// can be modelled
// ii) a more complex treatment of hillslopes explicitly incorporating the role of
// vegetation in driving sediment production and transport, and that copes with the
// with the transition from soil mantled-bedrock hillslopes at high erosion rates.
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// This object is written by
// Simon M. Mudd, University of Edinburgh
// David T. Milodowski, University of Edinburgh
// Martin D. Hurst, British Geological Survey
// Fiona Clubb, University of Edinburgh
// Stuart Grieve, University of Edinburgh
// James Jenkinson, University of Edinburgh
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Version 0.0.1 24/07/2013
//
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <string>
#include <math.h>
#include <string.h>
#include <sys/stat.h>
#include <ctime>
#include <cstdlib>
#include <complex>
#include "TNT/tnt.h"
#include "TNT/jama_lu.h"
#include "TNT/jama_eig.h"
#include <boost/numeric/mtl/mtl.hpp>
#include <boost/numeric/itl/itl.hpp>
#include "LSDRaster.hpp"
#include "LSDFlowInfo.hpp"
#include "LSDRasterSpectral.hpp"
#include "LSDStatsTools.hpp"
#include "LSDIndexRaster.hpp"
#include "LSDRasterModel.hpp"
#include "LSDSpatialCSVReader.hpp"
#include "LSDRasterInfo.hpp"
#include "LSDCRNParameters.hpp"
#include "LSDParticleColumn.hpp"
using namespace std;
using namespace TNT;
using namespace JAMA;
#define PI 3.14159265358
#ifndef LSDRasterModel_CPP
#define LSDRasterModel_CPP
// The assignment operator
LSDRasterModel& LSDRasterModel::operator=(const LSDRasterModel& rhs)
{
if (&rhs != this)
{
create(rhs.get_NRows(),rhs.get_NCols(),rhs.get_XMinimum(),rhs.get_YMinimum(),
rhs.get_DataResolution(),rhs.get_NoDataValue(),rhs.get_RasterData(),
rhs.get_GeoReferencingStrings());
}
return *this;
}
// The destructor method, used to clean up temporary files and dynamic memory
// Currently only used to delete K/D files
LSDRasterModel::~LSDRasterModel( void )
{
stringstream ss;
if (K_mode == 3)
{
ss << ".K_file_" << name << ".aux";
remove( ss.str().c_str() );
}
if (D_mode == 3)
{
ss << ".D_file_" << name << ".aux";
remove( ss.str().c_str() );
}
close_static_outfiles();
}
// the create function.
// This sets up a model domain with a default size and model parameters
// Imposes UTM zone 1
void LSDRasterModel::create()
{
NRows = 100;
NCols = 100;
DataResolution = 10;
NoDataValue = -9999;
XMinimum = 0;
YMinimum = 0;
RasterData = Array2D <float> (NRows, NCols, 0.0);
int zone = 1;
string NorS = "N";
impose_georeferencing_UTM(zone, NorS);
default_parameters();
}
// this creates a raster using an infile
void LSDRasterModel::create(string filename, string extension)
{
read_raster(filename,extension);
}
// this creates a raster filled with no data values
void LSDRasterModel::create(int nrows, int ncols, float xmin, float ymin,
float cellsize, float ndv, Array2D<float> data,
map<string,string> GRS)
{
NRows = nrows;
NCols = ncols;
XMinimum = xmin;
YMinimum = ymin;
DataResolution = cellsize;
NoDataValue = ndv;
GeoReferencingStrings = GRS;
RasterData = data.copy();
if (RasterData.dim1() != NRows)
{
cout << "dimension of data is not the same as stated in NRows!" << endl;
exit(EXIT_FAILURE);
}
if (RasterData.dim2() != NCols)
{
cout << "dimension of data is not the same as stated in NCols!" << endl;
exit(EXIT_FAILURE);
}
//int zone = 1;
//string NorS = "N";
//impose_georeferencing_UTM(zone, NorS);
}
// this creates a LSDRasterModel raster from another LSDRaster
void LSDRasterModel::create(LSDRaster& An_LSDRaster)
{
NRows = An_LSDRaster.get_NRows();
NCols = An_LSDRaster.get_NCols();
XMinimum = An_LSDRaster.get_XMinimum();
YMinimum = An_LSDRaster.get_YMinimum();
DataResolution = An_LSDRaster.get_DataResolution();
NoDataValue = An_LSDRaster.get_NoDataValue();
GeoReferencingStrings = An_LSDRaster.get_GeoReferencingStrings();
RasterData = An_LSDRaster.get_RasterData();
}
LSDRasterModel::LSDRasterModel(int NRows, int NCols)
{
this->NRows = NRows;
this->NCols = NCols;
this->DataResolution = 10;
this->NoDataValue = -9999;
XMinimum = 0;
YMinimum = 0;
RasterData = Array2D <float> (NRows, NCols, 0.0);
int zone = 1;
string NorS = "N";
impose_georeferencing_UTM(zone, NorS);
}
// this creates an LSDRasterModel using a master parameter file
void LSDRasterModel::create(string master_param)
{
NRows = 100;
NCols = 100;
DataResolution = 10;
NoDataValue = -9999;
XMinimum = 0;
YMinimum = 0;
int zone = 1;
string NorS = "N";
impose_georeferencing_UTM(zone, NorS);
default_parameters();
initialize_model(master_param);
}
// this sets default parameters for the model
void LSDRasterModel::default_parameters( void )
{
initialized = false;
name = "LSDRM";
report_name = "LSDRM";
reporting = true;
vector <string> bc(4, "n"); // Initialise boundaries to No flow
bc[0] = "b";
bc[1] = "p";
bc[2] = "b";
bc[3] = "p";
set_boundary_conditions( bc ); // Set these as default boundary conditions
set_uplift( 0, 0.0005 ); // Block uplift, 0.005m.yr^{-1}
set_baseline_uplift( 0.0005 );
set_timeStep( 100 ); // 100 years
set_maxtimeStep (1000); // this limits the adaptive timestep
set_endTime( 10000 );
endTime_mode = 0;
set_num_runs( 1 );
set_K( 0.0002 );
set_D( 0.02 );
set_rigidity( 1E7 );
set_m( 0.5 );
set_n( 1 );
set_threshold_drainage( -99 ); // Not used if negative
set_S_c( 1.0 ); // 45 degrees, slope of 1
set_print_interval( 10 ); // number of timesteps
set_float_print_interval (5000); // this is in years
set_next_printing_time (0);
set_steady_state_tolerance( 0.00001 );
current_time = 0;
noise = 0.1;
K_mode = 0;
D_mode = 0;
periodicity = 10000;
periodicity_2 = 20000;
period_mode = 1;
switch_time = endTime/2;
p_weight = 0.8;
K_amplitude = 0.001;
D_amplitude = 0.001;
report_delay = 0;
print_elevation = true;
print_hillshade = false;
print_erosion = false;
print_erosion_cycle = false;
print_slope_area = false;
quiet = false;
fluvial = true;
hillslope = true;
nonlinear = false;
isostasy = false;
flexure = false;
steady_state_tolerance = 0.0001;
steady_state_limit = -1;
initialized = false;
cycle_steady_check = false;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This returns the data in the raster model as a raster
LSDRaster LSDRasterModel::return_as_raster()
{
LSDRaster NewRaster(NRows, NCols, XMinimum, YMinimum,
DataResolution, NoDataValue, RasterData,
GeoReferencingStrings);
return NewRaster;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This adds a path to the run name and the report name
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDRasterModel::add_path_to_names( string pathname)
{
string lchar = pathname.substr(pathname.length()-2,1);
string slash = "/";
cout << "lchar is " << lchar << " and slash is " << slash << endl;
if (lchar != slash)
{
cout << "You forgot the frontslash at the end of the path. Appending." << endl;
pathname = pathname+slash;
}
name = pathname+name;
report_name = pathname+name;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// INITIALISATION MODULE
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This module initialises the model runs, calling the required function from
// the initial topography and loads the parameters from the parameter file.
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Initialise_model
//----------------------------------------------------------------------------
void LSDRasterModel::initialize_model( string& parameter_file, string& run_name,
float& dt, float& EndTime, float& PrintInterval,
float& k_w, float& b, float& m, float& n, float& K, float& ErosionThreshold,
float& K_nl, float& S_c, float& UpliftRate, float& PrecipitationRate,
float& NorthBoundaryElevation, float& SouthBoundaryElevation,
Array2D<float>& PrecipitationFlux, Array2D<float>& SlopesBetweenRows,
Array2D<float>& SlopesBetweenColumns, Array2D<float>& ErosionRate)
{
// load the parameters
// each parameter in the param file is preceded by its name
// these MUST be in the correct order
// the names MUST NOT have spaces
string temp;
ifstream param_in;
param_in.open(parameter_file.c_str());
param_in >> temp >> run_name;
cout << "run name is: " << run_name;
param_in >> temp >> dt;
param_in >> temp >> EndTime;
param_in >> temp >> PrintInterval;
cout << "dt: " << dt << " end_time: " << EndTime << " print_interval: " << PrintInterval << endl;
param_in >> temp >> k_w;
param_in >> temp >> b;
param_in >> temp >> m;
param_in >> temp >> n;
param_in >> temp >> K;
param_in >> temp >> ErosionThreshold;
cout << "k_w: " << k_w << " b: " << b << " m: " << m << " n: " << n << " K: " << K << " eros_thresh: " << ErosionThreshold << endl;
param_in >> temp >> K_nl;
param_in >> temp >> S_c;
cout << "D_nl: " << K_nl << " S_c: " << S_c << endl;
param_in >> temp >> UpliftRate;
param_in >> temp >> PrecipitationRate;
cout << "uplift_rate: " << UpliftRate << " precip_rate: " << PrecipitationRate << endl;
param_in >> temp >> NorthBoundaryElevation;
param_in >> temp >> SouthBoundaryElevation;
cout << "N bdry elev: " << NorthBoundaryElevation << " S bdry elev: " << SouthBoundaryElevation << endl;
// string surface_file;
// param_in >> surface_file;
//
// string file_extension;
// param_in >> file_extension;
//
// cout << "Surface_file is: " << surface_file << endl;
param_in.close();
float dx = get_DataResolution();
float dy = get_DataResolution();
cout << " NRows: " << NRows << " NCols: " << NCols << " dx: " << dx << " dy: " << dy
<< " xllcorn: " << XMinimum << " yllcorn: " << YMinimum << endl;
// now set up some arrays
// first the precipitation array
PrecipitationFlux = precip_array_from_precip_rate(PrecipitationRate);
// set up slope arrays of the correct size
Array2D<float> slopes_between_rows_temp(NRows+1,NCols,0.0);
Array2D<float> slopes_between_columns_temp(NRows,NCols+1,0.0);
SlopesBetweenRows = slopes_between_rows_temp.copy();
SlopesBetweenColumns = slopes_between_columns_temp.copy();
// set up erosion rate array of the correct size
Array2D<float> temp_erate(NRows,NCols,0.0);
ErosionRate = temp_erate.copy();
}
// -----------------------------------------------------------------------
// Alternative mode of initialising from parameter file
//
// Loads parameters using the void parse_line method found in LSDStatsTools
// Parameters are loaded into intrinsic class attributes
// Part of a move to using attributes vs passing through functions
// ----------------------------------------------------------------------
void LSDRasterModel::initialize_model(string param_file)
{
bool loaded_from_file = false;
initialized = true;
ifstream infile;
infile.open(param_file.c_str());
string parameter, value, lower;
while (infile.good())
{
parse_line(infile, parameter, value);
lower = parameter;
if (parameter == "NULL")
continue;
for (unsigned int i=0; i<parameter.length(); ++i)
lower[i] = tolower(parameter[i]);
if (lower == "run name") name = value;
else if (lower == "time step") timeStep = atof(value.c_str());
else if (lower == "end time") endTime = atof(value.c_str());
else if (lower == "num runs") num_runs = atoi(value.c_str());
else if (lower == "end time mode") endTime_mode = atoi(value.c_str());
else if (lower == "max uplift") max_uplift = atof(value.c_str());
else if (lower == "baseline uplift") baseline_uplift = atof(value.c_str());
else if (lower == "uplift mode") uplift_mode = atoi(value.c_str());
else if (lower == "tolerance") steady_state_tolerance = atof(value.c_str());
else if (lower == "steady limit") steady_state_limit = atof(value.c_str());
else if (lower == "boundary code") for (int i=0; i<4; ++i) boundary_conditions[i] = value[i];
else if (lower == "m") m = atof(value.c_str());
else if (lower == "n") n = atof(value.c_str());
else if (lower == "k") K_fluv = atof(value.c_str());
else if (lower == "threshold drainage") threshold_drainage = atof(value.c_str());
else if (lower == "d") K_soil = atof(value.c_str());
else if (lower == "s_c") S_c = atof(value.c_str());
else if (lower == "rigidity") rigidity = atof(value.c_str());
else if (lower == "nrows"){ if (loaded_from_file == false) NRows = atoi(value.c_str());}
else if (lower == "ncols"){ if (loaded_from_file == false) NCols = atoi(value.c_str());}
else if (lower == "resolution"){ if (loaded_from_file == false) DataResolution = atof(value.c_str()); }
else if (lower == "print interval") print_interval = atoi(value.c_str());
else if (lower == "k mode") K_mode = atoi(value.c_str());
else if (lower == "d mode") D_mode = atoi(value.c_str());
else if (lower == "periodicity") periodicity = atof(value.c_str());
else if (lower == "periodicity 2") periodicity_2 = atof(value.c_str());
else if (lower == "P ratio") {p_weight = atof(value.c_str()); if (p_weight > 1) p_weight = 1;}
else if (lower == "period mode") period_mode = atoi(value.c_str());
else if (lower == "switch time") switch_time = atof(value.c_str());
else if (lower == "k amplitude") K_amplitude = atof(value.c_str()) * K_fluv;
else if (lower == "d amplitude") D_amplitude = atof(value.c_str()) * K_soil;
else if (lower == "noise") noise = atof(value.c_str());
else if (lower == "report delay") report_delay = atof(value.c_str());
else if (lower == "fluvial") fluvial = (value == "on") ? true : false;
else if (lower == "hillslope") hillslope = (value == "on") ? true : false;
else if (lower == "non-linear") nonlinear = (value == "on") ? true : false;
else if (lower == "isostasy") isostasy = (value == "on") ? true : false;
else if (lower == "flexure") flexure = (value == "on") ? true : false;
else if (lower == "quiet") quiet = (value == "on") ? true : false;
else if (lower == "reporting") reporting = (value == "on") ? true : false;
else if (lower == "print elevation") print_elevation = (value == "on") ? true : false;
else if (lower == "print hillshade") print_hillshade = (value == "on") ? true : false;
else if (lower == "print erosion")
{
print_erosion = (value == "on") ? true : false;
cout << "I will print the erosion!" << endl;
}
else if (lower == "print erosion cycle") print_erosion_cycle = (value == "on") ? true : false;
else if (lower == "print slope-area") print_slope_area= (value == "on") ? true : false;
else if (lower == "load file")
{
ifstream file(value.c_str());
if (file)
{
file.close();
read_raster(value.substr(0, value.find(".")), value.substr(value.find(".")+1));
loaded_from_file = true;
}
else
cerr << "Warning, file '" << value << "' not found" << endl;
}
else cout << "Line " << __LINE__ << ": No parameter '" << parameter << "' expected.\n\t> Check spelling." << endl;
}
//if (hillslope)
// steady_state_tolerance *= pow(10, 2.8);
if (name != "")
report_name = name;
else
report_name = param_file;
if (loaded_from_file == false)
{
RasterData = Array2D<float>(NRows, NCols, 0.0);
// Generate random noise
random_surface_noise(0, noise);
// Fill the topography
LSDRaster *temp;
temp = new LSDRaster(*this);
float thresh_slope = 0.00001;
*temp = fill(thresh_slope);
RasterData = temp->get_RasterData();
delete temp;
}
root_depth = Array2D<float>(NRows, NCols, 0.0);
current_time = 0;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This function appends the run name, so that if you want you can add some
// details to the filename
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDRasterModel::append_run_name(string append_name)
{
name = name+append_name;
cout << "The model run name is: " << name << endl;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// this function adds a random float to each pixel in the raster
// Written sometime 2014 JAJ
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDRasterModel::random_surface_noise( float min, float max )
{
cout << "Seeding the surface with random asperities of the amplitude " << max-min << endl;
// Seed random numbers
short dimension;
int size;
bool periodic;
interpret_boundary(dimension, periodic, size);
int start_i, end_i;
int start_j, end_j;
if (dimension == 0)
{
start_i = 1; end_i = NRows-2;
start_j = 0; end_j = NCols-1;
}
else
{
start_i = 0; end_i = NRows-1;
start_j = 1; end_j = NCols-2;
}
srand( static_cast <unsigned> (time(0)) );
// Add random float to each pixel
for (int i=start_i; i<=end_i; ++i)
{
for (int j=start_j; j<=end_j; ++j)
{
if (is_base_level(i, j))
continue;
RasterData[i][j] += static_cast <float> ( rand()) / static_cast <float> (RAND_MAX/(max-min)) + min;
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Similar to above, but uses the noise parameter stored in the object
// SMM 17/6/2014
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LSDRasterModel::random_surface_noise()
{
// check on the noise data member. It needs to be bigger than 10-6)
// if not set 1 mm as default
if (noise < 0.000001)
{
noise = 0.001;
}
// we set the min and max between zero and noise. We don't go between
// -noise/2 and noise/2 just because we don't want negative elevations near
// a base level node.
float min = 0;
float max = noise;
cout << "Seeding the surface with random asperities of the amplitude " << noise << endl;
// Seed random numbers
short dimension;
int size;
bool periodic;
interpret_boundary(dimension, periodic, size);
int start_i, end_i;
int start_j, end_j;
if (dimension == 0)
{
start_i = 1; end_i = NRows-2;
start_j = 0; end_j = NCols-1;
}
else
{
start_i = 0; end_i = NRows-1;
start_j = 1; end_j = NCols-2;
}
srand( static_cast <unsigned> (time(0)) );
// Add random float to each pixel
for (int i=start_i; i<=end_i; ++i)
{
for (int j=start_j; j<=end_j; ++j)
{
if (is_base_level(i, j))
continue;
RasterData[i][j] += static_cast <float> ( rand()) / static_cast <float> (RAND_MAX/(max-min)) + min;
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// initializes a parabolic surface with elevations on the north and south edges at zero and
// elevation in the middle of 'peak elevation'
// the parabola also has random noise on it, with amplitude stored in
// the data member 'noise'
// The noise only adds to the elevations since we don't want elevations less
// than zero.
// Default noise is 1mm
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDRasterModel::initialise_parabolic_surface(float peak_elev, float edge_offset)
{
// check on the noise data member. It needs to be bigger than 10-6)
// if not set 1 mm as default
if (noise < 0.000001)
{
noise = 0.001;
}
// set up the length coordinate
float local_x;
float L = DataResolution*(NRows-1);
float row_elev;
float perturb;
// the seed for the random perturbation
long seed = time(NULL);
// loop through getting the parabolic elevation at each row, and then
// writing across the entire domain
for(int row = 0; row < NRows; row++)
{
local_x = row*DataResolution;
row_elev = - 4.0*(local_x*local_x-local_x*L)*peak_elev / (L*L);
for (int col = 0; col < NCols; col++)
{
// at N and S boundaries, the elevation is set to 0
if( row == 0 || row == NRows-1)
{
RasterData[row][col] = 0;
}
else // elsewhere initiate with a parabola
{
perturb = (ran3(&seed))*noise;
RasterData[row][col] = row_elev + perturb + edge_offset;
}
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// superimposes a parabolic surface with elevations on the north and south edges at zero and
// elevation in the middle of 'peak elevation'
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDRasterModel::superimpose_parabolic_surface(float peak_elev)
{
// set up the length coordinate
float local_x;
float L = DataResolution*(NRows-1);
float row_elev;
// loop through getting the parabolic elevation at each row, and then
// writing across the entire domain
for(int row = 0; row < NRows; row++)
{
local_x = row*DataResolution;
row_elev = - 4.0*(local_x*local_x-local_x*L)*peak_elev / (L*L);
for (int col = 0; col < NCols; col++)
{
// at N and S boundaries, tthere is no perturbation
if( row != 0 && row != NRows-1)
{
RasterData[row][col] = RasterData[row][col]+row_elev;
}
}
}
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Initialies a fractal-based terrain surface with given fractal dimension, D
// D should be between 2 and 3 for 'reasonable' landscapes
// This appraoch is known as the Midpoint method
// More algorithms can be found in Saupe (1987): Algorithms for random fractals
// (an interesting bedtime read!)
//
// DAV 15/10/14
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//void LSDRasterModel::intialise_MFD_fractal_surface(float fractal_D)
//{
//// set up the length coordinate
//float local_x;
//float L = DataResolution*(NRows-1);
//float row_elev;
//float perturb;
//// Arguments for the Fractalombulator
//float maxlevel; // maximum number of recursions to use; N = 2^maxlevel
//float sigma; // intial standard deviation
//float H; // H is the parameter determining the fractal dimension; D = 3 - H
//bool addition; // boolean paramter that turns random additions on or off
//float seed; // seed for the random number gen
//// Variables
//int i, N, stage; // i is just a wee counter deely. N is number of recursions, stage is the
//float delta; // holds the standard deviation
//int x, y, y0, D, d; // integers for the indexing of arrays
////Gaussian functions defined in LSDStatsTools
//// Lambda function (new in C++11) to return the gauss random number
//// the bit inside the [] are variables we want to 'capture' from the outer function.
//// the bit inside the { } is the lambda function itself
//double f3 = [delta, x0, x1, x2] { return ((x0 + x1 + x2)/3 + delta * Gauss_rand(100, 0.0, 1.0) }
//double f4 = [delta, x0, x1, x2, x3] { return ((x0 + x1 + x2 + x3)/4 + delta * Gauss_rand(100, 0.0, 1.0) }
////Fractalombulator
//N = pow(2, maxlevel);
//delta = sigma;
//Array2D<double> X(N+1,N+1);
//// set the four corners to random numbers
//X[0][0] = delta * Gauss_rand(100, 0.0, 1.0); // this is a bad way to do it, set them as named global? variables
//X[0][N] = delta * Gauss_rand(100, 0.0, 1.0);
//X[N][0] = delta * Gauss_rand(100, 0.0, 1.0);
//X[N][N] = delta * Gauss_rand(100, 0.0, 1.0);
//D = N;
//d = N/2;
//for (stage = 1, stage <= maxlevel, stage++)
//{
//delta = delta * pow(0.5, 0.5*H) // switch to the 45 degree grid type (See Saupe '87)
//for (x=d; x <= (N-d); x++)
//{
//for (y=d; y <= (N-d); y++)
//{
//X[x][y] = f4(delta... // TO COMPLETE
//}
//}
//}
//}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This creates a fractal surface DEM using the Fourier filtering method
// (Saupe 1987)
//
// DAV 17/10/2014
//
// SMM 10/08/2017 NOT WORKING
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDRasterModel::intialise_fourier_fractal_surface(float fractal_D)
//
// N is size of the array along 1 dimension (NRows)
//
// A[][] is a 2D array of complex variables, size N^2
{
int N = NRows;
//int N = RasterData.get_NRows;
float H = fractal_D;
Array2D< std::complex<float> > A;
Array2D<float> X;
std::complex<float> this_complex;
//int i0, j0;
float rad, phase; //Polar coordinates of the Fourier coefficient
for (int i = 0; i<=(N/2); i++)
{
for (int j = 0; j<=(N/2); j++)
{
phase = 2 * PI * rand();
if (i != 0 || j != 0)
{
rad = pow(i*i + j*j, (-(H+1)/2)) * Gauss_rand(100, 0.0, 1.0);
}
else
{
rad = 0;
}
this_complex.real(rad * cos(phase));
this_complex.imag(rad * sin(phase));
A[i][j] = this_complex;
//A[i][j] = {rad * cos(phase), rad * sin(phase)}; // left of the comma is real part, right of the comma is imaginary part
// This { } notation may only work in C++11 (i.e. the most recent standard as of 2014)
/* This stuff seems to have no effect
if (i==0)
{
i0 = 0;
}
else
{
i0 = N-i;
}
if (j==0)
{
j0 = 0;
}
else
{
j0 = N-j;
}
*/
this_complex.real(rad * cos(phase));
this_complex.imag(-rad * sin(phase));
A[i][j] = this_complex;
// A[i0][j0] = {rad * cos(phase), -rad * sin(phase)};
}
}
// Now for the *imaginary* numbers part
// We are setting the imaginary parts of the midpoint-edges of the array to zero
A[N/2][0].imag(0.0);
A[0][N/2].imag(0.0);
A[N/2][N/2].imag(0.0);
// Now generate the fractal surface
for (int i=1; i<=(N/2)-1; i++)
{
for (int j=1; j<=(N/2)-1; j++)
{
phase = 2 * PI * rand();
rad = pow(i*i + j*j, -(H+1)/2) * Gauss_rand(100, 0.0, 1.0);
this_complex.real(rad * cos(phase));
this_complex.imag(rad * sin(phase));
A[i][N-j] = this_complex;
this_complex.real(rad * cos(phase));
this_complex.imag(-rad * sin(phase));
A[N-i][j] = this_complex;
//A[i][N-j] = {rad * cos(phase), rad * sin(phase)}; // left of the comma is real part, right of the comma is imaginary part
//A[N-i][j] = {rad * cos(phase), -rad * sin(phase)};
}
}
// Here is the place to do the fast fourier transform: DAV
// Note: There looks to be a very similar function in LSDRasterSpectral!
// InvFFT2D(A,X,N)
dfftw2D_inv_complex(A, X, 1);
RasterData = X.copy();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This creates a fractal surface DEM using the Fourier filtering method
// (Saupe 1987)
//
// Like above, but uses the version in LSDRasterSpectral
// ONLY WORKS ON SMALL DEMS WITH pow(2) dimensions
//
// SMM 10/08/2017
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDRasterModel::intialise_fourier_fractal_surface_v2(float beta, float desired_relief)
{
Array2D<float> zeta=RasterData.copy();
// Create a raster spectral
LSDRasterSpectral Fourier(NRows, NCols, XMinimum, YMinimum, DataResolution, NoDataValue, zeta);
Fourier.generate_fractal_surface_spectral_method(beta,desired_relief);
zeta = Fourier.get_RasterData();
RasterData = zeta.copy();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This creates a fractal surface DEM using the diamond square algorithm.
// Believe it or not I lifted this algorithm from Notch, the creator of Minecraft,
// who posted it online and then had it modified by Charles Randall
// https://www.bluh.org/code-the-diamond-square-algorithm/
//
// SMM 10/08/2017
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDRasterModel::intialise_diamond_square_fractal_surface(int feature_order, float desired_relief)
{
Array2D<float> zeta=RasterData.copy();
// temporary raster for performing diamond square
LSDRaster temp_raster(NRows, NCols, XMinimum, YMinimum, DataResolution, NoDataValue, zeta);
// get the dimaond square raster
// IMPORTANT: this will be bigger than the original raster
LSDRaster DSRaster = temp_raster.DiamondSquare(feature_order, desired_relief);
// resample the raster to get a surface the correct size
// it won't wrap but the running to steady will take care of that.
for(int row = 0; row<NRows; row++)
{
for(int col = 0; col<NCols; col++)
{
zeta[row][col] = DSRaster.get_data_element(row,col);
}
}
RasterData = zeta.copy();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// This tapers the north and south boundaries to 0 elevation and raises the
// entire DEM above sea level
//
// SMM 10/08/2017
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
void LSDRasterModel::initialise_taper_edges_and_raise_raster(int rows_to_taper)
{
Array2D<float> zeta=RasterData.copy();
// first we need to loop through all the data and raise above the sea level,
// or lower to sea level accordingly
float MinElev = 9999999;
for(int row = 0; row<NRows; row++)
{
for(int col = 0; col<NCols; col++)
{
if (zeta[row][col] < MinElev)
{
MinElev = zeta[row][col];
}
}
}
cout << "Tapering. Found the mininum elevation, it is: " << MinElev << endl;
if (MinElev == -9999)
{
cout << "Your min elev is no data value. Setting it to 0. This means I won't raise or lower your raster" << endl;
MinElev = 0;
}
// now adjust elevations so the lowst points are at zero elevation
for(int row = 0; row<NRows; row++)
{
for(int col = 0; col<NCols; col++)
{
zeta[row][col] = zeta[row][col]-MinElev;
}
}
// now we loop through the edge nodes, muliplying each by a fraction so they taper to zero elevation
float this_frac;
for (int taper_row = 0; taper_row < rows_to_taper; taper_row++)
{
this_frac = float(taper_row)/float(rows_to_taper);
for(int col = 0; col<NCols; col++)
{
zeta[taper_row][col] = this_frac*zeta[taper_row][col];
zeta[NRows-1-taper_row][col] = this_frac*zeta[NRows-1-taper_row][col];
}