KronLinInv  0.3
Kronecker-product-based linear inversion
test_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 !-------------------------------------------
25 !
27 !
31 !
32 !-------------------------------------------
33 program test
34 
35  use realprec
36  use 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(:),postcov(:,:),postcov_diag(:,:)
52  real(dp),allocatable :: bandpostC(:,:)
53  integer :: i1,i2,j1,j2, lowdiag, updiag
54  real(dp) :: start,finish
55 
56  integer :: iar
57  character(len=256) :: simname
58  !----------------------------------
59 
60  !! simname -> name of the input file (without the .hdf5)
61 
62  iar = 1
63  call get_command_argument(iar, simname)
64 
65  inpfile = trim(simname)//'.h5'
66  outfile = trim(simname)//'_output.h5'
67 
68  print*,"inpfile:",trim(inpfile)
69  print*,"outfile:",trim(outfile)
70 
71  !----------------------------------
72 
73  !! read all input arrays
74  print*,'reading input arrays'
75  call readreal2darrh5(inpfile,'G1',g1)
76  call readreal2darrh5(inpfile,'G2',g2)
77  call readreal2darrh5(inpfile,'G3',g3)
78  call readreal2darrh5(inpfile,'Cm1',cm1)
79  call readreal2darrh5(inpfile,'Cm2',cm2)
80  call readreal2darrh5(inpfile,'Cm3',cm3)
81  call readreal2darrh5(inpfile,'Cd1',cd1)
82  call readreal2darrh5(inpfile,'Cd2',cd2)
83  call readreal2darrh5(inpfile,'Cd3',cd3)
84  call readreal1darrh5(inpfile,'mtrue',mtrue)
85  call readreal1darrh5(inpfile,'dobs',dobs)
86  call readreal1darrh5(inpfile,'mprior',mprior)
87 
88 
89  ! write arrays to output file
90  call writereal2darrh5(outfile,'G1',g1)
91  call writereal2darrh5(outfile,'G2',g2)
92  call writereal2darrh5(outfile,'G3',g3)
93  call writereal2darrh5(outfile,'Cm1',cm1)
94  call writereal2darrh5(outfile,'Cm2',cm2)
95  call writereal2darrh5(outfile,'Cm3',cm3)
96  call writereal2darrh5(outfile,'Cd1',cd1)
97  call writereal2darrh5(outfile,'Cd2',cd2)
98  call writereal2darrh5(outfile,'Cd3',cd3)
99  call writereal1darrh5(outfile,'mtrue',mtrue)
100  call writereal1darrh5(outfile,'dobs',dobs)
101  call writereal1darrh5(outfile,'mprior',mprior)
102 
103  !! sizes
104  nm1 = size(cm1,1)
105  nm2 = size(cm2,1)
106  nm3 = size(cm3,1)
107  nd1 = size(cd1,1)
108  nd2 = size(cd2,1)
109  nd3 = size(cd3,1)
110  nm = nm1*nm2*nm3
111  nd = nd1*nd2*nd3
112 
113  print*,"Sizes:"
114  print*,"M:",nm1,nm2,nm3,nm
115  print*,"D:",nd1,nd2,nd3,nd
116  print*
117 
118  !! allocate working arrays
119  print*,'Allocating working arrays'
120  allocate(u1(nm1,nm1),u2(nm2,nm2),u3(nm3,nm3))
121  allocate(invlambda(nm))
122  allocate(iucm1(nm1,nm1),iucm2(nm2,nm2),iucm3(nm3,nm3))
123  allocate(iucmgticd1(nm1,nd1),iucmgticd2(nm2,nd2),iucmgticd3(nm3,nd3))
124 
125  !! compute the factors for post mean and covariance
126  print*,'Computing the factors for post mean and covariance'
127  call calcfactors(g1,g2,g3,cm1,cm2,cm3,cd1,cd2,cd3,&
128  u1,u2,u3,invlambda,iucm1,iucm2,iucm3,&
129  iucmgticd1,iucmgticd2,iucmgticd3 )
130 
131 
132  call writereal2darrh5(outfile,'U1',u1)
133  call writereal2darrh5(outfile,'U2',u2)
134  call writereal2darrh5(outfile,'U3',u3)
135  call writereal1darrh5(outfile,'invlambda',invlambda)
136  call writereal2darrh5(outfile,'iUCm1',iucm1)
137  call writereal2darrh5(outfile,'iUCm2',iucm2)
138  call writereal2darrh5(outfile,'iUCm3',iucm3)
139  call writereal2darrh5(outfile,'iUCmGtiCd1',iucmgticd1)
140  call writereal2darrh5(outfile,'iUCmGtiCd2',iucmgticd2)
141  call writereal2darrh5(outfile,'iUCmGtiCd3',iucmgticd3)
142  ! call writereal1Darrh5(outfile,'nm',[nm1,nm2,nm3])
143  ! call writereal1Darrh5(outfile,'nd',[nd1,nd2,nd3])
144 
145 
146 
147  allocate(postm(nm))
148  postm=0.0_dp
149 
150  call cpu_time(start)
151  print*,'Computing post mean'
152  call posteriormean(u1,u2,u3, invlambda, iucmgticd1,iucmgticd2,iucmgticd3,&
153  g1,g2,g3,mprior,dobs, postm )
154  call cpu_time(finish)
155  print*,'postm time:',finish-start
156  !! write posterior mean
157  call writereal1darrh5(outfile,'postm',postm)
158 
159 
160 
161  ! print*,'Computing postcov'
162  ! i1 = 1
163  ! i2 = 1000
164  ! j1 = 1
165  ! j2 = 1000
166  ! allocate(postcov(i2-i1+1,j2-j1+1))
167  ! postcov = 0.0_dp
168  ! call cpu_time(start)
169  ! call blockpostcov(U1,U2,U3, invlambda,iUCm1,iUCm2,iUCm3, &
170  ! i1,i2,j1,j2, postcov(i1:i2,j1:j2))
171  ! call cpu_time(finish)
172  ! print*,'postCov block time:',finish-start
173  ! !! write posterior covariance
174  ! call writereal2Darrh5(outfile,'postcov',postcov)
175 
176 
177  ! print*,
178  ! print*,'computing postcov_diag'
179  ! !!http://www.netlib.org/lapack/lug/node124.html
180  ! lowdiag = 0
181  ! updiag = 0
182  ! allocate(postcov_diag(1,nm))
183  ! postcov_diag = 0.0_dp
184  ! call cpu_time(start)
185  ! call bandpostcov(U1,U2,U3, invlambda,iUCm1,iUCm2,iUCm3, &
186  ! lowdiag,updiag, postcov_diag)
187  ! call cpu_time(finish)
188  ! print*,'postCov band time:',finish-start
189  ! !! write posterior covariance
190  ! call writereal2Darrh5(outfile,'postcov_diag',postcov_diag)
191 
192 
193  ! print*,'Computing band of post cov'
194  ! lowdiag = 10
195  ! updiag = 10
196  ! !! allocate the array for band storage
197  ! allocate(bandpostC(lowdiag+updiag+1,nm))
198  ! call bandpostcov(U1,U2,U3, invlambda,iUCm1,iUCm2,iUCm3, &
199  ! lowdiag, updiag, bandpostC)
200  ! !! write posterior covariance
201  ! call writereal2Darrh5(outfile,'bandpostcov',bandpostC)
202 
203 
204 
205 end program test
subroutine writereal1darrh5(outfile, dsetname, arr)
Definition: rdwrhdf5.f08:55
Module to read and write HDF5 files.
Definition: rdwrhdf5.f08:32
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.
Definition: kronlininv.f08:187
subroutine readreal2darrh5(inpfile, dsetname, arr)
Definition: rdwrhdf5.f08:322
program test
Test program for KronLinInv.
subroutine writereal2darrh5(outfile, dsetname, arr)
Definition: rdwrhdf5.f08:158
subroutine, public posteriormean(U1, U2, U3, diaginvlambda, Z1, Z2, Z3, G1, G2, G3, mprior, dobs, postm)
Computes the posterior mean
Definition: kronlininv.f08:627
This file contains the parallel OpenMPI (distributed memory) version of KronLinInv. OpenMPI and LAPACK libraries are required to be installed in the system.
Procedures to perform linear inversion under gaussian assumptions using the Kronecker-product approac...
Definition: kronlininv.f08:73
subroutine readreal1darrh5(inpfile, dsetname, arr)
Definition: rdwrhdf5.f08:261