(git:9ca3469)
Loading...
Searching...
No Matches
topology_cp2k.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7! **************************************************************************************************
9
11 cell_type,&
23 USE cp_units, ONLY: cp_unit_to_cp2k
31 USE kinds, ONLY: default_string_length,&
32 dp,&
36 USE periodic_table, ONLY: nelem,&
37 ptable
38 USE string_table, ONLY: id2str,&
39 s2s,&
40 str2id
43#include "./base/base_uses.f90"
44
45 IMPLICIT NONE
46
47 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'topology_cp2k'
48
49 PRIVATE
50 PUBLIC :: read_coordinate_cp2k
51
52CONTAINS
53
54! **************************************************************************************************
55!> \brief Read the CP2K &COORD section from an external file, i.e. read
56!> atomic coordinates and molecule/residue information in CP2K format.
57!> \param topology ...
58!> \param para_env ...
59!> \param subsys_section ...
60!> \date 17.01.2011 (Creation, MK)
61!> \author Matthias Krack (MK)
62!> \version 1.0
63! **************************************************************************************************
64 SUBROUTINE read_coordinate_cp2k(topology, para_env, subsys_section)
65
67 TYPE(mp_para_env_type), POINTER :: para_env
68 TYPE(section_vals_type), POINTER :: subsys_section
69
70 CHARACTER(LEN=*), PARAMETER :: routinen = 'read_coordinate_cp2k'
71
72 CHARACTER(LEN=default_string_length) :: string
73 CHARACTER(LEN=max_line_length) :: error_message
74 INTEGER :: handle, i, ian, iw, natom, newsize, &
75 number_of_atoms
76 LOGICAL :: eof, explicit, scaled_coordinates
77 REAL(kind=dp) :: pfactor, unit_conv
78 REAL(kind=dp), DIMENSION(3) :: r
79 TYPE(atom_info_type), POINTER :: atom_info
80 TYPE(cell_type), POINTER :: cell
81 TYPE(cp_logger_type), POINTER :: logger
82 TYPE(cp_parser_type), POINTER :: parser
83 TYPE(section_vals_type), POINTER :: coord_section
84
85 CALL timeset(routinen, handle)
86
87 NULLIFY (coord_section)
88 NULLIFY (logger)
89 NULLIFY (parser)
90 logger => cp_get_default_logger()
91 iw = cp_print_key_unit_nr(logger, subsys_section, "PRINT%TOPOLOGY_INFO/XYZ_INFO", &
92 extension=".subsysLog")
93
94 ! Check if there is a &COORD section
95 coord_section => section_vals_get_subs_vals(subsys_section, "COORD")
96 CALL section_vals_get(coord_section, explicit=explicit)
97 IF (explicit) THEN
98 CALL section_vals_val_get(coord_section, "UNIT", c_val=string)
99 CALL section_vals_val_get(coord_section, "SCALED", l_val=scaled_coordinates)
100 ELSE
101 ! The default is Cartesian coordinates in Angstrom
102 scaled_coordinates = .false.
103 string = "angstrom"
104 END IF
105 unit_conv = cp_unit_to_cp2k(1.0_dp, trim(string))
106
107 atom_info => topology%atom_info
108 cell => topology%cell_muc
109
110 IF (iw > 0) THEN
111 WRITE (unit=iw, fmt="(T2,A)") &
112 "BEGIN of COORD section data read from file "//trim(topology%coord_file_name)
113 END IF
114
115 pfactor = section_get_rval(subsys_section, "TOPOLOGY%MEMORY_PROGRESSION_FACTOR")
116 number_of_atoms = section_get_ival(subsys_section, "TOPOLOGY%NUMBER_OF_ATOMS")
117 IF (number_of_atoms < 1) THEN
118 newsize = 1000
119 ELSE
120 newsize = number_of_atoms
121 END IF
122
123 CALL reallocate(atom_info%id_molname, 1, newsize)
124 CALL reallocate(atom_info%id_resname, 1, newsize)
125 CALL reallocate(atom_info%resid, 1, newsize)
126 CALL reallocate(atom_info%id_atmname, 1, newsize)
127 CALL reallocate(atom_info%r, 1, 3, 1, newsize)
128 CALL reallocate(atom_info%atm_mass, 1, newsize)
129 CALL reallocate(atom_info%atm_charge, 1, newsize)
130 CALL reallocate(atom_info%occup, 1, newsize)
131 CALL reallocate(atom_info%beta, 1, newsize)
132 CALL reallocate(atom_info%id_element, 1, newsize)
133
134 topology%molname_generated = .false.
135 ! Element is assigned on the basis of the atm_name
136 topology%aa_element = .true.
137
138 ALLOCATE (parser)
139 CALL parser_create(parser, topology%coord_file_name, para_env=para_env)
140
141 natom = 0
142 DO
143 CALL parser_get_object(parser, object=string, newline=.true., at_end=eof)
144 IF (eof) EXIT
145 natom = natom + 1
146 IF (natom > SIZE(atom_info%id_atmname)) THEN
147 newsize = int(pfactor*natom)
148 CALL reallocate(atom_info%id_molname, 1, newsize)
149 CALL reallocate(atom_info%id_resname, 1, newsize)
150 CALL reallocate(atom_info%resid, 1, newsize)
151 CALL reallocate(atom_info%id_atmname, 1, newsize)
152 CALL reallocate(atom_info%r, 1, 3, 1, newsize)
153 CALL reallocate(atom_info%atm_mass, 1, newsize)
154 CALL reallocate(atom_info%atm_charge, 1, newsize)
155 CALL reallocate(atom_info%occup, 1, newsize)
156 CALL reallocate(atom_info%beta, 1, newsize)
157 CALL reallocate(atom_info%id_element, 1, newsize)
158 END IF
159 error_message = ""
160 CALL read_integer_object(string, ian, error_message)
161 IF (len_trim(error_message) == 0) THEN
162 ! Integer value found: assume atomic number, check it, and load
163 ! the corresponding element symbol if valid
164 IF ((ian < 0) .OR. (ian > nelem)) THEN
165 error_message = "Invalid atomic number <"//trim(string)// &
166 "> found in the xyz file <"//trim(topology%coord_file_name)//">!"
167 cpabort(trim(error_message))
168 ELSE
169 atom_info%id_atmname(natom) = str2id(s2s(ptable(ian)%symbol))
170 END IF
171 ELSE
172 atom_info%id_atmname(natom) = str2id(s2s(string))
173 END IF
174 ! Read x, y, and z coordinate of the current atom
175 DO i = 1, 3
176 CALL parser_get_object(parser, object=r(i))
177 END DO
178 IF (scaled_coordinates) THEN
179 CALL scaled_to_real(atom_info%r(1:3, natom), r, cell)
180 ELSE
181 atom_info%r(1:3, natom) = r(1:3)*unit_conv
182 CALL cell_transform_input_cartesian(cell, atom_info%r(1:3, natom))
183 END IF
184 IF (parser_test_next_token(parser) /= "EOL") THEN
185 CALL parser_get_object(parser, object=string)
186 atom_info%id_molname(natom) = str2id(s2s(string))
187 IF (parser_test_next_token(parser) /= "EOL") THEN
188 CALL parser_get_object(parser, object=string)
189 atom_info%id_resname(natom) = str2id(s2s(string))
190 ELSE
191 atom_info%id_resname(natom) = atom_info%id_molname(natom)
192 END IF
193 ELSE
194 string = ""
195 WRITE (unit=string, fmt="(I0)") natom
196 atom_info%id_molname(natom) = str2id(s2s(trim(id2str(atom_info%id_atmname(natom)))//trim(string)))
197 atom_info%id_resname(natom) = atom_info%id_molname(natom)
198 topology%molname_generated = .true.
199 END IF
200 atom_info%resid(natom) = 1
201 atom_info%id_element(natom) = atom_info%id_atmname(natom)
202 atom_info%atm_mass(natom) = huge(0.0_dp)
203 atom_info%atm_charge(natom) = -huge(0.0_dp)
204 IF (iw > 0) THEN
205 WRITE (unit=iw, fmt="(T2,A,3F20.8,2(2X,A))") &
206 trim(id2str(atom_info%id_atmname(natom))), atom_info%r(1:3, natom), &
207 adjustl(trim(id2str(atom_info%id_molname(natom)))), &
208 adjustl(trim(id2str(atom_info%id_resname(natom))))
209 END IF
210 IF (natom == number_of_atoms) EXIT
211 END DO
212
213 CALL parser_release(parser)
214 DEALLOCATE (parser)
215
216 topology%natoms = natom
217 CALL reallocate(atom_info%id_molname, 1, natom)
218 CALL reallocate(atom_info%id_resname, 1, natom)
219 CALL reallocate(atom_info%resid, 1, natom)
220 CALL reallocate(atom_info%id_atmname, 1, natom)
221 CALL reallocate(atom_info%r, 1, 3, 1, natom)
222 CALL reallocate(atom_info%atm_mass, 1, natom)
223 CALL reallocate(atom_info%atm_charge, 1, natom)
224 CALL reallocate(atom_info%occup, 1, natom)
225 CALL reallocate(atom_info%beta, 1, natom)
226 CALL reallocate(atom_info%id_element, 1, natom)
227
228 CALL section_vals_val_set(subsys_section, "TOPOLOGY%NUMBER_OF_ATOMS", i_val=natom)
229
230 IF (iw > 0) THEN
231 WRITE (unit=iw, fmt="(T2,A)") &
232 "END of COORD section data read from file "//trim(topology%coord_file_name)
233 END IF
234
235 CALL cp_print_key_finished_output(iw, logger, subsys_section, &
236 "PRINT%TOPOLOGY_INFO/XYZ_INFO")
237
238 CALL timestop(handle)
239
240 END SUBROUTINE read_coordinate_cp2k
241
242END MODULE topology_cp2k
Handles all functions related to the CELL.
Definition cell_types.F:15
subroutine, public scaled_to_real(r, s, cell)
Transform scaled cell coordinates real coordinates. r=h*s.
Definition cell_types.F:565
subroutine, public cell_transform_input_cartesian(cell, vector)
Transform a Cartesian real-space vector from the user input cell frame into CP2K's canonical internal...
Definition cell_types.F:261
various routines to log and control the output. The idea is that decisions about where to log should ...
type(cp_logger_type) function, pointer, public cp_get_default_logger()
returns the default logger
routines to handle the output, The idea is to remove the decision of wheter to output and what to out...
integer function, public cp_print_key_unit_nr(logger, basis_section, print_key_path, extension, middle_name, local, log_filename, ignore_should_output, file_form, file_position, file_action, file_status, do_backup, on_file, is_new_file, mpi_io, fout)
...
subroutine, public cp_print_key_finished_output(unit_nr, logger, basis_section, print_key_path, local, ignore_should_output, on_file, mpi_io)
should be called after you finish working with a unit obtained with cp_print_key_unit_nr,...
Utility routines to read data from files. Kept as close as possible to the old parser because.
elemental subroutine, public read_integer_object(string, object, error_message)
Returns an integer number read from a string including products of integer numbers like iz1*iz2*iz3.
character(len=3) function, public parser_test_next_token(parser, string_length)
Test next input object.
Utility routines to read data from files. Kept as close as possible to the old parser because.
subroutine, public parser_release(parser)
releases the parser
subroutine, public parser_create(parser, file_name, unit_nr, para_env, end_section_label, separator_chars, comment_char, continuation_char, quote_char, section_char, parse_white_lines, initial_variables, apply_preprocessing)
Start a parser run. Initial variables allow to @SET stuff before opening the file.
unit conversion facility
Definition cp_units.F:30
real(kind=dp) function, public cp_unit_to_cp2k(value, unit_str, defaults, power)
converts to the internal cp2k units to the given unit
Definition cp_units.F:1210
objects that represent the structure of input sections and the data contained in an input section
real(kind=dp) function, public section_get_rval(section_vals, keyword_name)
...
subroutine, public section_vals_val_set(section_vals, keyword_name, i_rep_section, i_rep_val, val, l_val, i_val, r_val, c_val, l_vals_ptr, i_vals_ptr, r_vals_ptr, c_vals_ptr)
sets the requested value
integer function, public section_get_ival(section_vals, keyword_name)
...
recursive type(section_vals_type) function, pointer, public section_vals_get_subs_vals(section_vals, subsection_name, i_rep_section, can_return_null)
returns the values of the requested subsection
subroutine, public section_vals_get(section_vals, ref_count, n_repetition, n_subs_vals_rep, section, explicit)
returns various attributes about the section_vals
subroutine, public section_vals_val_get(section_vals, keyword_name, i_rep_section, i_rep_val, n_rep_val, val, l_val, i_val, r_val, c_val, l_vals, i_vals, r_vals, c_vals, explicit)
returns the requested value
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public max_line_length
Definition kinds.F:59
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
Utility routines for the memory handling.
Interface to the message passing library MPI.
Periodic Table related data definitions.
type(atom), dimension(0:nelem), public ptable
integer, parameter, public nelem
generates a unique id number for a string (str2id) that can be used two compare two strings....
character(len=default_string_length) function, public s2s(str)
converts a string in a string of default_string_length
integer function, public str2id(str)
returns a unique id for a given string, and stores the string for later retrieval using the id.
character(len=default_string_length) function, public id2str(id)
returns the string associated with a given id
subroutine, public read_coordinate_cp2k(topology, para_env, subsys_section)
Read the CP2K &COORD section from an external file, i.e. read atomic coordinates and molecule/residue...
Control for reading in different topologies and coordinates.
Definition topology.F:13
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
type of a logger, at the moment it contains just a print level starting at which level it should be l...
stores all the informations relevant to an mpi environment