File: test_dump_cfg.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 (169 lines) | stat: -rw-r--r-- 5,779 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
/* ----------------------------------------------------------------------
   LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
   https://www.lammps.org/, Sandia National Laboratories
   LAMMPS Development team: developers@lammps.org

   Copyright (2003) Sandia Corporation.  Under the terms of Contract
   DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
   certain rights in this software.  This software is distributed under
   the GNU General Public License.

   See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */

#include "../testing/core.h"
#include "../testing/systems/melt.h"
#include "../testing/utils.h"
#include "fmt/format.h"
#include "utils.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using ::testing::Eq;

bool verbose = false;

namespace LAMMPS_NS {
class DumpCfgTest : public MeltTest {
    std::string dump_style = "cfg";

public:
    void generate_dump(const std::string &dump_file, const std::string &fields,
                       const std::string &dump_modify_options, int ntimesteps)
    {
        BEGIN_HIDE_OUTPUT();
        command(fmt::format("dump id all {} 1 {} {}", dump_style, dump_file, fields));

        if (!dump_modify_options.empty()) {
            command(fmt::format("dump_modify id {}", dump_modify_options));
        }

        command(fmt::format("run {}", ntimesteps));
        END_HIDE_OUTPUT();
    }
};

TEST_F(DumpCfgTest, invalid_options)
{
    TEST_FAILURE(".*Dump cfg arguments must start with 'mass type xs ys zs'.*",
                 command("dump id all cfg 1 dump.cfg id type proc procp1 mass x y z"););
}

TEST_F(DumpCfgTest, require_multifile)
{
    const auto *dump_file = "dump.melt.cfg_run.cfg";
    const auto *fields =
        "mass type xs ys zs id proc procp1 x y z ix iy iz xu yu zu xsu ysu zsu vx vy vz fx fy fz";

    BEGIN_HIDE_OUTPUT();
    command(fmt::format("dump id all cfg 1 {} {}", dump_file, fields));
    END_HIDE_OUTPUT();

    TEST_FAILURE(".*Dump cfg requires one snapshot per file.*", command("run 0"););
}

TEST_F(DumpCfgTest, run0)
{
    const auto *dump_file = "dump_cfg_run*.melt.cfg";
    const auto *fields    = "mass type xs ys zs id proc procp1 x y z ix iy iz vx vy vz fx fy fz";

    generate_dump(dump_file, fields, "", 0);

    ASSERT_FILE_EXISTS("dump_cfg_run0.melt.cfg");
    auto lines = read_lines("dump_cfg_run0.melt.cfg");
    ASSERT_EQ(lines.size(), 124);
    ASSERT_THAT(lines[0], Eq("Number of particles = 32"));
    delete_file("dump_cfg_run0.melt.cfg");
}

TEST_F(DumpCfgTest, write_dump)
{
    const auto *dump_file = "dump_cfg_run*.melt.cfg";
    const auto *fields    = "mass type xs ys zs id proc procp1 x y z ix iy iz vx vy vz fx fy fz";

    BEGIN_HIDE_OUTPUT();
    command("run 0 post no");
    command(std::string("write_dump all cfg dump_cfg.melt.cfg ") + fields);
    command(std::string("write_dump all cfg dump_cfg*.melt.cfg ") + fields);
    END_HIDE_OUTPUT();

    ASSERT_FILE_EXISTS("dump_cfg.melt.cfg");
    auto lines = read_lines("dump_cfg.melt.cfg");
    ASSERT_EQ(lines.size(), 124);
    ASSERT_THAT(lines[0], Eq("Number of particles = 32"));
    delete_file("dump_cfg.melt.cfg");

    ASSERT_FILE_EXISTS("dump_cfg0.melt.cfg");
    lines = read_lines("dump_cfg0.melt.cfg");
    ASSERT_EQ(lines.size(), 124);
    ASSERT_THAT(lines[0], Eq("Number of particles = 32"));
    delete_file("dump_cfg0.melt.cfg");

    TEST_FAILURE(".*ERROR: Unrecognized dump style 'xxx'.*",
                 command("write_dump all xxx test.xxx"););
}

TEST_F(DumpCfgTest, unwrap_run0)
{
    const auto *dump_file = "dump_cfg_unwrap_run*.melt.cfg";
    const auto *fields    = "mass type xsu ysu zsu id proc procp1 x y z ix iy iz vx vy vz fx fy fz";

    generate_dump(dump_file, fields, "", 0);

    ASSERT_FILE_EXISTS("dump_cfg_unwrap_run0.melt.cfg");
    auto lines = read_lines("dump_cfg_unwrap_run0.melt.cfg");
    ASSERT_EQ(lines.size(), 124);
    ASSERT_THAT(lines[0], Eq("Number of particles = 32"));
    delete_file("dump_cfg_unwrap_run0.melt.cfg");
}

TEST_F(DumpCfgTest, no_buffer_run0)
{
    const auto *dump_file = "dump_cfg_no_buffer_run*.melt.cfg";
    const auto *fields    = "mass type xsu ysu zsu id proc procp1 x y z ix iy iz vx vy vz fx fy fz";

    generate_dump(dump_file, fields, "buffer no", 0);

    ASSERT_FILE_EXISTS("dump_cfg_no_buffer_run0.melt.cfg");
    auto lines = read_lines("dump_cfg_no_buffer_run0.melt.cfg");
    ASSERT_EQ(lines.size(), 124);
    ASSERT_THAT(lines[0], Eq("Number of particles = 32"));
    delete_file("dump_cfg_no_buffer_run0.melt.cfg");
}

TEST_F(DumpCfgTest, no_unwrap_no_buffer_run0)
{
    const auto *dump_file = "dump_cfg_no_unwrap_no_buffer_run*.melt.cfg";
    const auto *fields    = "mass type xs ys zs id proc procp1 x y z ix iy iz vx vy vz fx fy fz";

    generate_dump(dump_file, fields, "buffer no", 0);

    ASSERT_FILE_EXISTS("dump_cfg_no_unwrap_no_buffer_run0.melt.cfg");
    auto lines = read_lines("dump_cfg_no_unwrap_no_buffer_run0.melt.cfg");
    ASSERT_EQ(lines.size(), 124);
    ASSERT_THAT(lines[0], Eq("Number of particles = 32"));
    delete_file("dump_cfg_no_unwrap_no_buffer_run0.melt.cfg");
}
} // namespace LAMMPS_NS

int main(int argc, char **argv)
{
    MPI_Init(&argc, &argv);
    ::testing::InitGoogleMock(&argc, argv);

    // handle arguments passed via environment variable
    if (const char *var = getenv("TEST_ARGS")) {
        std::vector<std::string> env = LAMMPS_NS::utils::split_words(var);
        for (auto arg : env) {
            if (arg == "-v") {
                verbose = true;
            }
        }
    }

    if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = true;

    int rv = RUN_ALL_TESTS();
    MPI_Finalize();
    return rv;
}