preCICE v3.1.1
Loading...
Searching...
No Matches
StorageTest.cpp
Go to the documentation of this file.
1#include <Eigen/Core>
3#include "testing/Testing.hpp"
4#include "time/Storage.hpp"
5
6using namespace precice;
7using namespace precice::time;
8
10BOOST_AUTO_TEST_SUITE(StorageTests)
11
12// create storage and test for correct initial values.
13BOOST_AUTO_TEST_CASE(testInitialize)
14{
15 PRECICE_TEST(1_rank);
16 auto storage = Storage();
17 int nValues = 3;
18 BOOST_TEST(storage.nTimes() == 0);
19 storage.setSampleAtTime(0, time::Sample{1, Eigen::VectorXd::Ones(nValues)});
20 BOOST_TEST(storage.nDofs() == nValues);
21 BOOST_TEST(storage.nTimes() == 1);
22 for (int i = 0; i < nValues; i++) {
23 BOOST_TEST(storage.getSampleAtOrAfter(0).values(i) == 1);
24 BOOST_TEST(storage.getSampleAtOrAfter(0.5).values(i) == 1);
25 BOOST_TEST(storage.getSampleAtOrAfter(1).values(i) == 1);
26 }
27}
28
29// create storage and trim it.
31{
32 PRECICE_TEST(1_rank);
33 auto storage = Storage();
34 int nValues = 3;
35 BOOST_TEST(storage.nTimes() == 0);
36 storage.setSampleAtTime(0, time::Sample{1, Eigen::VectorXd::Ones(nValues)});
37 BOOST_TEST(storage.nDofs() == nValues);
38 BOOST_TEST(storage.nTimes() == 1);
39 BOOST_TEST(storage.maxStoredTime() == 0.0);
40 storage.setSampleAtTime(1, time::Sample{1, Eigen::VectorXd::Ones(nValues)});
41 BOOST_TEST(storage.nDofs() == nValues);
42 BOOST_TEST(storage.nTimes() == 2);
43 BOOST_TEST(storage.maxStoredTime() == 1.0);
44 storage.trim();
45 BOOST_TEST(storage.nDofs() == nValues);
46 BOOST_TEST(storage.nTimes() == 1);
47 BOOST_TEST(storage.maxStoredTime() == 0.0);
48}
49
50// create storage, add some values and then move to next window.
52{
53 PRECICE_TEST(1_rank);
54 auto storage = Storage();
55 int nValues = 3;
56 BOOST_TEST(storage.nTimes() == 0);
57 storage.setSampleAtTime(0, time::Sample{1, Eigen::VectorXd::Ones(nValues)});
58 BOOST_TEST(storage.nDofs() == nValues);
59 BOOST_TEST(storage.nTimes() == 1);
60 BOOST_TEST(storage.maxStoredTime() == 0.0);
61 storage.setSampleAtTime(0.5, time::Sample{1, Eigen::VectorXd::Ones(nValues)});
62 BOOST_TEST(storage.nTimes() == 2);
63 BOOST_TEST(storage.maxStoredTime() == 0.5);
64 storage.setSampleAtTime(1.0, time::Sample{1, Eigen::VectorXd::Zero(nValues)});
65 BOOST_TEST(storage.nTimes() == 3);
66 BOOST_TEST(storage.maxStoredTime() == 1.0);
67 for (int i = 0; i < nValues; i++) {
68 BOOST_TEST(storage.getSampleAtOrAfter(0).values(i) == 1);
69 BOOST_TEST(storage.getSampleAtOrAfter(0.5).values(i) == 1);
70 BOOST_TEST(storage.getSampleAtOrAfter(1).values(i) == 0);
71 }
72 storage.move();
73 BOOST_TEST(storage.nDofs() == nValues);
74 BOOST_TEST(storage.nTimes() == 1);
75 BOOST_TEST(storage.maxStoredTime() == 1.0);
76 for (int i = 0; i < nValues; i++) {
77 BOOST_TEST(storage.getSampleAtOrAfter(0).values(i) == 0);
78 BOOST_TEST(storage.getSampleAtOrAfter(1).values(i) == 0);
79 }
80}
81
82// get times and values
83BOOST_AUTO_TEST_CASE(testGetTimesAndValues)
84{
85 PRECICE_TEST(1_rank);
86 auto storage = Storage();
87 int nValues = 3;
88 storage.setSampleAtTime(0, time::Sample{1, Eigen::VectorXd::Ones(nValues)});
89 storage.setSampleAtTime(0.5, time::Sample{1, Eigen::VectorXd::Ones(nValues)});
90 storage.setSampleAtTime(1.0, time::Sample{1, Eigen::VectorXd::Zero(nValues)});
91 auto times = storage.getTimes();
92 BOOST_TEST(times[0] == 0.0);
93 BOOST_TEST(times[1] == 0.5);
94 BOOST_TEST(times[2] == 1.00);
95 auto timesAndValues = storage.getTimesAndValues();
96 BOOST_TEST(timesAndValues.first[0] == 0.0);
97 BOOST_TEST(timesAndValues.first[1] == 0.5);
98 BOOST_TEST(timesAndValues.first[2] == 1.00);
99 for (int i = 0; i < nValues; i++) {
100 BOOST_TEST(timesAndValues.second.col(0)(i) == 1);
101 BOOST_TEST(timesAndValues.second.col(1)(i) == 1);
102 BOOST_TEST(timesAndValues.second.col(2)(i) == 0);
103 }
104}
105
106BOOST_AUTO_TEST_SUITE(ExtrapolationTests)
107BOOST_AUTO_TEST_CASE(testExtrapolateDataZerothOrder)
108{
109 PRECICE_TEST(1_rank);
110
111 auto storage = Storage();
112 const int nValues = 1;
113
114 storage.setSampleAtTime(0.0, time::Sample{1, Eigen::VectorXd::Zero(nValues)});
115 auto times = storage.getTimes();
116 BOOST_TEST(times[0] == 0.0);
117 auto timesAndValues = storage.getTimesAndValues();
118 BOOST_TEST(timesAndValues.second.col(0)(0) == 0.0);
119
120 storage.setSampleAtTime(1.0, time::Sample{1, Eigen::VectorXd::Ones(nValues)});
121 times = storage.getTimes();
122 BOOST_TEST(times[0] == 0.0);
123 BOOST_TEST(times[1] == 1.0);
124 timesAndValues = storage.getTimesAndValues();
125 BOOST_TEST(timesAndValues.second.col(0)(0) == 0.0);
126 BOOST_TEST(timesAndValues.second.col(1)(0) == 1.0);
127
128 storage.move();
129
130 times = storage.getTimes();
131 BOOST_TEST(times[0] == 1.0);
132 timesAndValues = storage.getTimesAndValues();
133 BOOST_TEST(timesAndValues.second.col(0)(0) == 1.0);
134
135 // make sure that subcycling is ignored for extrapolation
136 storage.trim();
137 storage.setSampleAtTime(1.5, time::Sample{1, 2 * Eigen::VectorXd::Ones(nValues)});
138 storage.setSampleAtTime(2.0, time::Sample{1, 3 * Eigen::VectorXd::Ones(nValues)});
139
140 times = storage.getTimes();
141 BOOST_TEST(times[0] == 1.0);
142 BOOST_TEST(times[1] == 1.5);
143 BOOST_TEST(times[2] == 2.0);
144
145 storage.move();
146
147 times = storage.getTimes();
148 BOOST_TEST(times[0] == 2.0);
149 timesAndValues = storage.getTimesAndValues();
150 BOOST_TEST(timesAndValues.second.col(0)(0) == 3.0);
151}
152
BOOST_AUTO_TEST_SUITE(PreProcess)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(testInitialize)
#define PRECICE_TEST(...)
Definition Testing.hpp:27
contains the time interpolation logic.
Definition Sample.hpp:6
Main namespace of the precice library.