File: test_image_flags.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 (273 lines) | stat: -rw-r--r-- 7,442 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
/* ----------------------------------------------------------------------
   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 "atom.h"
#include "input.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include <cmath>
#include <cstring>
#include <vector>

// whether to print verbose output (i.e. not capturing LAMMPS screen output).
bool verbose = false;

using LAMMPS_NS::utils::split_words;

namespace LAMMPS_NS {
using ::testing::Eq;

class ImageFlagsTest : public LAMMPSTest {
protected:
    void SetUp() override
    {
        testbinary = "ImageFlagsTest";
        LAMMPSTest::SetUp();
        ASSERT_NE(lmp, nullptr);
        BEGIN_HIDE_OUTPUT();
        command("units real");
        command("dimension 3");
        command("region box block -2 2 -2 2 -2 2");
        command("create_box 1 box");
        command("create_atoms 1 single  0.0  0.0 0.0    units box");
        command("create_atoms 1 single  1.9 -1.9 1.9999 units box");
        command("pair_style zero 2.0");
        command("pair_coeff * *");
        command("mass * 1.0");
        command("set atom 1 image -1 2 3");
        command("set atom 2 image -2 1 -1");
        command("write_data test_image_flags.data");
        END_HIDE_OUTPUT();
    }

    void TearDown() override
    {
        LAMMPSTest::TearDown();
        remove("test_image_flags.data");
    }
};

TEST_F(ImageFlagsTest, change_box)
{
    auto *image = lmp->atom->image;
    int imx     = (image[0] & IMGMASK) - IMGMAX;
    int imy     = (image[0] >> IMGBITS & IMGMASK) - IMGMAX;
    int imz     = (image[0] >> IMG2BITS) - IMGMAX;

    ASSERT_EQ(imx, -1);
    ASSERT_EQ(imy, 2);
    ASSERT_EQ(imz, 3);

    imx = (image[1] & IMGMASK) - IMGMAX;
    imy = (image[1] >> IMGBITS & IMGMASK) - IMGMAX;
    imz = (image[1] >> IMG2BITS) - IMGMAX;

    ASSERT_EQ(imx, -2);
    ASSERT_EQ(imy, 1);
    ASSERT_EQ(imz, -1);

    BEGIN_HIDE_OUTPUT();
    command("change_box all boundary f p p");
    END_HIDE_OUTPUT();

    image = lmp->atom->image;
    imx   = (image[0] & IMGMASK) - IMGMAX;
    imy   = (image[0] >> IMGBITS & IMGMASK) - IMGMAX;
    imz   = (image[0] >> IMG2BITS) - IMGMAX;

    ASSERT_EQ(imx, 0);
    ASSERT_EQ(imy, 2);
    ASSERT_EQ(imz, 3);

    imx = (image[1] & IMGMASK) - IMGMAX;
    imy = (image[1] >> IMGBITS & IMGMASK) - IMGMAX;
    imz = (image[1] >> IMG2BITS) - IMGMAX;

    ASSERT_EQ(imx, 0);
    ASSERT_EQ(imy, 1);
    ASSERT_EQ(imz, -1);

    BEGIN_HIDE_OUTPUT();
    command("change_box all boundary f s p");
    END_HIDE_OUTPUT();

    image = lmp->atom->image;
    imx   = (image[0] & IMGMASK) - IMGMAX;
    imy   = (image[0] >> IMGBITS & IMGMASK) - IMGMAX;
    imz   = (image[0] >> IMG2BITS) - IMGMAX;

    ASSERT_EQ(imx, 0);
    ASSERT_EQ(imy, 0);
    ASSERT_EQ(imz, 3);

    imx = (image[1] & IMGMASK) - IMGMAX;
    imy = (image[1] >> IMGBITS & IMGMASK) - IMGMAX;
    imz = (image[1] >> IMG2BITS) - IMGMAX;

    ASSERT_EQ(imx, 0);
    ASSERT_EQ(imy, 0);
    ASSERT_EQ(imz, -1);

    BEGIN_HIDE_OUTPUT();
    command("change_box all boundary p p m");
    END_HIDE_OUTPUT();

    image = lmp->atom->image;
    imx   = (image[0] & IMGMASK) - IMGMAX;
    imy   = (image[0] >> IMGBITS & IMGMASK) - IMGMAX;
    imz   = (image[0] >> IMG2BITS) - IMGMAX;

    ASSERT_EQ(imx, 0);
    ASSERT_EQ(imy, 0);
    ASSERT_EQ(imz, 0);

    imx = (image[1] & IMGMASK) - IMGMAX;
    imy = (image[1] >> IMGBITS & IMGMASK) - IMGMAX;
    imz = (image[1] >> IMG2BITS) - IMGMAX;

    ASSERT_EQ(imx, 0);
    ASSERT_EQ(imy, 0);
    ASSERT_EQ(imz, 0);
}

TEST_F(ImageFlagsTest, read_data)
{
    BEGIN_HIDE_OUTPUT();
    command("clear");
    command("units real");
    command("dimension 3");
    command("boundary p p p");
    command("pair_style zero 2.0");
    command("read_data test_image_flags.data");
    END_HIDE_OUTPUT();

    auto *image = lmp->atom->image;
    int imx     = (image[0] & IMGMASK) - IMGMAX;
    int imy     = (image[0] >> IMGBITS & IMGMASK) - IMGMAX;
    int imz     = (image[0] >> IMG2BITS) - IMGMAX;

    ASSERT_EQ(imx, -1);
    ASSERT_EQ(imy, 2);
    ASSERT_EQ(imz, 3);

    imx = (image[1] & IMGMASK) - IMGMAX;
    imy = (image[1] >> IMGBITS & IMGMASK) - IMGMAX;
    imz = (image[1] >> IMG2BITS) - IMGMAX;

    ASSERT_EQ(imx, -2);
    ASSERT_EQ(imy, 1);
    ASSERT_EQ(imz, -1);

    BEGIN_HIDE_OUTPUT();
    command("clear");
    command("units real");
    command("dimension 3");
    command("boundary f p p");
    command("pair_style zero 2.0");
    command("read_data test_image_flags.data");
    END_HIDE_OUTPUT();

    image = lmp->atom->image;
    imx   = (image[0] & IMGMASK) - IMGMAX;
    imy   = (image[0] >> IMGBITS & IMGMASK) - IMGMAX;
    imz   = (image[0] >> IMG2BITS) - IMGMAX;

    ASSERT_EQ(imx, 0);
    ASSERT_EQ(imy, 2);
    ASSERT_EQ(imz, 3);

    imx = (image[1] & IMGMASK) - IMGMAX;
    imy = (image[1] >> IMGBITS & IMGMASK) - IMGMAX;
    imz = (image[1] >> IMG2BITS) - IMGMAX;

    ASSERT_EQ(imx, 0);
    ASSERT_EQ(imy, 1);
    ASSERT_EQ(imz, -1);

    BEGIN_HIDE_OUTPUT();
    command("clear");
    command("units real");
    command("dimension 3");
    command("boundary p s p");
    command("pair_style zero 2.0");
    command("read_data test_image_flags.data");
    END_HIDE_OUTPUT();

    image = lmp->atom->image;
    imx   = (image[0] & IMGMASK) - IMGMAX;
    imy   = (image[0] >> IMGBITS & IMGMASK) - IMGMAX;
    imz   = (image[0] >> IMG2BITS) - IMGMAX;

    ASSERT_EQ(imx, -1);
    ASSERT_EQ(imy, 0);
    ASSERT_EQ(imz, 3);

    imx = (image[1] & IMGMASK) - IMGMAX;
    imy = (image[1] >> IMGBITS & IMGMASK) - IMGMAX;
    imz = (image[1] >> IMG2BITS) - IMGMAX;

    ASSERT_EQ(imx, -2);
    ASSERT_EQ(imy, 0);
    ASSERT_EQ(imz, -1);

    BEGIN_HIDE_OUTPUT();
    command("clear");
    command("units real");
    command("dimension 3");
    command("boundary p p m");
    command("pair_style zero 2.0");
    command("read_data test_image_flags.data");
    END_HIDE_OUTPUT();

    image = lmp->atom->image;
    imx   = (image[0] & IMGMASK) - IMGMAX;
    imy   = (image[0] >> IMGBITS & IMGMASK) - IMGMAX;
    imz   = (image[0] >> IMG2BITS) - IMGMAX;

    ASSERT_EQ(imx, -1);
    ASSERT_EQ(imy, 2);
    ASSERT_EQ(imz, 0);

    imx = (image[1] & IMGMASK) - IMGMAX;
    imy = (image[1] >> IMGBITS & IMGMASK) - IMGMAX;
    imz = (image[1] >> IMG2BITS) - IMGMAX;

    ASSERT_EQ(imx, -2);
    ASSERT_EQ(imy, 1);
    ASSERT_EQ(imz, 0);
}

} // 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 = 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;
}