Octopus
coulomb_force.F90
Go to the documentation of this file.
1!! Copyright (C) 2020 Heiko Appel
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17!!
18
19#include "global.h"
20
22 use debug_oct_m
24 use global_oct_m
31
32 implicit none
33
34 private
35 public :: &
37
39 type, extends(force_interaction_t) :: coulomb_force_t
40 private
41 real(real64), pointer :: system_charge(:) => null()
42 real(real64), pointer :: system_pos(:,:) => null()
43
44 integer, public :: partner_np = 0
45 real(real64), allocatable, public :: partner_charge(:)
46 real(real64), allocatable, public :: partner_pos(:,:)
47
48 contains
49 procedure :: init => coulomb_force_init
50 procedure :: calculate => coulomb_force_calculate
51 procedure :: calculate_energy => coulomb_force_calculate_energy
53 end type coulomb_force_t
54
55 interface coulomb_force_t
56 module procedure coulomb_force_constructor
57 end interface coulomb_force_t
58
59contains
60
61 ! ---------------------------------------------------------
62 function coulomb_force_constructor(partner) result(this)
63 class(interaction_partner_t), target, intent(inout) :: partner
64 class(coulomb_force_t), pointer :: this
65
67
68 allocate(this)
69
70 this%label = "coulomb_force"
71
72 this%partner => partner
73
74 ! The Coulomb force needs the position and charge of the system
75 this%system_quantities = [position, charge]
76
77 ! The Coulomb force needs the position and charge of the partner
78 this%couplings_from_partner = [position, charge]
79
81 end function coulomb_force_constructor
82
83 ! ---------------------------------------------------------
84 subroutine coulomb_force_init(this, dim, system_np, system_charge, system_pos)
85 class(coulomb_force_t), intent(inout) :: this
86 integer, intent(in) :: dim
87 integer, intent(in) :: system_np
88 real(real64), target, intent(in) :: system_charge(:)
89 real(real64), target, intent(in) :: system_pos(:,:)
90
91 push_sub(coulomb_force_init)
92
93 this%dim = dim
94 this%system_np = system_np
95 safe_allocate(this%force(1:dim, 1:system_np))
96 this%force = m_zero
97
98 this%system_charge => system_charge
99 this%system_pos => system_pos
100
101 pop_sub(coulomb_force_init)
102 end subroutine coulomb_force_init
103
104 ! ---------------------------------------------------------
105 subroutine coulomb_force_calculate(this)
106 class(coulomb_force_t), intent(inout) :: this
107
108 integer :: ip, jp
109 real(real64), parameter :: COULCONST = m_one ! Coulomb constant in atomic units
110 real(real64) :: dist3
111
113
114 assert(allocated(this%partner_charge))
115 assert(allocated(this%partner_pos))
116
117 do ip = 1, this%system_np
118 do jp = 1, this%partner_np
119 if (this%intra_interaction .and. ip == jp ) cycle
120
121 dist3 = sum((this%partner_pos(1:this%dim, jp) - this%system_pos(1:this%dim, ip))**2)**(m_three/m_two)
122
123 this%force(1:this%dim, ip) = -(this%partner_pos(1:this%dim, jp) - this%system_pos(1:this%dim, ip)) &
124 / (dist3 + m_epsilon) * (coulconst * this%system_charge(ip) * this%partner_charge(jp))
125 end do
126 end do
127
129 end subroutine coulomb_force_calculate
130
131 ! ---------------------------------------------------------
133 class(coulomb_force_t), intent(inout) :: this
135 integer :: ip, jp
136 real(real64), parameter :: coulconst = m_one ! Coulomb constant in atomic units
137 real(real64) :: dist
140
141 assert(allocated(this%partner_charge))
142 assert(allocated(this%partner_pos))
144 this%energy = m_zero
145 do ip = 1, this%system_np
146 do jp = 1, this%partner_np
147 if (this%intra_interaction .and. ip == jp ) cycle
148
149 dist = sqrt(sum((this%partner_pos(1:this%dim, jp) - this%system_pos(1:this%dim, ip))**2))
150
151 this%energy = this%energy + m_half / (dist + m_epsilon) * (coulconst * this%system_charge(ip) * this%partner_charge(jp))
152 end do
153 end do
154
156 end subroutine coulomb_force_calculate_energy
157
158
159 ! ---------------------------------------------------------
160 subroutine coulomb_force_finalize(this)
161 type(coulomb_force_t), intent(inout) :: this
162
163 push_sub(coulomb_force_finalize)
164
165 this%force = m_zero
166 nullify(this%system_charge)
167 nullify(this%system_pos)
168 safe_deallocate_a(this%partner_pos)
169 safe_deallocate_a(this%partner_charge)
170 safe_deallocate_a(this%force)
171
172 call interaction_end(this)
173
175 end subroutine coulomb_force_finalize
176
178
179!! Local Variables:
180!! mode: f90
181!! coding: utf-8
182!! End:
double sqrt(double __x) __attribute__((__nothrow__
subroutine coulomb_force_calculate_energy(this)
subroutine coulomb_force_calculate(this)
subroutine coulomb_force_finalize(this)
subroutine coulomb_force_init(this, dim, system_np, system_charge, system_pos)
class(coulomb_force_t) function, pointer coulomb_force_constructor(partner)
real(real64), parameter, public m_two
Definition: global.F90:189
real(real64), parameter, public m_zero
Definition: global.F90:187
real(real64), parameter, public m_epsilon
Definition: global.F90:203
real(real64), parameter, public m_half
Definition: global.F90:193
real(real64), parameter, public m_one
Definition: global.F90:188
real(real64), parameter, public m_three
Definition: global.F90:190
This module defines the abstract interaction_t class, and some auxiliary classes for interactions.
subroutine, public interaction_end(this)
This module defines classes and functions for interaction partners.
This module defines the quantity_t class and the IDs for quantities, which can be exposed by a system...
Definition: quantity.F90:137
integer, parameter, public position
Definition: quantity.F90:146
integer, parameter, public charge
Definition: quantity.F90:146
Coulomb interaction between two systems of particles.