forked from amart/MAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInformation.hpp
7058 lines (5746 loc) · 368 KB
/
Information.hpp
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
/*
* File: Information.hpp
*
* Author: Matthew Supernaw
* National Oceanic and Atmospheric Administration
* National Marine Fisheries Service
* Sustainable Fisheries Division
* St. Petersburg, FL, 33701
*
* Created on September 16, 2016, 12:35 PM
*
* This File is part of the NOAA, National Marine Fisheries Service
* Metapopulation Assessment System project.
*
* This software is a "United States Government Work" under the terms of the
* United States Copyright Act. It was written as part of the author's official
* duties as a United States Government employee and thus cannot be copyrighted.
* This software is freely available to the public for use. The National Oceanic
* And Atmospheric Administration and the U.S. Government have not placed any
* restriction on its use or reproduction. Although all reasonable efforts have
* been taken to ensure the accuracy and reliability of the software and data,
* the National Oceanic And Atmospheric Administration and the U.S. Government
* do not and cannot warrant the performance or results that may be obtained by
* using this software or data. The National Oceanic And Atmospheric
* Administration and the U.S. Government disclaim all warranties, express or
* implied, including warranties of performance, merchantability or fitness
* for any particular purpose.
*
* Please cite the author(s) in any work or product based on this material.
*
*/
#ifndef MAS_INFORMATION_HPP
#define MAS_INFORMATION_HPP
#include "Common.hpp"
#include "Area.hpp"
#include "Population.hpp"
#include "Season.hpp"
#include "Movement.hpp"
#include "Selectivity.hpp"
#include "Survey.hpp"
#include "third_party/rapidjson/document.h"
#include "Fleet.hpp"
#include <ctime>
#ifdef MAS_USE_NETCDF
#include "NetCDF.hpp"
#endif
namespace mas {
template<typename REAL_T>
class Information : public mas::ModelObject<REAL_T> {
typedef typename VariableTrait<REAL_T>::variable variable;
std::map<int, std::vector<std::shared_ptr<DataObject<REAL_T> > > > data_dictionary;
std::vector<std::shared_ptr<DataObject<REAL_T> > > data;
typedef typename std::map<int, std::vector<std::shared_ptr<DataObject<REAL_T> > > >::iterator data_iterator;
std::string analyst = "NA";
std::string study_name = "NA";
std::string data_path;
public:
int nyears;
int nseasons = 1;
int first_year;
int last_year;
std::vector<REAL_T> ages;
bool natal_movement = false;
bool valid_configuration = true;
std::unordered_map<int, std::shared_ptr<mas::Area<REAL_T> > > areas;
std::unordered_map<int, std::shared_ptr<mas::Season<REAL_T> > > seasons;
//System sub model objects
std::unordered_map<int, std::shared_ptr<mas::Population<REAL_T> > > populations;
std::unordered_map<int, std::shared_ptr<mas::GrowthBase<REAL_T> > > growth_models;
std::unordered_map<int, std::shared_ptr<mas::RecruitmentBase<REAL_T> > > recruitment_models;
std::unordered_map<int, std::shared_ptr<mas::NaturalMortality<REAL_T> > > natural_mortality_models;
std::unordered_map<int, std::shared_ptr<mas::FishingMortality<REAL_T> > > finshing_mortality_models;
std::unordered_map<int, std::shared_ptr<mas::FecundityBase<REAL_T> > > fecundity_models;
std::unordered_map<int, std::shared_ptr<mas::Movement<REAL_T> > > movement_models;
std::unordered_map<int, std::shared_ptr<mas::SelectivityBase<REAL_T> > > selectivity_models;
std::unordered_map<int, std::shared_ptr<mas::Fleet<REAL_T> > > fleets;
std::vector<std::shared_ptr<mas::Survey<REAL_T> > > survey_models;
//System sub model iterators
typedef typename std::unordered_map<int, std::shared_ptr<mas::Season<REAL_T> > >::iterator seasons_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::Area<REAL_T> > >::iterator area_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::Population<REAL_T> > >::iterator population_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::GrowthBase<REAL_T> > >::iterator growth_model_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::RecruitmentBase<REAL_T> > >::iterator recruitment_model_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::NaturalMortality<REAL_T> > >::iterator natural_mortality_model_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::FishingMortality<REAL_T> > >::iterator fishing_mortality_model_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::FecundityBase<REAL_T> > >::iterator fecundity_model_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::Movement<REAL_T> > >::iterator movement_model_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::SelectivityBase<REAL_T> > >::iterator selectivity_model_iterator;
typedef typename std::unordered_map<int, std::shared_ptr<mas::Fleet<REAL_T> > >::iterator fleet_iterator;
typedef typename std::vector<std::shared_ptr<mas::Survey<REAL_T> > >::iterator survey_model_iterator;
Information() {
}
Information(const Information<REAL_T>& orig) {
}
virtual ~Information() {
}
void ParseConfig(const std::string& path) {
std::time_t result = std::time(nullptr);
mas_log << "MAS Run: " << std::asctime(std::localtime(&result)) << "\n";
std::stringstream ss;
std::ifstream config;
config.open(path.c_str());
if (!config.good()) {
std::cerr << "MAS Configuration file \"" << path << "\" not found.\n";
mas::mas_log << "MAS Configuration file \"" << path << "\" not found.\n";
this->valid_configuration = false;
}
while (config.good()) {
std::string line;
std::getline(config, line);
ss << line << "\n";
}
rapidjson::Document document;
document.Parse(ss.str().c_str());
rapidjson::Document::MemberIterator mit;
for (mit = document.MemberBegin(); mit != document.MemberEnd(); ++mit) {
if (std::string((*mit).name.GetString()) == "analyst") {
this->analyst = std::string((*mit).value.GetString());
}
if (std::string((*mit).name.GetString()) == "study_name") {
this->study_name = std::string((*mit).value.GetString());
}
if (std::string((*mit).name.GetString()) == "movement_type") {
if (std::string((*mit).value.GetString()) == std::string("natal_homing")) {
this->natal_movement = true;
}
}
if (std::string((*mit).name.GetString()) == "data") {
this->data_path = std::string((*mit).value.GetString());
}
if (std::string((*mit).name.GetString()) == "years") {
this->nyears = (*mit).value.GetInt();
}
if (std::string((*mit).name.GetString()) == "first_year") {
this->first_year = (*mit).value.GetInt();
}
if (std::string((*mit).name.GetString()) == "last_year") {
this->last_year = (*mit).value.GetInt();
}
if (std::string((*mit).name.GetString()) == "ages") {
rapidjson::Value& ages = (*mit).value;
if (ages.IsArray()) {
for (int i = 0; i < ages.Size(); i++) {
this->ages.push_back(static_cast<REAL_T> (ages[i].GetDouble()));
}
} else {
std::cout << "Configuration Error: \"ages\" should be a vector.\n";
mas::mas_log << "Configuration Error: \"ages\" should be a vector.\n";
}
}
if (std::string((*mit).name.GetString()) == "seasons") {
// HandleSeason(mit);
this->nseasons = (*mit).value.GetInt();
}
if (std::string((*mit).name.GetString()) == "recruitment") {
HandleRecruitmentModel(mit);
}
if (std::string((*mit).name.GetString()) == "growth") {
HandleGrowthModel(mit);
}
if (std::string((*mit).name.GetString()) == "selectivity") {
HandleSelectivityModel(mit);
}
if (std::string((*mit).name.GetString()) == "area") {
HandleAreaModel(mit);
}
if (std::string((*mit).name.GetString()) == "population") {
HandlePopulationModel(mit);
}
if (std::string((*mit).name.GetString()) == "fleet") {
HandleFleetModel(mit);
}
if (std::string((*mit).name.GetString()) == "movement") {
HandleMovementModel(mit);
}
if (std::string((*mit).name.GetString()) == "natural_mortality") {
HandleNaturalMortalityModel(mit);
}
if (std::string((*mit).name.GetString()) == "fishing_mortality") {
HandleFishingMortalityModel(mit);
}
if (std::string((*mit).name.GetString()) == "survey") {
HandleSurveyModel(mit);
}
}
}
void HandleSurveyModel(rapidjson::Document::MemberIterator& survey_model) {
std::shared_ptr<mas::Survey<REAL_T> > model(new mas::Survey<REAL_T>());
rapidjson::Document::MemberIterator rit = (*survey_model).value.FindMember("id");
int model_id = 0;
if (rit == (*survey_model).value.MemberEnd()) {
std::cout << "Configuration Error: Survey is required to have a unique identifier\n";
mas::mas_log << "Configuration Error: Survey is required to have a unique identifier\n";
this->valid_configuration = false;
} else {
model_id = (*rit).value.GetInt();
}
model->id = model_id;
rit = (*survey_model).value.FindMember("name");
if (rit != (*survey_model).value.MemberEnd()) {
model->name = std::string((*rit).value.GetString());
}
rit = (*survey_model).value.FindMember("population");
if (rit == (*survey_model).value.MemberEnd()) {
std::cout << "Configuration Error: Survey is required to have a population identifier\n";
mas::mas_log << "Configuration Error: Survey is required to have a population identifier\n";
this->valid_configuration = false;
} else {
model->population = (*rit).value.GetInt();
}
rit = (*survey_model).value.FindMember("selectivity");
if (rit != (*survey_model).value.MemberEnd()) {
if (!(*rit).value.IsArray()) {
std::cout << "Configuration Error: Fleets are required to have selectivity definitions in a vector\n";
std::cout << "Configuration Error: Fleets are required to have selectivity definitions in a vector\n";
this->valid_configuration = false;
} else {
for (int i = 0; i < (*rit).value.Size(); i++) {
rapidjson::Value& m = (*rit).value[i];
int sid = 0;
int sarea = 0;
int sseason = 0;
rapidjson::Document::MemberIterator mit = m.FindMember("id");
if (mit == m.MemberEnd()) {
std::cout << "Configuration Error: Fleets selectivity has no model identifier\n";
std::cout << "Configuration Error: Fleets selectivity has no model identifier\n";
this->valid_configuration = false;
} else {
sid = (*mit).value.GetInt();
}
mit = m.FindMember("season");
if (mit == m.MemberEnd()) {
std::cout << "Configuration Error: Fleets selectivity season has no identifier\n";
std::cout << "Configuration Error: Fleets selectivity season has no identifier\n";
this->valid_configuration = false;
} else {
sseason = (*mit).value.GetInt();
}
mit = m.FindMember("area");
if (mit == m.MemberEnd()) {
std::cout << "Configuration Error: Fleets selectivity area has no identifier\n";
std::cout << "Configuration Error: Fleets selectivity area has no identifier\n";
this->valid_configuration = false;
} else {
sarea = (*mit).value.GetInt();
}
model->season_area_selectivity_ids[sseason][sarea] = sid;
model->area_season_selectivity_ids[sarea][sseason] = sid;
}
}
}
this->survey_models.push_back(model);
}
void HandleFishingMortalityModel(rapidjson::Document::MemberIterator& mortality_model) {
std::shared_ptr<mas::FishingMortality<REAL_T> > model(new mas::FishingMortality<REAL_T>());
bool estimated = false;
int phase = 1;
bool bounded = false;
REAL_T min = std::numeric_limits<REAL_T>::min();
REAL_T max = std::numeric_limits<REAL_T>::max();
rapidjson::Document::MemberIterator rit = (*mortality_model).value.FindMember("id");
int model_id = 0;
if (rit == (*mortality_model).value.MemberEnd()) {
std::cout << "Configuration Error: Mortality is required to have a unique identifier\n";
mas::mas_log << "Configuration Error: Mortality is required to have a unique identifier\n";
this->valid_configuration = false;
} else {
model_id = (*rit).value.GetInt();
}
model->id = model_id;
rapidjson::Document::MemberIterator pit = (*mortality_model).value.FindMember("parameters");
if (pit == (*mortality_model).value.MemberEnd()) {
std::cout << "Configuration Error: Fishing Mortality model has no parameter definitions.\n";
mas::mas_log << "Configuration Error: Fishing Mortality model has no parameter definitions.\n";
this->valid_configuration = false;
} else {
rapidjson::Document::MemberIterator vit = (*pit).value.FindMember("estimated");
if (vit != (*pit).value.MemberEnd()) {
std::string estring = std::string((*vit).value.GetString());
if (estring == "true") {
estimated = true;
}
}
vit = (*pit).value.FindMember("min");
if (vit != (*pit).value.MemberEnd()) {
min = static_cast<REAL_T> ((*vit).value.GetDouble());
bounded = true;
}
vit = (*pit).value.FindMember("max");
if (vit != (*pit).value.MemberEnd()) {
max = static_cast<REAL_T> ((*vit).value.GetDouble());
bounded = true;
}
vit = (*pit).value.FindMember("phase");
if (vit != (*pit).value.MemberEnd()) {
std::string pstring = std::string((*vit).value.GetString());
phase = StringToNumber<uint32_t>(pstring);
}
vit = (*pit).value.FindMember("values");
if (vit == (*pit).value.MemberEnd()) {
std::cout << "Configuration Error: Fishing Mortality model supplied no values\n";
mas::mas_log << "Configuration Error: Fishing Mortality model supplied no values\n";
this->valid_configuration = false;
} else {
if ((*vit).value.IsArray()) {
rapidjson::Value & v = (*vit).value;
if (v.IsArray()) {
model->fishing_mortality.resize(v.Size());
for (int i = 0; i < v.Size(); i++) {
rapidjson::Value & vv = v[i];
model->fishing_mortality[i].resize(vv.Size());
for (int j = 0; j < vv.Size(); j++) {
mas::VariableTrait<REAL_T>::SetValue(model->fishing_mortality[i][j], static_cast<REAL_T> (vv[j].GetDouble()));
if (estimated) {
if (bounded) {
mas::VariableTrait<REAL_T>::SetMinBoundary(model->fishing_mortality[i][j], min);
mas::VariableTrait<REAL_T>::SetMaxBoundary(model->fishing_mortality[i][j], max);
}
std::stringstream ss;
ss << "fishing_mortality[" << i << "][" << j << "]_" << model_id;
mas::VariableTrait<REAL_T>::SetName(model->fishing_mortality[i][j], ss.str());
model->Register(model->fishing_mortality[i][j], phase);
}
}
}
} else {
std::cout << "Configuration Error: Fishing Mortality is requires values in a matrix[year,season] not a vector\n";
std::cout << "Configuration Error: Fishing Mortality is requires values in a matrix[year,season] not a vector\n";
this->valid_configuration = false;
}
} else {
std::cout << "Configuration Error: Fishing Mortality is requires values in a matrix[year,season]\n";
std::cout << "Configuration Error: Fishing Mortality is requires values in a matrix[year,season]\n";
this->valid_configuration = false;
}
}
}
fishing_mortality_model_iterator fit = this->finshing_mortality_models.find(model->id);
if (fit == this->finshing_mortality_models.end()) {
this->finshing_mortality_models[model->id] = model;
} else {
std::cout << "Configuration Error: More than one fishing mortality model with the same identifier defined. Fishing mortality models require a unique id.\n";
mas_log << "Configuration Error: More than one fishing mortality model with the same identifier defined. Fishing mortality models require a unique id.\n";
this->valid_configuration = false;
}
}
void HandleNaturalMortalityModel(rapidjson::Document::MemberIterator& mortality_model) {
std::shared_ptr<mas::NaturalMortality<REAL_T> > model(new mas::NaturalMortality<REAL_T>());
bool estimated = false;
int phase = 1;
rapidjson::Document::MemberIterator rit = (*mortality_model).value.FindMember("id");
int model_id = 0;
if (rit == (*mortality_model).value.MemberEnd()) {
std::cout << "Configuration Error: Mortality is required to have a unique identifier\n";
mas::mas_log << "Configuration Error: Mortality is required to have a unique identifier\n";
this->valid_configuration = false;
} else {
model_id = (*rit).value.GetInt();
}
model->id = model_id;
rapidjson::Document::MemberIterator pit = (*mortality_model).value.FindMember("parameters");
rapidjson::Document::MemberIterator mvit = (*pit).value.FindMember("values");
if ((*mvit).value.IsArray()) {
// model->male_mortality.resize((*mvit).value.Size());
rapidjson::Value& v = (*mvit).value;
model->mortality_vector.resize((*mvit).value.Size());
for (int i = 0; i < (*mvit).value.Size(); i++) {
std::stringstream ss;
ss << "male_mortality[" << i << "]_" << model_id;
mas::VariableTrait<REAL_T>::SetName(model->mortality_vector[i], ss.str());
mas::VariableTrait<REAL_T>::SetValue(model->mortality_vector[i], static_cast<REAL_T> (v[i].GetDouble()));
}
} else {
std::cout << "Configuration Error: Mortality model values for males must be a vector.\n";
mas::mas_log << "Configuration Error: Mortality model values for males must be a vector.\n";
this->valid_configuration = false;
return;
}
mvit = (*pit).value.FindMember("min");
if (mvit != (*pit).value.MemberEnd()) {
if ((*mvit).value.IsArray()) {
rapidjson::Value& v = (*mvit).value;
for (int i = 0; i < (*mvit).value.Size(); i++) {
mas::VariableTrait<REAL_T>::SetMinBoundary(model->mortality_vector[i], static_cast<REAL_T> (v[i].GetDouble()));
}
} else {
std::cout << "Configuration Error: Mortality model min values for males must be a vector.\n";
mas::mas_log << "Configuration Error: Mortality model min values for males must be a vector.\n";
this->valid_configuration = false;
}
}
mvit = (*pit).value.FindMember("max");
if (mvit != (*pit).value.MemberEnd()) {
if ((*mvit).value.IsArray()) {
rapidjson::Value& v = (*mvit).value;
for (int i = 0; i < (*mvit).value.Size(); i++) {
mas::VariableTrait<REAL_T>::SetMaxBoundary(model->mortality_vector[i], static_cast<REAL_T> (v[i].GetDouble()));
}
} else {
std::cout << "Configuration Error: Mortality model min values for males must be a vector.\n";
mas::mas_log << "Configuration Error: Mortality model min values for males must be a vector.\n";
this->valid_configuration = false;
}
}
rit = (*pit).value.FindMember("estimated");
if (rit != (*pit).value.MemberEnd()) {
std::string e = std::string((*rit).value.GetString());
if (e == "true") {
estimated = true;
}
}
if (estimated) {
phase = 1;
rit = (*pit).value.FindMember("phase");
if (rit != (*pit).value.MemberEnd()) {
phase = (*rit).value.GetInt();
}
//register male mortality parameters
for (int i = 0; i < model->mortality_vector.size(); i++) {
model->Register(model->mortality_vector[i], phase);
}
}
natural_mortality_model_iterator it = this->natural_mortality_models.find(model->id);
if (it != this->natural_mortality_models.end()) {
std::cout << "Configuration Error: More than one mortality model with the same identifier defined. Mortality models require a unique id.\n";
mas::mas_log << "Configuration Error: More than one mortality model with the same identifier defined. Mortality models require a unique id.\n";
this->valid_configuration = false;
} else {
this->natural_mortality_models[model->id] = model;
}
}
void HandleMovementModel(rapidjson::Document::MemberIterator& movement_model) {
std::shared_ptr<mas::Movement<REAL_T> > model(new mas::Movement<REAL_T>());
bool estimated = false;
int phase = 1;
rapidjson::Document::MemberIterator rit = (*movement_model).value.FindMember("id");
int model_id = 0;
if (rit == (*movement_model).value.MemberEnd()) {
std::cout << "Configuration Error: Movement is required to have a unique identifier\n";
mas::mas_log << "Configuration Error: Movement is required to have a unique identifier\n";
this->valid_configuration = false;
;
} else {
model_id = (*rit).value.GetInt();
}
model->id = model_id;
// rit = (*movement_model).value.FindMember("season");
// if (rit == (*movement_model).value.MemberEnd()) {
// std::cout << "Configuration Error: Movement is required to have a season defined\n";
// mas::mas_log << "Configuration Error: Movement is required to have a season defined\n";
// this->valid_configuration = false;
// ;
// } else {
// model->season = (*rit).value.GetInt();
// }
rit = (*movement_model).value.FindMember("recruits");
if ((*rit).value.IsArray()) {
model->recruit_connectivity.resize((*rit).value.Size());
for (int i = 0; i < (*rit).value.Size(); i++) {//season
rapidjson::Value& m = (*rit).value[i];
if (m.IsArray()) {
model->recruit_connectivity[i].resize(m.Size());
for (int j = 0; j < m.Size(); j++) {//area
rapidjson::Value& n = m[j];
if (n.IsArray()) {
model->recruit_connectivity[i][j].resize(n.Size());
for (int k = 0; k < n.Size(); k++) {//area
mas::VariableTrait<REAL_T>::SetValue(model->recruit_connectivity[i][j][k], static_cast<REAL_T> (n[k].GetDouble()));
}
}
}
} else {
std::cout << "Configuration Error: Movement connectivity rows must be a vector\n";
mas::mas_log << "Configuration Error: Movement connectivity rows must be a vector\n";
this->valid_configuration = false;
}
}
} else {
std::cout << "Configuration Error: Movement connectivity must be a matrix\n";
mas::mas_log << "Configuration Error: Movement connectivity must be a matrix\n";
this->valid_configuration = false;
}
rit = (*movement_model).value.FindMember("male");
if ((*rit).value.IsArray()) {
model->male_connectivity.resize((*rit).value.Size());
for (int i = 0; i < (*rit).value.Size(); i++) {//season
rapidjson::Value& m = (*rit).value[i];
if (m.IsArray()) {
model->male_connectivity[i].resize(m.Size());
for (int j = 0; j < m.Size(); j++) {//area
rapidjson::Value& n = m[j];
if (n.IsArray()) {
model->male_connectivity[i][j].resize(n.Size());
for (int k = 0; k < n.Size(); k++) {//area
mas::VariableTrait<REAL_T>::SetValue(model->male_connectivity[i][j][k], static_cast<REAL_T> (n[k].GetDouble()));
}
}
}
} else {
std::cout << "Configuration Error: Movement connectivity rows must be a vector\n";
mas::mas_log << "Configuration Error: Movement connectivity rows must be a vector\n";
this->valid_configuration = false;
}
}
} else {
std::cout << "Configuration Error: Movement connectivity must be a matrix\n";
mas::mas_log << "Configuration Error: Movement connectivity must be a matrix\n";
this->valid_configuration = false;
}
rit = (*movement_model).value.FindMember("female");
if ((*rit).value.IsArray()) {
model->female_connectivity.resize((*rit).value.Size());
for (int i = 0; i < (*rit).value.Size(); i++) {
rapidjson::Value& m = (*rit).value[i];
if (m.IsArray()) {
model->female_connectivity[i].resize(m.Size());
for (int j = 0; j < m.Size(); j++) {
rapidjson::Value& n = m[j];
if (n.IsArray()) {
model->female_connectivity[i][j].resize(n.Size());
for (int k = 0; k < n.Size(); k++) {
mas::VariableTrait<REAL_T>::SetValue(model->female_connectivity[i][j][k], static_cast<REAL_T> (n[k].GetDouble()));
}
}
}
} else {
std::cout << "Configuration Error: Movement connectivity rows must be a vector\n";
mas::mas_log << "Configuration Error: Movement connectivity rows must be a vector\n";
this->valid_configuration = false;
}
}
} else {
std::cout << "Configuration Error: Movement connectivity must be a matrix\n";
mas::mas_log << "Configuration Error: Movement connectivity must be a matrix\n";
this->valid_configuration = false;
;
}
rit = (*movement_model).value.FindMember("estimated");
if (rit != (*movement_model).value.MemberEnd()) {
std::string e = std::string((*rit).value.GetString());
if (e == "true") {
estimated = true;
}
}
if (estimated) {
phase = 1;
rit = (*movement_model).value.FindMember("phase");
if (rit != (*movement_model).value.MemberEnd()) {
phase = (*rit).value.GetInt();
}
//register movement coefficients
for (int i = 0; i < model->male_connectivity.size(); i++) {
for (int j = 0; j < model->male_connectivity[i].size(); j++) {
for (int k = 0; k < model->male_connectivity[i][0].size(); k++) {
std::stringstream ss;
ss << "male_connectivity[" << i << "][" << j << "][" << k << "]_" << model->id;
// model->Register(model->male_connectivity[i][j][k], phase);
// mas::VariableTrait<REAL_T>::SetName(model->male_connectivity[i][j][k], ss.str());
}
}
}
//register movement coefficients
for (int i = 0; i < model->female_connectivity.size(); i++) {
for (int j = 0; j < model->female_connectivity[i].size(); j++) {
for (int k = 0; k < model->female_connectivity[i][0].size(); k++) {
std::stringstream ss;
ss << "female_connectivity[" << i << "][" << j << "][" << k << "]_" << model->id;
// model->Register(model->male_connectivity[i][j][k], phase);
// mas::VariableTrait<REAL_T>::SetName(model->male_connectivity[i][j][k], ss.str());
}
}
}
}
movement_model_iterator it = this->movement_models.find(model->id);
if (it != this->movement_models.end()) {
std::cout << "Configuration Error: More than one movement model with the same identifier defined. Movement models require a unique id.\n";
mas::mas_log << "Configuration Error: More than one movement model with the same identifier defined. Movement models require a unique id.\n";
this->valid_configuration = false;
;
} else {
this->movement_models[model->id] = model;
}
}
void HandleFleetModel(rapidjson::Document::MemberIterator& fleet_model) {
std::shared_ptr<mas::Fleet<REAL_T> > model(new mas::Fleet<REAL_T>());
rapidjson::Document::MemberIterator rit = (*fleet_model).value.FindMember("id");
int model_id = 0;
if (rit == (*fleet_model).value.MemberEnd()) {
std::cout << "Configuration Error: Fleets are required to have a unique identifier\n";
mas::mas_log << "Configuration Error: Fleets are required to have a unique identifier\n";
this->valid_configuration = false;
;
} else {
model_id = (*rit).value.GetInt();
}
model->id = model_id;
rit = (*fleet_model).value.FindMember("name");
if (rit != (*fleet_model).value.MemberEnd()) {
model->name = std::string((*rit).value.GetString());
} else {
model->name = "NA";
}
rit = (*fleet_model).value.FindMember("selectivity");
if (rit != (*fleet_model).value.MemberEnd()) {
if (!(*rit).value.IsArray()) {
std::cout << "Configuration Error: Fleets are required to have selectivity definitions in a vector\n";
std::cout << "Configuration Error: Fleets are required to have selectivity definitions in a vector\n";
this->valid_configuration = false;
} else {
for (int i = 0; i < (*rit).value.Size(); i++) {
rapidjson::Value& m = (*rit).value[i];
int sid = 0;
int sarea = 0;
int sseason = 0;
rapidjson::Document::MemberIterator mit = m.FindMember("id");
if (mit == m.MemberEnd()) {
std::cout << "Configuration Error: Fleets selectivity has no model identifier\n";
std::cout << "Configuration Error: Fleets selectivity has no model identifier\n";
this->valid_configuration = false;
} else {
sid = (*mit).value.GetInt();
}
mit = m.FindMember("season");
if (mit == m.MemberEnd()) {
std::cout << "Configuration Error: Fleets selectivity season has no identifier\n";
std::cout << "Configuration Error: Fleets selectivity season has no identifier\n";
this->valid_configuration = false;
} else {
sseason = (*mit).value.GetInt();
}
mit = m.FindMember("area");
if (mit == m.MemberEnd()) {
std::cout << "Configuration Error: Fleets selectivity area has no identifier\n";
std::cout << "Configuration Error: Fleets selectivity area has no identifier\n";
this->valid_configuration = false;
} else {
sarea = (*mit).value.GetInt();
}
model->season_area_selectivity_ids[sseason][sarea] = sid;
model->area_season_selectivity_ids[sarea][sseason] = sid;
}
}
}
rit = (*fleet_model).value.FindMember("fishing_mortality");
if (rit != (*fleet_model).value.MemberEnd()) {
if (!(*rit).value.IsArray()) {
std::cout << "Configuration Error: Fleets are required to have fishing mortality definitions in a vector\n";
std::cout << "Configuration Error: Fleets are required to have fishing mortality definitions in a vector\n";
this->valid_configuration = false;
} else {
for (int i = 0; i < (*rit).value.Size(); i++) {
rapidjson::Value& m = (*rit).value[i];
int sid = 0;
int sarea = 0;
int sseason = 0;
rapidjson::Document::MemberIterator mit = m.FindMember("id");
if (mit == m.MemberEnd()) {
std::cout << "Configuration Error: Fleets fishing mortality has no model identifier\n";
std::cout << "Configuration Error: Fleets fishing mortality has no model identifier\n";
this->valid_configuration = false;
} else {
sid = (*mit).value.GetInt();
}
mit = m.FindMember("season");
if (mit == m.MemberEnd()) {
std::cout << "Configuration Error: Fleets fishing mortality season has no identifier\n";
std::cout << "Configuration Error: Fleets fishing mortality season has no identifier\n";
this->valid_configuration = false;
} else {
sseason = (*mit).value.GetInt();
}
mit = m.FindMember("area");
if (mit == m.MemberEnd()) {
std::cout << "Configuration Error: Fleets fishing mortality area has no identifier\n";
std::cout << "Configuration Error: Fleets fishing mortality area has no identifier\n";
this->valid_configuration = false;
} else {
sarea = (*mit).value.GetInt();
}
model->season_area_fishing_mortality_ids[sseason][sarea] = sid;
model->area_season_fishing_mortality_ids[sarea][sseason] = sid;
}
}
}
fleet_iterator fit;
fit = this->fleets.find(model->id);
if (fit == this->fleets.end()) {
this->fleets[model->id] = model;
} else {
std::cout << "Configuration Error: More than one fleet with the same identifier defined. Fleets require a unique id.\n";
mas::mas_log << "Configuration Error: More than one fleet with the same identifier defined. Fleets require a unique id.\n";
}
}
void HandlePopulationModel(rapidjson::Document::MemberIterator & population_model) {
std::shared_ptr<mas::Population<REAL_T> > model(new mas::Population<REAL_T>());
rapidjson::Document::MemberIterator rit = (*population_model).value.FindMember("id");
int model_id = 0;
if (rit == (*population_model).value.MemberEnd()) {
std::cout << "Configuration Error: Populations are required to have a unique identifier\n";
mas::mas_log << "Configuration Error: Populations are required to have a unique identifier\n";
this->valid_configuration = false;
;
} else {
model_id = (*rit).value.GetInt();
}
model->id = model_id;
rit = (*population_model).value.FindMember("name");
if (rit != (*population_model).value.MemberEnd()) {
model->name = std::string((*rit).value.GetString());
} else {
model->name = "NA";
}
rit = (*population_model).value.FindMember("natal_area");
if (rit != (*population_model).value.MemberEnd()) {
model->natal_area_id = (*rit).value.GetInt();
} else {
std::cout << "Configuration Error: Population is required to have a natal area identifier\n";
mas::mas_log << "Configuration Error: Population is required to have a natal area identifier\n";
this->valid_configuration = false;
}
rit = (*population_model).value.FindMember("maturity");
if (rit != (*population_model).value.MemberEnd()) {
// model->movement_model_id = (*rit).value.GetInt();
if ((*rit).value.IsArray()) {
for (int i = 0; i < (*rit).value.Size(); i++) {
rapidjson::Value& v = (*rit).value[i];
int sex = 0;
int area = -9999;
std::vector<REAL_T> values;
rapidjson::Document::MemberIterator mit = v.FindMember("sex");
if (std::string((*mit).value.GetString()) == std::string("female")) {
sex = 1;
}
mit = v.FindMember("area");
if (mit == v.MemberEnd()) {
std::cout << "Configuration Error: Population maturity models must define a area.\n";
mas::mas_log << "Configuration Error: Population maturity models must define a area.\n";
this->valid_configuration = false;
} else {
area = (*mit).value.GetInt();
}
mit = v.FindMember("values");
if (mit == v.MemberEnd()) {
std::cout << "Configuration Error: Population maturity models must define a value vector.\n";
mas::mas_log << "Configuration Error: Population maturity models must define a value vector.\n";
this->valid_configuration = false;
} else {
if ((*mit).value.IsArray()) {
for (int j = 0; j < (*mit).value.Size(); j++) {
rapidjson::Value& vv = (*mit).value[j];
values.push_back(static_cast<REAL_T> (vv.GetDouble()));
}
} else {
std::cout << "Configuration Error: Population maturity models must define a value vector.\n";
mas::mas_log << "Configuration Error: Population maturity models must define a value vector.\n";
}
}
model->maturity_models[area][sex] = values;
}
} else {
std::cout << "Configuration Error: Population maturity models must be defined in a vector.\n";
mas::mas_log << "Configuration Error: Population maturity models must be defined in a vector.\n";
this->valid_configuration = false;
}
} else {
std::cout << "Configuration Error: Population is required to have maturity model defined\n";
mas::mas_log << "Configuration Error: Population is required to have maturity model defined\n";
this->valid_configuration = false;
}
rit = (*population_model).value.FindMember("movement");
if (rit != (*population_model).value.MemberEnd()) {
// model->movement_model_id = (*rit).value.GetInt();
if ((*rit).value.IsArray()) {
for (int i = 0; i < (*rit).value.Size(); i++) {
rapidjson::Value& v = (*rit).value[i];
int s = 0;
int m = 0;
rapidjson::Document::MemberIterator mit = v.FindMember("year");
if ((mit) != v.MemberEnd()) {
s = (*mit).value.GetInt();
} else {
std::cout << "Configuration Error: Movement year not defined in population.\n";
mas::mas_log << "Configuration Error: Movement year not defined in population.\n";
this->valid_configuration = false;
}