|
| 1 | +#include "FileChecker.h" |
| 2 | +#include "StreamHandler.hpp" |
1 | 3 | #include "gtest/gtest.h"
|
2 | 4 | #include <Formulation_Manager.hpp>
|
3 | 5 | #include <Catchment_Formulation.hpp>
|
@@ -67,6 +69,30 @@ class Formulation_Manager_Test : public ::testing::Test {
|
67 | 69 | fabric->add_feature(feature);
|
68 | 70 | }
|
69 | 71 |
|
| 72 | + void add_feature(const std::string& id, geojson::PropertyMap properties) |
| 73 | + { |
| 74 | + geojson::three_dimensional_coordinates three_dimensions { |
| 75 | + { |
| 76 | + {1.0, 2.0}, |
| 77 | + {3.0, 4.0}, |
| 78 | + {5.0, 6.0} |
| 79 | + }, |
| 80 | + { |
| 81 | + {7.0, 8.0}, |
| 82 | + {9.0, 10.0}, |
| 83 | + {11.0, 12.0} |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + geojson::Feature feature = std::make_shared<geojson::PolygonFeature>(geojson::PolygonFeature( |
| 88 | + geojson::polygon(three_dimensions), |
| 89 | + id, |
| 90 | + properties |
| 91 | + )); |
| 92 | + |
| 93 | + fabric->add_feature(feature); |
| 94 | + } |
| 95 | + |
70 | 96 | std::vector<std::string> path_options = {
|
71 | 97 | "",
|
72 | 98 | "../",
|
@@ -638,6 +664,60 @@ const std::string EXAMPLE_4 = "{ "
|
638 | 664 | "} "
|
639 | 665 | "}";
|
640 | 666 |
|
| 667 | +const std::string EXAMPLE_5 = |
| 668 | +"{" |
| 669 | +" \"time\": {" |
| 670 | +" \"start_time\": \"2015-12-01 00:00:00\"," |
| 671 | +" \"end_time\": \"2015-12-30 23:00:00\"," |
| 672 | +" \"output_interval\": 3600" |
| 673 | +" }," |
| 674 | +" \"catchments\": {" |
| 675 | +" \"cat-67\": {" |
| 676 | +" \"formulations\": [" |
| 677 | +" {" |
| 678 | +" \"name\": \"bmi_c++\"," |
| 679 | +" \"params\": {" |
| 680 | +" \"model_type_name\": \"bmi_c++_sloth\"," |
| 681 | +" \"library_file\": \"" + |
| 682 | +utils::FileChecker::find_first_readable({ |
| 683 | + "../../extern/sloth/cmake_build/libslothmodel.so", |
| 684 | + "../extern/sloth/cmake_build/libslothmodel.so", |
| 685 | + "./extern/sloth/cmake_build/libslothmodel.so", |
| 686 | + "../../extern/sloth/build/libslothmodel.so", |
| 687 | + "../extern/sloth/build/libslothmodel.so", |
| 688 | + "./extern/sloth/build/libslothmodel.so", |
| 689 | +})+ |
| 690 | +"\"," |
| 691 | +" \"init_config\": \"/dev/null\"," |
| 692 | +" \"allow_exceed_end_time\": true," |
| 693 | +" \"main_output_variable\": \"Klf\"," |
| 694 | +" \"uses_forcing_file\": false," |
| 695 | +" \"model_params\": {" |
| 696 | +" \"Klf\": {" |
| 697 | +" \"source\": \"hydrofabric\"" |
| 698 | +" }," |
| 699 | +" \"Kn\": {" |
| 700 | +" \"source\": \"hydrofabric\"" |
| 701 | +" }," |
| 702 | +" \"nash_n\": {" |
| 703 | +" \"source\": \"hydrofabric\"," |
| 704 | +" \"from\": \"n\"" |
| 705 | +" }," |
| 706 | +" \"Cgw\": {" |
| 707 | +" \"source\": \"hydrofabric\"" |
| 708 | +" }," |
| 709 | +" \"static_var\": 0.0" |
| 710 | +" }" |
| 711 | +" }" |
| 712 | +" }" |
| 713 | +" ]," |
| 714 | +" \"forcing\": {" |
| 715 | +" \"path\": \"./data/forcing/cat-67_2015-12-01 00_00_00_2015-12-30 23_00_00.csv\"" |
| 716 | +" }" |
| 717 | +" }" |
| 718 | +" }" |
| 719 | +"}"; |
| 720 | + |
641 | 721 | TEST_F(Formulation_Manager_Test, basic_reading_1) {
|
642 | 722 | std::stringstream stream;
|
643 | 723 |
|
@@ -828,3 +908,47 @@ TEST_F(Formulation_Manager_Test, forcing_provider_specification) {
|
828 | 908 | }
|
829 | 909 | }
|
830 | 910 |
|
| 911 | +TEST_F(Formulation_Manager_Test, read_external_attributes) { |
| 912 | + std::stringstream stream; |
| 913 | + stream << fix_paths(EXAMPLE_5); |
| 914 | + |
| 915 | + std::ostream* ptr = &std::cout; |
| 916 | + std::shared_ptr<std::ostream> s_ptr(ptr, [](void*) {}); |
| 917 | + utils::StreamHandler catchment_output(s_ptr); |
| 918 | + |
| 919 | + auto manager = realization::Formulation_Manager(stream); |
| 920 | + this->add_feature("cat-67", geojson::PropertyMap{ |
| 921 | + { "Klf", geojson::JSONProperty{"Klf", 1.70352 } }, |
| 922 | + { "Kn", geojson::JSONProperty{"Kn", 0.03 } }, |
| 923 | + { "n", geojson::JSONProperty{"n", 2 } }, // nash_n |
| 924 | + { "Cgw", geojson::JSONProperty{"Cgw", 0.01 } } |
| 925 | + }); |
| 926 | + |
| 927 | + auto feature = this->fabric->get_feature("cat-67"); |
| 928 | + ASSERT_TRUE(feature->has_property("Klf")); |
| 929 | + ASSERT_TRUE(feature->has_property("Kn")); |
| 930 | + ASSERT_TRUE(feature->has_property("n")); |
| 931 | + ASSERT_TRUE(feature->has_property("Cgw")); |
| 932 | + |
| 933 | + manager.read(this->fabric, catchment_output); |
| 934 | + |
| 935 | + ASSERT_EQ(manager.get_size(), 1); |
| 936 | + ASSERT_TRUE(manager.contains("cat-67")); |
| 937 | + |
| 938 | + pdm03_struct pdm_et_data; |
| 939 | + pdm_et_data.scaled_distribution_fn_shape_parameter = 1.3; |
| 940 | + pdm_et_data.vegetation_adjustment = 0.99; |
| 941 | + pdm_et_data.model_time_step = 0.0; |
| 942 | + pdm_et_data.max_height_soil_moisture_storerage_tank = 400.0; |
| 943 | + pdm_et_data.maximum_combined_contents = pdm_et_data.max_height_soil_moisture_storerage_tank / (1.0+pdm_et_data.scaled_distribution_fn_shape_parameter); |
| 944 | + std::shared_ptr<pdm03_struct> et_params_ptr = std::make_shared<pdm03_struct>(pdm_et_data); |
| 945 | + |
| 946 | + auto formulation = manager.get_formulation("cat-67"); |
| 947 | + formulation->set_et_params(et_params_ptr); |
| 948 | + double result = formulation->get_response(0, 3600); |
| 949 | + |
| 950 | + |
| 951 | + ASSERT_NEAR(result, 1.70352, 1e-5); |
| 952 | + ASSERT_EQ(formulation->get_output_header_line(","), "Cgw,Klf,Kn,n,static_var"); |
| 953 | + ASSERT_EQ(formulation->get_output_line_for_timestep(0), "0.010000,1.703520,0.030000,2.000000,0.000000"); |
| 954 | +} |
0 commit comments