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
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()
105 IF (output_unit > 0)
THEN
106 WRITE (output_unit, *)
"@ i-PI SERVER BEING STARTED"
107 WRITE (output_unit, *)
"@ HOSTNAME: ", trim(drv_hostname)
108 WRITE (output_unit, *)
"@ PORT: ", drv_port
109 WRITE (output_unit, *)
"@ UNIX SOCKET: ", drv_unix
116 IF (drv_unix) i_drv_unix = 0
118 c_hostname = trim(drv_hostname)//c_null_char
125 CALL ipi_env_set(ipi_env=ipi_env, sockfd=comm_socket)
131 CALL ask_status(comm_socket, msgbuffer)
132 IF (trim(msgbuffer) ==
"NEEDINIT")
THEN
141 CALL timestop(handle)
153 CHARACTER(len=msglength),
PARAMETER :: msg =
"EXIT"
155 INTEGER :: output_unit
158 WRITE (output_unit, *) –
"@ iPI: Shutting down server."
171 CHARACTER(len=msglength) :: msgbuffer
172 INTEGER :: comm_socket, i, natom, p, xyz
173 REAL(kind=
dp) :: energy
174 REAL(kind=
dp),
DIMENSION(:, :),
POINTER :: forces
177 natom = ipi_env%subsys%particles%n_els
178 comm_socket = ipi_env%sockfd
181 CALL ask_status(comm_socket, msgbuffer)
182 IF (trim(msgbuffer) /=
"READY") &
183 cpabort(–
"iPI: Expected READY header but recieved "//trim(msgbuffer))
186 CALL send_posdata(comm_socket, subsys=ipi_env%subsys)
189 CALL ask_status(comm_socket, msgbuffer)
190 IF (trim(msgbuffer) /=
"HAVEDATA") &
191 cpabort(–
"iPI: Expected HAVEDATA header but recieved "//trim(msgbuffer))
194 ALLOCATE (forces(3, natom))
195 CALL ask_getforce(comm_socket, energy=energy, forces=forces)
198 IF (
SIZE(forces) /= (natom*3))
THEN
199 cpabort(––
"iPI: Mismatch in particle number between CP2K and iPI client")
205 ipi_env%subsys%particles%els(p)%f(xyz) = forces(xyz, p)
208 CALL ipi_env_set(ipi_env=ipi_env, ipi_energy=energy, ipi_forces=forces)
216 SUBROUTINE get_header(sockfd, buffer)
217 INTEGER,
INTENT(IN) :: sockfd
218 CHARACTER(len=msglength),
INTENT(OUT) :: buffer
220 INTEGER :: output_unit
224 IF (output_unit > 0)
WRITE (output_unit, *) –
" @ iPI Server: recieved ", trim(buffer)
225 END SUBROUTINE get_header
232 SUBROUTINE ask_status(sockfd, buffer)
233 INTEGER,
INTENT(IN) :: sockfd
234 CHARACTER(len=msglength),
INTENT(OUT) :: buffer
236 CHARACTER(len=msglength),
PARAMETER :: msg =
"STATUS"
239 CALL get_header(sockfd, buffer)
240 END SUBROUTINE ask_status
250 SUBROUTINE ask_getforce(sockfd, energy, forces, virial, extra)
251 INTEGER,
INTENT(IN) :: sockfd
252 REAL(kind=
dp),
INTENT(OUT) :: energy
253 REAL(kind=
dp),
DIMENSION(:, :),
INTENT(OUT), &
254 OPTIONAL,
POINTER :: forces
255 REAL(kind=
dp),
DIMENSION(3, 3),
INTENT(OUT), &
257 CHARACTER(len=:),
INTENT(OUT),
OPTIONAL,
POINTER :: extra
259 CHARACTER(len=msglength),
PARAMETER :: msg =
"GETFORCE"
261 CHARACTER(len=:),
ALLOCATABLE :: extra_buffer
262 CHARACTER(len=msglength) :: msgbuffer
263 INTEGER :: extralength, natom
264 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: forces_buffer
265 REAL(kind=
dp),
DIMENSION(9) :: virial_buffer
269 CALL get_header(sockfd, msgbuffer)
270 IF (trim(msgbuffer) /=
"FORCEREADY") &
271 cpabort(–
"iPI: Expected FORCEREADY header but recieved "//trim(msgbuffer))
276 ALLOCATE (forces_buffer(3*natom))
277 CALL readbuffer(sockfd, forces_buffer, natom*3)
280 ALLOCATE (
CHARACTER(len=extraLength) :: extra_buffer)
281 IF (extralength /= 0)
THEN
282 CALL readbuffer(sockfd, extra_buffer, extralength)
285 IF (
PRESENT(forces)) forces = reshape(forces_buffer, shape=[3, natom])
286 IF (
PRESENT(virial)) virial = reshape(virial_buffer, shape=[3, 3])
287 IF (
PRESENT(extra)) extra = extra_buffer
288 END SUBROUTINE ask_getforce
295 SUBROUTINE send_posdata(sockfd, subsys)
296 INTEGER,
INTENT(IN) :: sockfd
299 CHARACTER(len=msglength),
PARAMETER :: msg =
"POSDATA"
301 INTEGER :: i, natom, p, xyz
302 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: particle_buffer
303 REAL(kind=
dp),
DIMENSION(9) :: cell_data, icell_data
309 cell_data = reshape(transpose(subsys%cell%hmat), (/9/))
312 icell_data = reshape(transpose(subsys%cell%h_inv), (/9/))
315 natom = subsys%particles%n_els
318 ALLOCATE (particle_buffer(3*natom))
322 particle_buffer(i) = subsys%particles%els(p)%r(xyz)
327 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)
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)
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