|
| 1 | +/* |
| 2 | +For more information, please see: http://software.sci.utah.edu |
| 3 | +
|
| 4 | +The MIT License |
| 5 | +
|
| 6 | +Copyright (c) 2019 Scientific Computing and Imaging Institute, |
| 7 | +University of Utah. |
| 8 | +
|
| 9 | +Permission is hereby granted, free of charge, to any person obtaining a |
| 10 | +copy of this software and associated documentation files (the "Software"), |
| 11 | +to deal in the Software without restriction, including without limitation |
| 12 | +the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 13 | +and/or sell copies of the Software, and to permit persons to whom the |
| 14 | +Software is furnished to do so, subject to the following conditions: |
| 15 | +
|
| 16 | +The above copyright notice and this permission notice shall be included |
| 17 | +in all copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 20 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 24 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 25 | +DEALINGS IN THE SOFTWARE. |
| 26 | +*/ |
| 27 | + |
| 28 | +#include <gtest/gtest.h> |
| 29 | + |
| 30 | +#include <Core/Datatypes/Legacy/Field/VField.h> |
| 31 | +#include <Core/Datatypes/Legacy/Field/FieldInformation.h> |
| 32 | +#include <Core/Datatypes/Matrix.h> |
| 33 | +#include <Core/Algorithms/Legacy/Fields/StreamLines/GenerateStreamLines.h> |
| 34 | +#include <Testing/Utils/SCIRunUnitTests.h> |
| 35 | +#include <Core/Datatypes/DenseMatrix.h> |
| 36 | +#include <Core/Algorithms/Base/AlgorithmPreconditions.h> |
| 37 | +#include <Testing/Utils/MatrixTestUtilities.h> |
| 38 | +#include <Core/Datatypes/DenseMatrix.h> |
| 39 | + |
| 40 | +using namespace SCIRun; |
| 41 | +using namespace SCIRun::Core::Datatypes; |
| 42 | +using namespace SCIRun::Core::Geometry; |
| 43 | +using namespace SCIRun::Core::Algorithms::Fields; |
| 44 | +using namespace SCIRun::TestUtils; |
| 45 | + |
| 46 | +FieldHandle LoadMultiSeeds() |
| 47 | +{ |
| 48 | + return loadFieldFromFile(TestResources::rootDir() / "Fields/streamlines/multiSeeds.fld"); |
| 49 | +} |
| 50 | + |
| 51 | +FieldHandle LoadSingleSeed() |
| 52 | +{ |
| 53 | + return loadFieldFromFile(TestResources::rootDir() / "Fields/streamlines/singleSeed.fld"); |
| 54 | +} |
| 55 | + |
| 56 | +FieldHandle LoadTorsoSeeds() |
| 57 | +{ |
| 58 | + return loadFieldFromFile(TestResources::rootDir() / "Fields/seedPointsFromTorso.fld"); |
| 59 | +} |
| 60 | + |
| 61 | +FieldHandle LoadTorso() |
| 62 | +{ |
| 63 | + return loadFieldFromFile(TestResources::rootDir() / "Fields/utahtorso-lowres/gradient.fld"); |
| 64 | +} |
| 65 | + |
| 66 | +FieldHandle LoadVectorField() |
| 67 | +{ |
| 68 | + return loadFieldFromFile(TestResources::rootDir() / "Fields/streamlines/vectorField.fld"); |
| 69 | +} |
| 70 | + |
| 71 | +static std::vector<std::string> methods { "AdamsBashforth", "Heun", "RungeKutta", |
| 72 | + "RungeKuttaFehlberg", "CellWalk" }; |
| 73 | + |
| 74 | +TEST(GenerateStreamLinesTests, SingleSeedProducesSinglePositiveStreamlineSingleThreaded) |
| 75 | +{ |
| 76 | + auto singleSeed = LoadSingleSeed(); |
| 77 | + auto vectorField = LoadVectorField(); |
| 78 | + |
| 79 | + for (const auto& method : methods) |
| 80 | + { |
| 81 | + GenerateStreamLinesAlgo algo; |
| 82 | + FieldHandle output; |
| 83 | + |
| 84 | + //std::cout << "method: " << method << std::endl; |
| 85 | + algo.set(Parameters::UseMultithreading, false); |
| 86 | + algo.setOption(Parameters::StreamlineValue, "Distance from seed"); |
| 87 | + algo.setOption(Parameters::StreamlineMethod, method); |
| 88 | + |
| 89 | + algo.runImpl(vectorField, singleSeed, output); |
| 90 | + |
| 91 | + EXPECT_GT(output->vmesh()->num_nodes(), 0); |
| 92 | + EXPECT_GT(output->vmesh()->num_elems(), 0); |
| 93 | + EXPECT_GT(output->vfield()->num_values(), 0); |
| 94 | + |
| 95 | + double min,max; |
| 96 | + output->vfield()->minmax(min, max); |
| 97 | + //std::cout << min << " " << max << std::endl; |
| 98 | + |
| 99 | + // streamlines contained within 8x8x8 latvol |
| 100 | + EXPECT_EQ(min, 0.0); |
| 101 | + if (method == "AdamsBashforth") |
| 102 | + EXPECT_LT(max, 65.0); // corkscrew streamlines |
| 103 | + else |
| 104 | + EXPECT_LT(max, 12.0); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +TEST(GenerateStreamLinesTests, SingleSeedProducesSinglePositiveStreamlineMultiThreaded) |
| 109 | +{ |
| 110 | + auto singleSeed = LoadSingleSeed(); |
| 111 | + auto vectorField = LoadVectorField(); |
| 112 | + |
| 113 | + for (const auto& method : methods) |
| 114 | + { |
| 115 | + GenerateStreamLinesAlgo algo; |
| 116 | + FieldHandle output; |
| 117 | + |
| 118 | + algo.set(Parameters::UseMultithreading, true); |
| 119 | + algo.setOption(Parameters::StreamlineValue, "Distance from seed"); |
| 120 | + algo.setOption(Parameters::StreamlineMethod, method); |
| 121 | + |
| 122 | + algo.runImpl(vectorField, singleSeed, output); |
| 123 | + |
| 124 | + EXPECT_GT(output->vmesh()->num_nodes(), 0); |
| 125 | + EXPECT_GT(output->vmesh()->num_elems(), 0); |
| 126 | + EXPECT_GT(output->vfield()->num_values(), 0); |
| 127 | + |
| 128 | + double min,max; |
| 129 | + output->vfield()->minmax(min, max); |
| 130 | + //std::cout << min << " " << max << std::endl; |
| 131 | + |
| 132 | + // streamlines contained within 8x8x8 latvol |
| 133 | + EXPECT_EQ(min, 0.0); |
| 134 | + if (method == "AdamsBashforth") |
| 135 | + EXPECT_LT(max, 65.0); // corkscrew streamlines |
| 136 | + else |
| 137 | + EXPECT_LT(max, 12.0); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +//TODO: use > 16 seeds to force multithreading |
| 142 | +TEST(GenerateStreamLinesTests, MultipleSeedsProducesMultipleStreamlinesSingleThreaded) |
| 143 | +{ |
| 144 | + auto multiSeeds = LoadMultiSeeds(); |
| 145 | + auto vectorField = LoadVectorField(); |
| 146 | + |
| 147 | + for (const auto& method : methods) |
| 148 | + { |
| 149 | + GenerateStreamLinesAlgo algo; |
| 150 | + FieldHandle output; |
| 151 | + |
| 152 | + algo.set(Parameters::UseMultithreading, false); |
| 153 | + algo.setOption(Parameters::StreamlineValue, "Distance from seed"); |
| 154 | + algo.setOption(Parameters::StreamlineMethod, method); |
| 155 | + |
| 156 | + algo.runImpl(vectorField, multiSeeds, output); |
| 157 | + |
| 158 | + EXPECT_GT(output->vmesh()->num_nodes(), 0); |
| 159 | + EXPECT_GT(output->vmesh()->num_elems(), 0); |
| 160 | + EXPECT_GT(output->vfield()->num_values(), 0); |
| 161 | + |
| 162 | + double min,max; |
| 163 | + output->vfield()->minmax(min, max); |
| 164 | + //std::cout << min << " " << max << std::endl; |
| 165 | + |
| 166 | + // streamlines contained within 8x8x8 latvol |
| 167 | + EXPECT_EQ(min, 0.0); |
| 168 | + if (method == "AdamsBashforth") |
| 169 | + EXPECT_LT(max, 93.0); // corkscrew streamlines |
| 170 | + else |
| 171 | + EXPECT_LT(max, 17.0); |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +TEST(GenerateStreamLinesTests, MultipleSeedsProducesMultipleStreamlinesMultiThreaded) |
| 176 | +{ |
| 177 | + auto multiSeeds = LoadMultiSeeds(); |
| 178 | + auto vectorField = LoadVectorField(); |
| 179 | + |
| 180 | + for (const auto& method : methods) |
| 181 | + { |
| 182 | + GenerateStreamLinesAlgo algo; |
| 183 | + FieldHandle output; |
| 184 | + |
| 185 | + algo.set(Parameters::UseMultithreading, true); |
| 186 | + algo.setOption(Parameters::StreamlineValue, "Distance from seed"); |
| 187 | + algo.setOption(Parameters::StreamlineMethod, method); |
| 188 | + |
| 189 | + algo.runImpl(vectorField, multiSeeds, output); |
| 190 | + |
| 191 | + EXPECT_GT(output->vmesh()->num_nodes(), 0); |
| 192 | + EXPECT_GT(output->vmesh()->num_elems(), 0); |
| 193 | + EXPECT_GT(output->vfield()->num_values(), 0); |
| 194 | + |
| 195 | + double min,max; |
| 196 | + output->vfield()->minmax(min, max); |
| 197 | + //std::cout << min << " " << max << std::endl; |
| 198 | + |
| 199 | + // streamlines contained within 8x8x8 latvol |
| 200 | + EXPECT_EQ(min, 0.0); |
| 201 | + if (method == "AdamsBashforth") |
| 202 | + EXPECT_LT(max, 93.0); // corkscrew streamlines |
| 203 | + else |
| 204 | + EXPECT_LT(max, 17.0); |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +static std::map<std::string, std::vector<int>> meshOutputByMethod |
| 209 | + { { "AdamsBashforth", {400617, 399617, 400617} }, |
| 210 | + { "Heun", {400617, 399617, 400617} }, |
| 211 | + { "RungeKutta", {400617, 399617, 400617} }, |
| 212 | + { "RungeKuttaFehlberg", {214100, 213100, 214100} }, |
| 213 | + { "CellWalk", {98120, 97120, 98120} } |
| 214 | + }; |
| 215 | + |
| 216 | +TEST(GenerateStreamLinesTests, ManySeedsMultithreaded) |
| 217 | +{ |
| 218 | + auto torsoSeeds = LoadTorsoSeeds(); |
| 219 | + auto torso = LoadTorso(); |
| 220 | + |
| 221 | + for (const auto& method : methods) |
| 222 | + { |
| 223 | + GenerateStreamLinesAlgo algo; |
| 224 | + FieldHandle output; |
| 225 | + |
| 226 | + algo.set(Parameters::UseMultithreading, true); |
| 227 | + algo.set(Parameters::StreamlineMaxSteps, 200); |
| 228 | + algo.setOption(Parameters::StreamlineValue, "Distance from seed"); |
| 229 | + algo.setOption(Parameters::StreamlineMethod, method); |
| 230 | + |
| 231 | + //std::cout << "processing real streamlines using " << method << " method." << std::endl; |
| 232 | + algo.runImpl(torso, torsoSeeds, output); |
| 233 | + |
| 234 | + EXPECT_GT(output->vmesh()->num_nodes(), 0); |
| 235 | + EXPECT_GT(output->vmesh()->num_elems(), 0); |
| 236 | + EXPECT_GT(output->vfield()->num_values(), 0); |
| 237 | + |
| 238 | + EXPECT_EQ(output->vmesh()->num_nodes(), meshOutputByMethod[method][0]); |
| 239 | + EXPECT_EQ(output->vmesh()->num_elems(), meshOutputByMethod[method][1]); |
| 240 | + EXPECT_EQ(output->vfield()->num_values(), meshOutputByMethod[method][2]); |
| 241 | + |
| 242 | + double min,max; |
| 243 | + output->vfield()->minmax(min, max); |
| 244 | + //std::cout << min << " " << max << std::endl; |
| 245 | + |
| 246 | + // streamlines contained within mesh with greatest dimension <= 600 |
| 247 | + EXPECT_EQ(min, 0.0); |
| 248 | + if (method == "AdamsBashforth") |
| 249 | + EXPECT_LT(max, 6300.0); // corkscrew streamlines |
| 250 | + else if (method == "CellWalk") |
| 251 | + EXPECT_LT(max, 2900.0); |
| 252 | + else |
| 253 | + EXPECT_LT(max, 600.0); |
| 254 | + } |
| 255 | +} |
| 256 | + |
| 257 | +static std::map<std::string, std::pair<double,double>> meshOutputByMethodTotalLength |
| 258 | + { { "AdamsBashforth", {0.0269025, 6260.01} }, |
| 259 | + { "Heun", {0.0269025, 517.404} }, |
| 260 | + { "RungeKutta", {0.0269025, 476.269} }, |
| 261 | + { "RungeKuttaFehlberg", {0.767026, 127.16} }, |
| 262 | + { "CellWalk", {0.0, 2861.98} } |
| 263 | + }; |
| 264 | + |
| 265 | +TEST(GenerateStreamLinesTests, ManySeedsMultithreadedStreamlineLength) |
| 266 | +{ |
| 267 | + auto torsoSeeds = LoadTorsoSeeds(); |
| 268 | + auto torso = LoadTorso(); |
| 269 | + |
| 270 | + for (const auto& method : methods) |
| 271 | + { |
| 272 | + GenerateStreamLinesAlgo algo; |
| 273 | + FieldHandle output; |
| 274 | + |
| 275 | + algo.set(Parameters::UseMultithreading, true); |
| 276 | + algo.set(Parameters::StreamlineMaxSteps, 200); |
| 277 | + algo.setOption(Parameters::StreamlineValue, "Streamline length"); |
| 278 | + algo.setOption(Parameters::StreamlineMethod, method); |
| 279 | + |
| 280 | + //std::cout << "processing real streamlines using " << method << " method." << std::endl; |
| 281 | + algo.runImpl(torso, torsoSeeds, output); |
| 282 | + |
| 283 | + double min,max; |
| 284 | + output->vfield()->minmax(min, max); |
| 285 | + //std::cout << min << " " << max << std::endl; |
| 286 | + EXPECT_NEAR(min, meshOutputByMethodTotalLength[method].first, 1e-2); |
| 287 | + EXPECT_NEAR(max, meshOutputByMethodTotalLength[method].second, 1e-1); |
| 288 | + } |
| 289 | +} |
0 commit comments