Skip to content

Commit

Permalink
Merge pull request #52 from nirgoldman/test_branch
Browse files Browse the repository at this point in the history
Test branch
  • Loading branch information
RKLindsey authored Aug 3, 2023
2 parents 3fb100f + 5d01dc3 commit 231d01d
Show file tree
Hide file tree
Showing 9 changed files with 480 additions and 10 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ target_link_libraries (chimescalc-test_serial-C ChimesCalc)
target_compile_features (chimescalc-test_serial-C PRIVATE cxx_std_11)
target_compile_definitions(chimescalc-test_serial-C PRIVATE "DEBUG=${DEBUG}")

add_executable(chimescalc-test_serial-C_instance serial_interface/examples/c_instance/main.c)
target_link_libraries (chimescalc-test_serial-C_instance ChimesCalc)
target_compile_features (chimescalc-test_serial-C_instance PRIVATE cxx_std_11)
target_compile_definitions(chimescalc-test_serial-C_instance PRIVATE "DEBUG=${DEBUG}")


####################################################################################################
# Dynamically loadable library (e.g for Python)
Expand Down Expand Up @@ -163,6 +168,9 @@ if(WITH_FORTRAN_API OR WITH_FORTRAN08_API)
add_executable(chimescalc-test_serial-F serial_interface/examples/fortran/main.F90)
target_link_libraries(chimescalc-test_serial-F ChimesCalc_Fortran)
target_compile_definitions(chimescalc-test_serial-F PRIVATE "DEBUG=${DEBUG}")
add_executable(chimescalc-test_serial-F_instance serial_interface/examples/fortran_instance/main.F90)
target_link_libraries(chimescalc-test_serial-F_instance ChimesCalc_Fortran)
target_compile_definitions(chimescalc-test_serial-F_instance PRIVATE "DEBUG=${DEBUG}")

# Optional, as ancient Fortran compilers may have difficulties with it
if(WITH_FORTRAN08_API)
Expand Down Expand Up @@ -210,7 +218,9 @@ enable_testing()
set(apis
"cpp;${CMAKE_CURRENT_BINARY_DIR}/chimescalc"
"c;${CMAKE_CURRENT_BINARY_DIR}/chimescalc-test_serial-C"
"c_instance;${CMAKE_CURRENT_BINARY_DIR}/chimescalc-test_serial-C_instance"
"fortran;${CMAKE_CURRENT_BINARY_DIR}/chimescalc-test_serial-F"
"fortran_instance;${CMAKE_CURRENT_BINARY_DIR}/chimescalc-test_serial-F_instance"
"fortran08;${CMAKE_CURRENT_BINARY_DIR}/chimescalc-test_serial-F08")

set(_testdir "${CMAKE_CURRENT_SOURCE_DIR}/serial_interface/tests")
Expand Down
48 changes: 39 additions & 9 deletions serial_interface/api/chimescalc_serial_C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ using namespace std;
#include "chimescalc_serial_C.h"
static serial_chimes_interface chimes, *chimes_ptr;


void *chimes_open_instance()
{
serial_chimes_interface *new_ptr = nullptr;
new_ptr = new serial_chimes_interface;
return (void *) new_ptr;
}
void chimes_close_instance(void *handle)
{
auto ptr = (serial_chimes_interface *) handle;
delete ptr;
}
void set_chimes_serial(int small=1)
{
if ((small!=0)&&(small!=1))
Expand All @@ -30,17 +40,40 @@ void set_chimes_serial(int small=1)
cout << "Received: " << small << endl;
exit(0);
}
chimes_ptr = &chimes;
chimes_ptr->allow_replication = small;
chimes_ptr = &chimes;
set_chimes_serial_instance(chimes_ptr, small);

}
void set_chimes_serial_instance(void *handle, int small=1)
{
auto new_ptr = (serial_chimes_interface *) handle;
if ((small!=0)&&(small!=1))
{
cout << "ERROR: Small must be set to 0 (false) or 1 (true)" << endl;
cout << "Received: " << small << endl;
exit(0);
}
new_ptr->allow_replication = small;
}

void init_chimes_serial(char *param_file, int *rank)
{
chimes_ptr->init_chimesFF(param_file, *rank);
//chimes_ptr->init_chimesFF(param_file, *rank);
init_chimes_serial_instance(chimes_ptr, param_file, *rank);
}
void init_chimes_serial_instance(void *handle, char *param_file, int rank)
{
auto new_ptr = (serial_chimes_interface *) handle;
//printf("rank = %d\n", *rank);
new_ptr->init_chimesFF(param_file, 0);
}

void calculate_chimes(int natom, double *xc, double *yc, double *zc, char *atom_types[], double ca[3], double cb[3], double cc[3], double *energy, double fx[], double fy[], double fz[], double stress[9])
{
calculate_chimes_instance(chimes_ptr, natom, xc, yc, zc, atom_types, ca, cb, cc, energy, fx, fy, fz, stress);
}
void calculate_chimes_instance(void *handle, int natom, double *xc, double *yc, double *zc, char *atom_types[], double ca[3], double cb[3], double cc[3], double *energy, double fx[], double fy[], double fz[], double stress[9])
{
auto new_ptr = (serial_chimes_interface *) handle;
vector<double> x_vec(natom);
vector<double> y_vec(natom);
vector<double> z_vec(natom);
Expand All @@ -66,9 +99,6 @@ void calculate_chimes(int natom, double *xc, double *yc, double *zc, char *atom_
for (int i = 0; i < 9; i++) {
stress_vec[i] = stress[i];
}
//for (int i = 0; i < 9; i++) {
// stress[i] = stress_vec[i];
//}
vector<double>cell_a_vec(3);
vector<double>cell_b_vec(3);
vector<double>cell_c_vec(3);
Expand All @@ -82,7 +112,7 @@ void calculate_chimes(int natom, double *xc, double *yc, double *zc, char *atom_
cell_c_vec[1] = cc[1];
cell_c_vec[2] = cc[2];

chimes_ptr->calculate(x_vec, y_vec, z_vec, cell_a_vec, cell_b_vec, cell_c_vec, atom_types_vec, *energy, force_vec, stress_vec);
new_ptr->calculate(x_vec, y_vec, z_vec, cell_a_vec, cell_b_vec, cell_c_vec, atom_types_vec, *energy, force_vec, stress_vec);
for (int i = 0; i < natom; i++) {
fx[i] = force_vec[i][0];
fy[i] = force_vec[i][1];
Expand Down
5 changes: 5 additions & 0 deletions serial_interface/api/chimescalc_serial_C.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
extern "C" {
#endif

void *chimes_open_instance();
void chimes_close_instance(void *handle);
void set_chimes_serial(int small);
void set_chimes_serial_instance(void *handle, int small);
void init_chimes_serial(char *param_file, int *rank);
void init_chimes_serial_instance(void *handle, char *param_file, int rank);
void calculate_chimes(int natom, double *xc, double *yc, double *zc, char *atom_types[], double ca[3], double cb[3], double cc[3], double *energy, double fx[], double fy[], double fz[], double stress[9]);
void calculate_chimes_instance(void *handle, int natom, double *xc, double *yc, double *zc, char *atom_types[], double ca[3], double cb[3], double cc[3], double *energy, double fx[], double fy[], double fz[], double stress[9]);
#ifdef __cplusplus
}
#endif
44 changes: 44 additions & 0 deletions serial_interface/api/chimescalc_serial_F.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@ module chimescalc_serial

interface

function f_chimes_open_instance() &
& bind (C, name='chimes_open_instance')
use, intrinsic :: ISO_C_binding, only : C_ptr
type(C_ptr) :: f_chimes_open_instance
end function f_chimes_open_instance

subroutine f_chimes_close_instance(handle) &
& bind (C, name='chimes_close_instance')
use, intrinsic :: ISO_C_binding, only : C_ptr
type(C_ptr), value :: handle
end subroutine f_chimes_close_instance

subroutine f_calculate_chimes_instance(handle, natom, xc, yc, zc, cptr, ca, &
& cb, cc, energy, fx, fy, fz, stress) &
& bind (C, name='calculate_chimes_instance')
use, intrinsic :: ISO_C_binding, only : C_char, C_ptr, C_int, C_double
implicit none
integer(C_int), value :: natom
type(c_ptr), dimension(*) :: cptr(natom)
type(C_ptr), value :: handle
real(C_double) :: xc(natom), yc(natom), zc(natom)
real(C_double) :: fx(natom), fy(natom), fz(natom)
character(C_char), dimension(80) :: c_atom(natom)
real(C_double) :: ca(3), cb(3), cc(3)
real(C_double) :: energy
real(C_double) :: stress(9)
end subroutine f_calculate_chimes_instance

subroutine f_calculate_chimes (natom, xc, yc, zc, cptr, ca, &
& cb, cc, energy, fx, fy, fz, stress) &
& bind (C, name='calculate_chimes')
Expand All @@ -23,13 +51,29 @@ subroutine f_calculate_chimes (natom, xc, yc, zc, cptr, ca, &
real(C_double) :: stress(9)
end subroutine f_calculate_chimes

subroutine f_set_chimes_instance(handle, small) bind &
& (C, name='set_chimes_serial_instance')
use, intrinsic :: ISO_C_binding, only : C_ptr, C_int
implicit none
integer(C_int), value :: small
type(C_ptr), value :: handle
end subroutine f_set_chimes_instance

subroutine f_set_chimes(small) bind &
& (C, name='set_chimes_serial')
use, intrinsic :: ISO_C_binding, only : C_ptr, C_int
implicit none
integer(C_int), value :: small
end subroutine f_set_chimes

subroutine f_init_chimes_instance(handle, param_file, rank) &
& bind (C, name='init_chimes_serial_instance')
use, intrinsic :: ISO_C_binding, only : C_ptr, C_int, C_char
integer(C_int), value :: rank
character (kind=C_char), dimension(*) :: param_file
type(C_ptr), value :: handle
end subroutine f_init_chimes_instance

subroutine f_init_chimes(param_file, rank) &
& bind (C, name='init_chimes_serial')
import C_int, C_char
Expand Down
52 changes: 52 additions & 0 deletions serial_interface/examples/c_instance/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
CXX = g++ -O3 -std=c++11
CCOMP = gcc -O3
DEBUG = 1

C_LOC = $(realpath .)

CHIMESFF_LOC=$(C_LOC)/../../../chimesFF/src
CHIMESFF_SRC=$(CHIMESFF_LOC)/chimesFF.cpp
CHIMESFF_HDR=$(CHIMESFF_LOC)/chimesFF.h

chimesFF.o : $(CHIMESFF_SRC)
$(CXX) -c $(CHIMESFF_SRC) -I $(CHIMESFF_LOC)

SERIAL_LOC=$(C_LOC)/../../src
SERIAL_SRC=$(SERIAL_LOC)/serial_chimes_interface.cpp
SERIAL_HDR=$(SERIAL_LOC)/serial_chimes_interface.h

serial_chimes_interface.o : $(SERIAL_SRC)
$(CXX) -c $(SERIAL_SRC) -I $(SERIAL_LOC) -I $(CHIMESFF_LOC)

WRAPPER_LOC=$(C_LOC)/../../api
WRAPPER_SRC=$(WRAPPER_LOC)/chimescalc_serial_C.cpp
WRAPPER_HDR=$(WRAPPER_LOC)/chimescalc_serial_C.h

chimescalc_serial_C.o : $(WRAPPER_SRC)
$(CXX) -c $(WRAPPER_SRC) -I $(WRAPPER_LOC) -I $(SERIAL_LOC) -I $(CHIMESFF_LOC)

TEST_LOC=$(C_LOC)
TEST_SRC=$(TEST_LOC)/main.c

main.o: $(TEST_SRC)
$(CCOMP) -c $(TEST_SRC) -DDEBUG=${DEBUG} -I $(WRAPPER_LOC) -I $(SERIAL_LOC) -I $(CHIMESFF_LOC)

TEST_LNK = chimesFF.o serial_chimes_interface.o chimescalc_serial_C.o main.o

test-C: $(TEST_LNK)
$(CXX) $(TEST_LNK) -o chimescalc-test_serial-C_instance

clean:
rm -f *.o

clean-all:
make clean
rm -f chimescalc-test_serial-C_instance

all:
make chimesFF.o
make serial_chimes_interface.o
make chimescalc_serial_C.o
make main.o
make test-C
make clean
110 changes: 110 additions & 0 deletions serial_interface/examples/c_instance/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
ChIMES Calculator
Copyright (C) 2020 Rebecca K. Lindsey, Nir Goldman, and Laurence E. Fried
Contributing Author: Nir Goldman (2020)
*/

#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "chimescalc_serial_C.h"

int main (int argc, char **argv)
{
int small = 0; // 0 = false, 1 = true

if ((argc != 4)&&(argc !=3))
{
printf("To run: ./test.x <parameter file> <xyz config. file>\n");
printf("or\n");
printf("./test.x <parameter file> <xyz config. file> <allow_replicates(0/1)>\n");
printf("Exiting code.\n");
exit(0);
}
if(argc==4)
small = atoi(argv[3]);

const double GPa = 6.9479; // convert kcal/mol.A^3 to GPa
FILE *fconf;
int natom, i, j, k, l;
char junk;
double lx, ly, lz;
double ca[3], cb[3], cc[3];
fconf = fopen (argv[2],"r");
fscanf(fconf,"%d\n",&natom);
fscanf(fconf,"%lf %lf %lf %lf %lf %lf %lf %lf %lf\n",&ca[0],&ca[1],&ca[2],&cb[0],&cb[1],&cb[2],&cc[0],&cc[1],&cc[2]);
double vol = lx*ly*lz;
double *stress = (double *) calloc(9,sizeof(double));
double energy = 0.0;
double xij, yij, zij, rij, dr[3];
double xc[natom], yc[natom], zc[natom];
double fx[natom], fy[natom], fz[natom];
char atom_types[natom][50], *atom[natom];
char *atype2b[2], *atype3b[3], *atype4b[4];

for (i = 0; i < natom; i++)
{
fx[i] = 0.0;
fy[i] = 0.0;
fz[i] = 0.0;
}
for (i = 0; i < natom; i++)
{
fscanf(fconf,"%s %lf %lf %lf\n",atom_types[i],&xc[i],&yc[i],&zc[i]);
atom[i] = atom_types[i];
}
fclose(fconf);
//set_chimes_serial(small);
void *chimes_ptr = chimes_open_instance();
set_chimes_serial_instance(chimes_ptr, small);

printf("Read args:\n");
for (i=1; i<argc; i++)
printf("%i %s\n",i, argv[i]);

int rank = 0;
//init_chimes_serial(argv[1],&rank);
init_chimes_serial_instance(chimes_ptr, argv[1], rank);

//calculate_chimes(natom, xc, yc, zc, atom, ca, cb, cc, &energy, fx, fy, fz, stress);
calculate_chimes_instance(chimes_ptr, natom, xc, yc, zc, atom, ca, cb, cc, &energy, fx, fy, fz, stress);
for (i = 0; i < 9; i++) {
stress[i] *= GPa;
}

printf("\n%s\n", "Success!");
printf("%s\t%f\n","Energy (kcal/mol):", energy);
printf("%s\n", "Stress tensors (GPa)");
printf("%s %f\n","\ts_xx: ",stress[0]);
printf("%s %f\n","\ts_yy: ",stress[4]);
printf("%s %f\n","\ts_zz: ",stress[8]);
printf("%s %f\n","\ts_xy: ",stress[1]);
printf("%s %f\n","\ts_xz: ",stress[2]);
printf("%s %f\n","\ts_yz: ",stress[5]);
printf("%s\n", "Forces (kcal/mol/A)");
for (i = 0; i <natom; i++)
printf("\t%f\t%f\t%f\n", fx[i], fy[i], fz[i]);
printf("\n");

#if DEBUG==1

FILE *fout;
fout = fopen("debug.dat","w");
fprintf(fout,"%0.6f\n", energy);
fprintf(fout,"%0.6f\n", stress[0]);
fprintf(fout,"%0.6f\n", stress[4]);
fprintf(fout,"%0.6f\n", stress[8]);
fprintf(fout,"%0.6f\n", stress[1]);
fprintf(fout,"%0.6f\n", stress[2]);
fprintf(fout,"%0.6f\n", stress[5]);

for (i = 0; i <natom; i++)
fprintf(fout,"%0.6f\n%0.6e\n%0.6e\n", fx[i], fy[i],fz[i]);
fclose(fout);
chimes_close_instance(chimes_ptr);

#endif

}
Loading

0 comments on commit 231d01d

Please sign in to comment.