File: test_lammps_class.cpp

package info (click to toggle)
lammps 20250204%2Bdfsg.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 474,368 kB
  • sloc: cpp: 1,060,070; python: 27,785; ansic: 8,956; f90: 7,254; sh: 6,044; perl: 4,171; fortran: 2,442; xml: 1,714; makefile: 1,352; objc: 238; lisp: 188; yacc: 58; csh: 16; awk: 14; tcl: 6; javascript: 2
file content (383 lines) | stat: -rw-r--r-- 12,663 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// unit tests for the LAMMPS base class

#include "comm.h"
#include "info.h"
#include "lammps.h"
#include <cstdio>  // for stdin, stdout
#include <cstdlib> // for setenv
#include <mpi.h>
#include <string>

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

using ::testing::ContainsRegex;
using ::testing::StartsWith;

namespace LAMMPS_NS {
// test fixture for regular tests
class LAMMPS_plain : public ::testing::Test {
protected:
    LAMMPS *lmp;
    LAMMPS_plain() : lmp(nullptr)
    {
        const char *args[] = {"LAMMPS_test", nullptr};
        char **argv        = (char **)args;
        int argc           = 1;

        int flag;
        MPI_Initialized(&flag);
        if (!flag) MPI_Init(&argc, &argv);
    }

    ~LAMMPS_plain() override { lmp = nullptr; }

    void SetUp() override
    {
        LAMMPS::argv args = {"LAMMPS_test", "-log", "none", "-echo", "both", "-nocite"};

        ::testing::internal::CaptureStdout();
        lmp                = new LAMMPS(args, MPI_COMM_WORLD);
        std::string output = ::testing::internal::GetCapturedStdout();
        EXPECT_THAT(output, StartsWith("LAMMPS ("));
    }

    void TearDown() override
    {
        ::testing::internal::CaptureStdout();
        delete lmp;
        std::string output = ::testing::internal::GetCapturedStdout();
        EXPECT_THAT(output, StartsWith("Total wall time:"));
    }
};

TEST_F(LAMMPS_plain, InitMembers)
{
    EXPECT_NE(lmp->memory, nullptr);
    EXPECT_NE(lmp->error, nullptr);
    EXPECT_NE(lmp->universe, nullptr);
    EXPECT_NE(lmp->input, nullptr);

    EXPECT_NE(lmp->atom, nullptr);
    EXPECT_NE(lmp->update, nullptr);
    EXPECT_NE(lmp->neighbor, nullptr);
    EXPECT_NE(lmp->comm, nullptr);
    EXPECT_NE(lmp->domain, nullptr);
    EXPECT_NE(lmp->force, nullptr);
    EXPECT_NE(lmp->modify, nullptr);
    EXPECT_NE(lmp->group, nullptr);
    EXPECT_NE(lmp->output, nullptr);
    EXPECT_NE(lmp->timer, nullptr);

    EXPECT_EQ(lmp->world, MPI_COMM_WORLD);
    EXPECT_EQ(lmp->infile, stdin);
    EXPECT_EQ(lmp->screen, stdout);
    EXPECT_EQ(lmp->logfile, nullptr);
    EXPECT_GE(lmp->initclock, 0.0);

    EXPECT_EQ(lmp->suffix_enable, 0);
    EXPECT_EQ(lmp->suffix, nullptr);
    EXPECT_EQ(lmp->suffix2, nullptr);

    EXPECT_STREQ(lmp->exename, "LAMMPS_test");
    EXPECT_EQ(lmp->num_package, 0);

    EXPECT_EQ(lmp->mdicomm, nullptr);
    EXPECT_EQ(lmp->kokkos, nullptr);
    EXPECT_EQ(lmp->atomKK, nullptr);
    EXPECT_EQ(lmp->memoryKK, nullptr);
    EXPECT_NE(lmp->python, nullptr);
    EXPECT_EQ(lmp->citeme, nullptr);
    if (LAMMPS::has_git_info()) {
        EXPECT_STRNE(LAMMPS::git_commit(), "");
        EXPECT_STRNE(LAMMPS::git_branch(), "");
        EXPECT_STRNE(LAMMPS::git_descriptor(), "");
    } else {
        EXPECT_STREQ(LAMMPS::git_commit(), "(unknown)");
        EXPECT_STREQ(LAMMPS::git_branch(), "(unknown)");
        EXPECT_STREQ(LAMMPS::git_descriptor(), "(unknown)");
    }
}

TEST_F(LAMMPS_plain, TestStyles)
{
    // skip tests if base class is not available
    if (lmp == nullptr) return;
    const char *found;

    const char *atom_styles[] = {"atomic", "body",   "charge", "ellipsoid", "hybrid",
                                 "line",   "sphere", "tri",    nullptr};
    for (int i = 0; atom_styles[i] != nullptr; ++i) {
        found = lmp->match_style("atom", atom_styles[i]);
        EXPECT_STREQ(found, nullptr);
    }

    const char *molecule_atom_styles[] = {"angle",     "bond",     "full",
                                          "molecular", "template", nullptr};
    for (int i = 0; molecule_atom_styles[i] != nullptr; ++i) {
        found = lmp->match_style("atom", molecule_atom_styles[i]);
        EXPECT_STREQ(found, "MOLECULE");
    }

    const char *kokkos_atom_styles[] = {"angle/kk",     "bond/kk",   "full/kk",
                                        "molecular/kk", "hybrid/kk", nullptr};
    for (int i = 0; kokkos_atom_styles[i] != nullptr; ++i) {
        found = lmp->match_style("atom", kokkos_atom_styles[i]);
        EXPECT_STREQ(found, "KOKKOS");
    }
    found = lmp->match_style("atom", "dipole");
    EXPECT_STREQ(found, "DIPOLE");
    found = lmp->match_style("atom", "peri");
    EXPECT_STREQ(found, "PERI");
    found = lmp->match_style("atom", "spin");
    EXPECT_STREQ(found, "SPIN");
    found = lmp->match_style("atom", "wavepacket");
    EXPECT_STREQ(found, "AWPMD");
    found = lmp->match_style("atom", "dpd");
    EXPECT_STREQ(found, "DPD-REACT");
    found = lmp->match_style("atom", "edpd");
    EXPECT_STREQ(found, "DPD-MESO");
    found = lmp->match_style("atom", "mdpd");
    EXPECT_STREQ(found, "DPD-MESO");
    found = lmp->match_style("atom", "tdpd");
    EXPECT_STREQ(found, "DPD-MESO");
    found = lmp->match_style("atom", "spin");
    EXPECT_STREQ(found, "SPIN");
    found = lmp->match_style("atom", "smd");
    EXPECT_STREQ(found, "MACHDYN");
    found = lmp->match_style("atom", "sph");
    EXPECT_STREQ(found, "SPH");
    found = lmp->match_style("atom", "i_don't_exist");
    EXPECT_STREQ(found, nullptr);
}

// test fixture for OpenMP with 2 threads
class LAMMPS_omp : public ::testing::Test {
protected:
    LAMMPS *lmp;
    LAMMPS_omp() : lmp(nullptr)
    {
        const char *args[] = {"LAMMPS_test", nullptr};
        char **argv        = (char **)args;
        int argc           = 1;

        int flag;
        MPI_Initialized(&flag);
        if (!flag) MPI_Init(&argc, &argv);
    }

    ~LAMMPS_omp() override { lmp = nullptr; }

    void SetUp() override
    {
        LAMMPS::argv args = {"LAMMPS_test", "-log", "none", "-screen", "none", "-echo", "screen",
                             "-pk",         "omp",  "2",    "neigh",   "yes",  "-sf",   "omp"};

        // only run this test fixture with omp suffix if OPENMP package is installed

        if (LAMMPS::is_installed_pkg("OPENMP"))
            lmp = new LAMMPS(args, MPI_COMM_WORLD);
        else
            GTEST_SKIP();
    }

    void TearDown() override { delete lmp; }
};

TEST_F(LAMMPS_omp, InitMembers)
{
    EXPECT_NE(lmp->memory, nullptr);
    EXPECT_NE(lmp->error, nullptr);
    EXPECT_NE(lmp->universe, nullptr);
    EXPECT_NE(lmp->input, nullptr);

    EXPECT_NE(lmp->atom, nullptr);
    EXPECT_NE(lmp->update, nullptr);
    EXPECT_NE(lmp->neighbor, nullptr);
    EXPECT_NE(lmp->comm, nullptr);
    EXPECT_NE(lmp->domain, nullptr);
    EXPECT_NE(lmp->force, nullptr);
    EXPECT_NE(lmp->modify, nullptr);
    EXPECT_NE(lmp->group, nullptr);
    EXPECT_NE(lmp->output, nullptr);
    EXPECT_NE(lmp->timer, nullptr);

    EXPECT_EQ(lmp->world, MPI_COMM_WORLD);
    EXPECT_EQ(lmp->infile, stdin);
    EXPECT_EQ(lmp->screen, nullptr);
    EXPECT_EQ(lmp->logfile, nullptr);
    EXPECT_GE(lmp->initclock, 0.0);

    EXPECT_EQ(lmp->suffix_enable, 1);
    EXPECT_STREQ(lmp->suffix, "omp");
    EXPECT_EQ(lmp->suffix2, nullptr);

    EXPECT_STREQ(lmp->exename, "LAMMPS_test");
    EXPECT_EQ(lmp->num_package, 1);

    EXPECT_EQ(lmp->mdicomm, nullptr);
    EXPECT_EQ(lmp->kokkos, nullptr);
    EXPECT_EQ(lmp->atomKK, nullptr);
    EXPECT_EQ(lmp->memoryKK, nullptr);
    EXPECT_NE(lmp->python, nullptr);
    EXPECT_NE(lmp->citeme, nullptr);
    if (LAMMPS::has_git_info()) {
        EXPECT_STRNE(LAMMPS::git_commit(), "");
        EXPECT_STRNE(LAMMPS::git_branch(), "");
        EXPECT_STRNE(LAMMPS::git_descriptor(), "");
    } else {
        EXPECT_STREQ(LAMMPS::git_commit(), "(unknown)");
        EXPECT_STREQ(LAMMPS::git_branch(), "(unknown)");
        EXPECT_STREQ(LAMMPS::git_descriptor(), "(unknown)");
    }
}

// test fixture for Kokkos using 2 threads if threading is available
class LAMMPS_kokkos : public ::testing::Test {
protected:
    LAMMPS *lmp;
    LAMMPS_kokkos() : lmp(nullptr)
    {
        const char *args[] = {"LAMMPS_test", nullptr};
        char **argv        = (char **)args;
        int argc           = 1;

        int flag;
        MPI_Initialized(&flag);
        if (!flag) MPI_Init(&argc, &argv);
    }

    ~LAMMPS_kokkos() override { lmp = nullptr; }

    void SetUp() override
    {
        LAMMPS::argv args = {"LAMMPS_test", "-log", "none", "-echo", "none", "-screen", "none",
                             "-k",          "on",   "t",    "1",     "-sf",  "kk"};

        // when GPU support is enabled in KOKKOS, it *must* be used
        if (Info::has_accelerator_feature("KOKKOS", "api", "hip") ||
            Info::has_accelerator_feature("KOKKOS", "api", "cuda") ||
            Info::has_accelerator_feature("KOKKOS", "api", "sycl")) {
            args = {"LAMMPS_test", "-log", "none", "-echo", "none", "-screen", "none", "-k",
                    "on",          "t",    "1",    "g",     "1",    "-sf",     "kk"};
        }

        if (Info::has_accelerator_feature("KOKKOS", "api", "openmp")) args[10] = "2";

        if (LAMMPS::is_installed_pkg("KOKKOS")) {
            ::testing::internal::CaptureStdout();
            lmp = new LAMMPS(args, MPI_COMM_WORLD);
            ::testing::internal::GetCapturedStdout();
        } else
            GTEST_SKIP();
    }

    void TearDown() override { delete lmp; }
};

TEST_F(LAMMPS_kokkos, InitMembers)
{
    EXPECT_NE(lmp->memory, nullptr);
    EXPECT_NE(lmp->error, nullptr);
    EXPECT_NE(lmp->universe, nullptr);
    EXPECT_NE(lmp->input, nullptr);

    EXPECT_NE(lmp->atom, nullptr);
    EXPECT_NE(lmp->update, nullptr);
    EXPECT_NE(lmp->neighbor, nullptr);
    EXPECT_NE(lmp->comm, nullptr);
    EXPECT_NE(lmp->domain, nullptr);
    EXPECT_NE(lmp->force, nullptr);
    EXPECT_NE(lmp->modify, nullptr);
    EXPECT_NE(lmp->group, nullptr);
    EXPECT_NE(lmp->output, nullptr);
    EXPECT_NE(lmp->timer, nullptr);

    EXPECT_EQ(lmp->world, MPI_COMM_WORLD);
    EXPECT_EQ(lmp->infile, stdin);
    EXPECT_EQ(lmp->screen, nullptr);
    EXPECT_EQ(lmp->logfile, nullptr);
    EXPECT_GE(lmp->initclock, 0.0);

    EXPECT_EQ(lmp->suffix_enable, 1);
    EXPECT_STREQ(lmp->suffix, "kk");
    EXPECT_EQ(lmp->suffix2, nullptr);

    EXPECT_STREQ(lmp->exename, "LAMMPS_test");
    EXPECT_EQ(lmp->num_package, 0);

    if (Info::has_accelerator_feature("KOKKOS", "api", "openmp"))
        EXPECT_EQ(lmp->comm->nthreads, 2);
    else
        EXPECT_EQ(lmp->comm->nthreads, 1);
    EXPECT_EQ(lmp->mdicomm, nullptr);
    EXPECT_NE(lmp->kokkos, nullptr);
    EXPECT_NE(lmp->atomKK, nullptr);
    EXPECT_NE(lmp->memoryKK, nullptr);
    EXPECT_NE(lmp->python, nullptr);
    EXPECT_NE(lmp->citeme, nullptr);
    if (LAMMPS::has_git_info()) {
        EXPECT_STRNE(LAMMPS::git_commit(), "");
        EXPECT_STRNE(LAMMPS::git_branch(), "");
        EXPECT_STRNE(LAMMPS::git_descriptor(), "");
    } else {
        EXPECT_STREQ(LAMMPS::git_commit(), "(unknown)");
        EXPECT_STREQ(LAMMPS::git_branch(), "(unknown)");
        EXPECT_STREQ(LAMMPS::git_descriptor(), "(unknown)");
    }
}

TEST(LAMMPS_init, OpenMP)
{
    if (!LAMMPS::is_installed_pkg("OPENMP")) GTEST_SKIP();
    if (platform::openmp_standard() == "OpenMP not enabled") GTEST_SKIP();

    FILE *fp = fopen("in.lammps_empty", "w");
    fputs("\n", fp);
    fclose(fp);

    LAMMPS::argv args = {"LAMMPS_init", "-in", "in.lammps_empty", "-log", "none", "-nocite"};

    ::testing::internal::CaptureStdout();
    auto *lmp          = new LAMMPS(args, MPI_COMM_WORLD);
    std::string output = ::testing::internal::GetCapturedStdout();
    EXPECT_THAT(output, ContainsRegex(".*using 2 OpenMP thread.*per MPI task.*"));

    if (LAMMPS_NS::Info::has_accelerator_feature("OPENMP", "api", "openmp"))
        EXPECT_EQ(lmp->comm->nthreads, 2);
    else
        EXPECT_EQ(lmp->comm->nthreads, 1);
    ::testing::internal::CaptureStdout();
    delete lmp;
    ::testing::internal::GetCapturedStdout();

    remove("in.lammps_empty");
}

// check no OMP_NUM_THREADS warning message printing. this must be the
// last OpenMP related test as threads will be locked to 1 from here on.

TEST(LAMMPS_init, NoOpenMP)
{
    if (!LAMMPS_NS::Info::has_accelerator_feature("OPENMP", "api", "openmp"))
        GTEST_SKIP() << "No threading enabled";

    FILE *fp = fopen("in.lammps_class_noomp", "w");
    fputs("\n", fp);
    fclose(fp);
    platform::unsetenv("OMP_NUM_THREADS");

    LAMMPS::argv args = {"LAMMPS_init", "-in", "in.lammps_class_noomp", "-log", "none", "-nocite"};

    ::testing::internal::CaptureStdout();
    auto *lmp          = new LAMMPS(args, MPI_COMM_WORLD);
    std::string output = ::testing::internal::GetCapturedStdout();
    EXPECT_THAT(output, ContainsRegex(
                            ".*OMP_NUM_THREADS environment is not set.*Defaulting to 1 thread.*"));
    EXPECT_EQ(lmp->comm->nthreads, 1);
    ::testing::internal::CaptureStdout();
    delete lmp;
    ::testing::internal::GetCapturedStdout();
}

} // namespace LAMMPS_NS