247 TYPE(
nnp_type),
INTENT(INOUT),
POINTER :: nnp_env
248 CHARACTER(LEN=*),
INTENT(IN) :: printtag
250 CHARACTER(len=*),
PARAMETER :: routinen =
'nnp_init_model'
251 INTEGER,
PARAMETER :: def_str_len = 256, &
254 CHARACTER(len=1),
ALLOCATABLE,
DIMENSION(:) :: cactfnct
255 CHARACTER(len=2) :: ele
256 CHARACTER(len=def_str_len) :: dummy, line
257 CHARACTER(len=default_path_length) :: base_name, file_name
258 INTEGER :: handle, i, i_com, io, iweight, j, k, l, &
259 n_weight, nele, nuc_ele, symfnct_type, &
261 LOGICAL :: at_end, atom_e_found, explicit, first, &
263 REAL(kind=
dp) :: energy
264 REAL(kind=
dp),
ALLOCATABLE,
DIMENSION(:) :: weights
265 REAL(kind=
dp),
DIMENSION(7) :: test_array
266 REAL(kind=
dp),
DIMENSION(:),
POINTER :: work
271 CALL timeset(routinen, handle)
277 IF (logger%para_env%is_source())
THEN
279 WRITE (unit_nr, *)
""
280 WRITE (unit_nr, *) trim(printtag)//
"| Neural Network Potential Force Environment"
285 ALLOCATE (nnp_env%atomic_energy(nnp_env%num_atoms, nnp_env%n_committee))
286 ALLOCATE (nnp_env%committee_energy(nnp_env%n_committee))
287 ALLOCATE (nnp_env%myforce(3, nnp_env%num_atoms, nnp_env%n_committee))
288 ALLOCATE (nnp_env%committee_forces(3, nnp_env%num_atoms, nnp_env%n_committee))
289 ALLOCATE (nnp_env%committee_stress(3, 3, nnp_env%n_committee))
292 CALL parser_create(parser, file_name, para_env=logger%para_env)
295 nnp_env%scale_acsf = .false.
296 nnp_env%scale_sigma_acsf = .false.
298 nnp_env%scmin = 0.0_dp
299 nnp_env%scmax = 1.0_dp
300 nnp_env%center_acsf = .false.
301 nnp_env%normnodes = .false.
304 IF (logger%para_env%is_source())
THEN
306 WRITE (unit_nr, *) trim(printtag)//
"| Reading NNP input from file: ", trim(file_name)
310 search_from_begin_of_file=.true.)
312 READ (line, *) dummy, nnp_env%n_ele
314 CALL cp_abort(__location__, trim(printtag)// &
315 "| number of elements missing in NNP_INPUT_FILE")
319 search_from_begin_of_file=.true.)
320 nnp_env%scale_sigma_acsf = found
323 search_from_begin_of_file=.true.)
324 nnp_env%scale_acsf = found
328 IF (found .AND. nnp_env%scale_sigma_acsf)
THEN
329 cpwarn(
'Two scaling keywords in the input, we will ignore sigma scaling in this case')
330 nnp_env%scale_sigma_acsf = .false.
331 ELSE IF (.NOT. found .AND. nnp_env%scale_sigma_acsf)
THEN
332 nnp_env%scale_acsf = .false.
336 search_from_begin_of_file=.true.)
337 IF (found)
READ (line, *) dummy, nnp_env%scmin
340 search_from_begin_of_file=.true.)
341 IF (found)
READ (line, *) dummy, nnp_env%scmax
344 search_from_begin_of_file=.true.)
345 nnp_env%center_acsf = found
347 IF (nnp_env%scale_sigma_acsf .AND. nnp_env%center_acsf)
THEN
348 nnp_env%scale_sigma_acsf = .false.
352 search_from_begin_of_file=.true.)
353 nnp_env%normnodes = found
356 search_from_begin_of_file=.true.)
358 READ (line, *) dummy, nnp_env%cut_type
360 CALL cp_abort(__location__, trim(printtag)// &
361 "| no cutoff type specified in NNP_INPUT_FILE")
365 search_from_begin_of_file=.true.)
367 READ (line, *) dummy, nnp_env%n_hlayer
369 CALL cp_abort(__location__, trim(printtag)// &
370 "| number of hidden layers missing in NNP_INPUT_FILE")
372 nnp_env%n_layer = nnp_env%n_hlayer + 2
375 ALLOCATE (nnp_env%rad(nele))
376 ALLOCATE (nnp_env%ang(nele))
377 ALLOCATE (nnp_env%n_rad(nele))
378 ALLOCATE (nnp_env%n_ang(nele))
379 ALLOCATE (nnp_env%actfnct(nnp_env%n_hlayer + 1))
380 ALLOCATE (cactfnct(nnp_env%n_hlayer + 1))
381 ALLOCATE (nnp_env%ele(nele))
382 ALLOCATE (nnp_env%nuc_ele(nele))
383 ALLOCATE (nnp_env%arc(nele))
385 ALLOCATE (nnp_env%arc(i)%layer(nnp_env%n_layer))
386 ALLOCATE (nnp_env%arc(i)%n_nodes(nnp_env%n_layer))
388 ALLOCATE (nnp_env%n_hnodes(nnp_env%n_hlayer))
389 ALLOCATE (nnp_env%atom_energies(nele))
390 nnp_env%atom_energies = 0.0_dp
398 IF (trim(adjustl(dummy)) ==
"elements")
THEN
399 READ (line, *) dummy, nnp_env%ele(:)
404 CALL cp_abort(__location__, trim(printtag)// &
405 "| elements not specified in NNP_INPUT_FILE")
410 search_from_begin_of_file=.true.)
412 IF (atom_e_found)
THEN
418 READ (line, *) dummy, ele, energy
420 IF (nnp_env%ele(j) == trim(ele))
THEN
422 nnp_env%atom_energies(j) = energy
427 CALL cp_abort(__location__, trim(printtag)// &
428 "| atom energies are not specified")
434 search_from_begin_of_file=.true.)
436 READ (line, *) dummy, nnp_env%n_hnodes(:)
438 CALL cp_abort(__location__, trim(printtag)// &
439 "NNP| global_nodes_short not specified in NNP_INPUT_FILE")
443 search_from_begin_of_file=.true.)
445 READ (line, *) dummy, cactfnct(:)
447 CALL cp_abort(__location__, trim(printtag)// &
448 "| global_activation_short not specified in NNP_INPUT_FILE")
451 DO i = 1, nnp_env%n_hlayer + 1
452 SELECT CASE (cactfnct(i))
472 CALL cp_abort(__location__, trim(printtag)// &
473 "| Activation function unkown")
489 READ (line, *) dummy, ele, symfnct_type
491 IF (trim(ele) .EQ. nnp_env%ele(i))
THEN
492 IF (symfnct_type .EQ. 2)
THEN
493 nnp_env%n_rad(i) = nnp_env%n_rad(i) + 1
494 ELSE IF (symfnct_type .EQ. 3)
THEN
495 nnp_env%n_ang(i) = nnp_env%n_ang(i) + 1
497 CALL cp_abort(__location__, trim(printtag)// &
498 "| Symmetry function type not supported")
504 IF (first)
CALL cp_abort(__location__, trim(printtag)// &
505 "| no symfunction_short specified in NNP_INPUT_FILE")
512 ALLOCATE (nnp_env%rad(i)%y(nnp_env%n_rad(i)))
513 ALLOCATE (nnp_env%rad(i)%funccut(nnp_env%n_rad(i)))
514 ALLOCATE (nnp_env%rad(i)%eta(nnp_env%n_rad(i)))
515 ALLOCATE (nnp_env%rad(i)%rs(nnp_env%n_rad(i)))
516 ALLOCATE (nnp_env%rad(i)%loc_min(nnp_env%n_rad(i)))
517 ALLOCATE (nnp_env%rad(i)%loc_max(nnp_env%n_rad(i)))
518 ALLOCATE (nnp_env%rad(i)%loc_av(nnp_env%n_rad(i)))
519 ALLOCATE (nnp_env%rad(i)%sigma(nnp_env%n_rad(i)))
520 ALLOCATE (nnp_env%rad(i)%ele(nnp_env%n_rad(i)))
521 ALLOCATE (nnp_env%rad(i)%nuc_ele(nnp_env%n_rad(i)))
522 nnp_env%rad(i)%funccut = 0.0_dp
523 nnp_env%rad(i)%eta = 0.0_dp
524 nnp_env%rad(i)%rs = 0.0_dp
525 nnp_env%rad(i)%ele =
'X'
526 nnp_env%rad(i)%nuc_ele = 0
528 ALLOCATE (nnp_env%ang(i)%y(nnp_env%n_ang(i)))
529 ALLOCATE (nnp_env%ang(i)%funccut(nnp_env%n_ang(i)))
530 ALLOCATE (nnp_env%ang(i)%eta(nnp_env%n_ang(i)))
531 ALLOCATE (nnp_env%ang(i)%zeta(nnp_env%n_ang(i)))
532 ALLOCATE (nnp_env%ang(i)%prefzeta(nnp_env%n_ang(i)))
533 ALLOCATE (nnp_env%ang(i)%lam(nnp_env%n_ang(i)))
534 ALLOCATE (nnp_env%ang(i)%loc_min(nnp_env%n_ang(i)))
535 ALLOCATE (nnp_env%ang(i)%loc_max(nnp_env%n_ang(i)))
536 ALLOCATE (nnp_env%ang(i)%loc_av(nnp_env%n_ang(i)))
537 ALLOCATE (nnp_env%ang(i)%sigma(nnp_env%n_ang(i)))
538 ALLOCATE (nnp_env%ang(i)%ele1(nnp_env%n_ang(i)))
539 ALLOCATE (nnp_env%ang(i)%ele2(nnp_env%n_ang(i)))
540 ALLOCATE (nnp_env%ang(i)%nuc_ele1(nnp_env%n_ang(i)))
541 ALLOCATE (nnp_env%ang(i)%nuc_ele2(nnp_env%n_ang(i)))
542 nnp_env%ang(i)%funccut = 0.0_dp
543 nnp_env%ang(i)%eta = 0.0_dp
544 nnp_env%ang(i)%zeta = 0.0_dp
545 nnp_env%ang(i)%prefzeta = 1.0_dp
546 nnp_env%ang(i)%lam = 0.0_dp
547 nnp_env%ang(i)%ele1 =
'X'
548 nnp_env%ang(i)%ele2 =
'X'
549 nnp_env%ang(i)%nuc_ele1 = 0
550 nnp_env%ang(i)%nuc_ele2 = 0
553 nnp_env%arc(i)%n_nodes(1) = nnp_env%n_rad(i) + nnp_env%n_ang(i)
554 nnp_env%arc(i)%n_nodes(2:nnp_env%n_layer - 1) = nnp_env%n_hnodes
555 nnp_env%arc(i)%n_nodes(nnp_env%n_layer) = 1
556 DO j = 1, nnp_env%n_layer
557 ALLOCATE (nnp_env%arc(i)%layer(j)%node(nnp_env%arc(i)%n_nodes(j)))
558 ALLOCATE (nnp_env%arc(i)%layer(j)%node_grad(nnp_env%arc(i)%n_nodes(j)))
559 ALLOCATE (nnp_env%arc(i)%layer(j)%tmp_der(nnp_env%arc(i)%n_nodes(1), nnp_env%arc(i)%n_nodes(j)))
570 nnp_env%max_cut = 0.0_dp
574 READ (line, *) dummy, ele, symfnct_type
576 IF (trim(ele) .EQ. nnp_env%ele(i))
THEN
577 IF (symfnct_type .EQ. 2)
THEN
578 nnp_env%n_rad(i) = nnp_env%n_rad(i) + 1
579 READ (line, *) dummy, ele, symfnct_type, &
580 nnp_env%rad(i)%ele(nnp_env%n_rad(i)), &
581 nnp_env%rad(i)%eta(nnp_env%n_rad(i)), &
582 nnp_env%rad(i)%rs(nnp_env%n_rad(i)), &
583 nnp_env%rad(i)%funccut(nnp_env%n_rad(i))
584 IF (nnp_env%max_cut < nnp_env%rad(i)%funccut(nnp_env%n_rad(i)))
THEN
585 nnp_env%max_cut = nnp_env%rad(i)%funccut(nnp_env%n_rad(i))
587 ELSE IF (symfnct_type .EQ. 3)
THEN
588 nnp_env%n_ang(i) = nnp_env%n_ang(i) + 1
589 READ (line, *) dummy, ele, symfnct_type, &
590 nnp_env%ang(i)%ele1(nnp_env%n_ang(i)), &
591 nnp_env%ang(i)%ele2(nnp_env%n_ang(i)), &
592 nnp_env%ang(i)%eta(nnp_env%n_ang(i)), &
593 nnp_env%ang(i)%lam(nnp_env%n_ang(i)), &
594 nnp_env%ang(i)%zeta(nnp_env%n_ang(i)), &
595 nnp_env%ang(i)%funccut(nnp_env%n_ang(i))
596 nnp_env%ang(i)%prefzeta(nnp_env%n_ang(i)) = &
597 2.0_dp**(1.0_dp - nnp_env%ang(i)%zeta(nnp_env%n_ang(i)))
598 IF (nnp_env%max_cut < nnp_env%ang(i)%funccut(nnp_env%n_ang(i)))
THEN
599 nnp_env%max_cut = nnp_env%ang(i)%funccut(nnp_env%n_ang(i))
602 CALL cp_abort(__location__, trim(printtag)// &
603 "| Symmetry function type not supported")
609 IF (first)
CALL cp_abort(__location__, trim(printtag)// &
610 "| no symfunction_short specified in NNP_INPUT_FILE")
617 DO j = 1, nnp_env%n_rad(i)
618 CALL get_ptable_info(nnp_env%rad(i)%ele(j), number=nnp_env%rad(i)%nuc_ele(j))
620 DO j = 1, nnp_env%n_ang(i)
621 CALL get_ptable_info(nnp_env%ang(i)%ele1(j), number=nnp_env%ang(i)%nuc_ele1(j))
622 CALL get_ptable_info(nnp_env%ang(i)%ele2(j), number=nnp_env%ang(i)%nuc_ele2(j))
624 IF (nnp_env%ang(i)%nuc_ele1(j) .GT. nnp_env%ang(i)%nuc_ele2(j))
THEN
625 ele = nnp_env%ang(i)%ele1(j)
626 nnp_env%ang(i)%ele1(j) = nnp_env%ang(i)%ele2(j)
627 nnp_env%ang(i)%ele2(j) = ele
628 nuc_ele = nnp_env%ang(i)%nuc_ele1(j)
629 nnp_env%ang(i)%nuc_ele1(j) = nnp_env%ang(i)%nuc_ele2(j)
630 nnp_env%ang(i)%nuc_ele2(j) = nuc_ele
643 IF (nnp_env%scale_acsf .OR. nnp_env%center_acsf .OR. nnp_env%scale_sigma_acsf)
THEN
644 IF (logger%para_env%is_source())
THEN
645 WRITE (unit_nr, *) trim(printtag)//
"| Reading scaling information from file: ", trim(file_name)
649 CALL parser_create(parser, file_name, para_env=logger%para_env)
655 READ (parser%input_line, *, iostat=io) test_array(1:k)
661 IF (k == 5 .AND. nnp_env%scale_sigma_acsf)
THEN
662 cpabort(
"Sigma scaling requested, but scaling.data does not contain sigma.")
666 DO i = 1, nnp_env%n_ele
667 DO j = 1, nnp_env%n_rad(i)
669 IF (nnp_env%scale_sigma_acsf)
THEN
670 READ (parser%input_line, *) dummy, dummy, &
671 nnp_env%rad(i)%loc_min(j), &
672 nnp_env%rad(i)%loc_max(j), &
673 nnp_env%rad(i)%loc_av(j), &
674 nnp_env%rad(i)%sigma(j)
676 READ (parser%input_line, *) dummy, dummy, &
677 nnp_env%rad(i)%loc_min(j), &
678 nnp_env%rad(i)%loc_max(j), &
679 nnp_env%rad(i)%loc_av(j)
682 DO j = 1, nnp_env%n_ang(i)
684 IF (nnp_env%scale_sigma_acsf)
THEN
685 READ (parser%input_line, *) dummy, dummy, &
686 nnp_env%ang(i)%loc_min(j), &
687 nnp_env%ang(i)%loc_max(j), &
688 nnp_env%ang(i)%loc_av(j), &
689 nnp_env%ang(i)%sigma(j)
691 READ (parser%input_line, *) dummy, dummy, &
692 nnp_env%ang(i)%loc_min(j), &
693 nnp_env%ang(i)%loc_max(j), &
694 nnp_env%ang(i)%loc_av(j)
704 DO i = 1, nnp_env%n_ele
705 DO j = 2, nnp_env%n_layer
706 ALLOCATE (nnp_env%arc(i)%layer(j)%weights(nnp_env%arc(i)%n_nodes(j - 1), &
707 nnp_env%arc(i)%n_nodes(j), nnp_env%n_committee))
708 ALLOCATE (nnp_env%arc(i)%layer(j)%bweights(nnp_env%arc(i)%n_nodes(j), nnp_env%n_committee))
711 DO i_com = 1, nnp_env%n_committee
713 IF (logger%para_env%is_source())
THEN
714 WRITE (unit_nr, *) trim(printtag)//
"| Initializing weights for model: ", i_com
716 DO i = 1, nnp_env%n_ele
717 WRITE (file_name,
'(A,I0.3,A)') trim(base_name)//
".", nnp_env%nuc_ele(i),
".data"
718 IF (logger%para_env%is_source())
THEN
719 WRITE (unit_nr, *) trim(printtag)//
"| Reading weights from file: ", trim(file_name)
721 CALL parser_create(parser, file_name, para_env=logger%para_env)
726 n_weight = n_weight + 1
729 ALLOCATE (weights(n_weight))
734 READ (parser%input_line, *) weights(j)
740 DO j = 2, nnp_env%n_layer
741 DO k = 1, nnp_env%arc(i)%n_nodes(j - 1)
742 DO l = 1, nnp_env%arc(i)%n_nodes(j)
743 iweight = iweight + 1
744 nnp_env%arc(i)%layer(j)%weights(k, l, i_com) = weights(iweight)
748 DO k = 1, nnp_env%arc(i)%n_nodes(j)
749 iweight = iweight + 1
750 nnp_env%arc(i)%layer(j)%bweights(k, i_com) = weights(iweight)
762 NULLIFY (bias_section)
766 IF (
ASSOCIATED(bias_section))
CALL section_vals_get(bias_section, explicit=explicit)
767 nnp_env%bias = .false.
769 IF (nnp_env%n_committee > 1)
THEN
770 IF (logger%para_env%is_source())
THEN
771 WRITE (unit_nr, *)
"NNP| Biasing of committee disagreement enabled"
773 nnp_env%bias = .true.
774 ALLOCATE (nnp_env%bias_forces(3, nnp_env%num_atoms))
775 ALLOCATE (nnp_env%bias_e_avrg(nnp_env%n_committee))
778 nnp_env%bias_e_avrg(:) = 0.0_dp
780 nnp_env%bias_align = explicit
784 IF (
SIZE(work) .NE. nnp_env%n_committee)
THEN
785 cpabort(
"ALIGN_NNP_ENERGIES size mismatch wrt committee size.")
787 nnp_env%bias_e_avrg(:) = work
788 IF (logger%para_env%is_source())
THEN
789 WRITE (unit_nr, *) trim(printtag)//
"| Biasing is aligned by shifting the energy prediction of the C-NNP members"
793 cpwarn(
"NNP committee size is 1, BIAS section is ignored.")
797 IF (logger%para_env%is_source())
THEN
798 WRITE (unit_nr, *) trim(printtag)//
"| NNP force environment initialized"
801 CALL timestop(handle)