sources_gravity.f90
Go to the documentation of this file.
1 !#############################################################################
2 !# #
3 !# fosite - 3D hydrodynamical simulation program #
4 !# module: sources_gravity.f90 #
5 !# #
6 !# Copyright (C) 2014-2018 #
7 !# Björn Sperling <sperling@astrophysik.uni-kiel.de> #
8 !# Tobias Illenseer <tillense@astrophysik.uni-kiel.de> #
9 !# Jannes Klee <jklee@astrophysik.uni-kiel.de> #
10 !# #
11 !# This program is free software; you can redistribute it and/or modify #
12 !# it under the terms of the GNU General Public License as published by #
13 !# the Free Software Foundation; either version 2 of the License, or (at #
14 !# your option) any later version. #
15 !# #
16 !# This program is distributed in the hope that it will be useful, but #
17 !# WITHOUT ANY WARRANTY; without even the implied warranty of #
18 !# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or #
19 !# NON INFRINGEMENT. See the GNU General Public License for more #
20 !# details. #
21 !# #
22 !# You should have received a copy of the GNU General Public License #
23 !# along with this program; if not, write to the Free Software #
24 !# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. #
25 !# #
26 !#############################################################################
30 !----------------------------------------------------------------------------!
40 !----------------------------------------------------------------------------!
43  USE mesh_base_mod
44  USE marray_base_mod
51  USE fluxes_base_mod
53  USE common_dict
54  IMPLICIT NONE
55  !--------------------------------------------------------------------------!
56  PRIVATE
57  CHARACTER(LEN=32), PARAMETER :: source_name = "gravity"
58  TYPE, EXTENDS(sources_c_accel) :: sources_gravity
59  CLASS(gravity_base), POINTER :: glist => null()
62  LOGICAL :: addtoenergy
63  CONTAINS
64  PROCEDURE :: initsources_gravity
65  PROCEDURE :: infosources
66  PROCEDURE :: updategravity
68  PROCEDURE :: calcdiskheight
69  PROCEDURE :: finalize
70  END TYPE sources_gravity
71  abstract INTERFACE
72  END INTERFACE
73  !--------------------------------------------------------------------------!
74  PUBLIC :: &
75  ! types
77  !--------------------------------------------------------------------------!
78 
79 CONTAINS
80 
81  SUBROUTINE initsources_gravity(this,Mesh,Physics,Fluxes,config,IO)
82  IMPLICIT NONE
83  !------------------------------------------------------------------------!
84  CLASS(sources_gravity), INTENT(INOUT) :: this
85  CLASS(mesh_base), INTENT(IN) :: Mesh
86  CLASS(fluxes_base), INTENT(IN) :: Fluxes
87  CLASS(physics_base), INTENT(IN) :: Physics
88  TYPE(Dict_TYP), POINTER :: config, IO
89  !------------------------------------------------------------------------!
90  INTEGER :: err, stype,i, valwrite
91  !------------------------------------------------------------------------!
92  CALL getattr(config,"stype",stype)
93  CALL this%InitLogging(stype,source_name)
94  CALL this%InitSources(mesh,fluxes,physics,config,io)
95 
96  this%accel = marray_base(physics%VDIM)
97  this%accel%data1d(:) = 0.
98  this%pot = marray_base(4)
99  this%pot%data1d(:) = 0.
100 
101  ! Add source terms to energy equation?
102  ! Set this to zero, if a potential is defined in physics_euler2Diamt
103  CALL getattr(config, "energy", i, 1)
104  IF(i.EQ.0) THEN
105  this%addtoenergy = .false.
106  ELSE
107  this%addtoenergy = .true.
108  END IF
109 
110  ! enable update of disk scale height if requested
111  CALL getattr(config, "update_disk_height", i, 0)
112  IF (i.EQ.1) THEN
115  IF (physics%VDIM.EQ.2) THEN
116  this%update_disk_height = .true.
117 
118  this%height = marray_base()
119  this%h_ext= marray_base()
120  this%invheight2 = marray_base()
121  this%height%data1d = 0.0
122  this%h_ext%data1d = 0.0
123  this%invheight2%data1d = 0.0
124  ELSE
125  CALL this%Error("InitGravity", "DiskHeight is only supported in 2D")
126  END IF
127  ELSE
128  this%update_disk_height = .false.
129  END IF
130 
131  ! initialize gravity
132  CALL new_gravity(this%glist,mesh,fluxes,physics,config,io)
133 
134  CALL getattr(config, "output/height", valwrite, 0)
135  IF (valwrite .EQ. 1) THEN
136  CALL setattr(config, "update_disk_height", 1)
137  IF (.NOT.ASSOCIATED(this%height%data1d)) THEN
138  ALLOCATE(this%height%data3d(mesh%IGMIN:mesh%IGMAX,mesh%JGMIN:mesh%JGMAX,mesh%KGMIN:mesh%KGMAX), &
139  stat=err)
140  IF (err.NE.0) CALL this%Error("SetOutput", &
141  "Memory allocation failed for this%height!")
142  END IF
143  CALL setattr(io, "height", &
144  this%height%data3d(mesh%IMIN:mesh%IMAX,mesh%JMIN:mesh%JMAX,mesh%KMIN:mesh%KMAX))
145  END IF
146 
147  END SUBROUTINE initsources_gravity
148 
153  SUBROUTINE externalsources_single(this,Mesh,Physics,Fluxes,Sources,time,dt,pvar,cvar,sterm)
156  IMPLICIT NONE
157  !------------------------------------------------------------------------!
158  CLASS(sources_gravity), INTENT(INOUT) :: this
159  CLASS(mesh_base), INTENT(IN) :: Mesh
160  CLASS(physics_base), INTENT(INOUT) :: Physics
161  CLASS(fluxes_base), INTENT(IN) :: Fluxes
162  CLASS(sources_base), INTENT(INOUT) :: Sources
163  REAL, INTENT(IN) :: time, dt
164  CLASS(marray_compound), INTENT(INOUT) :: pvar,cvar,sterm
165  !------------------------------------------------------------------------!
166  ! go through all gravity terms in the list
167  CALL this%UpdateGravity(mesh,physics,fluxes,pvar,time,dt)
168 
169  ! update disk scale height if requested
170  IF (this%update_disk_height) THEN
171  SELECT TYPE(phys => physics)
172  CLASS IS (physics_eulerisotherm)
173  CALL this%CalcDiskHeight(mesh,phys,pvar)
174  END SELECT
175  END IF
176 
177  ! gravitational source terms
178  CALL physics%ExternalSources(this%accel,pvar,cvar,sterm)
179 
182  ! Set src term in energy equation to zero, if it is handeled in the physics
183  ! module
184  SELECT TYPE(s => sterm)
185  TYPE IS (statevector_euler)
186  IF (.NOT.this%addtoenergy) s%energy%data1d(:) = 0.
187  END SELECT
188 
189  END SUBROUTINE externalsources_single
190 
192  SUBROUTINE updategravity(this,Mesh,Physics,Fluxes,pvar,time,dt)
193  IMPLICIT NONE
194  !------------------------------------------------------------------------!
195  CLASS(sources_gravity), TARGET, INTENT(INOUT) :: this
196  CLASS(mesh_base), INTENT(IN) :: Mesh
197  CLASS(physics_base), INTENT(INOUT) :: Physics
198  CLASS(fluxes_base), INTENT(IN) :: Fluxes
199  CLASS(marray_compound), INTENT(INOUT) :: pvar
200  REAL, INTENT(IN) :: time, dt
201  !------------------------------------------------------------------------!
202  CLASS(gravity_base), POINTER :: gravptr
203  !------------------------------------------------------------------------!
204  ! update acceleration of all gravity sources
205  ! reset gterm
206  this%pot%data1d(:) = 0.
207  this%accel%data1d(:) = 0.
208 
209  gravptr => this%glist
210  DO WHILE (ASSOCIATED(gravptr))
211  ! get gravitational acceleration of each gravity module
212  CALL gravptr%UpdateGravity_single(mesh,physics,fluxes,pvar,time,dt)
213 
214  ! add contribution to the overall gravitational acceleration
215  this%accel = this%accel + gravptr%accel
216 
217  ! add potential if available
218  IF(ASSOCIATED(gravptr%pot)) &
219  this%pot%data4d(:,:,:,:) = this%pot%data4d(:,:,:,:) + gravptr%pot(:,:,:,:)
220 
221  ! next source term
222  gravptr => gravptr%next
223  END DO
224  END SUBROUTINE
225 
226  SUBROUTINE calcdiskheight(this,Mesh,Physics,pvar)
230  IMPLICIT NONE
231  !------------------------------------------------------------------------!
232  CLASS(sources_gravity),TARGET,INTENT(INOUT) :: this
233  CLASS(mesh_base), INTENT(IN) :: Mesh
234  CLASS(physics_eulerisotherm), INTENT(INOUT) :: Physics
235  CLASS(marray_compound), INTENT(INOUT) :: pvar
236  !------------------------------------------------------------------------!
237  CLASS(gravity_base), POINTER :: grav_ptr,selfgrav_ptr => null()
238  LOGICAL :: has_external_potential = .false.
239  !------------------------------------------------------------------------!
240  ! reset inverse scale height^2
241  ! go through all gravity terms in the list
242  grav_ptr => this%glist
243  DO WHILE(ASSOCIATED(grav_ptr))
244  SELECT TYPE (grav => grav_ptr)
245  CLASS IS (gravity_pointmass)
246 !CDIR IEXPAND
247  CALL grav%CalcDiskHeight_single(mesh,physics,pvar,physics%bccsound,this%h_ext,this%height)
248  this%invheight2%data1d(:) = this%invheight2%data1d(:) + 1./this%h_ext%data1d(:)**2
249  has_external_potential = .true.
250  CLASS IS (gravity_spectral)
251  selfgrav_ptr => grav_ptr
252  END SELECT
253 
254  ! next gravity term
255  grav_ptr => grav_ptr%next
256  END DO
257 
258  ! self-gravity of the disk needs special treatment
259  IF (ASSOCIATED(selfgrav_ptr)) THEN
260  IF (has_external_potential) THEN
261  ! compute the resultant height due to all external gravitational forces
262  this%h_ext%data1d(:) = 1./sqrt(this%invheight2%data1d(:))
263  END IF
264  CALL selfgrav_ptr%CalcDiskHeight_single(mesh,physics,pvar,physics%bccsound,this%h_ext,this%height)
265  ELSE
266  ! non-selfgravitating disk
267  this%height%data1d(:) = 1./sqrt(this%invheight2%data1d(:))
268  END IF
269 
270  END SUBROUTINE calcdiskheight
271 
272 
273 
274  SUBROUTINE infosources(this,Mesh)
275  IMPLICIT NONE
276  !------------------------------------------------------------------------!
277  CLASS(sources_gravity), INTENT(IN) :: this
278  CLASS(mesh_base), INTENT(IN) :: Mesh
279  !------------------------------------------------------------------------!
280  END SUBROUTINE infosources
281 
282  SUBROUTINE finalize(this)
283  IMPLICIT NONE
284  !------------------------------------------------------------------------!
285  CLASS(sources_gravity), INTENT(INOUT) :: this
286  CLASS(gravity_base), POINTER :: gravptr
287  !------------------------------------------------------------------------!
288  CALL this%pot%Destroy()
289  CALL this%accel%Destroy()
290  CALL this%height%Destroy()
291  CALL this%invheight2%Destroy()
292  CALL this%h_ext%Destroy()
293 
294  gravptr => this%glist
295  DO WHILE (ASSOCIATED(gravptr))
296 
297  CALL gravptr%Finalize()
298 
299  gravptr => gravptr%next
300  END DO
301 
302  CALL this%sources_c_accel%Finalize()
303  END SUBROUTINE finalize
304 
305 END MODULE sources_gravity_mod
subroutine finalize(this)
Destructor of common class.
subroutine infosources(this, Mesh)
generic source terms module providing functionaly common to all source terms
subroutine updategravity(this, Mesh, Physics, Fluxes, pvar, time, dt)
Updates gravity of all gravity source modules.
source terms module for constant acceleration
2D poisson solver using spectral methods for direct integration
derived class for compound of mesh arrays
base class for mesh arrays
Definition: marray_base.f90:36
character(len=32) source_name
Basic fosite module.
generic gravity terms module providing functionaly common to all gravity terms
source terms module for gravitational acceleration due to a point mass at the center of the coordinat...
generic gravity terms module providing functionaly common to all gravity terms
basic mesh array class
Definition: marray_base.f90:64
physics module for 1D,2D and 3D isothermal Euler equations
subroutine new_gravity(this, Mesh, Fluxes, Physics, config, IO)
subroutine initsources_gravity(this, Mesh, Physics, Fluxes, config, IO)
subroutine calcdiskheight(this, Mesh, Physics, pvar)
Basic physics module.
Dictionary for generic data types.
Definition: common_dict.f90:61
physics module for 1D,2D and 3D non-isothermal Euler equations
base module for numerical flux functions
Definition: fluxes_base.f90:39
subroutine externalsources_single(this, Mesh, Physics, Fluxes, Sources, time, dt, pvar, cvar, sterm)