test-boundary-condition-manager.cc 10 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <gtest/gtest.h>

#ifdef HAVE_CONFIG_H
    #include "config.h"
#endif

#include <memory>

#include <yaml-cpp/yaml.h>

#include <dune/common/exceptions.hh>
#include <dune/common/float_cmp.hh>
#include <dune/common/parallel/mpihelper.hh>

#include <dune/grid/yaspgrid.hh>
#include <dune/grid/common/gridview.hh>

#include <dune/dorie/common/setup.hh>
#include <dune/dorie/common/logging.hh>
#include <dune/dorie/common/grid_creator.hh>
#include <dune/dorie/common/boundary_condition/manager.hh>
#include <dune/dorie/common/boundary_condition/factory.hh>

// Store command line arguments
int _argc;
char **_argv;

/// The traits mock-up for this test
struct Traits
{
    static constexpr int dim = 2;
    using RF = double;
    using Grid = Dune::YaspGrid<dim>;
    using GV = typename Grid::LeafGridView;
    using Intersection = typename GV::Traits::Intersection;
};

/// Alias for the boundary condition type
using BC = Dune::Dorie::BoundaryCondition<typename Traits::RF>;

/* ------------------------------------------------------------------------- */
// Test BC factory creating BCs from YAML nodes
43 44
// This tests checks if the construction throws appropriate exceptions for
// incompatible input.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

/// BCFactory fixture. Create three factories with a different mode each.
class BCFactory : public ::testing::Test
{
protected:
    using Mode = Dune::Dorie::BCMMode;

    template <Mode mode>
    using BCF = Dune::Dorie::BoundaryConditionFactory<double, mode>;

    /// Initialize the DUNE MPI Helper from the command line arguments
    Dune::MPIHelper& helper = Dune::MPIHelper::instance(_argc, _argv);

    std::unique_ptr<BCF<Mode::none>> _bcf_none;
    std::unique_ptr<BCF<Mode::richards>> _bcf_richards;
    std::unique_ptr<BCF<Mode::transport>> _bcf_transport;

    void SetUp () override
    {
64
        auto log = Dune::Dorie::create_logger(Dune::Dorie::log_base, helper);
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        _bcf_none = std::make_unique<BCF<Mode::none>>(log);
        _bcf_richards = std::make_unique<BCF<Mode::richards>>(log);
        _bcf_transport = std::make_unique<BCF<Mode::transport>>(log);
    }

    void TearDown () override
    {
        // Remove loggers
        spdlog::drop_all();
    }
};

TEST_F(BCFactory, Dirichlet)
{
    YAML::Node config = YAML::Load("{type: Dirichlet,"
                                    "value: 1.0,"
                                    "time: 0.0"
                                   "}");
    EXPECT_NO_THROW(_bcf_none->create("name", config, 1.0));
    EXPECT_NO_THROW(_bcf_richards->create("name", config, 1.0));
    EXPECT_THROW(_bcf_transport->create("name", config, 1.0), Dune::IOError);

    config = YAML::Load("{type: Dirichlet,"
                         "value: 1.0,"
                         "time: 0.0,"
                         "concentration_type: water_phase"
                        "}");
    EXPECT_NO_THROW(_bcf_none->create("name", config, 1.0));
    EXPECT_NO_THROW(_bcf_richards->create("name", config, 1.0));
    EXPECT_NO_THROW(_bcf_transport->create("name", config, 1.0));

    config = YAML::Load("{type: Dirichlet,"
                         "value: 1.0,"
                         "time: 0.0,"
                         "concentration_type: none"
                        "}");
    EXPECT_NO_THROW(_bcf_none->create("name", config, 1.0));
    EXPECT_NO_THROW(_bcf_richards->create("name", config, 1.0));
    EXPECT_THROW(_bcf_transport->create("name", config, 1.0), Dune::IOError);
}

TEST_F(BCFactory, Neumann)
{
    YAML::Node config = YAML::Load("{type: Neumann,"
                                    "value: 1.0,"
                                    "time: 0.0,"
                                    "horizontal_projection: true"
                                   "}");
    EXPECT_NO_THROW(_bcf_none->create("name", config, 1.0));
    EXPECT_NO_THROW(_bcf_richards->create("name", config, 1.0));
    EXPECT_NO_THROW(_bcf_transport->create("name", config, 1.0));
}

TEST_F(BCFactory, Outflow)
{
    YAML::Node config = YAML::Load("{type: Outflow,"
                                    "value: 1.0,"
                                    "time: 0.0"
                                   "}");
    EXPECT_NO_THROW(_bcf_none->create("name", config, 1.0));
    EXPECT_THROW(_bcf_richards->create("name", config, 1.0), Dune::IOError);
    EXPECT_NO_THROW(_bcf_transport->create("name", config, 1.0));
}

/* ------------------------------------------------------------------------- */
// Test BC manager and loaded BC data.
131
// This verifies that YAML data is properly interpreted
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

/// BoundaryConditionManager fixture
/** \tparam T BCMMode wrapped into an integral constant to appear as type
 */
// template<typename T>
class BCManager : public ::testing::Test
{
protected:
    /// Initialize the DUNE MPI Helper from the command line arguments
    Dune::MPIHelper& helper = Dune::MPIHelper::instance(_argc, _argv);

    /// Pointer to the grid
    std::shared_ptr<Traits::Grid> _grid;

    using BCM = Dune::Dorie::BoundaryConditionManager<
        Traits, Dune::Dorie::BCMMode::none
    >;
    /// Pointer to the manager
    std::unique_ptr<BCM> _manager;

    /// Initialize
    void SetUp () override
    {
        // Initialize ALL the things!
        auto [inifile, log, helper] = Dune::Dorie::Setup::init(_argc, _argv);

        // create the grid
        Dune::Dorie::GridCreator<Traits::Grid> gc(inifile, helper);
        _grid = gc.grid();
        const auto& index_map = gc.boundary_index_map();

        inifile = Dune::Dorie::Setup::prep_ini_for_richards(inifile);

        const auto log_level = inifile.get<std::string>("output.logLevel");
        Dune::Dorie::create_logger(Dune::Dorie::log_richards,
                                   helper,
                                   spdlog::level::from_str(log_level));

        _manager = std::make_unique<BCM>(inifile, index_map);
    }

    void TearDown () override
    {
        // remove loggers
        spdlog::drop_all();
    }
};

TEST_F(BCManager, TimeSteps)
{
    // check correct maximum time steps
    double max_dt = 0.0;
    double time_prev = 0.0;

    for (auto time : {time_prev, 0.5, 1.5})
    {
        EXPECT_DOUBLE_EQ(time, time_prev+max_dt);
        time_prev = time;
        this->_manager->set_time(time);
        max_dt = this->_manager->max_time_step(time);
    }
    EXPECT_DOUBLE_EQ(max_dt, 0.5);

    // assert exception if out of interval
    EXPECT_THROW(this->_manager->set_time(-0.5), Dune::InvalidStateException);
    EXPECT_THROW(this->_manager->set_time(2.0), Dune::InvalidStateException);
}

TEST_F(BCManager, Neumann)
{
    auto gv = _grid->leafGridView();
    auto cell = *Dune::elements(gv).begin();
    if (not cell.hasBoundaryIntersections()) {
205
        FAIL();
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    }
    for (auto&& is : Dune::intersections(gv, cell)) {
        const auto coords = is.geometry().center();
        if (Dune::FloatCmp::eq(coords[1], 1.0))
        {
            for (auto time: {0.0, 1.0, 1.5, 1.9}) {
                using namespace Dune::Dorie;
                _manager->set_time(time);
                const auto bc = _manager->bc(is);
                EXPECT_EQ(bc->name(), "neumann");
                EXPECT_EQ(bc->type(), Operator::BCType::Neumann);
                EXPECT_DOUBLE_EQ(bc->evaluate(time), 1.0);
                auto &time_int = bc->time_interval();
                EXPECT_DOUBLE_EQ(time_int.begin, 0.0);
                EXPECT_DOUBLE_EQ(time_int.end, 2.0);

                auto &bc_neumann = 
                    dynamic_cast<NeumannBoundaryCondition<double>&>(*bc);
                EXPECT_TRUE(bc_neumann.horizontal_projection());
            }
226
            return;
227 228
        }
    }
229 230

    FAIL();
231 232 233 234 235 236 237
}

TEST_F(BCManager, Dirichlet)
{
    auto gv = _grid->leafGridView();
    auto cell = *Dune::elements(gv).begin();
    if (not cell.hasBoundaryIntersections()) {
238
        FAIL();
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    }
    for (auto&& is : Dune::intersections(gv, cell)) {
        const auto coords = is.geometry().center();
        if (Dune::FloatCmp::eq(coords[1], 0.0))
        {
            using namespace Dune::Dorie;
            for (auto time: {0.5, 1.4}) {
                _manager->set_time(time);
                const auto bc = _manager->bc(is);
                EXPECT_EQ(bc->name(), "dirichlet");
                EXPECT_EQ(bc->type(), Operator::BCType::Dirichlet);
                EXPECT_DOUBLE_EQ(bc->evaluate(time), time-0.5);
                auto &time_int = bc->time_interval();
                EXPECT_DOUBLE_EQ(time_int.begin, 0.5);
                EXPECT_DOUBLE_EQ(time_int.end, 1.5);

                auto &bc_dirichlet =
                    dynamic_cast<DirichletBoundaryCondition<double>&>(*bc);
                EXPECT_EQ(bc_dirichlet.concentration_type(),
                          SoluteConcentration::water_phase);
            }

            for (auto time: {0.0, 1.5, 1.9}) {
                _manager->set_time(time);
                const auto bc = _manager->bc(is);
                EXPECT_EQ(bc->name(), "default");
                EXPECT_EQ(bc->type(), Operator::BCType::Neumann);
                EXPECT_DOUBLE_EQ(bc->evaluate(time), 0.0);
            }
268
            return;
269 270
        }
    }
271 272

    FAIL();
273 274 275 276 277 278 279
}

TEST_F(BCManager, Outflow)
{
    auto gv = _grid->leafGridView();
    auto cell = *Dune::elements(gv).begin();
    if (not cell.hasBoundaryIntersections()) {
280
        FAIL();
281 282 283
    }
    for (auto&& is : Dune::intersections(gv, cell)) {
        const auto coords = is.geometry().center();
284
        if (Dune::FloatCmp::eq(coords[0], 0.0))
285 286 287 288 289 290 291 292 293 294 295
        {
            for (auto time: {0.0, 1.0, 1.5, 1.9}) {
                _manager->set_time(time);
                const auto bc = _manager->bc(is);
                EXPECT_EQ(bc->name(), "outflow");
                EXPECT_EQ(bc->type(), Dune::Dorie::Operator::BCType::Outflow);
                EXPECT_DOUBLE_EQ(bc->evaluate(time), 0.0);
                auto &time_int = bc->time_interval();
                EXPECT_DOUBLE_EQ(time_int.begin, 0.0);
                EXPECT_DOUBLE_EQ(time_int.end, 2.0);
            }
296
            return;
297 298
        }
    }
299 300

    FAIL();
301 302 303 304 305 306 307 308 309 310 311 312 313
}

// --- Main Function --- //

int main(int argc, char **argv)
{
    // Parse input arguments
    ::testing::InitGoogleTest(&argc, argv);
    _argc = argc;
    _argv = argv;

    return RUN_ALL_TESTS();
}