-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
1257 lines (1085 loc) · 54.3 KB
/
main.cpp
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
////
//// Bayesian parameter inference in rainfall-runoff hydrological modelling
//// using a Hamiltonian Monte Carlo approach with a time-scale separation
/// and a stochastic rain model
////
//// Created by Ulzega Simone, October 2022
//// simone.ulzega@zhaw.ch
////
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <random>
#include <numeric>
#include <cmath>
#include <boost/math/special_functions/erf.hpp>
#include <boost/math/constants/constants.hpp>
#include "vector_operations.h"
#include "hmc_functions.h"
#include "xi_u_transformation.h"
#include "napa.h"
#include "adept_source.h"
#include <limits>
#include "adept_functions.h"
#define EXIT_FILE_ERROR (1)
using namespace std;
using adept::adouble;
//// ********************************************************************* ////
//// *************************** Boost Libraries ************************* ////
//// ********************************************************************* ////
//// https://www.boost.org/doc/libs/1_75_0/more/getting_started/unix-variants.html
////
//// The most reliable way (as of June 2021) to get a copy of Boost
//// is to download a distribution from SourceForge:
//// 1. Download boost_1_75_0.tar.bz2.
//// 2. In the directory where you want to put the Boost installation, execute:
//// tar --bzip2 -xf /path/to/boost_1_75_0.tar.bz2
////
//// > cd boost_1_75_0
//// > ./bootstrap.sh
//// > ./b2
//// > ./b2 install
////
//// ********************************************************************* ////
//// ********************************************************************* ////
//// ********************************************************************* ////
//// ********************* Data-less or no-rain tests ******************** ////
//// ********************************************************************* ////
//// Just one thing to do: modify "hmc_functions.cpp" and "adept_functions.cpp"
//// by simply commenting out the data-dependent components
//// (detailed instructions in the functions)
//// ********************************************************************* ////
//// ********************************************************************* ////
//// *************************** Global variables ************************* ////
//// ******************* (declared in global_variables.h) ***************** ////
double A, alpha, beta, tau, pi, inf;
//// A = catchment area
//// alpha, beta = coefficients of the heteroscedasticity transformation
//// tau = correlation time of the OU process (stochastic process describing the rain)
//// pi = 3.14....
//// inf = infinity
//// ********************************************************************** ////
//// ****************************** FUNCTIONS ***************************** ////
//// ********************************************************************** ////
//// To sample from truncated normal
////
//// THIS WAS USED IN AN OLD VERSION OF THE CODE (with Gibbs sampling)
//// I KEEP IT HERE ALTHOUGH WE DON'T NEED IT.
//// ONE NEVER KNOWS WHAT MIGHT BE USEFUL AGAIN IN THE FUTURE!
////
//double sample_from_truncated_normal(double b, double mu, double si){
// //// The distribution is defined over the support (-inf, b] with mean 'mu' and standard deviation 'si'
//
// static mt19937_64 engine1(0);
// static uniform_real_distribution<double> r(0,1);
// double r1 = r(engine1);
// double beta = (b-mu)/si;
// double arg = (1 + boost::math::erf(beta/sqrt(2.0))) * r1 - 1;
// double res = mu + si * sqrt(2.0) * boost::math::erf_inv(arg);
// return res;
//}
//// **********************************************************************
//// N.B.: functions are seeded here with constant values.
//// Seeds should be changed to generate different random chains.
int seed1 = 1, seed2 = 2, seed3 = 3;
//// **********************************************************************
//// To fill in a vector with normally distributed random numbers
//// (used to generate momenta in the HMC loop)
void norm_rand(vector<double> & vec, double mu = 0.0, double std = 1.0)
{
static mt19937_64 engine2(seed1);
// static mt19937_64 engine(time(NULL));
static normal_distribution<double> norm_dist_vec(mu, std);
for (size_t ix = 0; ix < vec.size(); ++ix)
vec[ix] = norm_dist_vec(engine2);
}
//// **********************************************************************
//// **********************************************************************
//// To generate random real numbers from normal distribution N[0,1]
//// (used to generate initial OU state)
double n_rand(double mu = 0.0, double std = 1.0)
{
static mt19937_64 engine3(seed2);
// static mt19937_64 engine(time(NULL));
static normal_distribution<double> norm_dist(mu, std);
double res = norm_dist(engine3);
return res;
}
//// **********************************************************************
//// **********************************************************************
//// To generate random real numbers from uniform distribution U[0,1]
//// (used in the Metropolis accept-reject step)
double u_rand(double a = 0.0, double b = 1.0)
{
static mt19937_64 engine4(seed3);
// static mt19937_64 engine(time(NULL));
static uniform_real_distribution<double> uniform_dist(a,b);
double res = uniform_dist(engine4);
return res;
}
//// **********************************************************************
//// *************************** END OF FUNCTIONS ************************* ////
//// ************************* Useful directories ************************* ////
// LOCAL:
const string datadir = "/Users/ulzg/CLionProjects/HMC_SIP/data/"; // Input data (observations)
const string outdir = "/Users/ulzg/CLionProjects/HMC_SIP/output/"; // HMC-generated outputs
// CLUSTER:
//const string datadir = "/cfs/earth/scratch/ulzg/hmc_sip_norain/data/";
//const string outdir = "/cfs/earth/scratch/ulzg/hmc_sip_norain/output/";
//// ********************************************************************** ////
//// ********************************************************************* ////
//// ******************************* Main ******************************** ////
//// ********************************************************************* ////
int main() {
cout << endl;
cout << "************************************" << endl;
cout << "************* Starting *************" << endl;
cout << "************************************" << endl;
cout << endl;
bool save_init_polymer = false; // Flag to 'save' or 'not save' initial configuration (in data directory)
bool continue_from_previous = false; // Flag to continue from previous file or start from scratch
const string previous_file_name = "type_here_file_name_to_continue_from"; // Used only if continue_from_previous = true
bool use_sc2 = true; // Flag to select scenario Sc2 (inaccurate) or Sc1 (accurate)
string scenario;
// Set the selected scenario
if (use_sc2){
scenario = "Sc2";
} else {
scenario = "Sc1";
}
//// ********************************************************************* ////
//// ***************************** LOAD DATA ***************************** ////
//// ********************************************************************* ////
//// N.B.: Original work by Del Giudice et al., Water Resources Research 52, 2016,
//// included 3 rain events, each including 1 discharge and 2 precipitation time-series (Sc1 and Sc2)
//// for a total of 9 data files.
//// HERE: INFERENCE IS BASED ONLY ON EVENT 1. The other observations (events 2 and 3)
//// are still in the data folder, but ARE NOT USED.
cout << "*** Loading data ***" << endl;
//// **************************** Rain **************************** ////
vector<vector<double>> x01;
// Open file streams //
ifstream ifs_rain1( datadir + "event1_rain_" + scenario + ".dat" );
if (!ifs_rain1.is_open())
{
cout << "Exiting: unable to open file for rain event" << endl;
exit(EXIT_FILE_ERROR);
}
string line;
while (getline(ifs_rain1, line)) // read one line from ifs
{
stringstream ss(line); // access line as a stream
std::vector<double> values;
double value;
while (ss >> value)
{
values.push_back(value);
}
x01.push_back(values);
}
x01.erase(x01.begin()); // to delete headers
// Close file streams
ifs_rain1.close();
//// ************************** Discharge ************************** ////
//// Discharge data is the same for both Sc1 and Sc2
vector<vector<double>> y1;
// Open file streams //
ifstream ifs_out1( datadir + "event1_discharge.dat" );
if (!ifs_out1.is_open())
{
cout << "Exiting: unable to open file for discharge event" << endl;
exit(EXIT_FILE_ERROR);
}
while (getline(ifs_out1, line)) // read one line from ifs
{
stringstream ss(line); // access line as a stream
std::vector<double> values;
double value;
while (ss >> value)
{
values.push_back(value);
// std::cout << value << std::endl;
}
y1.push_back(values);
}
y1.erase(y1.begin()); // to delete headers
// Close file streams
ifs_out1.close();
//// ******************************************************************************** ////
//// GETTING RID OF EXTRA POINTS
//// This is done to have rain and discharge observations start and end at the same time point ////
//// IT IS NOT IDEAL, ONLY A TEMPORARY SOLUTION,
//// THE CODE SHOULD BE ADAPTED TO ALLOW DIFFERENT INITIAL AND FINAL TIMES
//// MAYBE ONE DAY ...
// In Sc1:
// Rain observation times: {1053., 1054., 1055., 1056., 1057., 1058., 1059., 1060., 1061., ...
// ... 1292., 1293., 1294., 1295., 1296., 1297., 1298., 1299., 1300., 1301., 1302., 1303.}
// Discharge observation times: {1056., 1060., 1064., 1068., ... 1284., 1288., 1292., 1296., 1300.}
// -> we delete the first and the last 3 precipitation data points so that both
// precipitation and discharge begin at time 1056 and end at time 1300
if (!use_sc2){
x01.erase(x01.begin(),x01.begin()+3);
x01.erase(x01.end() - 3, x01.end());
}
// In Sc2:
// Rain observation times: {1060., 1070., 1080., 1090., ... 1250., 1260., 1270., 1280., 1290., 1300.}
// Discharge observation times (as in Sc1): {1056., 1060., 1064., 1068., ... 1284., 1288., 1292., 1296., 1300.}
// -> we delete the first data point of the discharge time-series, so that both
// precipitation and discharge begin at time 1060 and end at time 1300
if (use_sc2){
y1.erase(y1.begin());
};
//// A FEW MORE THINGS TO DO BEFORE WE ARE ALL SET...
//// To have rain inputs in mm/s instead of mm/min
for (size_t ix = 0; ix < x01.size(); ++ix)
x01[ix][1] = x01[ix][1]/60.;
//// Create a vector containing observed rain without times
vector<double> x0;
for (size_t ix = 0; ix < x01.size(); ++ix)
x0.push_back(x01[ix][1]);
//// ********************************************************************* ////
//// ************************ Get measurements times ************************ ////
//// This is actually not really necessary.
/// However, since it doesn't bother anyone, I'll keep it here.
vector<double> tx01;
vector<double> ty1;
for ( size_t ir = 0; ir < x01.size(); ir++ ) {
tx01.push_back(x01[ir][0]);
}
for ( size_t ir = 0; ir < y1.size(); ir++ ) {
ty1.push_back(y1[ir][0]);
}
/*for (auto vi: tx01)
cout << vi*60 << ' ';
cout << endl << endl;
for (auto vi: ty1)
cout << vi*60 << ' ';
return(0);*/
//// ********************************************************************* ////
// Print file dimensions
cout << "Dimensions of rain event datasets: (" << x01.size() << "," << x01[0].size() << ")" << endl;
cout << "Dimensions of discharge datasets: (" << y1.size() << "," << y1[0].size() << ")" << endl;
cout << endl;
//// ********************************************************************* ////
//// ************************* END of DATA LOADING *********************** ////
//// ********************************************************************* ////
//// ********************************************************************* ////
//// ***************************** PARAMETERS **************************** ////
//// ********************************************************************* ////
const int n_params = 8;
//// Parameters are (LN = log-normal prior, N = normal prior):
/// K (LN), xgw (LN), sigma_z (LN), sigma_xi (LN), lambda (LN), gamma (LN), xir (N), S0 (N)
//// ****************** Model parameters: fixed values ****************** ////
// Fixed parameters are global variables and are declared in global_variables.h
A = 11815.8; // Area [m^2]
alpha = 25.0; // Coefficients of the discharge heteroscedasticity transformation H [l/s]
beta = 50.0; // Coefficients of the discharge heteroscedasticity transformation H [l/s]
tau = 636.0; // Auto correlation coefficient of the OU process [s]
pi = boost::math::constants::pi<double>();
inf = std::numeric_limits<double>::infinity();
//// **************** Model parameters: mu and sigma for priors **************** ////
vector<double> mu_thetas_temp(n_params), mu_thetas(n_params);
vector<double> sigma_thetas_temp(n_params), sigma_thetas(n_params);
//// Parameters mean and sd (they need to be transformed for LN distributions)
mu_thetas_temp = {284.4, 6.0, 4.5, 0.65, 0.1/60.0, 0.5, 0.5, 0.0};
sigma_thetas_temp = {57.6, 1.0, 0.45, 0.3, 0.05/60.0, 0.25, 0.1, 5000.0};
mu_thetas = mu_thetas_temp;
sigma_thetas = sigma_thetas_temp;
for (size_t ix = 0; ix < 6; ++ix){
mu_thetas[ix] = log(pow(mu_thetas_temp[ix],2)/sqrt(pow(mu_thetas_temp[ix],2)+
pow(sigma_thetas_temp[ix],2)));
sigma_thetas[ix] = sqrt(log(1+(pow(sigma_thetas_temp[ix],2)/pow(mu_thetas_temp[ix],2))));
}
//// **************** Model parameters: init values for inferred parameters **************** ////
vector<double> theta(n_params);
double K;
double xgw;
double sigma_z;
double sigma_xi;
double lambda;
double gamma;
double xir;
double S0;
if (continue_from_previous == true){
cout << "*** Loading parameters from previous run ***" << endl;
vector<vector<double>> thetas_from_previous;
ifstream ifs_theta( outdir + "thetas_" + previous_file_name + ".dat" );
// Reads the whole thetas file, while we need only last line, obviously not efficient
// It's not a problem (we read it only once before beginning HMC), but should be changed
if (!ifs_theta.is_open())
{
cout << "Exiting: unable to open file for parameter values" << endl;
exit(EXIT_FILE_ERROR);
}
while (getline(ifs_theta , line)) // read one line from ifs
{
stringstream ss(line); // access line as a stream
std::vector<double> values;
double value;
while (ss >> value)
{
values.push_back(value);
}
thetas_from_previous.push_back(values);
}
K = thetas_from_previous[thetas_from_previous.size()-1][0]; // Reservoir retention rate [s]
xgw =thetas_from_previous[thetas_from_previous.size()-1][1]; // Groundwater base flow [l/s]
sigma_z = thetas_from_previous[thetas_from_previous.size()-1][2]; // Standard deviation of the observational error model for H(y) [l/s]
sigma_xi = thetas_from_previous[thetas_from_previous.size()-1][3]; //// Does starting with a larger sigma_xi change the posterior?
lambda = thetas_from_previous[thetas_from_previous.size()-1][4]; // Scaling factor within rainfall transformation r [l/(s*m^2)]
gamma = thetas_from_previous[thetas_from_previous.size()-1][5]; // Exponential factor within rainfall transformation r
xir = thetas_from_previous[thetas_from_previous.size()-1][6]; // Domain split location within rainfall transformation r
S0 = thetas_from_previous[thetas_from_previous.size()-1][7]; // Reservoir level at t=0 [l] -> mean of N(0, 5000) truncated to [0, inf)
theta[0] = K;
theta[1] = xgw;
theta[2] = sigma_z;
theta[3] = sigma_xi;
theta[4] = lambda;
theta[5] = gamma;
theta[6] = xir;
theta[7] = S0;
} else {
K = 284.4; // Reservoir retention rate [s]
xgw = 6.0; // Groundwater base flow [l/s]
sigma_z = 4.5; // Standard deviation of the observational error model for H(y) [l/s]
sigma_xi = 0.65;
lambda = 0.1/60.0; // Scaling factor within rainfall transformation r [l/(s*m^2)]
gamma = 0.5; // Exponential factor within rainfall transformation r
xir = 0.5; // Domain split location within rainfall transformation r
S0 = 5000.0 * sqrt(2.0/pi); // Reservoir level at t=0 [l] -> mean of N(0, 5000) truncated to [0, inf)
theta[0] = K;
theta[1] = xgw;
theta[2] = sigma_z;
theta[3] = sigma_xi;
theta[4] = lambda;
theta[5] = gamma;
theta[6] = xir;
theta[7] = S0;
}
if (continue_from_previous == true){
cout << "*** Starting from file *" << previous_file_name << "* with the following parameter values: ***" << endl;
} else {
cout << "*** Starting from scratch with the following parameter values: ***" << endl;
}
cout << "K = " << theta[0] << endl;
cout << "Xgw = " << theta[1] << endl;
cout << "sigma_z = " << theta[2] << endl;
cout << "sigma_xi = " << theta[3] << endl;
cout << "lambda = " << theta[4] << endl;
cout << "gamma = " << theta[5] << endl;
cout << "xi_r = " << theta[6] << endl;
cout << "S0 = " << theta[7] << endl;
cout << endl;
//// Set parameter limits (used a bit artificially in HMC to prevent divergence issues)
double K_min = 0.0, K_max = 5e3;
double xgw_min = 0.0, xgw_max = 200;
double sigma_z_min = 0.0, sigma_z_max = 100;
double sigma_xi_min = 0.0, sigma_xi_max = 50;
double lambda_min = 0.0, lambda_max = 100;
double gamma_min = 0.0, gamma_max = 50;
double xir_min = -50.0, xir_max = 50.5;
double S0_min = -1e4, S0_max = 5e4;
//// ********************************************************************* ////
//// Priors, mean and sd for the parameters
//// (numbers in parenthesis are mean and sd of the random variable)
//// ********************************************************************* ////
//// K -> LN(284.4, 57.6)
//// xgw -> LN(6, 1)
//// sigma_z -> LN(4.5, 0.45)
//// sigma_xi -> LN(0.65, 0.3)
//// lambda -> LN(0.1, 0.05)
//// gamma -> LN(0.5, 0.25)
//// xir -> N(0.5, 0.1)
//// S0 -> N(0, 5000) truncated to [0,inf)
//// ********************************************************************* ////
//// *********************** Discretization parameters ********************** ////
const int nx1= x01.size() - 1; // Number of data points is nx1 + 1
const int ny1= y1.size() - 1; // Number of data points is ny1 + 1
int jx; // Number of sub-intervals between consecutive data points
if (use_sc2){
jx = 60; // Delta_t between data points = 10 minutes = 600 seconds -> fine grid step dt = 10 seconds
} else {
jx = 6; // Delta_t between data points = 1 minute = 60 seconds -> fine grid step dt = 10 seconds
}
const int jy = 24; // Delta_t between data points = 4 minutes = 240 seconds -> fine grid step dt = 10 seconds
const int N1 = nx1 * jx + 1;
cout << "*** Total number of discretization points (Nx): " << N1 << " ***" << endl;
//// *********************** Time step ********************** ////
double dt = round((tx01[1]-tx01[0])/jx *3600);
cout << "*** Time unit dt: " << dt << " seconds ***" << endl;
cout << endl;
//// ********************************************************************* ////
//// ********************** END of PARAMETERS SECTION ******************** ////
//// ********************************************************************* ////
//// ********************************************************************* ////
//// ******************* INDEXES OF MEASUREMENT POINTS ****************** ////
//// ************************ AND ZERO-RAIN POINTS *********************** ////
//// ********************************************************************* ////
//// Indexes of data points in the discretized framework (size N)
//// Indexes start at 1
//// (P.S.: this might not be used anywhere, but I would keep it.
//// Might be useful for debugging or just curiosity.)
vector<size_t> obs_ind_1(nx1+1);
for (size_t ix = 0; ix < nx1+1; ++ix)
obs_ind_1[ix] = (int)ix * jx + 1;
/*for (auto i: obs_ind_1)
cout << i << ' ';
return 0;*/
//// ********************************************************************* ////
//// Indices of zero- and non-zero-rain points (indices in the data files of size nx + 1)
//// Indices start at 1
vector<size_t> zeros;
auto it1 = find_if(x01.begin(), x01.end(), [](vector<double> v){return v[1] == 0;});
while (it1 != x01.end()) {
zeros.push_back(distance(x01.begin(), it1) + 1);
it1 = find_if(next(it1), x01.end(), [](vector<double> v){return v[1] == 0;});
}
vector<size_t> non_zeros;
it1 = find_if(x01.begin(), x01.end(), [](vector<double> v){return v[1] != 0;});
while (it1 != x01.end()) {
non_zeros.push_back(distance(x01.begin(), it1) + 1);
it1 = find_if(next(it1), x01.end(), [](vector<double> v){return v[1] != 0;});
}
/*for (auto i: zeros)
cout << i << ' ';
cout << endl;
for (auto i: non_zeros)
cout << i << ' ';
cout << endl;
return(0);*/
//// ********************************************************************* ////
//// Now zero-rain indices in the discretized framework (size N)
//// As usual, indices start at 1
vector<size_t> zeros_N(zeros.size());
for (time_t ix = 0; ix < zeros.size(); ++ix)
zeros_N[ix] = ((int)zeros[ix] - 1) * jx + 1;
cout << setprecision(3) << "*** Number of zero-rain points: "
<< zeros.size() << "(" << (double)zeros.size()/(nx1+1)*100 << "%) " << "***" << endl;
cout << endl;
//// ********************************************************************* ////
//// ************************************************************************* ////
//// ***************************** INITIAL STATE ***************************** ////
//// ************************ (linear interpolation) ************************* ////
//// ************************************************************************* ////
cout << "*** Generating initial configuration ***" << endl;
//// Container for rainfall potential in the discretized framework (size N)
vector<double> xi1(N1);
if (continue_from_previous == true) {
cout << "*** Loading xi configuration from previous run ***" << endl;
vector<double> xi_from_previous;
ifstream ifs_xi_last( outdir + "pred_xi_last_" + previous_file_name + ".dat" );
if (!ifs_xi_last.is_open())
{
cout << "Exiting: unable to open file for xi values" << endl;
exit(EXIT_FILE_ERROR);
}
while (getline(ifs_xi_last , line)) // read one line from ifs
{
stringstream ss(line); // access line as a stream
// std::vector<double> values;
double value;
while (ss >> value)
xi_from_previous.push_back(value);
}
for (size_t ix = 0; ix < N1; ++ix)
xi1[ix] = xi_from_previous[ix];
} else {
//// Run OU process - Initial state is just a random realization of an OU process
xi1[0] = n_rand();
for (size_t ix = 0; ix < N1-1; ++ix)
xi1[ix+1] = xi1[ix] - dt * (xi1[ix]/tau) + sqrt(dt) * sqrt(2/tau) * n_rand();
}
if (save_init_polymer == true)
{
ofstream ofs_init_xi1(datadir + "init_polymer_xi1.dat");
if (!ofs_init_xi1.is_open())
{
cout << "Exiting: unable to open file to write initial configuration" << endl;
exit(EXIT_FILE_ERROR);
}
for (size_t ix = 0; ix < N1; ++ix)
ofs_init_xi1 << xi1[ix] << endl;
ofs_init_xi1.close();
}
//// ************************************************************************* ////
//// ****************** COORDINATE TRANSFORMATION: xi -> u ******************* ////
//// ************ (eqs 2.16-17 in Tuckerman et al., JCP 99, 1993) ************ ////
//// ************************************************************************* ////
cout << "*** Coordinates transformation xi -> u ***" << endl;
//// Container for discretized rainfall potential in
//// the space of transformed coordinates u
vector<double> u1(N1);
/// IMPORTANT: the transformation that should be applied to the intermediate points is:
//// u[s*j+k-1] = xi[s*j+k-1] - ( (k-1)*xi[s*j+k] + xi[s*j] )/k;
//// BUT: it is always = 0 when the xi[sj+k-1] between data points are
//// a linear interpolation of the data points xi[sj]
//// When discretization points are a linear interpolation of data points, therefore one could use
//// u[s*jx + k - 1] = 0.0;
for (size_t s = 0; s < nx1; ++s)
{
u1[s*jx] = xi1[s*jx];
for (int k = 2; k <= jx; ++k)
u1[s*jx+k-1] = xi1[s*jx+k-1] - ( ( (k-1.0)*xi1[s*jx+k] + xi1[s*jx] )/(double)k );
}
u1[N1-1] = xi1[N1-1];
if (save_init_polymer == true)
{
ofstream ofs_init_u1(datadir + "init_polymer_u1.dat");
if (!ofs_init_u1.is_open())
{
cout << "Exiting: unable to open file to write initial configuration" << endl;
exit(EXIT_FILE_ERROR);
}
for (size_t ix = 0; ix < N1; ++ix)
ofs_init_u1 << u1[ix] << endl;
ofs_init_u1.close();
}
//// ************************************************************************* ////
//// ********************** PREPARE CONTAINERS FOR HMC *********************** ////
//// ************************************************************************* ////
cout << "*** Preparing HMC ***" << endl;
//// Containers for Markov chains
vector< vector<double> > theta_sample; // store parameter chains
vector< vector<double> > u1_sample; // store rain realizations (coordinates u)
vector<double> energies; // store energies
vector<double> u0_sample; // store first point of each rain realization (coordinates u)
//// +++++++ STORE INITIAL VALUES ++++++++ ////
theta_sample.push_back(theta);
u1_sample.push_back(u1);
//// +++++++++++++++++++++++++++++++++++++ ////
//// Containers for temporary values during HMC steps
vector<double> theta_save;
vector<double> u1_save;
//// Containers for masses and momenta
vector<double> mp(n_params), m1(N1);
vector<double> sqrt_mp(n_params), sqrt_m1(N1);
vector<double> pp, p1;
//// Containers for random numbers (to generate momenta p)
vector<double> rand_vec_p(n_params), rand_vec_1(N1);
//// Containers for energies and Metropolis accept/reject probability
double V_old, V_new, H_old, H_new;
double accept_prob;
//// Containers for execution times
vector<double> time_napa;
//// Containers for AD
adept::Stack stack;
vector<adouble> x(n_params+N1);
//// Containers for computing times
vector<double> t_energy_tot(2, 0.0);
vector<double> t_derivatives_tot(2, 0.0);
// These are used to estimate the contributions of energy calculations (-> function evaluations) compared to
// the derivative estimations (in the 'napa' integration loop). We store both number of evaluations and time.
//// ************************************************************************* ////
//// *********************** Get ready to save results *********************** ////
//// ************************************************************************* ////
//// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ////
const string output_file_name = "output_files_name"; // name of output files, choose it carefully!
//// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ////
ofstream ofs_thetas(outdir + "thetas_" + output_file_name + ".dat");
if (!ofs_thetas.is_open())
{
cout << "Exiting: unable to open file to write parameter samples" << endl;
exit(EXIT_FILE_ERROR);
}
ofs_thetas << "K" << "\t" << "xgw" << "\t" << "sigma_z" << "\t" << "sigma_xi" << "\t" << "lambda" << "\t" << "gamma" << "\t" <<
"xi_r" << "\t" << "S_0" << "\t" << endl;
//// **************************** ////
ofstream ofs_energies(outdir + "energies_" + output_file_name + ".dat");
if (!ofs_energies.is_open())
{
cout << "Exiting: unable to open file to write energy samples" << endl;
exit(EXIT_FILE_ERROR);
}
//// **************************** ////
ofstream ofs_pred_x(outdir + "pred_x_" + output_file_name + ".dat");
if (!ofs_pred_x.is_open())
{
cout << "Exiting: unable to open file to write input predictions" << endl;
exit(EXIT_FILE_ERROR);
}
//// **************************** ////
ofstream ofs_pred_xis(outdir + "pred_xis_" + output_file_name + ".dat");
if (!ofs_pred_xis.is_open())
{
cout << "Exiting: unable to open file to write xis" << endl;
exit(EXIT_FILE_ERROR);
}
//// **************************** ////
ofstream ofs_pred_xi_init(outdir + "pred_xi_init_" + output_file_name + ".dat");
if (!ofs_pred_xi_init.is_open())
{
cout << "Exiting: unable to open file to write xis (first point)" << endl;
exit(EXIT_FILE_ERROR);
}
//// **************************** ////
ofstream ofs_pred_xi_last(outdir + "pred_xi_last_" + output_file_name + ".dat");
if (!ofs_pred_xi_last.is_open())
{
cout << "Exiting: unable to open file to write xis (last realization)" << endl;
exit(EXIT_FILE_ERROR);
}
//// **************************** ////
ofstream ofs_pred_y(outdir + "pred_y_" + output_file_name + ".dat");
if (!ofs_pred_y.is_open())
{
cout << "Exiting: unable to open file to write output predictions" << endl;
exit(EXIT_FILE_ERROR);
}
//// _________________________________________________________________________ ////
//// _________________________________________________________________________ ////
//// ************************************************************************* ////
//// ************************************************************************* ////
//// ****************************** HMC BLOCK ******************************** ////
//// ************************************************************************* ////
//// ************************************************************************* ////
//// _________________________________________________________________________ ////
//// _________________________________________________________________________ ////
//// *********************************************************************** ////
//// ************************ DEFINITION OF MASSES ************************* ////
//// *********************************************************************** ////
//// For a data-less (or rain-less) run, recommended masses are:
double m_bdy = 1.0; // m = m_q / dt
double m_stg = 1.0; // we assume m_q proportional to dt ==> m = costant
//// For a normal run with data, recommended masses are:
//double m_bdy = 1.6; // m = m_q / dt
//double m_stg = 0.4; // we assume m_q proportional to dt ==> m = costant
//// Masses for parameters (used for data-less, rain-less and normal [with data] runs)
mp[0] = 1e-5; // refers to K
mp[1] = 1.0; // refers to Qgw
mp[2] = 1.0; // refers to sigma_z
mp[3] = 1.0; // refers to sigma_xi
mp[4] = 2e5; // refers to lambda
mp[5] = 0.5; // refers to gamma
mp[6] = 15.0; // refers to xir
mp[7] = 1e-7; // refers to S0
//// Assign masses to coordinates
for (int s = 0; s < nx1; ++s){
m1[s*jx] = m_bdy; // data points
for (int k = 2; k <= jx; ++k)
m1[s*jx + k - 1] = m_stg; // discretization points
}
m1[N1-1] = m_bdy;
//// Square roots (needed below in the HMC loop to generate momenta)
sqrt_mp = vsqrt(mp);
sqrt_m1 = vsqrt(m1);
//// *********************************************************************** ////
//// ************************ SIMULATION PARAMETERS ************************ ////
//// *********************************************************************** ////
const int n_samples = 75000; // Total number of points in the MCMC
const double dtau = 0.015; // Integration time step
const int n_napa = 3; //
//// Prepare indices of rain and discharge realizations to be saved
//// (we save only a selection due to memory limitations)
int red_size = 2e4; // number of realizations to be saved
red_size = min(red_size, n_samples);
double steppo = (double)(n_samples) / (double)(red_size);
vector<size_t> red_ind; // indices of realizations to be saved
for (size_t ix = 1; ix <= red_size; ++ix){
red_ind.push_back(round(ix*steppo));
}
vector<size_t> red_ind_with_0;
for (size_t ix = 0; ix <= red_size; ++ix){
red_ind_with_0.push_back(round(ix*steppo));
}
//// ************************************************************************* ////
//// ************************************************************************* ////
//// ****************************** HMC LOOP ********************************* ////
//// ************************************************************************* ////
//// ************************************************************************* ////
cout << endl << " ======================================================== " << endl;
cout << "*** Starting HMC loops ***" << endl;
cout << " ======================================================== " << endl << endl;
int reject_counter = 0;
int params_lim_counter = 0;
int u_lim_counter = 0;
int red_ind_ix = 0;
clock_t t_init = clock(); // To estimate total simulation time
clock_t t_energy_calc; // To estimate energy evaluations (or 'functions evaluations') time
for (int counter = 1; counter <= n_samples; ++counter){
//// ********************************************************************* ////
//// Sample momenta
//// ********************************************************************* ////
norm_rand(rand_vec_p); // for model parameters to be inferred
norm_rand(rand_vec_1); // for coordinates (= discretization and data points)
// From Albert et al., PRE 93, 2016:
// Momenta are drawn from normal distributions:
// p^2/(2m)
// So, sigma -> sqrt(m), and thus, p = sqrt_m * random_normal
//
pp = vtimes(sqrt_mp, rand_vec_p); // sqrt of mass * random vector
p1 = vtimes(sqrt_m1, rand_vec_1);
/*for (auto ix: theta)
cout << ix << " ";
cout << endl;*/
//// ********************************************************************* ////
//// Calculate energy
//// THE POTENTIAL ENERGY V IS CALCULATED ONLY ONCE, AT THE END OF THE LOOP
//// EXCEPT FOR THE FIRST STEP OF THE CHAIN
//// ********************************************************************* ////
if (counter==1){
//// This is done only once, at the first step of the chain.
/// Afterwards, V_old is stored at the end of this loop
V_old = V_N(nx1, jx, dt, u1) +
V_n(nx1, ny1, jx, jy, dt, x0, y1, theta, u1, non_zeros, zeros) +
V_1(N1, nx1, jx, dt, u1) + V_p(theta, mu_thetas, sigma_thetas);
}
H_old = vreduce(vdiv(vsquare(pp), ctimes(2.0, mp))) +
vreduce(vdiv(vsquare(p1), ctimes(2.0, m1))) + V_old;
//// ********************************************************************* ////
//// ********************************************************************* ////
//// Finite energy check and safe way out
//// ********************************************************************* ////
//// ********************************************************************* ////
if ( isnan(H_old) != 0) {
cout << "Exiting at sample " << counter << ": energy value -- " << H_old << " -- not acceptable" << endl;
cout << " *******************" << endl;
cout << "Current parameter values: " ;
for (auto ix: theta)
cout << ix << " ";
cout << endl;
cout << " *******************" << endl;
int red_size_911;
red_size_911 = min(red_size, counter + 1);
double steppo = (double)(counter) / (double)(red_size_911 - 1.0);
vector<size_t> red_ind_911; // indexes of realizations to be saved
for (size_t ix = 0; ix < red_size_911 - 1; ++ix)
// 'red_size_911 - 1' because the current iteration (step = counter) does not count
red_ind_911.push_back(round(ix*steppo));
cout << "Saving chains before exiting ... " ;
//// From u to xi
vector< vector<double> > xis_911;
for (size_t ix = 0; ix < u1_sample.size(); ++ix){
xis_911.push_back(u2xi(nx1, jx, u1_sample[ix]));
}
//// From xis to rain
vector< vector<double> > xs_911(xis_911.size(), vector<double> (xis_911[0].size()));
for (size_t ic = 0; ic < xis_911[0].size(); ++ic){
for (size_t ir = 0; ir < xis_911.size(); ++ir) {
xs_911[ir][ic] = r(xis_911[ir][ic], theta_sample[ir]);
}
}
//// From rain to discharges
vector< vector<double> > ys_911(xs_911.size(), vector<double> (xs_911[0].size()));
vector< vector<double> > Qs_911(xs_911.size(), vector<double> (xs_911[0].size()));
vector<double> current_theta_911(n_params);
for (size_t ir = 0; ir < xs_911.size(); ++ir)
Qs_911[ir][0] = 0.0;
for (size_t ic = 1; ic < xs_911[0].size(); ++ic) {
for (size_t ir = 0; ir < xs_911.size(); ++ir) {
Qs_911[ir][ic] = (1-(dt/theta_sample[ir][0])) * Qs_911[ir][ic-1] + xs_911[ir][ic-1];
}
}
for (size_t ir = 0; ir < xs_911.size(); ++ir)
ys_911[ir][0] = (theta_sample[ir][7]/theta_sample[ir][0]);
for (size_t ic = 1; ic < xs_911[0].size(); ++ic){
for (size_t ir = 0; ir < xs_911.size(); ++ir) {
current_theta_911 = theta_sample[ir];
ys_911[ir][ic] = (current_theta_911[7]/current_theta_911[0]) * pow(1.0-(dt/current_theta_911[0]),ic) +
(1-pow(1-(dt/current_theta_911[0]),ic)) * current_theta_911[1] +
A * (dt/current_theta_911[0]) * Qs_911[ir][ic];
}
}
//// ****** Parameters Markov chains ******
//// **************************************
for (size_t ir = 0; ir < theta_sample.size(); ++ir) {
ofs_thetas << theta_sample[ir][0] << "\t" << theta_sample[ir][1] << "\t" << theta_sample[ir][2] << "\t" <<
theta_sample[ir][3] << "\t" << theta_sample[ir][4] << "\t" << theta_sample[ir][5] << "\t" <<
theta_sample[ir][6] << "\t" << theta_sample[ir][7] << "\t" << endl;
}
ofs_thetas.close();
//// ****** Energy chains ******
//// ***************************
for (size_t ir = 0; ir < energies.size(); ++ir) {
ofs_energies << energies[ir] << endl;
}
ofs_energies.close();
//// ****** Rain (x) ******
//// ********************************
for (int row = 0; row < red_ind_911.size(); ++row)
{
for (int col = 0; col < N1; ++col)
{
ofs_pred_x << xs_911[red_ind_911[row]][col] << " ";
}
ofs_pred_x << endl;
}
ofs_pred_x.close();
//// ****** Rainfall potential (xi) ******
//// ********************************
for (int row = 0; row < red_ind_911.size(); ++row)
{
ofs_pred_xis << red_ind_911[row] << " ";
for (int col = 0; col < N1; ++col)
{
ofs_pred_xis << xis_911[red_ind_911[row]][col] << " ";
}
ofs_pred_xis << endl;
}
ofs_pred_xis.close();
//// ****** First point of the rainfall potential ******
//// ********************************
for (int row = 0; row < xis_911.size(); ++row)
{
ofs_pred_xi_init << xis_911[row][0] << endl;
}
ofs_pred_xi_init.close();
/// ****** Last realization of the rainfall potential ******
//// ********************************
for (int col = 0; col < N1; ++col)
{
ofs_pred_xi_last << xis_911[xis_911.size()-1][col] << endl;
}
ofs_pred_xi_last.close();
//// ****** Discharges (y) ******
//// ********************************
for (int row = 0; row < red_ind_911.size(); ++row)
{
for (int col = 0; col < N1; ++col)
{
ofs_pred_y << ys_911[red_ind_911[row]][col] << " ";
}
ofs_pred_y << endl;
}
ofs_pred_y.close();
cout << " DONE " << endl;
exit(EXIT_FILE_ERROR);}
//// ********************************************************************* ////
//// ********************************************************************* ////
//// End of safe way out
//// ********************************************************************* ////
//// ********************************************************************* ////
//// ********************************************************************* ////
//// Store current state for parameters and coordinates
//// ********************************************************************* ////