86 CHARACTER(LEN=*),
INTENT(IN) :: path
87 LOGICAL,
INTENT(IN) :: physical
89 INTEGER,
INTENT(IN) :: io_unit
90 CHARACTER(LEN=256) ::
header
91 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 INTEGER,
ALLOCATABLE :: shape(:), extended(:), index(:), next(:), zero(:), points(:), permutation(:, :)
94 LOGICAL,
ALLOCATABLE :: checked(:)
95 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 COMPLEX(KIND=dp),
ALLOCATABLE :: raw(:, :), link(:, :), sewing(:, :), u(:, :, :), pqs(:, :, :)
101 LOGICAL :: common_reference, prefix
102 INTEGER(KIND=int64) :: record_size, record_start, total
104 CALL open_file(path, unit_number=input, file_status=
'OLD', file_action=
'READ')
105 READ (input,
'(A)', iostat=stat)
header
108 IF (trim(
header) /=
'CP2K_PHASON_MESH 1')
THEN
109 cpabort(
'Expected CP2K_PHASON_MESH 1')
112 IF (trim(
header) /=
'CP2K_TOPOLOGY_LINKS 1')
THEN
113 cpabort(
'Expected CP2K_TOPOLOGY_LINKS 1')
116 READ (input, *, iostat=stat) dim, rank
118 IF ((dim /= 2 .AND. dim /= 4) .OR. rank < 1)
THEN
119 cpabort(
'Invalid mesh dimension or rank')
121 ALLOCATE (shape(dim), extended(dim), index(dim), next(dim), zero(dim))
122 READ (input, *, iostat=stat) shape
124 IF (any(shape < 3))
THEN
125 cpabort(
'At least three samples per mesh axis are required')
129 IF (total > int(huge(1), int64)/int(shape(axis) + 1, int64))
THEN
130 cpabort(
'Mesh index overflow')
132 total = total*int(shape(axis) + 1, int64)
134 extended(:) = shape + 1
135 nvertex = product(shape)
137 report%dimension = dim
139 report%shape(1:dim) = shape
140 READ (input, *, iostat=stat) tol_metric, tol_gap, tol_sv, phase_limit
142 IF (.NOT. all(ieee_is_finite([tol_metric, tol_gap, tol_sv, phase_limit])))
THEN
143 cpabort(
'Nonfinite tolerances')
145 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 cpabort(
'Invalid mesh tolerances')
149 ALLOCATE (raw(rank, rank), link(rank, rank), sewing(rank, rank), u(rank, rank, 4), pqs(rank, rank, dim*(dim - 1)/2))
151 CALL open_file(unit_number=store, file_status=
'SCRATCH', file_access=
'STREAM', &
152 file_form=
'UNFORMATTED', file_action=
'READWRITE')
153 INQUIRE (unit=store, pos=record_start, iostat=stat)
155 minimum = huge(1.0_dp)
157 min_gap = huge(1.0_dp)
159 valence_max = -huge(1.0_dp)
160 conduction_min = huge(1.0_dp)
161 common_reference = .false.
164 ALLOCATE (a, b, base)
165 ALLOCATE (files(nframe), points(nframe), offsets(nframe), checked(nframe), directions(3, dim))
167 READ (input, *, iostat=stat) origin
169 IF (.NOT. all(ieee_is_finite(origin)))
THEN
170 cpabort(
'Nonfinite mesh origin')
173 READ (input, *, iostat=stat) directions(:, axis)
176 IF (.NOT. all(ieee_is_finite(directions)))
THEN
177 cpabort(
'Nonfinite reciprocal periods')
179 IF (maxval(abs(directions - anint(directions))) > 1.e-10_dp)
THEN
180 cpabort(
'Noninteger reciprocal periods')
182 READ (input, *, iostat=stat) nat, common_reference
185 cpabort(
'Invalid mesh atom count')
187 ALLOCATE (permutation(nat, dim))
189 READ (input, *, iostat=stat) permutation(:, axis)
191 IF (any(permutation(:, axis) < 1) .OR. any(permutation(:, axis) > nat))
THEN
192 cpabort(
'Invalid seam permutation')
195 IF (count(permutation(:, axis) ==
idx) /= 1)
THEN
196 cpabort(
'Non-bijective seam map')
202 IF (any(permutation(permutation(:, mu), nu) /= permutation(permutation(:, nu), mu)))
THEN
203 cpabort(
'The declared torus atom permutations do not commute')
208 READ (input, *, iostat=stat) files(
idx), points(
idx), offsets(
idx)
210 IF (points(
idx) < 1 .OR. .NOT. ieee_is_finite(offsets(
idx)))
THEN
211 cpabort(
'Invalid frame record')
215 report%origin(:) = origin
216 report%directions(:, 1:dim) = directions
217 report%first_file = files(1)
218 report%point = points(1)
220 DO vertex = 1, nvertex
221 CALL unflat(vertex, shape, index)
223 na =
flat(index, extended)
225 ka(:) = origin + matmul(directions, real(index,
dp)/real(shape,
dp))
230 next(axis) = next(axis) + 1
231 nb =
flat(next, extended)
233 kb(:) = origin + matmul(directions, real(next,
dp)/real(shape,
dp))
235 IF (next(axis) == shape(axis))
THEN
238 nbase =
flat(zero, extended)
239 CALL frame(nbase, base)
243 link(:, :) = matmul(conjg(transpose(sewing)), sewing)
247 err = maxval(abs(link))
248 seam_error = max(seam_error, err)
249 IF (err > tol_metric)
THEN
250 cpabort(
'Endpoint selected subspaces do not close')
252 CALL polar_link(sewing, link, local_min, stat, tol_sv)
254 cpabort(
'Invalid endpoint sewing link')
256 raw(:, :) = matmul(raw, link)
261 READ (input, *, iostat=stat) ka(1:2)
263 raw(i, j) = cmplx(ka(1), ka(2),
dp)
267 CALL polar_link(raw, link, local_min, stat, tol_sv)
269 cpabort(
'Singular/nonfinite mesh link; refine or check the subspace')
271 minimum = min(minimum, local_min)
272 WRITE (store, iostat=stat) link
274 IF (vertex == 1 .AND. axis == 1)
THEN
276 INQUIRE (unit=store, pos=record_size, iostat=stat)
278 record_size = record_size - record_start
283 READ (input,
'(A)', iostat=stat)
header
285 IF (stat /= 0 .OR. len_trim(
header) /= 0)
THEN
286 cpabort(
'Unexpected trailing mesh data')
291 compensation = 0.0_dp
293 DO vertex = 1, nvertex
294 CALL unflat(vertex, shape, index)
299 CALL read_link(vertex, mu, u(:, :, 1))
301 next(mu) = mod(next(mu) + 1, shape(mu))
302 CALL read_link(
flat(next, shape), nu, u(:, :, 2))
304 next(nu) = mod(next(nu) + 1, shape(nu))
305 CALL read_link(
flat(next, shape), mu, u(:, :, 3))
306 CALL read_link(vertex, nu, u(:, :, 4))
307 CALL link_plaquette(u(:, :, 1), u(:, :, 2), u(:, :, 3), u(:, :, 4), pqs(:, :, p))
312 cpabort(
'Unresolved/nonunitary plaquette; refine the mesh')
314 max_phase = max(max_phase, local_phase)
315 y = contribution - compensation
317 compensation = (t -
value) - y
322 WRITE (io_unit,
'(A,I0)')
'PARAMETER_DIMENSION ', dim
323 WRITE (io_unit,
'(A,*(I0,1X))')
'MESH ', shape
324 WRITE (io_unit,
'(A,ES26.17)')
'CHERN_RAW ',
value
325 WRITE (io_unit,
'(A,ES26.17)')
'MINIMUM_LINK_SINGULAR_VALUE ', minimum
326 WRITE (io_unit,
'(A,ES26.17)')
'MAXIMUM_PLAQUETTE_PHASE ', max_phase
328 WRITE (io_unit,
'(A,ES26.17)')
'MAXIMUM_METRIC_ERROR ', max_metric
329 WRITE (io_unit,
'(A,ES26.17)')
'MAXIMUM_SEAM_ERROR ', seam_error
330 WRITE (io_unit,
'(A,ES26.17)')
'MINIMUM_DIRECT_GAP_HA ', min_gap
331 IF (common_reference .AND. prefix)
THEN
332 WRITE (io_unit,
'(A,ES26.17)')
'SAMPLED_INDIRECT_GAP_HA ', conduction_min - valence_max
334 WRITE (io_unit,
'(A)')
'INDIRECT_GAP_NOT_EVALUATED: no asserted common energy reference or not a lowest-band prefix'
337 WRITE (io_unit,
'(A)')
'MESH_CONVERGENCE_NOT_ESTABLISHED: repeat on finer meshes; retain unrounded C2 values.'
344 SUBROUTINE frame(number, state)
345 INTEGER,
INTENT(IN) :: number
350 CALL read_snapshot(trim(files(number)), points(number), state)
351 IF (state%rank /= rank .OR.
SIZE(state%atoms) /= nat)
THEN
352 cpabort(
'Frame rank/atom count differs from manifest')
354 IF (.NOT. checked(number))
THEN
356 max_metric = max(max_metric, err)
357 min_gap = min(min_gap, gap)
358 checked(number) = .true.
360 IF (state%bands(iband) /= iband) prefix = .false.
362 IF (rank >=
SIZE(state%energies)) prefix = .false.
364 energy_shift = offsets(number)
365 valence_max = max(valence_max, state%energies(rank) + energy_shift)
366 conduction_min = min(conduction_min, state%energies(rank + 1) + energy_shift)
376 SUBROUTINE read_link(vertex_index, direction, matrix)
377 INTEGER,
INTENT(IN) :: vertex_index, direction
378 COMPLEX(KIND=dp),
INTENT(OUT) :: matrix(:, :)
380 INTEGER(KIND=int64) :: position
382 position = (int(vertex_index - 1, int64)*int(dim, int64) + int(direction - 1, int64))*record_size + 1_int64
383 READ (store, pos=position, iostat=stat) matrix
385 END SUBROUTINE read_link