Octopus
lennard_jones.F90
Go to the documentation of this file.
1!! Copyright (C) 2023 F. Bonafe
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
27 use, intrinsic :: iso_fortran_env
31
32 implicit none
33
34 private
35 public :: &
37
39 type, extends(force_interaction_t) :: lennard_jones_t
40 private
41 real(real64), pointer :: system_pos(:,:) => null()
42 real(real64), public :: lj_epsilon
43 real(real64), public :: lj_sigma
44
45 integer, public :: partner_np = 0
46 real(real64), allocatable, public :: partner_pos(:,:)
47
48 contains
49 procedure :: init => lennard_jones_init
50 procedure :: calculate => lennard_jones_calculate
51 procedure :: calculate_energy => lennard_jones_calculate_energy
53 end type lennard_jones_t
54
55 interface lennard_jones_t
56 module procedure lennard_jones_constructor
57 end interface lennard_jones_t
58
59contains
60
61 ! ---------------------------------------------------------
62 function lennard_jones_constructor(partner) result(this)
63 class(interaction_partner_t), target, intent(inout) :: partner
64 class(lennard_jones_t), pointer :: this
65
67
68 allocate(this)
69
70 this%label = "lennard_jones"
71
72 this%partner => partner
73
74 ! The Lennard Jones interaction needs the position of the particles and the parameters
75 this%system_quantities = ["position"]
76
77 ! The Lennard Jones interaction needs the position of the partner
78 this%couplings_from_partner = ["position"]
79
81 end function lennard_jones_constructor
82
83 ! ---------------------------------------------------------
84 subroutine lennard_jones_init(this, dim, system_np, system_pos, system_eps, system_sigma)
85 class(lennard_jones_t), intent(inout) :: this
86 integer, intent(in) :: dim
87 integer, intent(in) :: system_np
88 real(real64), target, intent(in) :: system_pos(:,:)
89 real(real64), intent(in) :: system_eps
90 real(real64), intent(in) :: system_sigma
91
92 push_sub(lennard_jones_init)
93
94 this%dim = dim
95 this%system_np = system_np
96 safe_allocate(this%force(1:dim, 1:system_np))
97 this%force = m_zero
98
99 this%lj_epsilon = system_eps
100 this%lj_sigma = system_sigma
101
102 this%system_pos => system_pos
103
104 pop_sub(lennard_jones_init)
105 end subroutine lennard_jones_init
106
107 ! ---------------------------------------------------------
108 subroutine lennard_jones_calculate(this)
109 class(lennard_jones_t), intent(inout) :: this
110
111 integer :: ip, jp
112 real(real64) :: dist, rr(1:this%dim), lj_force
113
115
116 assert(allocated(this%partner_pos))
117
118 do ip = 1, this%system_np
119 do jp = 1, this%partner_np
120 if (this%intra_interaction .and. ip == jp ) cycle
121
122 ! r_ij = r_i - r_j
123 rr(1:this%dim) = this%system_pos(1:this%dim, ip) - this%partner_pos(1:this%dim, jp)
124 dist = sqrt(sum(rr(1:this%dim)**2))
125
126 ! lj_force = -d U(|r_ij|) / d |r_ij|
127 lj_force = 48.0_real64 * this%lj_epsilon * (this%lj_sigma**12 / dist**13 - &
128 m_half * this%lj_sigma**6 / dist**7)
129
130 ! F_i = - d U(r) / d r_i = - (d U (r) / d |r_ij|) * (d r_ij / d r_i) =
131 ! = - d U (r) / d r_ij, because d r_ij / d r_i = 1
132 this%force(1:this%dim, ip) = rr(1:this%dim) / dist * lj_force
133 end do
134 end do
137 end subroutine lennard_jones_calculate
139 ! ---------------------------------------------------------
140 subroutine lennard_jones_calculate_energy(this)
141 class(lennard_jones_t), intent(inout) :: this
143 integer :: ip, jp
144 real(real64) :: dist
147
148 assert(allocated(this%partner_pos))
149
150 this%energy = m_zero
151 do ip = 1, this%system_np
152 do jp = 1, this%partner_np
153 if (this%intra_interaction .and. ip == jp ) cycle
154
155 dist = sqrt(sum((this%system_pos(1:this%dim, ip) - this%partner_pos(1:this%dim, jp))**2)) + m_epsilon
156
157 ! this%energy = this%energy + 0.5 * U(r_ij)
158 this%energy = this%energy + m_two * this%lj_epsilon * ( (this%lj_sigma / dist)**12 - &
159 (this%lj_sigma / dist)**6 )
160 end do
161 end do
162
164 end subroutine lennard_jones_calculate_energy
165
166
167 ! ---------------------------------------------------------
168 subroutine lennard_jones_finalize(this)
169 type(lennard_jones_t), intent(inout) :: this
170
171 push_sub(lennard_jones_finalize)
172
173 this%force = m_zero
174 nullify(this%system_pos)
175 safe_deallocate_a(this%partner_pos)
176 safe_deallocate_a(this%force)
178 call interaction_end(this)
179
181 end subroutine lennard_jones_finalize
182
183end module lennard_jones_oct_m
184
185!! Local Variables:
186!! mode: f90
187!! coding: utf-8
188!! End:
double sqrt(double __x) __attribute__((__nothrow__
real(real64), parameter, public m_two
Definition: global.F90:190
real(real64), parameter, public m_zero
Definition: global.F90:188
real(real64), parameter, public m_epsilon
Definition: global.F90:204
real(real64), parameter, public m_half
Definition: global.F90:194
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.
subroutine lennard_jones_calculate_energy(this)
subroutine lennard_jones_init(this, dim, system_np, system_pos, system_eps, system_sigma)
subroutine lennard_jones_calculate(this)
class(lennard_jones_t) function, pointer lennard_jones_constructor(partner)
subroutine lennard_jones_finalize(this)
Lennard-Jones interaction between two systems of particles.