Octopus
gravity.F90
Go to the documentation of this file.
1!! Copyright (C) 2020 M. Oliveira, 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
21module gravity_oct_m
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
41 type, extends(force_interaction_t) :: gravity_t
42 private
43 real(real64), pointer :: system_mass(:) => null()
44 real(real64), pointer :: system_pos(:,:) => null()
45
46 integer, public :: partner_np = 0
47 real(real64), allocatable, public :: partner_mass(:)
48 real(real64), allocatable, public :: partner_pos(:,:)
49
50 contains
51 procedure :: init => gravity_init
52 procedure :: calculate => gravity_calculate
53 procedure :: calculate_energy => gravity_calculate_energy
54 final :: gravity_finalize
55 end type gravity_t
56
57 interface gravity_t
58 module procedure gravity_constructor
59 end interface gravity_t
60
61contains
62 ! ---------------------------------------------------------
63 function gravity_constructor(partner) result(this)
64 class(interaction_partner_t), target, intent(inout) :: partner
65 class(gravity_t), pointer :: this
66
67 push_sub(gravity_constructor)
68
69 allocate(this)
70
71 this%label = "gravity"
72
73 this%partner => partner
74
75 ! Gravity interaction needs two quantities from each system: the position and the mass
76 ! From the sytem:
77 this%system_quantities = [character(8) :: "position", "mass"]
78
79 ! From the partner:
80 this%couplings_from_partner = [character(8) :: "position", "mass"]
81
82 pop_sub(gravity_constructor)
83 end function gravity_constructor
84
85 ! ---------------------------------------------------------
86 subroutine gravity_init(this, dim, system_np, system_mass, system_pos)
87 class(gravity_t), intent(inout) :: this
88 integer, intent(in) :: dim
89 integer, intent(in) :: system_np
90 real(real64), target, intent(in) :: system_mass(:)
91 real(real64), target, intent(in) :: system_pos(:,:)
92
93 push_sub(gravity_init)
94
95 this%dim = dim
96 this%system_np = system_np
97 safe_allocate(this%force(1:dim, 1:system_np))
98 this%force = m_zero
99 this%system_mass => system_mass
100 this%system_pos => system_pos
101
102 pop_sub(gravity_init)
103 end subroutine gravity_init
104
105 ! ---------------------------------------------------------
106 subroutine gravity_calculate(this)
107 class(gravity_t), intent(inout) :: this
108
109 integer :: ip, jp
110 real(real64), parameter :: GG = 6.67430e-11_real64 ! In S.I. units!
111 real(real64) :: dist3
112 push_sub(gravity_calculate)
113
114 assert(allocated(this%partner_mass))
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) * (gg * this%system_mass(ip) * this%partner_mass(jp))
125 end do
126 end do
127
128 pop_sub(gravity_calculate)
129 end subroutine gravity_calculate
130
131 ! ---------------------------------------------------------
132 subroutine gravity_calculate_energy(this)
133 class(gravity_t), intent(inout) :: this
135 integer :: ip, jp
136 real(real64), parameter :: gg = 6.67430e-11_real64 ! In S.I. units!
137 real(real64) :: dist
138
141 assert(allocated(this%partner_mass))
142 assert(allocated(this%partner_pos))
143
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) * (gg * this%system_mass(ip) * this%partner_mass(jp))
152 end do
153 end do
154
157
158
159 ! ---------------------------------------------------------
160 subroutine gravity_finalize(this)
161 type(gravity_t), intent(inout) :: this
162
163 push_sub(gravity_finalize)
164
165 this%force = m_zero
166 nullify(this%system_mass)
167 nullify(this%system_pos)
168 safe_deallocate_a(this%partner_pos)
169 safe_deallocate_a(this%partner_mass)
170 safe_deallocate_a(this%force)
171
172 call interaction_end(this)
173
174 pop_sub(gravity_finalize)
175 end subroutine gravity_finalize
176
177end module gravity_oct_m
178
179!! Local Variables:
180!! mode: f90
181!! coding: utf-8
182!! 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
real(real64), parameter, public m_three
Definition: global.F90:191
subroutine gravity_calculate_energy(this)
Definition: gravity.F90:226
subroutine gravity_calculate(this)
Definition: gravity.F90:200
class(gravity_t) function, pointer gravity_constructor(partner)
Definition: gravity.F90:157
subroutine gravity_init(this, dim, system_np, system_mass, system_pos)
Definition: gravity.F90:180
subroutine gravity_finalize(this)
Definition: gravity.F90:254
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.
Gravity interaction between two systems of particles. This should be used for testing purposes only....
Definition: gravity.F90:134