15 USE iso_c_binding,
ONLY: c_char, &
56#include "./base/base_uses.f90"
62 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'ipi_server'
63 INTEGER,
PARAMETER :: msglength = 12
83 CHARACTER(len=*),
PARAMETER :: routinen =
'start_server'
87 CALL timeset(routinen, handle)
88 cpabort(
"CP2K was compiled with the __NO_SOCKETS option!")
90 CHARACTER(len=default_path_length) :: c_hostname, drv_hostname, drv_prefix
91 INTEGER :: drv_port, handle, i_drv_unix, &
92 output_unit, socket, comm_socket
93 CHARACTER(len=msglength) :: msgbuffer
94 CHARACTER(len=msglength),
PARAMETER :: initmsg =
"INIT"
95 LOGICAL :: drv_unix, ionode
97 CALL timeset(routinen, handle)
98 ionode = para_env%is_source()
106 IF (output_unit > 0)
THEN
107 WRITE (output_unit, *)
"@ i-PI SERVER BEING STARTED"
108 WRITE (output_unit, *)
"@ HOSTNAME: ", trim(drv_hostname)
109 WRITE (output_unit, *)
"@ PORT: ", drv_port
110 WRITE (output_unit, *)
"@ UNIX SOCKET: ", drv_unix
117 IF (drv_unix) i_drv_unix = 0
123 c_hostname =
"/tmp/"//trim(drv_prefix)//
"_"//trim(drv_hostname)//c_null_char
125 c_hostname = trim(drv_hostname)//c_null_char
133 CALL ipi_env_set(ipi_env=ipi_env, sockfd=comm_socket)
139 CALL ask_status(comm_socket, msgbuffer)
140 IF (trim(msgbuffer) ==
"NEEDINIT")
THEN
149 CALL timestop(handle)
161 CHARACTER(len=msglength),
PARAMETER :: msg =
"EXIT"
163 INTEGER :: output_unit
166 WRITE (output_unit, *) –
"@ iPI: Shutting down server."
179 CHARACTER(len=msglength) :: msgbuffer
180 INTEGER :: comm_socket, i, natom, p, xyz
181 REAL(kind=
dp) :: energy
182 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: forces
185 natom = ipi_env%subsys%particles%n_els
186 comm_socket = ipi_env%sockfd
189 CALL ask_status(comm_socket, msgbuffer)
190 IF (trim(msgbuffer) /=
"READY")
THEN
191 cpabort(–
"iPI: Expected READY header but recieved "//trim(msgbuffer))
195 CALL send_posdata(comm_socket, subsys=ipi_env%subsys)
198 CALL ask_status(comm_socket, msgbuffer)
199 IF (trim(msgbuffer) /=
"HAVEDATA")
THEN
200 cpabort(–
"iPI: Expected HAVEDATA header but recieved "//trim(msgbuffer))
204 ALLOCATE (forces(3, natom))
205 CALL ask_getforce(comm_socket, energy=energy, forces=forces)
208 IF (
SIZE(forces) /= (natom*3))
THEN
209 cpabort(––
"iPI: Mismatch in particle number between CP2K and iPI client")
215 ipi_env%subsys%particles%els(p)%f(xyz) = forces(xyz, p)
218 CALL ipi_env_set(ipi_env=ipi_env, ipi_energy=energy, ipi_forces=forces)
226 SUBROUTINE get_header(sockfd, buffer)
227 INTEGER,
INTENT(IN) :: sockfd
228 CHARACTER(len=msglength),
INTENT(OUT) :: buffer
230 INTEGER :: output_unit
234 IF (output_unit > 0)
WRITE (output_unit, *) –
" @ iPI Server: recieved ", trim(buffer)
235 END SUBROUTINE get_header
242 SUBROUTINE ask_status(sockfd, buffer)
243 INTEGER,
INTENT(IN) :: sockfd
244 CHARACTER(len=msglength),
INTENT(OUT) :: buffer
246 CHARACTER(len=msglength),
PARAMETER :: msg =
"STATUS"
249 CALL get_header(sockfd, buffer)
250 END SUBROUTINE ask_status
260 SUBROUTINE ask_getforce(sockfd, energy, forces, virial, extra)
261 INTEGER,
INTENT(IN) :: sockfd
262 REAL(kind=
dp),
INTENT(OUT) :: energy
263 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(OUT), &
264 OPTIONAL,
POINTER :: forces
265 REAL(kind=
dp),
DIMENSION(3, 3),
INTENT(OUT), &
267 CHARACTER(len=:),
INTENT(OUT),
OPTIONAL,
POINTER :: extra
269 CHARACTER(len=msglength),
PARAMETER :: msg =
"GETFORCE"
271 CHARACTER(len=:),
ALLOCATABLE :: extra_buffer
272 CHARACTER(len=msglength) :: msgbuffer
273 INTEGER :: extralength, natom
274 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: forces_buffer
275 REAL(kind=
dp),
DIMENSION(9) :: virial_buffer
279 CALL get_header(sockfd, msgbuffer)
280 IF (trim(msgbuffer) /=
"FORCEREADY")
THEN
281 cpabort(–
"iPI: Expected FORCEREADY header but recieved "//trim(msgbuffer))
287 ALLOCATE (forces_buffer(3*natom))
288 CALL readbuffer(sockfd, forces_buffer, natom*3)
291 ALLOCATE (
CHARACTER(len=extraLength) :: extra_buffer)
292 IF (extralength /= 0)
THEN
293 CALL readbuffer(sockfd, extra_buffer, extralength)
296 IF (
PRESENT(forces)) forces = reshape(forces_buffer, shape=[3, natom])
297 IF (
PRESENT(virial)) virial = reshape(virial_buffer, shape=[3, 3])
298 IF (
PRESENT(extra)) extra = extra_buffer
299 END SUBROUTINE ask_getforce
306 SUBROUTINE send_posdata(sockfd, subsys)
307 INTEGER,
INTENT(IN) :: sockfd
310 CHARACTER(len=msglength),
PARAMETER :: msg =
"POSDATA"
312 INTEGER :: i, natom, p, xyz
313 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: particle_buffer
314 REAL(kind=
dp),
DIMENSION(9) :: cell_data, icell_data
320 cell_data = reshape(transpose(subsys%cell%hmat), [9])
323 icell_data = reshape(transpose(subsys%cell%h_inv), [9])
326 natom = subsys%particles%n_els
329 ALLOCATE (particle_buffer(3*natom))
333 particle_buffer(i) = subsys%particles%els(p)%r(xyz)
338 END SUBROUTINE send_posdata
Handles all functions related to the CELL.
subroutine, public init_cell(cell, hmat, periodic)
Initialise/readjust a simulation cell after hmat has been changed.
subroutine, public cell_create(cell, hmat, periodic, tag)
allocates and initializes a cell
Handles all functions related to the CELL.
subroutine, public cell_release(cell)
releases the given cell (see doc/ReferenceCounting.html)
Routines to handle the external control of CP2K.
subroutine, public external_control(should_stop, flag, globenv, target_time, start_time, force_check)
External manipulations during a run : when the <PROJECT_NAME>.EXIT_$runtype command is sent the progr...
various routines to log and control the output. The idea is that decisions about where to log should ...
integer function, public cp_logger_get_default_io_unit(logger)
returns the unit nr for the ionode (-1 on all other processors) skips as well checks if the procs cal...
types that represent a subsys, i.e. a part of the system
subroutine, public cp_subsys_set(subsys, atomic_kinds, particles, local_particles, molecules, molecule_kinds, local_molecules, para_env, colvar_p, shell_particles, core_particles, gci, multipoles, results, cell, cell_ref, use_ref_cell)
sets various propreties of the subsys
subroutine, public cp_subsys_get(subsys, ref_count, atomic_kinds, atomic_kind_set, particles, particle_set, local_particles, molecules, molecule_set, molecule_kinds, molecule_kind_set, local_molecules, para_env, colvar_p, shell_particles, core_particles, gci, multipoles, natom, nparticle, ncore, nshell, nkind, atprop, virial, results, cell, cell_ref, use_ref_cell)
returns information about various attributes of the given subsys
Define type storing the global information of a run. Keep the amount of stored data small....
The environment for the empirical interatomic potential methods.
subroutine, public ipi_env_set(ipi_env, ipi_energy, ipi_forces, subsys, atomic_kind_set, particle_set, local_particles, molecule_kind_set, molecule_set, local_molecules, force_env_input, cell_ref, sockfd)
Sets various attributes of the ipi environment.
i–PI server mode: Communication with i–PI clients
subroutine, public shutdown_server(ipi_env)
Shut down the i–PI server.
subroutine, public request_forces(ipi_env)
Send atomic positions to a client and retrieve forces.
subroutine, public start_server(driver_section, para_env, ipi_env)
Starts the i–PI server. Will block until it recieves a connection.
Defines the basic variable types.
integer, parameter, public dp
integer, parameter, public default_string_length
integer, parameter, public default_path_length
integer, parameter, public int_4
Interface to the message passing library MPI.
represent a simple array based list of the given type
Define the data structure for the particle information.
Implements UNIX and INET sockets.
Type defining parameters related to the simulation cell.
represents a system: atoms, molecules, their pos,vel,...
contains the initially parsed file and the initial parallel environment
stores all the informations relevant to an mpi environment
represent a list of objects