Afivo  0.3
m_a3_interp.f90
1 
5 module m_a3_interp
6  use m_a3_types
7 
8  implicit none
9  private
10 
11  public :: a3_interp0
12  public :: a3_interp1
13  public :: a3_interp0_to_grid
14 
15 contains
16 
18  function a3_interp0(tree, rr, ivs, n_var) result(vals)
19  use m_a3_utils, only: a3_get_loc
20  type(a3_t), intent(in) :: tree
21  real(dp), intent(in) :: rr(3)
22  integer, intent(in) :: n_var
23  integer, intent(in) :: ivs(n_var)
24  real(dp) :: vals(n_var)
25  type(a3_loc_t) :: loc
26 
27  loc = a3_get_loc(tree, rr)
28 
29  if (loc%id == -1) then
30  print *, "a3_interp0 error, no box at ", rr
31  stop
32  end if
33 
34  vals = tree%boxes(loc%id)%cc(loc%ix(1), loc%ix(2), loc%ix(3), ivs)
35  end function a3_interp0
36 
38  function a3_interp1(tree, r, ivs, n_var, id_guess) result(vals)
39  use m_a3_utils, only: a3_get_id_at
40  type(a3_t), intent(in) :: tree
41  real(dp), intent(in) :: r(3)
42  integer, intent(in) :: n_var
43  integer, intent(in) :: ivs(n_var)
44  integer, intent(in), optional :: id_guess
45  real(dp) :: vals(n_var)
46  integer :: i, iv, id, ix(3)
47  real(dp) :: r_loc(3), dvec(3), ovec(3), w(2, 2, 2)
48 
49  id = a3_get_id_at(tree, r, id_guess)
50 
51  if (id <= af_no_box) then
52  print *, "a3_interp1: point outside domain", r
53  stop
54  end if
55 
56  ! Compute ix such that r lies between cell centers at ix and ix + 1
57  ix = nint((r - tree%boxes(id)%r_min) / tree%boxes(id)%dr)
58  r_loc = a3_r_cc(tree%boxes(id), ix)
59  dvec = r - r_loc
60 
61  ! Normalize dvec to a value [0, 1]
62  dvec = dvec / tree%boxes(id)%dr
63  ovec = 1 - dvec
64 
65  ! Compute weights of linear interpolation
66  w(1, 1, 1) = ovec(1) * ovec(2) * ovec(3)
67  w(2, 1, 1) = dvec(1) * ovec(2) * ovec(3)
68  w(1, 2, 1) = ovec(1) * dvec(2) * ovec(3)
69  w(2, 2, 1) = dvec(1) * dvec(2) * ovec(3)
70  w(1, 1, 2) = ovec(1) * ovec(2) * dvec(3)
71  w(2, 1, 2) = dvec(1) * ovec(2) * dvec(3)
72  w(1, 2, 2) = ovec(1) * dvec(2) * dvec(3)
73  w(2, 2, 2) = dvec(1) * dvec(2) * dvec(3)
74 
75  do i = 1, size(ivs)
76  iv = ivs(i)
77  vals(i) = sum(w * tree%boxes(id)%cc(ix(1):ix(1)+1, &
78  ix(2):ix(2)+1, ix(3):ix(3)+1, iv))
79  end do
80  end function a3_interp1
81 
83  subroutine a3_interp0_to_grid(tree, rr, iv, amount, to_density)
84  use m_a3_utils, only: a3_get_loc
85  type(a3_t), intent(inout) :: tree
86  integer, intent(in) :: iv
87  real(dp), intent(in) :: rr(3)
88  real(dp), intent(in) :: amount
89  logical, intent(in) :: to_density
90  real(dp) :: actual_amount
91  type(a3_loc_t) :: loc
92  integer :: id, ix(3)
93 
94  loc = a3_get_loc(tree, rr)
95 
96  if (loc%id == -1) then
97  print *, "a3_interp0_to_grid error, no box at ", rr
98  stop
99  end if
100 
101  id = loc%id
102  ix = loc%ix
103 
105  if (to_density) then
106  actual_amount = amount / tree%boxes(id)%dr**3
107  else
108  actual_amount = amount
109  end if
110 
111  tree%boxes(id)%cc(ix(1), ix(2), ix(3), iv) = &
112  tree%boxes(id)%cc(ix(1), ix(2), ix(3), iv) + &
113  actual_amount
114  end subroutine a3_interp0_to_grid
115 
116 end module m_a3_interp
Type specifying the location of a cell.
Definition: m_a3_types.f90:162
This module contains all kinds of different &#39;helper&#39; routines for Afivo. If the number of routines fo...
Definition: m_a3_utils.f90:5
Type which stores all the boxes and levels, as well as some information about the number of boxes...
Definition: m_a3_types.f90:131
This module contains routines related to interpolation, which can interpolate &#39;to&#39; the grid and &#39;from...
Definition: m_a3_interp.f90:5
This module contains the basic types and constants that are used in the 3-dimensional version of Afiv...
Definition: m_a3_types.f90:5