Implemented extract_fix, extract_variable, flush_buffers; unit test for extract_fix
This commit is contained in:
@ -65,6 +65,10 @@ if(CMAKE_Fortran_COMPILER)
|
||||
target_link_libraries(test_fortran_extract_compute PRIVATE flammps lammps MPI::MPI_Fortran GTest::GTestMain)
|
||||
add_test(NAME FortranExtractCompute COMMAND test_fortran_extract_compute)
|
||||
|
||||
add_executable(test_fortran_extract_fix wrap_extract_fix.cpp test_fortran_extract_fix.f90)
|
||||
target_link_libraries(test_fortran_extract_fix PRIVATE flammps lammps MPI::MPI_Fortran GTest::GTestMain)
|
||||
add_test(NAME FortranExtractFix COMMAND test_fortran_extract_fix)
|
||||
|
||||
else()
|
||||
message(STATUS "Skipping Tests for the LAMMPS Fortran Module: no Fortran compiler")
|
||||
endif()
|
||||
|
||||
118
unittest/fortran/test_fortran_extract_fix.f90
Normal file
118
unittest/fortran/test_fortran_extract_fix.f90
Normal file
@ -0,0 +1,118 @@
|
||||
MODULE keepfix
|
||||
USE liblammps
|
||||
TYPE(LAMMPS) :: lmp
|
||||
CHARACTER(LEN=40), DIMENSION(3), PARAMETER :: demo_input = &
|
||||
[ CHARACTER(LEN=40) :: &
|
||||
'region box block 0 $x 0 3 0 4', &
|
||||
'create_box 1 box', &
|
||||
'create_atoms 1 single 1.0 1.0 ${zpos}' ]
|
||||
CHARACTER(LEN=40), DIMENSION(3), PARAMETER :: cont_input = &
|
||||
[ CHARACTER(LEN=40) :: &
|
||||
'create_atoms 1 single &', &
|
||||
' 0.2 0.1 0.1', &
|
||||
'create_atoms 1 single 0.5 0.5 0.5' ]
|
||||
CHARACTER(LEN=40), DIMENSION(3), PARAMETER :: pair_input = &
|
||||
[ CHARACTER(LEN=40) :: &
|
||||
'pair_style lj/cut 2.5', &
|
||||
'pair_coeff 1 1 1.0 1.0', &
|
||||
'mass 1 2.0' ]
|
||||
END MODULE keepfix
|
||||
|
||||
FUNCTION f_lammps_with_args() BIND(C)
|
||||
USE ISO_C_BINDING, ONLY: C_ptr
|
||||
USE liblammps
|
||||
USE keepfix, ONLY: lmp
|
||||
IMPLICIT NONE
|
||||
TYPE(C_ptr) :: f_lammps_with_args
|
||||
CHARACTER(len=12), DIMENSION(12), PARAMETER :: args = &
|
||||
[ CHARACTER(len=12) :: 'liblammps', '-log', 'none', &
|
||||
'-echo','screen','-nocite','-var','zpos','1.5','-var','x','2']
|
||||
|
||||
lmp = lammps(args)
|
||||
f_lammps_with_args = lmp%handle
|
||||
END FUNCTION f_lammps_with_args
|
||||
|
||||
SUBROUTINE f_lammps_close() BIND(C)
|
||||
USE ISO_C_BINDING, ONLY: c_null_ptr
|
||||
USE liblammps
|
||||
USE keepfix, ONLY: lmp
|
||||
IMPLICIT NONE
|
||||
|
||||
CALL lmp%close()
|
||||
lmp%handle = C_NULL_PTR
|
||||
END SUBROUTINE f_lammps_close
|
||||
|
||||
SUBROUTINE f_lammps_setup_extract_fix () BIND(C)
|
||||
USE LIBLAMMPS
|
||||
USE keepfix, ONLY : lmp, demo_input, cont_input, pair_input
|
||||
IMPLICIT NONE
|
||||
|
||||
CALL lmp%commands_list(demo_input)
|
||||
CALL lmp%commands_list(cont_input)
|
||||
CALL lmp%commands_list(pair_input)
|
||||
CALL lmp%command("fix state all store/state 0 z") ! per-atom vector
|
||||
CALL lmp%command("fix move all move linear 0 0 0") ! for per-atom array
|
||||
CALL lmp%command("fix recenter all recenter 1 1 1") ! global scalar, vector
|
||||
CALL lmp%command("variable natoms equal count(all)")
|
||||
CALL lmp%command("variable ts equal step")
|
||||
CALL lmp%command("fix vec all vector 1 v_natoms v_ts") ! global array
|
||||
CALL lmp%command("run 1") ! must be 1, otherwise move/recenter won't happen
|
||||
END SUBROUTINE f_lammps_setup_extract_fix
|
||||
|
||||
FUNCTION f_lammps_extract_fix_global_scalar () BIND(C) RESULT(scalar)
|
||||
USE, INTRINSIC :: ISO_C_BINDING, ONLY : C_double
|
||||
USE LIBLAMMPS
|
||||
USE keepfix, ONLY : lmp
|
||||
IMPLICIT NONE
|
||||
REAL(C_double) :: scalar
|
||||
|
||||
scalar = lmp%extract_fix("recenter", lmp%style%global, lmp%type%scalar)
|
||||
END FUNCTION f_lammps_extract_fix_global_scalar
|
||||
|
||||
FUNCTION f_lammps_extract_fix_global_vector (i) BIND(C) RESULT(element)
|
||||
USE, INTRINSIC :: ISO_C_BINDING, ONLY : C_double, C_int
|
||||
USE LIBLAMMPS
|
||||
USE keepfix, ONLY : lmp
|
||||
IMPLICIT NONE
|
||||
INTEGER(C_int), INTENT(IN), VALUE :: i
|
||||
REAL(C_double) :: element
|
||||
|
||||
element = lmp%extract_fix("recenter", lmp%style%global, lmp%type%vector, i)
|
||||
END FUNCTION f_lammps_extract_fix_global_vector
|
||||
|
||||
FUNCTION f_lammps_extract_fix_global_array (i,j) BIND(C) RESULT(element)
|
||||
USE, INTRINSIC :: ISO_C_BINDING, ONLY : C_double, C_int
|
||||
USE LIBLAMMPS
|
||||
USE keepfix, ONLY : lmp
|
||||
IMPLICIT NONE
|
||||
INTEGER(C_int), INTENT(IN), VALUE :: i, j
|
||||
REAL(C_double) :: element
|
||||
|
||||
element = lmp%extract_fix("vec", lmp%style%global, lmp%type%array, i, j)
|
||||
END FUNCTION f_lammps_extract_fix_global_array
|
||||
|
||||
FUNCTION f_lammps_extract_fix_peratom_vector (i) BIND(C)
|
||||
USE, INTRINSIC :: ISO_C_BINDING, ONLY : C_double, C_int
|
||||
USE LIBLAMMPS
|
||||
USE keepfix, ONLY : lmp
|
||||
IMPLICIT NONE
|
||||
INTEGER(C_int), INTENT(IN), VALUE :: i
|
||||
REAL(C_double) :: f_lammps_extract_fix_peratom_vector
|
||||
REAL(C_double), DIMENSION(:), POINTER :: vector
|
||||
|
||||
vector = lmp%extract_fix("state", lmp%style%atom, lmp%type%vector, -1, -1)
|
||||
f_lammps_extract_fix_peratom_vector = vector(i)
|
||||
END FUNCTION f_lammps_extract_fix_peratom_vector
|
||||
|
||||
FUNCTION f_lammps_extract_fix_peratom_array (i,j) BIND(C)
|
||||
USE, INTRINSIC :: ISO_C_BINDING, ONLY : C_double, C_int
|
||||
USE LIBLAMMPS
|
||||
USE keepfix, ONLY : lmp
|
||||
IMPLICIT NONE
|
||||
INTEGER(C_int), INTENT(IN), VALUE :: i, j
|
||||
REAL(C_double) :: f_lammps_extract_fix_peratom_array
|
||||
REAL(C_double), DIMENSION(:,:), POINTER :: array
|
||||
|
||||
array = lmp%extract_fix("move", lmp%style%atom, lmp%type%array, -1, -1)
|
||||
f_lammps_extract_fix_peratom_array = array(i,j)
|
||||
END FUNCTION f_lammps_extract_fix_peratom_array
|
||||
107
unittest/fortran/wrap_extract_fix.cpp
Normal file
107
unittest/fortran/wrap_extract_fix.cpp
Normal file
@ -0,0 +1,107 @@
|
||||
// unit tests for extracting compute data from a LAMMPS instance through the
|
||||
// Fortran wrapper
|
||||
#include <cstdio>
|
||||
|
||||
#include "lammps.h"
|
||||
#include "library.h"
|
||||
#include <mpi.h>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
|
||||
#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();
|
||||
double *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();
|
||||
double *x = (double*) lammps_extract_fix(lmp, "recenter",
|
||||
LMP_STYLE_GLOBAL, LMP_TYPE_VECTOR, 0, -1);
|
||||
double *y = (double*) lammps_extract_fix(lmp, "recenter",
|
||||
LMP_STYLE_GLOBAL, LMP_TYPE_VECTOR, 1, -1);
|
||||
double *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);
|
||||
};
|
||||
Reference in New Issue
Block a user