1!--------------------------------------------------------------------------------------------------!
2!   CP2K: A general program to perform molecular dynamics simulations                              !
3!   Copyright (C) 2000 - 2020  CP2K developers group                                               !
4!--------------------------------------------------------------------------------------------------!
5
6! **************************************************************************************************
7!> \brief initialize embed environment: clone of the mixed environment
8!> \author Vladimir Rybkin
9! **************************************************************************************************
10MODULE embed_environment
11   USE atomic_kind_types,               ONLY: atomic_kind_type
12   USE cell_methods,                    ONLY: read_cell,&
13                                              write_cell
14   USE cell_types,                      ONLY: cell_release,&
15                                              cell_type,&
16                                              get_cell
17   USE cp_para_types,                   ONLY: cp_para_env_type
18   USE cp_subsys_methods,               ONLY: cp_subsys_create
19   USE cp_subsys_types,                 ONLY: cp_subsys_release,&
20                                              cp_subsys_set,&
21                                              cp_subsys_type
22   USE distribution_1d_types,           ONLY: distribution_1d_release,&
23                                              distribution_1d_type
24   USE distribution_methods,            ONLY: distribute_molecules_1d
25   USE embed_types,                     ONLY: embed_env_type,&
26                                              set_embed_env
27   USE input_section_types,             ONLY: section_vals_get_subs_vals,&
28                                              section_vals_type
29   USE kinds,                           ONLY: dp
30   USE molecule_kind_types,             ONLY: molecule_kind_type,&
31                                              write_molecule_kind_set
32   USE molecule_types,                  ONLY: molecule_type
33   USE particle_types,                  ONLY: particle_type
34#include "./base/base_uses.f90"
35
36   IMPLICIT NONE
37
38   PRIVATE
39
40   CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'embed_environment'
41   PUBLIC :: embed_init
42
43CONTAINS
44
45! **************************************************************************************************
46!> \brief reads the input and database file for embedding
47!> \param embed_env ...
48!> \param root_section ...
49!> \param para_env ...
50!> \param force_env_section ...
51!> \param use_motion_section ...
52!> \par Used By
53!>      embed_main
54!> \author Vladimir Rybkin
55! **************************************************************************************************
56   SUBROUTINE embed_init(embed_env, root_section, para_env, force_env_section, &
57                         use_motion_section)
58
59      TYPE(embed_env_type), POINTER                      :: embed_env
60      TYPE(section_vals_type), POINTER                   :: root_section
61      TYPE(cp_para_env_type), POINTER                    :: para_env
62      TYPE(section_vals_type), POINTER                   :: force_env_section
63      LOGICAL, INTENT(IN)                                :: use_motion_section
64
65      CHARACTER(len=*), PARAMETER :: routineN = 'embed_init', routineP = moduleN//':'//routineN
66
67      INTEGER                                            :: handle
68      LOGICAL                                            :: use_ref_cell
69      REAL(KIND=dp), DIMENSION(3)                        :: abc
70      TYPE(cell_type), POINTER                           :: cell, cell_ref
71      TYPE(cp_subsys_type), POINTER                      :: subsys
72      TYPE(section_vals_type), POINTER                   :: cell_section, subsys_section
73
74      CALL timeset(routineN, handle)
75
76      NULLIFY (subsys, cell, cell_ref)
77      NULLIFY (cell_section)
78
79      subsys_section => section_vals_get_subs_vals(force_env_section, "SUBSYS")
80      cell_section => section_vals_get_subs_vals(subsys_section, "CELL")
81
82      CALL set_embed_env(embed_env, input=force_env_section)
83      CALL cp_subsys_create(subsys, para_env, root_section, &
84                            force_env_section=force_env_section, &
85                            use_motion_section=use_motion_section)
86
87      CALL read_cell(cell, cell_ref, use_ref_cell=use_ref_cell, &
88                     cell_section=cell_section, para_env=para_env)
89      CALL get_cell(cell, abc=abc)
90
91      ! Print the cell parameters ***
92      CALL write_cell(cell, subsys_section, cell_ref)
93
94      CALL embed_init_subsys(embed_env, subsys, cell, cell_ref, &
95                             force_env_section, subsys_section)
96
97      CALL cell_release(cell)
98      CALL cell_release(cell_ref)
99      CALL cp_subsys_release(subsys)
100
101      CALL timestop(handle)
102
103   END SUBROUTINE embed_init
104
105! **************************************************************************************************
106!> \brief   Read the input and the database files for the setup of the
107!>          embed environment.
108!> \param embed_env ...
109!> \param subsys ...
110!> \param cell ...
111!> \param cell_ref ...
112!> \param force_env_section ...
113!> \param subsys_section ...
114!> \date    02.2018
115!> \author  Vladimir Rybkin
116! **************************************************************************************************
117   SUBROUTINE embed_init_subsys(embed_env, subsys, cell, cell_ref, &
118                                force_env_section, subsys_section)
119
120      TYPE(embed_env_type), POINTER                      :: embed_env
121      TYPE(cp_subsys_type), POINTER                      :: subsys
122      TYPE(cell_type), POINTER                           :: cell, cell_ref
123      TYPE(section_vals_type), POINTER                   :: force_env_section, subsys_section
124
125      CHARACTER(len=*), PARAMETER :: routineN = 'embed_init_subsys', &
126         routineP = moduleN//':'//routineN
127
128      INTEGER                                            :: handle
129      TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
130      TYPE(distribution_1d_type), POINTER                :: local_molecules, local_particles
131      TYPE(molecule_kind_type), DIMENSION(:), POINTER    :: molecule_kind_set
132      TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
133      TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
134
135      CALL timeset(routineN, handle)
136      NULLIFY (local_molecules, local_particles)
137      particle_set => subsys%particles%els
138      atomic_kind_set => subsys%atomic_kinds%els
139      molecule_set => subsys%molecules%els
140      molecule_kind_set => subsys%molecule_kinds%els
141
142      ! Print the molecule kind set
143      CALL write_molecule_kind_set(molecule_kind_set, subsys_section)
144
145      ! Distribute molecules and atoms using the new data structures ***
146      CALL distribute_molecules_1d(atomic_kind_set=atomic_kind_set, &
147                                   particle_set=particle_set, &
148                                   local_particles=local_particles, &
149                                   molecule_kind_set=molecule_kind_set, &
150                                   molecule_set=molecule_set, &
151                                   local_molecules=local_molecules, &
152                                   force_env_section=force_env_section)
153
154      CALL cp_subsys_set(subsys, cell=cell)
155
156      ! set the embed_env
157      CALL set_embed_env(embed_env=embed_env, subsys=subsys)
158      CALL set_embed_env(embed_env=embed_env, &
159                         cell_ref=cell_ref, &
160                         local_molecules=local_molecules, &
161                         local_particles=local_particles)
162
163      CALL distribution_1d_release(local_particles)
164      CALL distribution_1d_release(local_molecules)
165
166      CALL timestop(handle)
167
168   END SUBROUTINE embed_init_subsys
169
170END MODULE embed_environment
171