KronLinInv  0.3
Kronecker-product-based linear inversion
test_ompi_kronlininv.f08
Go to the documentation of this file.
1 
2 !------------------------------------------------------------------------
3 !
4 ! Copyright 2018, Andrea Zunino
5 !
6 ! This file is part of KronLinInv.
7 !
8 ! KronLinInv is free software: you can redistribute it and/or modify
9 ! it under the terms of the GNU General Public License as published by
10 ! the Free Software Foundation, either version 3 of the License, or
11 ! (at your option) any later version.
12 !
13 ! KronLinInv is distributed in the hope that it will be useful,
14 ! but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ! GNU General Public License for more details.
17 !
18 ! You should have received a copy of the GNU General Public License
19 ! along with KronLinInv. If not, see <http://www.gnu.org/licenses/>.
20 !
21 !------------------------------------------------------------------------
22 
23 !-----------------------------------------------------
24 !
26 !
30 !
31 !-----------------------------------------------------
32 program test
33 
34  use realprec
35  use parautil
36  use ompi_kronlininv
37  use readwriteh5
38 
39  implicit none
40 
41  real(dp),allocatable :: G1(:,:),G2(:,:),G3(:,:),&
42  Cm1(:,:),Cm2(:,:),Cm3(:,:),&
43  Cd1(:,:),Cd2(:,:),Cd3(:,:),&
44  mprior(:),dobs(:),mtrue(:)
45  real(dp),allocatable :: U1(:,:),U2(:,:),U3(:,:),&
46  iUCm1(:,:),iUCm2(:,:),iUCm3(:,:),&
47  iUCmGtiCd1(:,:),iUCmGtiCd2(:,:),iUCmGtiCd3(:,:),&
48  invlambda(:)
49  character(len=1024) :: inpfile,outfile
50  integer :: nm,nd,nm1,nm2,nm3,nd1,nd2,nd3
51  real(dp),allocatable :: postm(:),postmFAST(:),postcov(:,:)
52  real(dp),allocatable :: bandpostC(:,:)
53  integer :: i1,i2,j1,j2, lowdiag, updiag
54  real(dp) :: start,finish
55 
56  integer :: iar,mpierr
57  character(len=256) :: simname
58  !----------------------------------
59 
60 
61  !! Initialize OpenMPI
62  call para_init()
63 
64  !! simname -> name of the input file (without the .hdf5)
65 
66  iar = 1
67  call get_command_argument(iar, simname)
68 
69  inpfile = trim(simname)//'.h5'
70  outfile = trim(simname)//'_output.h5'
71 
72  print*,"inpfile:",trim(inpfile)
73  print*,"outfile:",trim(outfile)
74 
75  !----------------------------------
76 
77  !! read all input arrays
78  print*,'reading input arrays'
79  call readreal2darrh5(inpfile,'G1',g1)
80  call readreal2darrh5(inpfile,'G2',g2)
81  call readreal2darrh5(inpfile,'G3',g3)
82  call readreal2darrh5(inpfile,'Cm1',cm1)
83  call readreal2darrh5(inpfile,'Cm2',cm2)
84  call readreal2darrh5(inpfile,'Cm3',cm3)
85  call readreal2darrh5(inpfile,'Cd1',cd1)
86  call readreal2darrh5(inpfile,'Cd2',cd2)
87  call readreal2darrh5(inpfile,'Cd3',cd3)
88  call readreal1darrh5(inpfile,'mtrue',mtrue)
89  call readreal1darrh5(inpfile,'dobs',dobs)
90  call readreal1darrh5(inpfile,'mprior',mprior)
91 
92  call para_barrier()
93 
94  if (myrank==masterrank) then
95  !! write arrays to output file
96  print*,"WRITE hello from ",myrank,masterrank
97  call writereal2darrh5(outfile,'G1',g1)
98  call writereal2darrh5(outfile,'G2',g2)
99  call writereal2darrh5(outfile,'G3',g3)
100  call writereal2darrh5(outfile,'Cm1',cm1)
101  call writereal2darrh5(outfile,'Cm2',cm2)
102  call writereal2darrh5(outfile,'Cm3',cm3)
103  call writereal2darrh5(outfile,'Cd1',cd1)
104  call writereal2darrh5(outfile,'Cd2',cd2)
105  call writereal2darrh5(outfile,'Cd3',cd3)
106  call writereal1darrh5(outfile,'mtrue',mtrue)
107  call writereal1darrh5(outfile,'dobs',dobs)
108  call writereal1darrh5(outfile,'mprior',mprior)
109  end if
110 
111 
112  !! sizes
113  nm1 = size(cm1,1)
114  nm2 = size(cm2,1)
115  nm3 = size(cm3,1)
116  nd1 = size(cd1,1)
117  nd2 = size(cd2,1)
118  nd3 = size(cd3,1)
119  nm = nm1*nm2*nm3
120  nd = nd1*nd2*nd3
121 
122  print*,"Sizes:"
123  print*,"M:",nm1,nm2,nm3,nm
124  print*,"D:",nd1,nd2,nd3,nd
125  print*
126 
127  !! allocate working arrays
128  print*,'Allocating working arrays'
129  allocate(u1(nm1,nm1),u2(nm2,nm2),u3(nm3,nm3))
130  allocate(invlambda(nm))
131  allocate(iucm1(nm1,nm1),iucm2(nm2,nm2),iucm3(nm3,nm3))
132  allocate(iucmgticd1(nm1,nd1),iucmgticd2(nm2,nd2),iucmgticd3(nm3,nd3))
133 
134 
135  !! compute the factors for post mean and covariance
136  print*,'Computing the factors for post mean and covariance'
137  call calcfactors(g1,g2,g3,cm1,cm2,cm3,cd1,cd2,cd3,&
138  u1,u2,u3,invlambda,iucm1,iucm2,iucm3,&
139  iucmgticd1,iucmgticd2,iucmgticd3 )
140  print*,'END computing the factors for post mean and covariance'
141 
142 
143  call para_barrier()
144  if (myrank==masterrank) then
145  call writereal2darrh5(outfile,'U1',u1)
146  call writereal2darrh5(outfile,'U2',u2)
147  call writereal2darrh5(outfile,'U3',u3)
148  call writereal1darrh5(outfile,'invlambda',invlambda)
149  call writereal2darrh5(outfile,'iUCm1',iucm1)
150  call writereal2darrh5(outfile,'iUCm2',iucm2)
151  call writereal2darrh5(outfile,'iUCm3',iucm3)
152  call writereal2darrh5(outfile,'iUCmGtiCd1',iucmgticd1)
153  call writereal2darrh5(outfile,'iUCmGtiCd2',iucmgticd2)
154  call writereal2darrh5(outfile,'iUCmGtiCd3',iucmgticd3)
155  end if
156 
157 
158 
159 
160  !!=====================================================================
161  call para_barrier()
162  allocate(postm(nm))
163  postm=0.0_dp
164 
165  if (myrank==masterrank) then
166  call cpu_time(start)
167  print*,'Computing post mean'
168  end if
169 
170  call posteriormean(u1,u2,u3, invlambda, iucmgticd1,iucmgticd2,iucmgticd3,&
171  g1,g2,g3,mprior,dobs, postm )
172 
173  if (myrank==masterrank) then
174  call cpu_time(finish)
175  print*,'postm time:',finish-start
176  !! write posterior mean
177  call writereal1darrh5(outfile,'postm',postm)
178  end if
179  !!=====================================================================
180 
181 
182 
183 
184 
185  !!=====================================================================
186  call para_barrier()
187  allocate(postcov(nm,nm))
188  postcov = 0.0_dp
189  i1 = 10
190  i2 = 1000
191  j1 = 15
192  j2 = 1000
193  if (myrank==masterrank) then
194  call cpu_time(start)
195  print*,'Computing block postcov'
196  end if
197 
198  call blockpostcov(u1,u2,u3, invlambda,iucm1,iucm2,iucm3, &
199  i1,i2,j1,j2, postcov(i1:i2,j1:j2))
200 
201  if (myrank==masterrank) then
202  call cpu_time(finish)
203  print*,'postcov block time:',finish-start
204  !! write posterior covariance
205  call writereal2darrh5(outfile,'postcov',postcov)
206  end if
207  !!=====================================================================
208 
209 
210 
211 
212 
213  !!=====================================================================
214  call para_barrier()
215  if (myrank==masterrank) print*,'Computing band of post cov'
216  lowdiag = 10
217  updiag = 10
218  !! allocate the array for band storage
219  allocate(bandpostc(lowdiag+updiag+1,nm))
220  call bandpostcov(u1,u2,u3, invlambda,iucm1,iucm2,iucm3, &
221  lowdiag, updiag, bandpostc)
222 
223  !! write posterior covariance
224  if (myrank==masterrank) then
225  call writereal2darrh5(outfile,'bandpostcov',bandpostc)
226  end if
227  !!=====================================================================
228 
229 
230 
231  call para_finish()
232  !stop
233  !!-------------------------------------------------------------
234 
235 end program
subroutine writereal1darrh5(outfile, dsetname, arr)
Definition: rdwrhdf5.f08:55
Module to read and write HDF5 files.
Definition: rdwrhdf5.f08:32
integer, parameter, public masterrank
subroutine para_barrier()
subroutine para_init()
integer, public, protected myrank
subroutine readreal2darrh5(inpfile, dsetname, arr)
Definition: rdwrhdf5.f08:322
subroutine, public blockpostcov(U1, U2, U3, diaginvlambda, iUCm1, iUCm2, iUCm3, astart, aend, bstart, bend, postC)
Computes a block of the posterior covariance.
subroutine, public calcfactors(G1, G2, G3, Cm1, Cm2, Cm3, Cd1, Cd2, Cd3, U1, U2, U3, diaginvlambda, iUCm1, iUCm2, iUCm3, iUCmGtiCd1, iUCmGtiCd2, iUCmGtiCd3)
Computes the factors necessary to solve the inverse problem.
program test
Test program for KronLinInv.
subroutine writereal2darrh5(outfile, dsetname, arr)
Definition: rdwrhdf5.f08:158
This file contains the parallel OpenMPI (distributed memory) version of KronLinInv. OpenMPI and LAPACK libraries are required to be installed in the system.
subroutine, public bandpostcov(U1, U2, U3, diaginvlambda, iUCm1, iUCm2, iUCm3, lowdiag, updiag, bandpostC)
Computes a band of the posterior covariance. See http://www.netlib.org/lapack/lug/node124.html
subroutine para_finish()
Procedures to perform linear inversion under gaussian assumptions using the Kronecker-product approac...
subroutine readreal1darrh5(inpfile, dsetname, arr)
Definition: rdwrhdf5.f08:261
Subroutines/utilities for the parallel version of KronLinInv using OpenMPI.
subroutine, public posteriormean(U1, U2, U3, diaginvlambda, Z1, Z2, Z3, G1, G2, G3, mprior, dobs, postm)
Computes the posterior mean