Line data Source code
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 :
8 : ! **************************************************************************************************
9 : !> \brief Native streaming mesh analysis with physical endpoint sewing.
10 : ! **************************************************************************************************
11 : MODULE topology_phason_analysis
12 : USE cp_files, ONLY: close_file,&
13 : open_file
14 : USE ieee_arithmetic, ONLY: ieee_is_finite
15 : USE iso_fortran_env, ONLY: int64
16 : USE kinds, ONLY: default_path_length,&
17 : dp
18 : USE topology_curvature, ONLY: curvature_density,&
19 : link_plaquette,&
20 : polar_link
21 : USE topology_snapshot, ONLY: check_snapshot,&
22 : check_snapshot_seam,&
23 : read_snapshot,&
24 : snapshot_overlap,&
25 : snapshot_type
26 : #include "./base/base_uses.f90"
27 :
28 : IMPLICIT NONE
29 : CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'topology_phason_analysis'
30 : PRIVATE
31 : TYPE :: phason_result_type
32 : INTEGER :: dimension = 0, rank = 0, SHAPE(4) = 0, nat = 0, point = 0
33 : REAL(KIND=dp) :: value = 0.0_dp, origin(3) = 0.0_dp, directions(3, 4) = 0.0_dp
34 : CHARACTER(LEN=default_path_length) :: first_file = ''
35 : END TYPE phason_result_type
36 : PUBLIC :: analyze_mesh, phason_result_type, compare_refinements
37 :
38 : CONTAINS
39 :
40 : ! **************************************************************************************************
41 : !> \brief Flatten a zero-based index with the first coordinate varying fastest.
42 : !> \param index Zero-based coordinate along each axis
43 : !> \param shape Number of entries along each axis
44 : !> \return One-based linear index
45 : ! **************************************************************************************************
46 0 : INTEGER FUNCTION flat(index, shape) RESULT(value)
47 : INTEGER, INTENT(IN) :: index(:), SHAPE(:)
48 :
49 : INTEGER :: d, stride
50 :
51 0 : value = 1
52 0 : stride = 1
53 0 : DO d = 1, SIZE(shape)
54 0 : value = value + INDEX(d)*stride
55 0 : stride = stride*SHAPE(d)
56 : END DO
57 0 : END FUNCTION flat
58 :
59 : ! **************************************************************************************************
60 : !> \brief Inverse of flat; first coordinate varies fastest.
61 : !> \param value One-based linear index
62 : !> \param shape Number of entries along each axis
63 : !> \param index Zero-based coordinate along each axis
64 : ! **************************************************************************************************
65 0 : SUBROUTINE unflat(value, shape, index)
66 : INTEGER, INTENT(IN) :: value, SHAPE(:)
67 : INTEGER, INTENT(OUT) :: index(:)
68 :
69 : INTEGER :: d, remainder
70 :
71 0 : remainder = value - 1
72 0 : DO d = 1, SIZE(shape)
73 0 : INDEX(d) = MOD(remainder, SHAPE(d))
74 0 : remainder = remainder/SHAPE(d)
75 : END DO
76 0 : END SUBROUTINE unflat
77 :
78 : ! **************************************************************************************************
79 : !> \brief Build physical links or read reference links; stream plaquettes through an anonymous file.
80 : !> \param path Manifest or reference-link filename
81 : !> \param physical True for physical STATE_EXPORT frames, false for reference links
82 : !> \param report Mesh metadata and unrounded Chern estimate for refinement comparison
83 : !> \param io_unit Unit receiving mesh diagnostics
84 : ! **************************************************************************************************
85 0 : SUBROUTINE analyze_mesh(path, physical, report, io_unit)
86 : CHARACTER(LEN=*), INTENT(IN) :: path
87 : LOGICAL, INTENT(IN) :: physical
88 : TYPE(phason_result_type), INTENT(OUT) :: report
89 : INTEGER, INTENT(IN) :: io_unit
90 : CHARACTER(LEN=256) :: header
91 0 : CHARACTER(LEN=default_path_length), ALLOCATABLE :: files(:)
92 : INTEGER :: input, store, stat, dim, rank, nvertex, nframe, nat, vertex, axis, mu, nu, p, i, j, na, nb, nbase, idx
93 0 : INTEGER, ALLOCATABLE :: shape(:), extended(:), INDEX(:), next(:), zero(:), points(:), permutation(:, :)
94 0 : LOGICAL, ALLOCATABLE :: checked(:)
95 0 : REAL(KIND=dp), ALLOCATABLE :: offsets(:), directions(:, :)
96 : REAL(KIND=dp) :: origin(3), ka(3), kb(3), tol_metric, tol_gap, tol_sv, phase_limit, minimum, local_min, &
97 : max_metric, min_gap, err, gap, value, contribution, max_phase, local_phase, seam_error, &
98 : valence_max, conduction_min, energy_shift, compensation, y, t
99 0 : COMPLEX(KIND=dp), ALLOCATABLE :: raw(:, :), link(:, :), sewing(:, :), u(:, :, :), pqs(:, :, :)
100 0 : TYPE(snapshot_type), ALLOCATABLE :: a, b, base
101 : LOGICAL :: common_reference, prefix
102 : INTEGER(KIND=int64) :: record_size, record_start, total
103 :
104 0 : CALL open_file(path, unit_number=input, file_status='OLD', file_action='READ')
105 0 : READ (input, '(A)', IOSTAT=stat) header
106 0 : CPASSERT(stat == 0)
107 0 : IF (physical) THEN
108 0 : IF (TRIM(header) /= 'CP2K_PHASON_MESH 1') THEN
109 0 : CPABORT('Expected CP2K_PHASON_MESH 1')
110 : END IF
111 : ELSE
112 0 : IF (TRIM(header) /= 'CP2K_TOPOLOGY_LINKS 1') THEN
113 0 : CPABORT('Expected CP2K_TOPOLOGY_LINKS 1')
114 : END IF
115 : END IF
116 0 : READ (input, *, IOSTAT=stat) dim, rank
117 0 : CPASSERT(stat == 0)
118 0 : IF ((dim /= 2 .AND. dim /= 4) .OR. rank < 1) THEN
119 0 : CPABORT('Invalid mesh dimension or rank')
120 : END IF
121 0 : ALLOCATE (SHAPE(dim), extended(dim), INDEX(dim), next(dim), zero(dim))
122 0 : READ (input, *, IOSTAT=stat) shape
123 0 : CPASSERT(stat == 0)
124 0 : IF (ANY(shape < 3)) THEN
125 0 : CPABORT('At least three samples per mesh axis are required')
126 : END IF
127 0 : total = 1_int64
128 0 : DO axis = 1, dim
129 0 : IF (total > INT(HUGE(1), int64)/INT(SHAPE(axis) + 1, int64)) THEN
130 0 : CPABORT('Mesh index overflow')
131 : END IF
132 0 : total = total*INT(SHAPE(axis) + 1, int64)
133 : END DO
134 0 : extended(:) = shape + 1
135 0 : nvertex = PRODUCT(shape)
136 0 : nframe = INT(total)
137 0 : report%dimension = dim
138 0 : report%rank = rank
139 0 : report%shape(1:dim) = shape
140 0 : READ (input, *, IOSTAT=stat) tol_metric, tol_gap, tol_sv, phase_limit
141 0 : CPASSERT(stat == 0)
142 0 : IF (.NOT. ALL(ieee_is_finite([tol_metric, tol_gap, tol_sv, phase_limit]))) THEN
143 0 : CPABORT('Nonfinite tolerances')
144 : END IF
145 0 : IF (MIN(tol_metric, tol_gap, tol_sv, phase_limit) <= 0.0_dp .OR. MAX(tol_metric, tol_sv) >= 1.0_dp .OR. &
146 : phase_limit >= ACOS(-1.0_dp)) THEN
147 0 : CPABORT('Invalid mesh tolerances')
148 : END IF
149 0 : ALLOCATE (raw(rank, rank), link(rank, rank), sewing(rank, rank), u(rank, rank, 4), pqs(rank, rank, dim*(dim - 1)/2))
150 : ! Anonymous scratch storage avoids persistent, stale link caches.
151 : CALL open_file(unit_number=store, file_status='SCRATCH', file_access='STREAM', &
152 : file_form='UNFORMATTED', file_action='READWRITE')
153 0 : INQUIRE (UNIT=store, POS=record_start, IOSTAT=stat)
154 0 : CPASSERT(stat == 0)
155 0 : minimum = HUGE(1.0_dp)
156 0 : max_metric = 0.0_dp
157 0 : min_gap = HUGE(1.0_dp)
158 0 : seam_error = 0.0_dp
159 0 : valence_max = -HUGE(1.0_dp)
160 0 : conduction_min = HUGE(1.0_dp)
161 0 : common_reference = .FALSE.
162 0 : prefix = .TRUE.
163 0 : IF (physical) THEN
164 0 : ALLOCATE (a, b, base)
165 0 : ALLOCATE (files(nframe), points(nframe), offsets(nframe), checked(nframe), directions(3, dim))
166 0 : checked(:) = .FALSE.
167 0 : READ (input, *, IOSTAT=stat) origin
168 0 : CPASSERT(stat == 0)
169 0 : IF (.NOT. ALL(ieee_is_finite(origin))) THEN
170 0 : CPABORT('Nonfinite mesh origin')
171 : END IF
172 0 : DO axis = 1, dim
173 0 : READ (input, *, IOSTAT=stat) directions(:, axis)
174 0 : CPASSERT(stat == 0)
175 : END DO
176 0 : IF (.NOT. ALL(ieee_is_finite(directions))) THEN
177 0 : CPABORT('Nonfinite reciprocal periods')
178 : END IF
179 0 : IF (MAXVAL(ABS(directions - ANINT(directions))) > 1.e-10_dp) THEN
180 0 : CPABORT('Noninteger reciprocal periods')
181 : END IF
182 0 : READ (input, *, IOSTAT=stat) nat, common_reference
183 0 : CPASSERT(stat == 0)
184 0 : IF (nat < 1) THEN
185 0 : CPABORT('Invalid mesh atom count')
186 : END IF
187 0 : ALLOCATE (permutation(nat, dim))
188 0 : DO axis = 1, dim
189 0 : READ (input, *, IOSTAT=stat) permutation(:, axis)
190 0 : CPASSERT(stat == 0)
191 0 : IF (ANY(permutation(:, axis) < 1) .OR. ANY(permutation(:, axis) > nat)) THEN
192 0 : CPABORT('Invalid seam permutation')
193 : END IF
194 0 : DO idx = 1, nat
195 0 : IF (COUNT(permutation(:, axis) == idx) /= 1) THEN
196 0 : CPABORT('Non-bijective seam map')
197 : END IF
198 : END DO
199 : END DO
200 0 : DO mu = 1, dim
201 0 : DO nu = mu + 1, dim
202 0 : IF (ANY(permutation(permutation(:, mu), nu) /= permutation(permutation(:, nu), mu))) THEN
203 0 : CPABORT('The declared torus atom permutations do not commute')
204 : END IF
205 : END DO
206 : END DO
207 0 : DO idx = 1, nframe
208 0 : READ (input, *, IOSTAT=stat) files(idx), points(idx), offsets(idx)
209 0 : CPASSERT(stat == 0)
210 0 : IF (points(idx) < 1 .OR. .NOT. ieee_is_finite(offsets(idx))) THEN
211 0 : CPABORT('Invalid frame record')
212 : END IF
213 : END DO
214 0 : report%nat = nat
215 0 : report%origin(:) = origin
216 0 : report%directions(:, 1:dim) = directions
217 0 : report%first_file = files(1)
218 0 : report%point = points(1)
219 : END IF
220 0 : DO vertex = 1, nvertex
221 0 : CALL unflat(vertex, shape, index)
222 0 : IF (physical) THEN
223 0 : na = flat(index, extended)
224 0 : CALL frame(na, a)
225 0 : ka(:) = origin + MATMUL(directions, REAL(index, dp)/REAL(shape, dp))
226 : END IF
227 0 : DO axis = 1, dim
228 0 : IF (physical) THEN
229 0 : next(:) = index
230 0 : next(axis) = next(axis) + 1
231 0 : nb = flat(next, extended)
232 0 : CALL frame(nb, b)
233 0 : kb(:) = origin + MATMUL(directions, REAL(next, dp)/REAL(shape, dp))
234 0 : CALL snapshot_overlap(a, b, ka, kb, raw)
235 0 : IF (next(axis) == SHAPE(axis)) THEN
236 0 : zero(:) = next
237 0 : zero(axis) = 0
238 0 : nbase = flat(zero, extended)
239 0 : CALL frame(nbase, base)
240 0 : CALL check_snapshot_seam(base, b, permutation(:, axis), tol_metric)
241 0 : CALL snapshot_overlap(b, base, kb, kb, sewing)
242 : ! Both endpoint frames must span the same physical subspace, not merely have full rank.
243 0 : link(:, :) = MATMUL(CONJG(TRANSPOSE(sewing)), sewing)
244 0 : DO idx = 1, rank
245 0 : link(idx, idx) = link(idx, idx) - 1.0_dp
246 : END DO
247 0 : err = MAXVAL(ABS(link))
248 0 : seam_error = MAX(seam_error, err)
249 0 : IF (err > tol_metric) THEN
250 0 : CPABORT('Endpoint selected subspaces do not close')
251 : END IF
252 0 : CALL polar_link(sewing, link, local_min, stat, tol_sv)
253 0 : IF (stat /= 0) THEN
254 0 : CPABORT('Invalid endpoint sewing link')
255 : END IF
256 0 : raw(:, :) = MATMUL(raw, link)
257 : END IF
258 : ELSE
259 0 : DO j = 1, rank
260 0 : DO i = 1, rank
261 0 : READ (input, *, IOSTAT=stat) ka(1:2)
262 0 : CPASSERT(stat == 0)
263 0 : raw(i, j) = CMPLX(ka(1), ka(2), dp)
264 : END DO
265 : END DO
266 : END IF
267 0 : CALL polar_link(raw, link, local_min, stat, tol_sv)
268 0 : IF (stat /= 0) THEN
269 0 : CPABORT('Singular/nonfinite mesh link; refine or check the subspace')
270 : END IF
271 0 : minimum = MIN(minimum, local_min)
272 0 : WRITE (store, IOSTAT=stat) link
273 0 : CPASSERT(stat == 0)
274 0 : IF (vertex == 1 .AND. axis == 1) THEN
275 : ! Measure the stride in stream-position units; IOLENGTH may use different units.
276 0 : INQUIRE (UNIT=store, POS=record_size, IOSTAT=stat)
277 0 : CPASSERT(stat == 0)
278 0 : record_size = record_size - record_start
279 : END IF
280 : END DO
281 : END DO
282 : DO
283 0 : READ (input, '(A)', IOSTAT=stat) header
284 0 : IF (stat < 0) EXIT
285 0 : IF (stat /= 0 .OR. LEN_TRIM(header) /= 0) THEN
286 0 : CPABORT('Unexpected trailing mesh data')
287 : END IF
288 : END DO
289 0 : CALL close_file(input)
290 0 : value = 0.0_dp
291 0 : compensation = 0.0_dp
292 0 : max_phase = 0.0_dp
293 0 : DO vertex = 1, nvertex
294 0 : CALL unflat(vertex, shape, index)
295 0 : p = 0
296 0 : DO mu = 1, dim
297 0 : DO nu = mu + 1, dim
298 0 : p = p + 1
299 0 : CALL read_link(vertex, mu, u(:, :, 1))
300 0 : next(:) = index
301 0 : next(mu) = MOD(next(mu) + 1, SHAPE(mu))
302 0 : CALL read_link(flat(next, shape), nu, u(:, :, 2))
303 0 : next(:) = index
304 0 : next(nu) = MOD(next(nu) + 1, SHAPE(nu))
305 0 : CALL read_link(flat(next, shape), mu, u(:, :, 3))
306 0 : CALL read_link(vertex, nu, u(:, :, 4))
307 0 : CALL link_plaquette(u(:, :, 1), u(:, :, 2), u(:, :, 3), u(:, :, 4), pqs(:, :, p))
308 : END DO
309 : END DO
310 0 : CALL curvature_density(pqs, dim, phase_limit, contribution, local_phase, stat)
311 0 : IF (stat /= 0) THEN
312 0 : CPABORT('Unresolved/nonunitary plaquette; refine the mesh')
313 : END IF
314 0 : max_phase = MAX(max_phase, local_phase)
315 0 : y = contribution - compensation
316 0 : t = value + y
317 0 : compensation = (t - value) - y
318 0 : value = t
319 : END DO
320 0 : CALL close_file(store)
321 0 : report%value = value
322 0 : WRITE (io_unit, '(A,I0)') 'PARAMETER_DIMENSION ', dim
323 0 : WRITE (io_unit, '(A,*(I0,1X))') 'MESH ', shape
324 0 : WRITE (io_unit, '(A,ES26.17)') 'CHERN_RAW ', value
325 0 : WRITE (io_unit, '(A,ES26.17)') 'MINIMUM_LINK_SINGULAR_VALUE ', minimum
326 0 : WRITE (io_unit, '(A,ES26.17)') 'MAXIMUM_PLAQUETTE_PHASE ', max_phase
327 0 : IF (physical) THEN
328 0 : WRITE (io_unit, '(A,ES26.17)') 'MAXIMUM_METRIC_ERROR ', max_metric
329 0 : WRITE (io_unit, '(A,ES26.17)') 'MAXIMUM_SEAM_ERROR ', seam_error
330 0 : WRITE (io_unit, '(A,ES26.17)') 'MINIMUM_DIRECT_GAP_HA ', min_gap
331 0 : IF (common_reference .AND. prefix) THEN
332 0 : WRITE (io_unit, '(A,ES26.17)') 'SAMPLED_INDIRECT_GAP_HA ', conduction_min - valence_max
333 : ELSE
334 0 : WRITE (io_unit, '(A)') 'INDIRECT_GAP_NOT_EVALUATED: no asserted common energy reference or not a lowest-band prefix'
335 : END IF
336 : END IF
337 0 : WRITE (io_unit, '(A)') 'MESH_CONVERGENCE_NOT_ESTABLISHED: repeat on finer meshes; retain unrounded C2 values.'
338 : CONTAINS
339 : ! **************************************************************************************************
340 : !> \brief Read one frame and update validation diagnostics on its first use.
341 : !> \param number One-based frame index in the extended mesh
342 : !> \param state Physical frame with selected coefficients
343 : ! **************************************************************************************************
344 0 : SUBROUTINE frame(number, state)
345 : INTEGER, INTENT(IN) :: number
346 : TYPE(snapshot_type), INTENT(OUT) :: state
347 :
348 : INTEGER :: iband
349 :
350 0 : CALL read_snapshot(TRIM(files(number)), points(number), state)
351 0 : IF (state%rank /= rank .OR. SIZE(state%atoms) /= nat) THEN
352 0 : CPABORT('Frame rank/atom count differs from manifest')
353 : END IF
354 0 : IF (.NOT. checked(number)) THEN
355 : CALL check_snapshot(state, tol_metric, tol_gap, err, gap)
356 0 : max_metric = MAX(max_metric, err)
357 0 : min_gap = MIN(min_gap, gap)
358 0 : checked(number) = .TRUE.
359 0 : DO iband = 1, rank
360 0 : IF (state%bands(iband) /= iband) prefix = .FALSE.
361 : END DO
362 0 : IF (rank >= SIZE(state%energies)) prefix = .FALSE.
363 0 : IF (prefix) THEN
364 0 : energy_shift = offsets(number)
365 0 : valence_max = MAX(valence_max, state%energies(rank) + energy_shift)
366 0 : conduction_min = MIN(conduction_min, state%energies(rank + 1) + energy_shift)
367 : END IF
368 : END IF
369 0 : END SUBROUTINE frame
370 : ! **************************************************************************************************
371 : !> \brief Read a previously validated link from the scratch stream.
372 : !> \param vertex_index One-based mesh vertex
373 : !> \param direction One-based parameter axis
374 : !> \param matrix Unitary selected-state link
375 : ! **************************************************************************************************
376 0 : SUBROUTINE read_link(vertex_index, direction, matrix)
377 : INTEGER, INTENT(IN) :: vertex_index, direction
378 : COMPLEX(KIND=dp), INTENT(OUT) :: matrix(:, :)
379 :
380 : INTEGER(KIND=int64) :: position
381 :
382 0 : position = (INT(vertex_index - 1, int64)*INT(dim, int64) + INT(direction - 1, int64))*record_size + 1_int64
383 0 : READ (store, POS=position, IOSTAT=stat) matrix
384 0 : CPASSERT(stat == 0)
385 0 : END SUBROUTINE read_link
386 : END SUBROUTINE analyze_mesh
387 :
388 : ! **************************************************************************************************
389 : !> \brief Compare two jointly refined meshes of the same declared family; never round the estimate.
390 : !> \param coarse Coarser mesh report
391 : !> \param fine Finer mesh report
392 : !> \param tolerance Maximum inter-mesh difference and distance to an integer
393 : !> \param io_unit Unit receiving convergence diagnostics
394 : ! **************************************************************************************************
395 0 : SUBROUTINE compare_refinements(coarse, fine, tolerance, io_unit)
396 : TYPE(phason_result_type), INTENT(IN) :: coarse, fine
397 : REAL(KIND=dp), INTENT(IN) :: tolerance
398 : INTEGER, INTENT(IN) :: io_unit
399 0 : TYPE(snapshot_type), ALLOCATABLE :: a, b
400 0 : COMPLEX(KIND=dp), ALLOCATABLE :: m(:, :), metric(:, :)
401 : INTEGER :: dim, i
402 : REAL(KIND=dp) :: difference, residual
403 0 : IF (.NOT. ieee_is_finite(tolerance)) THEN
404 0 : CPABORT('Nonfinite convergence tolerance')
405 : END IF
406 0 : IF (tolerance <= 0.0_dp .OR. tolerance >= 0.5_dp) THEN
407 0 : CPABORT('Invalid convergence tolerance')
408 : END IF
409 0 : dim = coarse%dimension
410 0 : IF (fine%dimension /= dim .OR. fine%rank /= coarse%rank .OR. fine%nat /= coarse%nat) THEN
411 0 : CPABORT('Incompatible refinement reports')
412 : END IF
413 0 : DO i = 1, dim
414 0 : IF (fine%shape(i) < 2*coarse%shape(i) .OR. MOD(fine%shape(i), coarse%shape(i)) /= 0) THEN
415 0 : CPABORT('Refine every mesh axis by an integer factor of at least two')
416 : END IF
417 : END DO
418 0 : IF (MAXVAL(ABS(coarse%origin - fine%origin)) > 1.e-10_dp .OR. &
419 : MAXVAL(ABS(coarse%directions - fine%directions)) > 1.e-10_dp) THEN
420 0 : CPABORT('Refinement changes the parameter origin or reciprocal periods')
421 : END IF
422 0 : IF (coarse%nat > 0) THEN
423 0 : ALLOCATE (a, b)
424 0 : CALL read_snapshot(TRIM(coarse%first_file), coarse%point, a)
425 0 : CALL read_snapshot(TRIM(fine%first_file), fine%point, b)
426 0 : ALLOCATE (m(a%rank, a%rank), metric(a%rank, a%rank))
427 0 : CALL snapshot_overlap(a, b, a%k, a%k, m)
428 0 : metric(:, :) = MATMUL(CONJG(TRANSPOSE(m)), m)
429 0 : DO i = 1, a%rank
430 0 : metric(i, i) = metric(i, i) - 1.0_dp
431 : END DO
432 0 : IF (MAXVAL(ABS(metric)) > 1.e-7_dp) THEN
433 0 : CPABORT('Refinement changes the initial physical subspace')
434 : END IF
435 : END IF
436 0 : difference = ABS(fine%value - coarse%value)
437 0 : residual = ABS(fine%value - ANINT(fine%value))
438 0 : WRITE (io_unit, '(A,ES26.17)') 'REFINEMENT_CHANGE ', difference
439 0 : WRITE (io_unit, '(A,ES26.17)') 'INTEGER_RESIDUAL ', residual
440 0 : IF (difference > tolerance .OR. residual > tolerance) THEN
441 0 : CPABORT('Chern mesh refinement has not converged to the requested tolerance')
442 : END IF
443 0 : WRITE (io_unit, '(A)') 'REFINEMENT_CHECK_PASSED: numerical check, not a certificate of a bulk gap.'
444 0 : END SUBROUTINE compare_refinements
445 0 : END MODULE topology_phason_analysis
|