preCICE v3.2.0
Loading...
Searching...
No Matches
ExampleTests.cpp
Go to the documentation of this file.
1#include <Eigen/Core>
2#include <memory>
5#include "m2n/M2N.hpp"
6#include "math/constants.hpp"
8#include "testing/Testing.hpp"
9#include "utils/IntraComm.hpp"
10
11using namespace precice;
12
13BOOST_AUTO_TEST_SUITE(TestingTests) // Use name of the module, e.g. subdirectory below src/, suffixed with Tests
14
15BOOST_AUTO_TEST_SUITE(Examples) // If your file contains multiple tests, put them in a test suite
16
17
19BOOST_AUTO_TEST_CASE(SingleProcessor)
20{
22 /* Do not use DEBUG, TRACE, INFO calls inside tests, if you need to log a message use
23 BOOST_TEST_MESSAGE("I have done that " << whatIHaveDone);
24
25 From Boost Test Documentation:
26 "Messages generated by this tool do not appear in test log output with default value of the
27 active log level threshold. For these messages to appear the active log level threshold has to
28 be set to a value below or equal to "message"."
29
30 In order to get this output to the terminal, use testprecice with --log-level=message.
31 */
32
33 BOOST_TEST(0 == 0); // Always use BOOST_TEST
34
35 Eigen::Vector3d one(1, 2, 3);
36 Eigen::Vector3d two(1, 2, 3);
37 // Use testing::equals instead of math::equals when comparing in tests.
38 // This gives you a report which coordinates fail to compare.
39 BOOST_TEST(testing::equals(one, two));
40}
41
44BOOST_AUTO_TEST_CASE(NumericalTolerance,
45 *boost::unit_test::tolerance(1e-4))
46{
48 // Default tolerance is 1e-9, it can be changed for the entire case or even suite
49 // using the decorator above
50 BOOST_TEST(1.0 == 1.0001);
51
52 // Or on a per test basis
53 BOOST_TEST(1.0 == 1.01, boost::test_tools::tolerance(0.1));
54}
55
57/*
58 * If less than 4 procs are available the test will fail.
59 */
60PRECICE_TEST_SETUP(4_ranks)
62{
64 // Don't copy over that line, it's for testing the example
65 BOOST_TEST(context.size == 4);
66 BOOST_TEST(context.hasSize(4));
67}
68
70PRECICE_TEST_SETUP(2_ranks)
72{
74
75 // Put your test code here
76 BOOST_TEST(context.hasSize(2));
77}
78
79#ifndef PRECICE_NO_MPI
81/*
82 * For some primary tests, you might need an intra-participant communication. This example shows how to set one up.
83 * Please note: Such tests always need to be excluded for compilation without MPI (PRECICE_NO_MPI).
84 *
85 * The short syntax won't work here. You have to name the context
86 */
87PRECICE_TEST_SETUP(""_on(4_ranks).setupIntraComm())
88BOOST_AUTO_TEST_CASE(FourProcTestsWithPrimaryCommmunication)
89{
91 // In this test you can use an intra-participant communication, here is an example how:
92 BOOST_TEST(context.hasSize(4));
93 BOOST_TEST(utils::IntraComm::getCommunication()->isConnected());
94}
95
97PRECICE_TEST_SETUP("A"_on(1_rank), "B"_on(2_ranks))
98BOOST_AUTO_TEST_CASE(NamedContexts)
99{
100 PRECICE_TEST();
101
102 if (context.isNamed("A")) {
103 BOOST_TEST(context.hasSize(1));
104 } else if (context.isNamed("B")) {
105 BOOST_TEST(context.hasSize(2));
106 } else {
107 BOOST_TEST(false);
108 }
109}
110
112/*
113 * For some primary tests, you might need an m2n communication (e.g. partition or cplscheme).
114 * This example shows how to set up one. Call .connectPrimary() on the context and pass the participants to be connected.
115 * M2N requires Events, thus you also need to list it as a requirement using Require::Events.
116 * Please note: Such tests always need to be excluded for compilation without MPI (PRECICE_NO_MPI).
117 */
118PRECICE_TEST_SETUP("A"_on(1_rank), "B"_on(1_rank), Require::Events)
119BOOST_AUTO_TEST_CASE(TwoProcTestsWithM2NCommunication)
120{
121 PRECICE_TEST();
122 BOOST_TEST(context.hasSize(1));
123 BOOST_TEST(context.isRank(0));
124 BOOST_TEST(context.isPrimary());
125
126 auto m2n = context.connectPrimaryRanks("A", "B");
127
128 // This is how you can access the m2n communication
129 BOOST_TEST(m2n->getPrimaryRankCommunication()->isConnected());
130
131 // Automatically finalizes Events
132}
133
134#endif // PRECICE_NO_MPI
135
137BOOST_AUTO_TEST_CASE(TwoProcTestsWithPETSc)
138{
139 PRECICE_TEST(); // implies Require::Events
140 BOOST_TEST(context.hasSize(2));
141
142 // Automatically finalizes PETSc and Events
143}
144
146/*
147 * For integration tests (tests that directly use the preCICE API), often, you need two participants
148 * where each participant uses it own communicator, i.e. each participant should not see that he is
149 * part of a test.
150 * In this case, you can simply create the participants and create a Participant object.
151 * The context-object is of type TestContext and provides access to the name of the current context and the rank and size of its communicator.
152 */
153PRECICE_TEST_SETUP("Solid"_on(2_ranks), "Fluid"_on(2_ranks))
154BOOST_AUTO_TEST_CASE(IntegrationTestsWithTwoParticipants)
155{
156 PRECICE_TEST();
157
158 if (context.isNamed("Solid")) {
159 // This is the participant Solid
160 BOOST_TEST(context.hasSize(2));
161
162 // You can now create a Participant object for your first participant
163 // You can use context.name, context.rank, context.size
164 } else {
165 // This is the participant Fluid
166 BOOST_TEST(context.hasSize(2));
167
168 // You can now create a Participant object for your second participant
169 // You can use context.name, context.rank, context.size
170 }
171}
172
173BOOST_AUTO_TEST_SUITE_END() // Examples
174BOOST_AUTO_TEST_SUITE_END() // TestingTests
BOOST_AUTO_TEST_CASE(testIQNIMVJPPWithSubsteps)
BOOST_AUTO_TEST_CASE(SingleProcessor)
This test runs on a single processor.
BOOST_AUTO_TEST_SUITE(PreProcess)
BOOST_AUTO_TEST_SUITE_END()
#define PRECICE_TEST()
Definition Testing.hpp:39
#define PRECICE_TEST_SETUP(...)
Creates and attaches a TestSetup to a Boost test case.
Definition Testing.hpp:29
static com::PtrCommunication & getCommunication()
Intra-participant communication.
Definition IntraComm.hpp:31
contains the logic of the parallel communication between participants.
Definition BoundM2N.cpp:12
@ PETSc
Require to initialize PETSc. This implies the initialization of Events.
@ Events
Require to initialize Event.
boost::test_tools::predicate_result equals(const std::vector< float > &VectorA, const std::vector< float > &VectorB, float tolerance)
equals to be used in tests. Compares two std::vectors using a given tolerance. Prints both operands o...
Definition Testing.cpp:93
Main namespace of the precice library.