File: wrap_extract_fix.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 (107 lines) | stat: -rw-r--r-- 3,864 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
// unit tests for extracting compute data from a LAMMPS instance through the
// Fortran wrapper
#include <cstdio>

#include "lammps.h"
#include "library.h"
#include <cstdint>
#include <cstdlib>
#include <mpi.h>
#include <string>

#include "gtest/gtest.h"

// prototypes for Fortran reverse wrapper functions
extern "C" {
void *f_lammps_with_args();
void f_lammps_close();
void f_lammps_setup_extract_fix();
double f_lammps_extract_fix_global_scalar();
double f_lammps_extract_fix_global_vector(int);
double f_lammps_extract_fix_global_array(int, int);
double f_lammps_extract_fix_peratom_vector(int);
double f_lammps_extract_fix_peratom_array(int, int);
double f_lammps_extract_fix_local_vector(int);
double f_lammps_extract_fix_local_array(int, int);
}

class LAMMPS_extract_fix : public ::testing::Test {
protected:
    LAMMPS_NS::LAMMPS *lmp;
    LAMMPS_extract_fix()           = default;
    ~LAMMPS_extract_fix() override = default;

    void SetUp() override
    {
        ::testing::internal::CaptureStdout();
        lmp                = (LAMMPS_NS::LAMMPS *)f_lammps_with_args();
        std::string output = ::testing::internal::GetCapturedStdout();
        EXPECT_STREQ(output.substr(0, 8).c_str(), "LAMMPS (");
    }
    void TearDown() override
    {
        ::testing::internal::CaptureStdout();
        f_lammps_close();
        std::string output = ::testing::internal::GetCapturedStdout();
        EXPECT_STREQ(output.substr(0, 16).c_str(), "Total wall time:");
        lmp = nullptr;
    }
};

TEST_F(LAMMPS_extract_fix, global_scalar)
{
    f_lammps_setup_extract_fix();
    auto *scalar =
        (double *)lammps_extract_fix(lmp, "recenter", LMP_STYLE_GLOBAL, LMP_TYPE_SCALAR, -1, -1);
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_global_scalar(), *scalar);
    lammps_free(scalar);
};

TEST_F(LAMMPS_extract_fix, global_vector)
{
    f_lammps_setup_extract_fix();
    auto *x =
        (double *)lammps_extract_fix(lmp, "recenter", LMP_STYLE_GLOBAL, LMP_TYPE_VECTOR, 0, -1);
    auto *y =
        (double *)lammps_extract_fix(lmp, "recenter", LMP_STYLE_GLOBAL, LMP_TYPE_VECTOR, 1, -1);
    auto *z =
        (double *)lammps_extract_fix(lmp, "recenter", LMP_STYLE_GLOBAL, LMP_TYPE_VECTOR, 2, -1);
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_global_vector(1), *x);
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_global_vector(2), *y);
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_global_vector(3), *z);
    lammps_free(x);
    lammps_free(y);
    lammps_free(z);
};

TEST_F(LAMMPS_extract_fix, global_array)
{
    f_lammps_setup_extract_fix();
    double natoms = lammps_get_natoms(lmp);
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_global_array(1, 1), natoms);
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_global_array(1, 2), natoms);
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_global_array(2, 1), 0.0);
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_global_array(2, 2), 1.0);
};

TEST_F(LAMMPS_extract_fix, peratom_vector)
{
    f_lammps_setup_extract_fix();
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_peratom_vector(1), 1.5);
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_peratom_vector(2), 0.1);
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_peratom_vector(3), 0.5);
};

TEST_F(LAMMPS_extract_fix, peratom_array)
{
    f_lammps_setup_extract_fix();
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_peratom_array(1, 1), 1.0);
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_peratom_array(2, 1), 1.0);
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_peratom_array(3, 1), 1.5);
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_peratom_array(1, 2), 0.2);
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_peratom_array(2, 2), 0.1);
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_peratom_array(3, 2), 0.1);
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_peratom_array(1, 3), 0.5);
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_peratom_array(2, 3), 0.5);
    EXPECT_DOUBLE_EQ(f_lammps_extract_fix_peratom_array(3, 3), 0.5);
};