230 TYPE(
nnp_type),
INTENT(INOUT),
POINTER :: nnp_env
231 CHARACTER(LEN=*),
INTENT(IN) :: printtag
233 CHARACTER(len=*),
PARAMETER :: routinen =
'nnp_init_model'
234 INTEGER,
PARAMETER :: def_str_len = 256, &
237 CHARACTER(len=1),
ALLOCATABLE,
DIMENSION(:) :: cactfnct
238 CHARACTER(len=2) :: ele
239 CHARACTER(len=def_str_len) :: dummy, line
240 CHARACTER(len=default_path_length) :: base_name, file_name
241 INTEGER :: handle, i, i_com, io, iweight, j, k, l, &
242 n_weight, nele, nuc_ele, symfnct_type, &
244 LOGICAL :: at_end, atom_e_found, explicit, first, &
246 REAL(kind=
dp) :: energy
247 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: weights
248 REAL(kind=
dp),
DIMENSION(7) :: test_array
249 REAL(kind=
dp),
DIMENSION(:),
POINTER :: work
254 CALL timeset(routinen, handle)
260 IF (logger%para_env%is_source())
THEN
262 WRITE (unit_nr, *)
""
263 WRITE (unit_nr, *) trim(printtag)//
"| Neural Network Potential Force Environment"
268 ALLOCATE (nnp_env%atomic_energy(nnp_env%num_atoms, nnp_env%n_committee))
269 ALLOCATE (nnp_env%committee_energy(nnp_env%n_committee))
270 ALLOCATE (nnp_env%myforce(3, nnp_env%num_atoms, nnp_env%n_committee))
271 ALLOCATE (nnp_env%committee_forces(3, nnp_env%num_atoms, nnp_env%n_committee))
272 ALLOCATE (nnp_env%committee_stress(3, 3, nnp_env%n_committee))
275 CALL parser_create(parser, file_name, para_env=logger%para_env)
278 nnp_env%scale_acsf = .false.
279 nnp_env%scale_sigma_acsf = .false.
281 nnp_env%scmin = 0.0_dp
282 nnp_env%scmax = 1.0_dp
283 nnp_env%center_acsf = .false.
284 nnp_env%normnodes = .false.
287 IF (logger%para_env%is_source())
THEN
289 WRITE (unit_nr, *) trim(printtag)//
"| Reading NNP input from file: ", trim(file_name)
293 search_from_begin_of_file=.true.)
295 READ (line, *) dummy, nnp_env%n_ele
297 CALL cp_abort(__location__, trim(printtag)// &
298 "| number of elements missing in NNP_INPUT_FILE")
302 search_from_begin_of_file=.true.)
303 nnp_env%scale_sigma_acsf = found
306 search_from_begin_of_file=.true.)
307 nnp_env%scale_acsf = found
311 IF (found .AND. nnp_env%scale_sigma_acsf)
THEN
312 cpwarn(
'Two scaling keywords in the input, we will ignore sigma scaling in this case')
313 nnp_env%scale_sigma_acsf = .false.
314 ELSE IF (.NOT. found .AND. nnp_env%scale_sigma_acsf)
THEN
315 nnp_env%scale_acsf = .false.
319 search_from_begin_of_file=.true.)
320 IF (found)
READ (line, *) dummy, nnp_env%scmin
323 search_from_begin_of_file=.true.)
324 IF (found)
READ (line, *) dummy, nnp_env%scmax
327 search_from_begin_of_file=.true.)
328 nnp_env%center_acsf = found
330 IF (nnp_env%scale_sigma_acsf .AND. nnp_env%center_acsf)
THEN
331 nnp_env%scale_sigma_acsf = .false.
334 IF (nnp_env%center_acsf .AND. nnp_env%scale_acsf)
THEN
335 IF ((abs(nnp_env%scmin) > epsilon(0.0_dp)*1.0e+4_dp) .OR. (abs(nnp_env%scmax - 1.0_dp) > epsilon(0.0_dp)*1.0e+4_dp))
THEN
336 CALL cp_warn(__location__, &
337 "Centering and scaling of symmetry functions requested while scale_min_short_atomic != 0 and/or "// &
338 "scale_max_short_atomic != 1. Make sure that scaling and centering of symmetry functions in CP2K "// &
339 "is consistent with your training code. "// &
340 "In CP2K: G* = (G - ave(G)) / (max(G) - min(G)) * (Smax - Smin) + Smin")
345 search_from_begin_of_file=.true.)
346 nnp_env%normnodes = found
349 search_from_begin_of_file=.true.)
351 READ (line, *) dummy, nnp_env%cut_type
353 CALL cp_abort(__location__, trim(printtag)// &
354 "| no cutoff type specified in NNP_INPUT_FILE")
358 search_from_begin_of_file=.true.)
360 READ (line, *) dummy, nnp_env%n_hlayer
362 CALL cp_abort(__location__, trim(printtag)// &
363 "| number of hidden layers missing in NNP_INPUT_FILE")
365 nnp_env%n_layer = nnp_env%n_hlayer + 2
368 ALLOCATE (nnp_env%rad(nele))
369 ALLOCATE (nnp_env%ang(nele))
370 ALLOCATE (nnp_env%n_rad(nele))
371 ALLOCATE (nnp_env%n_ang(nele))
372 ALLOCATE (nnp_env%actfnct(nnp_env%n_hlayer + 1))
373 ALLOCATE (cactfnct(nnp_env%n_hlayer + 1))
374 ALLOCATE (nnp_env%ele(nele))
375 ALLOCATE (nnp_env%nuc_ele(nele))
376 ALLOCATE (nnp_env%arc(nele))
378 ALLOCATE (nnp_env%arc(i)%layer(nnp_env%n_layer))
379 ALLOCATE (nnp_env%arc(i)%n_nodes(nnp_env%n_layer))
381 ALLOCATE (nnp_env%n_hnodes(nnp_env%n_hlayer))
382 ALLOCATE (nnp_env%atom_energies(nele))
383 nnp_env%atom_energies = 0.0_dp
391 IF (trim(adjustl(dummy)) ==
"elements")
THEN
392 READ (line, *) dummy, nnp_env%ele(:)
397 CALL cp_abort(__location__, trim(printtag)// &
398 "| elements not specified in NNP_INPUT_FILE")
403 search_from_begin_of_file=.true.)
405 IF (atom_e_found)
THEN
411 READ (line, *) dummy, ele, energy
413 IF (nnp_env%ele(j) == trim(ele))
THEN
415 nnp_env%atom_energies(j) = energy
420 CALL cp_abort(__location__, trim(printtag)// &
421 "| atom energies are not specified")
427 search_from_begin_of_file=.true.)
429 READ (line, *) dummy, nnp_env%n_hnodes(:)
431 CALL cp_abort(__location__, trim(printtag)// &
432 "NNP| global_nodes_short not specified in NNP_INPUT_FILE")
436 search_from_begin_of_file=.true.)
438 READ (line, *) dummy, cactfnct(:)
440 CALL cp_abort(__location__, trim(printtag)// &
441 "| global_activation_short not specified in NNP_INPUT_FILE")
444 DO i = 1, nnp_env%n_hlayer + 1
445 SELECT CASE (cactfnct(i))
465 CALL cp_abort(__location__, trim(printtag)// &
466 "| Activation function unkown")
482 READ (line, *) dummy, ele, symfnct_type
484 IF (trim(ele) == nnp_env%ele(i))
THEN
485 IF (symfnct_type == 2)
THEN
486 nnp_env%n_rad(i) = nnp_env%n_rad(i) + 1
487 ELSE IF (symfnct_type == 3)
THEN
488 nnp_env%n_ang(i) = nnp_env%n_ang(i) + 1
490 CALL cp_abort(__location__, trim(printtag)// &
491 "| Symmetry function type not supported")
497 IF (first)
CALL cp_abort(__location__, trim(printtag)// &
498 "| no symfunction_short specified in NNP_INPUT_FILE")
505 ALLOCATE (nnp_env%rad(i)%y(nnp_env%n_rad(i)))
506 ALLOCATE (nnp_env%rad(i)%funccut(nnp_env%n_rad(i)))
507 ALLOCATE (nnp_env%rad(i)%eta(nnp_env%n_rad(i)))
508 ALLOCATE (nnp_env%rad(i)%rs(nnp_env%n_rad(i)))
509 ALLOCATE (nnp_env%rad(i)%loc_min(nnp_env%n_rad(i)))
510 ALLOCATE (nnp_env%rad(i)%loc_max(nnp_env%n_rad(i)))
511 ALLOCATE (nnp_env%rad(i)%loc_av(nnp_env%n_rad(i)))
512 ALLOCATE (nnp_env%rad(i)%sigma(nnp_env%n_rad(i)))
513 ALLOCATE (nnp_env%rad(i)%ele(nnp_env%n_rad(i)))
514 ALLOCATE (nnp_env%rad(i)%nuc_ele(nnp_env%n_rad(i)))
515 nnp_env%rad(i)%funccut = 0.0_dp
516 nnp_env%rad(i)%eta = 0.0_dp
517 nnp_env%rad(i)%rs = 0.0_dp
518 nnp_env%rad(i)%ele =
'X'
519 nnp_env%rad(i)%nuc_ele = 0
521 ALLOCATE (nnp_env%ang(i)%y(nnp_env%n_ang(i)))
522 ALLOCATE (nnp_env%ang(i)%funccut(nnp_env%n_ang(i)))
523 ALLOCATE (nnp_env%ang(i)%eta(nnp_env%n_ang(i)))
524 ALLOCATE (nnp_env%ang(i)%zeta(nnp_env%n_ang(i)))
525 ALLOCATE (nnp_env%ang(i)%prefzeta(nnp_env%n_ang(i)))
526 ALLOCATE (nnp_env%ang(i)%lam(nnp_env%n_ang(i)))
527 ALLOCATE (nnp_env%ang(i)%loc_min(nnp_env%n_ang(i)))
528 ALLOCATE (nnp_env%ang(i)%loc_max(nnp_env%n_ang(i)))
529 ALLOCATE (nnp_env%ang(i)%loc_av(nnp_env%n_ang(i)))
530 ALLOCATE (nnp_env%ang(i)%sigma(nnp_env%n_ang(i)))
531 ALLOCATE (nnp_env%ang(i)%ele1(nnp_env%n_ang(i)))
532 ALLOCATE (nnp_env%ang(i)%ele2(nnp_env%n_ang(i)))
533 ALLOCATE (nnp_env%ang(i)%nuc_ele1(nnp_env%n_ang(i)))
534 ALLOCATE (nnp_env%ang(i)%nuc_ele2(nnp_env%n_ang(i)))
535 nnp_env%ang(i)%funccut = 0.0_dp
536 nnp_env%ang(i)%eta = 0.0_dp
537 nnp_env%ang(i)%zeta = 0.0_dp
538 nnp_env%ang(i)%prefzeta = 1.0_dp
539 nnp_env%ang(i)%lam = 0.0_dp
540 nnp_env%ang(i)%ele1 =
'X'
541 nnp_env%ang(i)%ele2 =
'X'
542 nnp_env%ang(i)%nuc_ele1 = 0
543 nnp_env%ang(i)%nuc_ele2 = 0
546 nnp_env%arc(i)%n_nodes(1) = nnp_env%n_rad(i) + nnp_env%n_ang(i)
547 nnp_env%arc(i)%n_nodes(2:nnp_env%n_layer - 1) = nnp_env%n_hnodes
548 nnp_env%arc(i)%n_nodes(nnp_env%n_layer) = 1
549 DO j = 1, nnp_env%n_layer
550 ALLOCATE (nnp_env%arc(i)%layer(j)%node(nnp_env%arc(i)%n_nodes(j)))
551 ALLOCATE (nnp_env%arc(i)%layer(j)%node_grad(nnp_env%arc(i)%n_nodes(j)))
552 ALLOCATE (nnp_env%arc(i)%layer(j)%tmp_der(nnp_env%arc(i)%n_nodes(1), nnp_env%arc(i)%n_nodes(j)))
563 nnp_env%max_cut = 0.0_dp
567 READ (line, *) dummy, ele, symfnct_type
569 IF (trim(ele) == nnp_env%ele(i))
THEN
570 IF (symfnct_type == 2)
THEN
571 nnp_env%n_rad(i) = nnp_env%n_rad(i) + 1
572 READ (line, *) dummy, ele, symfnct_type, &
573 nnp_env%rad(i)%ele(nnp_env%n_rad(i)), &
574 nnp_env%rad(i)%eta(nnp_env%n_rad(i)), &
575 nnp_env%rad(i)%rs(nnp_env%n_rad(i)), &
576 nnp_env%rad(i)%funccut(nnp_env%n_rad(i))
577 IF (nnp_env%max_cut < nnp_env%rad(i)%funccut(nnp_env%n_rad(i)))
THEN
578 nnp_env%max_cut = nnp_env%rad(i)%funccut(nnp_env%n_rad(i))
580 ELSE IF (symfnct_type == 3)
THEN
581 nnp_env%n_ang(i) = nnp_env%n_ang(i) + 1
582 READ (line, *) dummy, ele, symfnct_type, &
583 nnp_env%ang(i)%ele1(nnp_env%n_ang(i)), &
584 nnp_env%ang(i)%ele2(nnp_env%n_ang(i)), &
585 nnp_env%ang(i)%eta(nnp_env%n_ang(i)), &
586 nnp_env%ang(i)%lam(nnp_env%n_ang(i)), &
587 nnp_env%ang(i)%zeta(nnp_env%n_ang(i)), &
588 nnp_env%ang(i)%funccut(nnp_env%n_ang(i))
589 nnp_env%ang(i)%prefzeta(nnp_env%n_ang(i)) = &
590 2.0_dp**(1.0_dp - nnp_env%ang(i)%zeta(nnp_env%n_ang(i)))
591 IF (nnp_env%max_cut < nnp_env%ang(i)%funccut(nnp_env%n_ang(i)))
THEN
592 nnp_env%max_cut = nnp_env%ang(i)%funccut(nnp_env%n_ang(i))
595 CALL cp_abort(__location__, trim(printtag)// &
596 "| Symmetry function type not supported")
602 IF (first)
CALL cp_abort(__location__, trim(printtag)// &
603 "| no symfunction_short specified in NNP_INPUT_FILE")
610 DO j = 1, nnp_env%n_rad(i)
611 CALL get_ptable_info(nnp_env%rad(i)%ele(j), number=nnp_env%rad(i)%nuc_ele(j))
613 DO j = 1, nnp_env%n_ang(i)
614 CALL get_ptable_info(nnp_env%ang(i)%ele1(j), number=nnp_env%ang(i)%nuc_ele1(j))
615 CALL get_ptable_info(nnp_env%ang(i)%ele2(j), number=nnp_env%ang(i)%nuc_ele2(j))
617 IF (nnp_env%ang(i)%nuc_ele1(j) > nnp_env%ang(i)%nuc_ele2(j))
THEN
618 ele = nnp_env%ang(i)%ele1(j)
619 nnp_env%ang(i)%ele1(j) = nnp_env%ang(i)%ele2(j)
620 nnp_env%ang(i)%ele2(j) = ele
621 nuc_ele = nnp_env%ang(i)%nuc_ele1(j)
622 nnp_env%ang(i)%nuc_ele1(j) = nnp_env%ang(i)%nuc_ele2(j)
623 nnp_env%ang(i)%nuc_ele2(j) = nuc_ele
636 IF (nnp_env%scale_acsf .OR. nnp_env%center_acsf .OR. nnp_env%scale_sigma_acsf)
THEN
637 IF (logger%para_env%is_source())
THEN
638 WRITE (unit_nr, *) trim(printtag)//
"| Reading scaling information from file: ", trim(file_name)
642 CALL parser_create(parser, file_name, para_env=logger%para_env)
648 READ (parser%input_line, *, iostat=io) test_array(1:k)
654 IF (k == 5 .AND. nnp_env%scale_sigma_acsf)
THEN
655 cpabort(
"Sigma scaling requested, but scaling.data does not contain sigma.")
659 DO i = 1, nnp_env%n_ele
660 DO j = 1, nnp_env%n_rad(i)
662 IF (nnp_env%scale_sigma_acsf)
THEN
663 READ (parser%input_line, *) dummy, dummy, &
664 nnp_env%rad(i)%loc_min(j), &
665 nnp_env%rad(i)%loc_max(j), &
666 nnp_env%rad(i)%loc_av(j), &
667 nnp_env%rad(i)%sigma(j)
669 READ (parser%input_line, *) dummy, dummy, &
670 nnp_env%rad(i)%loc_min(j), &
671 nnp_env%rad(i)%loc_max(j), &
672 nnp_env%rad(i)%loc_av(j)
675 DO j = 1, nnp_env%n_ang(i)
677 IF (nnp_env%scale_sigma_acsf)
THEN
678 READ (parser%input_line, *) dummy, dummy, &
679 nnp_env%ang(i)%loc_min(j), &
680 nnp_env%ang(i)%loc_max(j), &
681 nnp_env%ang(i)%loc_av(j), &
682 nnp_env%ang(i)%sigma(j)
684 READ (parser%input_line, *) dummy, dummy, &
685 nnp_env%ang(i)%loc_min(j), &
686 nnp_env%ang(i)%loc_max(j), &
687 nnp_env%ang(i)%loc_av(j)
697 DO i = 1, nnp_env%n_ele
698 DO j = 2, nnp_env%n_layer
699 ALLOCATE (nnp_env%arc(i)%layer(j)%weights(nnp_env%arc(i)%n_nodes(j - 1), &
700 nnp_env%arc(i)%n_nodes(j), nnp_env%n_committee))
701 ALLOCATE (nnp_env%arc(i)%layer(j)%bweights(nnp_env%arc(i)%n_nodes(j), nnp_env%n_committee))
704 DO i_com = 1, nnp_env%n_committee
706 IF (logger%para_env%is_source())
THEN
707 WRITE (unit_nr, *) trim(printtag)//
"| Initializing weights for model: ", i_com
709 DO i = 1, nnp_env%n_ele
710 WRITE (file_name,
'(A,I0.3,A)') trim(base_name)//
".", nnp_env%nuc_ele(i),
".data"
711 IF (logger%para_env%is_source())
THEN
712 WRITE (unit_nr, *) trim(printtag)//
"| Reading weights from file: ", trim(file_name)
714 CALL parser_create(parser, file_name, para_env=logger%para_env)
719 n_weight = n_weight + 1
722 ALLOCATE (weights(n_weight))
727 READ (parser%input_line, *) weights(j)
733 DO j = 2, nnp_env%n_layer
734 DO k = 1, nnp_env%arc(i)%n_nodes(j - 1)
735 DO l = 1, nnp_env%arc(i)%n_nodes(j)
736 iweight = iweight + 1
737 nnp_env%arc(i)%layer(j)%weights(k, l, i_com) = weights(iweight)
741 DO k = 1, nnp_env%arc(i)%n_nodes(j)
742 iweight = iweight + 1
743 nnp_env%arc(i)%layer(j)%bweights(k, i_com) = weights(iweight)
755 NULLIFY (bias_section)
759 IF (
ASSOCIATED(bias_section))
CALL section_vals_get(bias_section, explicit=explicit)
760 nnp_env%bias = .false.
762 IF (nnp_env%n_committee > 1)
THEN
763 IF (logger%para_env%is_source())
THEN
764 WRITE (unit_nr, *)
"NNP| Biasing of committee disagreement enabled"
766 nnp_env%bias = .true.
767 ALLOCATE (nnp_env%bias_forces(3, nnp_env%num_atoms))
768 ALLOCATE (nnp_env%bias_e_avrg(nnp_env%n_committee))
771 nnp_env%bias_e_avrg(:) = 0.0_dp
773 nnp_env%bias_align = explicit
777 IF (
SIZE(work) /= nnp_env%n_committee)
THEN
778 cpabort(
"ALIGN_NNP_ENERGIES size mismatch wrt committee size.")
780 nnp_env%bias_e_avrg(:) = work
781 IF (logger%para_env%is_source())
THEN
782 WRITE (unit_nr, *) trim(printtag)//
"| Biasing is aligned by shifting the energy prediction of the C-NNP members"
786 cpwarn(
"NNP committee size is 1, BIAS section is ignored.")
790 IF (logger%para_env%is_source())
THEN
791 WRITE (unit_nr, *) trim(printtag)//
"| NNP force environment initialized"
794 CALL timestop(handle)