preCICE v3.1.1
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
18BOOST_AUTO_TEST_CASE(SingleProcessor)
19{
20 PRECICE_TEST(1_rank);
21 /* Do not use DEBUG, TRACE, INFO calls inside tests, if you need to log a message use
22 BOOST_TEST_MESSAGE("I have done that " << whatIHaveDone);
23
24 From Boost Test Documentation:
25 "Messages generated by this tool do not appear in test log output with default value of the
26 active log level threshold. For these messages to appear the active log level threshold has to
27 be set to a value below or equal to "message"."
28
29 In order to get this output to the terminal, use testprecice with --log-level=message.
30 */
31
32 BOOST_TEST(0 == 0); // Always use BOOST_TEST
33
34 Eigen::Vector3d one(1, 2, 3);
35 Eigen::Vector3d two(1, 2, 3);
36 // Use testing::equals instead of math::equals when comparing in tests.
37 // This gives you a report which coordinates fail to compare.
38 BOOST_TEST(testing::equals(one, two));
39}
40
42BOOST_AUTO_TEST_CASE(NumericalTolerance,
43 *boost::unit_test::tolerance(1e-4))
44{
45 PRECICE_TEST(1_rank);
46 // Default tolerance is 1e-9, it can be changed for the entire case or even suite
47 // using the decorator above
48 BOOST_TEST(1.0 == 1.0001);
49
50 // Or on a per test basis
51 BOOST_TEST(1.0 == 1.01, boost::test_tools::tolerance(0.1));
52}
53
55/*
56 * If less than 4 procs are available the test will fail.
57 */
59{
60 PRECICE_TEST(4_ranks);
61 // Don't copy over that line, it's for testing the example
62 BOOST_TEST(context.size == 4);
63 BOOST_TEST(context.hasSize(4));
64}
65
68{
69 PRECICE_TEST(2_ranks);
70
71 // Put your test code here
72 BOOST_TEST(context.hasSize(2));
73}
74
75#ifndef PRECICE_NO_MPI
77/*
78 * For some primary tests, you might need an intra-participant communication. This example shows how to set one up.
79 * Please note: Such tests always need to be excluded for compilation without MPI (PRECICE_NO_MPI).
80 */
81BOOST_AUTO_TEST_CASE(FourProcTestsWithPrimaryCommmunication)
82{
83 // The short syntax won't work here. You have to name the context
84 PRECICE_TEST(""_on(4_ranks).setupIntraComm())
85 // In this test you can use an intra-participant communication, here is an example how:
86 BOOST_TEST(context.hasSize(4));
87 BOOST_TEST(utils::IntraComm::getCommunication()->isConnected());
88}
89
92{
93 PRECICE_TEST("A"_on(1_rank), "B"_on(2_ranks));
94
95 if (context.isNamed("A")) {
96 BOOST_TEST(context.hasSize(1));
97 } else if (context.isNamed("B")) {
98 BOOST_TEST(context.hasSize(2));
99 } else {
100 BOOST_TEST(false);
101 }
102}
103
105/*
106 * For some primary tests, you might need an m2n communication (e.g. partition or cplscheme).
107 * This example shows how to set up one. Call .connectPrimary() on the context and pass the participants to be connected.
108 * M2N requires Events, thus you also need to list it as a requirement using Require::Events.
109 * Please note: Such tests always need to be excluded for compilation without MPI (PRECICE_NO_MPI).
110 */
111BOOST_AUTO_TEST_CASE(TwoProcTestsWithM2NCommunication)
112{
113 PRECICE_TEST("A"_on(1_rank), "B"_on(1_rank), Require::Events);
114 BOOST_TEST(context.hasSize(1));
115 BOOST_TEST(context.isRank(0));
116 BOOST_TEST(context.isPrimary());
117
118 auto m2n = context.connectPrimaryRanks("A", "B");
119
120 //This is how you can access the m2n communication
121 BOOST_TEST(m2n->getPrimaryRankCommunication()->isConnected());
122
123 // Automatically finalizes Events
124}
125
126#endif // PRECICE_NO_MPI
127
128BOOST_AUTO_TEST_CASE(TwoProcTestsWithPETSc)
129{
130 PRECICE_TEST(2_ranks, Require::PETSc); // implies Require::Events
131 BOOST_TEST(context.hasSize(2));
132
133 // Automatically finalizes PETSc and Events
134}
135
137/*
138 * For integration tests (tests that directly use the preCICE API), often, you need two participants
139 * where each participant uses it own communicator, i.e. each participant should not see that he is
140 * part of a test.
141 * In this case, you can simply create the participants and create a Participant object.
142 * 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.
143 */
144BOOST_AUTO_TEST_CASE(IntegrationTestsWithTwoParticipants)
145{
146 PRECICE_TEST("Solid"_on(2_ranks), "Fluid"_on(2_ranks));
147
148 if (context.isNamed("Solid")) {
149 // This is the participant Solid
150 BOOST_TEST(context.hasSize(2));
151
152 // You can now create a Participant object for your first participant
153 // You can use context.name, context.rank, context.size
154 } else {
155 // This is the participant Fluid
156 BOOST_TEST(context.hasSize(2));
157
158 // You can now create a Participant object for your second participant
159 // You can use context.name, context.rank, context.size
160 }
161}
162
163BOOST_AUTO_TEST_SUITE_END() // Examples
164BOOST_AUTO_TEST_SUITE_END() // TestingTests
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:27
static com::PtrCommunication & getCommunication()
Intra-participant communication.
Definition IntraComm.hpp:31
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:65
Main namespace of the precice library.