MPI-AMRVAC  3.0
The MPI - Adaptive Mesh Refinement - Versatile Advection Code
mod_oneblock.t
Go to the documentation of this file.
1 !=============================================================================
2 ! Module oneblock to hold a datastructure as output by convert_type='oneblock'.
3 ! Currently only ASCII
4 ! Can be handy to read in initial conditions.
5 ! 2015-02-18 Oliver Porth
6 !=============================================================================
8 implicit none
9 save
10 
11 double precision, dimension({^D&:|,},:), allocatable :: woneblock
12 double precision, dimension({^D&:|,},:), allocatable :: xoneblock
13 integer :: nc^d
14 integer :: unit=15 !file unit to read on
15 !-----------------------------------------------------------------------------
16 
17 
18 contains
19 !=============================================================================
20 subroutine read_oneblock(filename)
21 
23 
24 character(len=*), intent(in) :: filename
25 ! .. local ..
26 character(len=1024) :: outfilehead
27 integer :: nctot, ix^D, ixp^D
28 double precision :: time
29 integer :: idim
30 integer,dimension(^ND) :: sendbuff
31 !-----------------------------------------------------------------------------
32 
33 if (mype == 0) write(*,*) 'mod_oneblock: reading ',nw,' variables on unit:', unit
34 
35 !----------------------------------------
36 ! Root does the reading:
37 !----------------------------------------
38 if (mype == 0) then
39  open(unit,file=filename,status='unknown')
40  ! The header information:
41  read(unit,'(A)') outfilehead
42  read(unit,*) nctot,nc^d
43  read(unit,*) time
44 
45  ! Allocate and read the grid and variables:
46  allocate(xoneblock(nc^d,1:^nd))
47  allocate(woneblock(nc^d,1:nw))
48 
49  {do ix^db=1,nc^db\}
50 
51  read(unit,*) xoneblock(ix^d,1:^nd), woneblock(ix^d,1:nw)
52 
53  {end do\}
54 
55 ! Close the file
56  close(unit)
57 end if! mype==0
58 
59 !----------------------------------------
60 ! Broadcast what mype=0 read:
61 !----------------------------------------
62 if (npe>1) then
63  {sendbuff(^d)=nc^d;}
64  call mpi_bcast(sendbuff,^nd,mpi_integer,0,icomm,ierrmpi)
65  if (mype .ne. 0) then
66  {nc^d=sendbuff(^d);}
67  ! Allocate the grid and variables:
68  allocate(xoneblock(nc^d,1:^nd))
69  allocate(woneblock(nc^d,1:nw))
70  end if
71  call mpi_bcast(xoneblock,{nc^d|*}*^nd,mpi_double_precision,0,icomm,ierrmpi)
72  call mpi_bcast(woneblock,{nc^d|*}*nw,mpi_double_precision,0,icomm,ierrmpi)
73 end if! npe>1
74 
75 
76 
77 end subroutine read_oneblock
78 !=============================================================================
79 subroutine interpolate_oneblock(x,iw,out)
80 
81 double precision, dimension(^ND),intent(in) :: x
82 integer, intent(in) :: iw
83 double precision, intent(out) :: out
84 ! .. local ..
85 double precision, dimension(^ND) :: xloc
86 integer :: ic^D, ic1^D, ic2^D
87 double precision :: xd^D
88 {^iftwod
89 double precision :: c00, c10
90 }
91 {^ifthreed
92 double precision :: c0, c1, c00, c10, c01, c11
93 }
94 integer :: ipivot^D, idir
95 !-----------------------------------------------------------------------------
96 
97 xloc=x
98 
99 !--------------------------------------------
100 ! Hunt for the index closest to the point
101 ! This is a bit slow but allows for stretched grids
102 ! (still need to be orthogonal for interpolation though)
103 !--------------------------------------------
104 ipivot^d=1;
105 {^ifoned
106 ic1 = minloc(dabs(xloc(1)-xoneblock(:,1)),1,mask=.true.)
107 }
108 {^iftwod
109 do idir = 1, ^nd
110  select case (idir)
111  case (1)
112  ic1 = minloc(dabs(xloc(1)-xoneblock(:,ipivot2,idir)),1,mask=.true.)
113  case (2)
114  ic2 = minloc(dabs(xloc(2)-xoneblock(ipivot1,:,idir)),1,mask=.true.)
115  case default
116  call mpistop("error1 in interpolate_oneblock")
117  end select
118 end do
119 }
120 {^ifthreed
121 do idir = 1, ^nd
122  select case (idir)
123  case (1)
124  ic1 = minloc(dabs(xloc(1)-xoneblock(:,ipivot2,ipivot3,idir)),1, &
125  mask=.true.)
126  case (2)
127  ic2 = minloc(dabs(xloc(2)-xoneblock(ipivot1,:,ipivot3,idir)),1, &
128  mask=.true.)
129  case (3)
130  ic3 = minloc(dabs(xloc(3)-xoneblock(ipivot1,ipivot2,:,idir)),1, &
131  mask=.true.)
132  case default
133  call mpistop("error1 in interpolate_oneblock")
134  end select
135 end do
136 }
137 
138 ! flat interpolation would simply be:
139 !out = woneblock(ic^D,iw)
140 !return
141 
142 !-------------------------------------------
143 ! Get the left and right indices
144 !-------------------------------------------
145 {
146 if (xoneblock({ic^dd},^d) .lt. xloc(^d)) then
147  ic1^d = ic^d
148 else
149  ic1^d = ic^d -1
150 end if
151 ic2^d = ic1^d + 1
152 \}
153 
154 !--------------------------------------------
155 ! apply flat interpolation if outside of range,
156 ! change point-location to make this easy!
157 !--------------------------------------------
158 {
159 if (ic1^d .lt. 1) then
160  ic1^d = 1
161  ic2^d = ic1^d + 1
162  xloc(^d) = xoneblock(ic^dd,^d)
163 end if
164 \}
165 {
166 if (ic2^d .gt. nc^d) then
167  ic2^d = nc^d
168  ic1^d = ic2^d - 1
169  xloc(^d) = xoneblock(ic^dd,^d)
170 end if
171 \}
172 
173 
174 !-------------------------------------------
175 ! linear, bi- and tri- linear interpolations
176 !-------------------------------------------
177 {^ifoned
178 xd1 = (xloc(1)-xoneblock(ic11,1)) / (xoneblock(ic21,1) - xoneblock(ic11,1))
179 out = woneblock(ic11,iw) * (1.0d0 - xd1) + woneblock(ic21,iw) * xd1
180 }
181 {^iftwod
182 xd1 = (xloc(1)-xoneblock(ic11,ic12,1)) / (xoneblock(ic21,ic12,1) - xoneblock(ic11,ic12,1))
183 xd2 = (xloc(2)-xoneblock(ic11,ic12,2)) / (xoneblock(ic11,ic22,2) - xoneblock(ic11,ic12,2))
184 c00 = woneblock(ic11,ic12,iw) * (1.0d0 - xd1) + woneblock(ic21,ic12,iw) * xd1
185 c10 = woneblock(ic11,ic22,iw) * (1.0d0 - xd1) + woneblock(ic21,ic22,iw) * xd1
186 out = c00 * (1.0d0 - xd2) + c10 * xd2
187 }
188 {^ifthreed
189 xd1 = (xloc(1)-xoneblock(ic11,ic12,ic13,1)) / (xoneblock(ic21,ic12,ic13,1) - xoneblock(ic11,ic12,ic13,1))
190 xd2 = (xloc(2)-xoneblock(ic11,ic12,ic13,2)) / (xoneblock(ic11,ic22,ic13,2) - xoneblock(ic11,ic12,ic13,2))
191 xd3 = (xloc(3)-xoneblock(ic11,ic12,ic13,3)) / (xoneblock(ic11,ic12,ic23,3) - xoneblock(ic11,ic12,ic13,3))
192 
193 c00 = woneblock(ic11,ic12,ic13,iw) * (1.0d0 - xd1) + woneblock(ic21,ic12,ic13,iw) * xd1
194 c10 = woneblock(ic11,ic22,ic13,iw) * (1.0d0 - xd1) + woneblock(ic21,ic22,ic13,iw) * xd1
195 c01 = woneblock(ic11,ic12,ic23,iw) * (1.0d0 - xd1) + woneblock(ic21,ic12,ic23,iw) * xd1
196 c11 = woneblock(ic11,ic22,ic23,iw) * (1.0d0 - xd1) + woneblock(ic21,ic22,ic23,iw) * xd1
197 
198 c0 = c00 * (1.0d0 - xd2) + c10 * xd2
199 c1 = c01 * (1.0d0 - xd2) + c11 * xd2
200 
201 out = c0 * (1.0d0 - xd3) + c1 * xd3
202 }
203 
204 end subroutine interpolate_oneblock
205 !=============================================================================
206 end module mod_oneblock
207 !=============================================================================
subroutine mpistop(message)
Exit MPI-AMRVAC with an error message.
Definition: comm_lib.t:194
This module contains definitions of global parameters and variables and some generic functions/subrou...
integer mype
The rank of the current MPI task.
integer unit
Definition: mod_oneblock.t:14
integer nc
Definition: mod_oneblock.t:13
subroutine read_oneblock(filename)
Definition: mod_oneblock.t:21
double precision, dimension({^d &:|,},:), allocatable xoneblock
Definition: mod_oneblock.t:12
subroutine interpolate_oneblock(x, iw, out)
Definition: mod_oneblock.t:80
double precision, dimension({^d &:|,},:), allocatable woneblock
Definition: mod_oneblock.t:11