File: test_library_scatter_gather.cpp

package info (click to toggle)
lammps 20220106.git7586adbb6a%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 348,064 kB
  • sloc: cpp: 831,421; python: 24,896; xml: 14,949; f90: 10,845; ansic: 7,967; sh: 4,226; perl: 4,064; fortran: 2,424; makefile: 1,501; objc: 238; lisp: 163; csh: 16; awk: 14; tcl: 6
file content (133 lines) | stat: -rw-r--r-- 4,507 bytes parent folder | download
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
131
132
133
// unit tests for testing scatter/gather operations through the library interface

#include "lammps.h"
#include "library.h"
#include "lmptype.h"
#include <string>

#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "test_main.h"

#define STRINGIFY(val) XSTR(val)
#define XSTR(val) #val

using ::LAMMPS_NS::bigint;
using ::LAMMPS_NS::tagint;
using ::testing::HasSubstr;
using ::testing::StartsWith;

class GatherProperties : public ::testing::Test {
protected:
    void *lmp;
    std::string INPUT_DIR = STRINGIFY(TEST_INPUT_FOLDER);

    GatherProperties(){};
    ~GatherProperties() override{};

    void SetUp() override
    {
        const char *args[] = {"LAMMPS_test", "-log",      "none",
                              "-echo",       "screen",    "-nocite",
                              "-var",        "input_dir", STRINGIFY(TEST_INPUT_FOLDER)};

        char **argv = (char **)args;
        int argc    = sizeof(args) / sizeof(char *);

        ::testing::internal::CaptureStdout();
        lmp                = lammps_open_no_mpi(argc, argv, NULL);
        std::string output = ::testing::internal::GetCapturedStdout();
        if (verbose) std::cout << output;
        EXPECT_THAT(output, StartsWith("LAMMPS ("));
    }
    void TearDown() override
    {
        ::testing::internal::CaptureStdout();
        lammps_close(lmp);
        std::string output = ::testing::internal::GetCapturedStdout();
        EXPECT_THAT(output, HasSubstr("Total wall time:"));
        if (verbose) std::cout << output;
        lmp = nullptr;
    }
};

TEST_F(GatherProperties, gather_bonds_newton_on)
{
    if (!lammps_has_style(lmp, "atom", "full")) GTEST_SKIP();
    std::string input = INPUT_DIR + PATH_SEP + "in.fourmol";
    if (!verbose) ::testing::internal::CaptureStdout();
    lammps_command(lmp, "newton on on");
    lammps_file(lmp, input.c_str());
    if (!verbose) ::testing::internal::GetCapturedStdout();

    bigint nbonds = *(bigint *)lammps_extract_global(lmp, "nbonds");
    EXPECT_EQ(nbonds, 24);

    tagint *bonds = new tagint[3 * nbonds];
    lammps_gather_bonds(lmp, bonds);

#define CHECK_BOND(idx, type, atom1, atom2)                                 \
    if (((bonds[3 * idx + 1] == atom1) && (bonds[3 * idx + 2] == atom2)) || \
        ((bonds[3 * idx + 1] == atom2) && (bonds[3 * idx + 2] == atom1))) { \
        EXPECT_EQ(bonds[3 * idx], type);                                    \
        ++count;                                                            \
    }

    // check validity of a few bonds by comparing the bond type and counting the matches.
    int count = 0;
    for (bigint i = 0; i < nbonds; ++i) {
        CHECK_BOND(i, 5, 1, 2);
        CHECK_BOND(i, 3, 1, 3);
        CHECK_BOND(i, 2, 3, 4);
        CHECK_BOND(i, 2, 3, 5);
        CHECK_BOND(i, 1, 3, 6);
        CHECK_BOND(i, 3, 6, 8);
        CHECK_BOND(i, 4, 6, 7);
        CHECK_BOND(i, 5, 8, 9);
        CHECK_BOND(i, 5, 27, 28);
        CHECK_BOND(i, 5, 27, 29);
    }
    EXPECT_EQ(count, 10);
    delete[] bonds;
};

TEST_F(GatherProperties, gather_bonds_newton_off)
{
    if (!lammps_has_style(lmp, "atom", "full")) GTEST_SKIP();
    std::string input = INPUT_DIR + PATH_SEP + "in.fourmol";
    if (!verbose) ::testing::internal::CaptureStdout();
    lammps_command(lmp, "newton off off");
    lammps_file(lmp, input.c_str());
    if (!verbose) ::testing::internal::GetCapturedStdout();

    bigint nbonds = *(bigint *)lammps_extract_global(lmp, "nbonds");
    EXPECT_EQ(nbonds, 24);

    tagint *bonds = new tagint[3 * nbonds];
    lammps_gather_bonds(lmp, bonds);

#define CHECK_BOND(idx, type, atom1, atom2)                                 \
    if (((bonds[3 * idx + 1] == atom1) && (bonds[3 * idx + 2] == atom2)) || \
        ((bonds[3 * idx + 1] == atom2) && (bonds[3 * idx + 2] == atom1))) { \
        EXPECT_EQ(bonds[3 * idx], type);                                    \
        ++count;                                                            \
    }

    // check validity of a few bonds by comparing the bond type and counting the matches.
    int count = 0;
    for (bigint i = 0; i < nbonds; ++i) {
        CHECK_BOND(i, 5, 1, 2);
        CHECK_BOND(i, 3, 1, 3);
        CHECK_BOND(i, 2, 3, 4);
        CHECK_BOND(i, 2, 3, 5);
        CHECK_BOND(i, 1, 3, 6);
        CHECK_BOND(i, 3, 6, 8);
        CHECK_BOND(i, 4, 6, 7);
        CHECK_BOND(i, 5, 8, 9);
        CHECK_BOND(i, 5, 27, 28);
        CHECK_BOND(i, 5, 27, 29);
    }
    EXPECT_EQ(count, 10);
    delete[] bonds;
};