EMPIRE DA  v1.9.1
Data assimilation codes using EMPIRE communication
 All Classes Files Functions Variables Pages
threedvar_fcn.f90
Go to the documentation of this file.
1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2 !!! Time-stamp: <2015-10-13 15:25:35 pbrowne>
3 !!!
4 !!! subroutine to compute 3DVar objective function and gradient
5 !!! Copyright (C) 2015 Philip A. Browne
6 !!!
7 !!! This program is free software: you can redistribute it and/or modify
8 !!! it under the terms of the GNU General Public License as published by
9 !!! the Free Software Foundation, either version 3 of the License, or
10 !!! (at your option) any later version.
11 !!!
12 !!! This program is distributed in the hope that it will be useful,
13 !!! but WITHOUT ANY WARRANTY; without even the implied warranty of
14 !!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 !!! GNU General Public License for more details.
16 !!!
17 !!! You should have received a copy of the GNU General Public License
18 !!! along with this program. If not, see <http://www.gnu.org/licenses/>.
19 !!!
20 !!! Email: p.browne @ reading.ac.uk
21 !!! Mail: School of Mathematical and Physical Sciences,
22 !!! University of Reading,
23 !!! Reading, UK
24 !!! RG6 6BB
25 !!!
26 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
27 
28 
54 subroutine threedvar_fcn(n, x, f, g )
55  use sizes
56  use timestep_data
57  use comms
58  use threedvar_data
59  include 'mpif.h'
60  integer, intent(in) :: n
61  integer, parameter :: rk = kind(1.0d0)
62  real(kind=rk), dimension(n), intent(in) :: x
63  real(kind=rk), dimension(n), intent(out) :: g
65  real(kind=rk), intent(out) :: f
66 
67  real(kind=rk), dimension(obs_dim) :: y,Hx
68  real(kind=rk), dimension(n) :: gtemp
69  real(kind=rk), dimension(n) :: xtemp
70  real(kind=rk) :: ftemp,ftemp3
71  integer :: mpi_err
72  !start with observation component:
73  ! Hx = H(x)
74  call h(obs_dim,1,x,hx,tsdata%current_timestep)
75 
76  call get_observation_data(y,tsdata%current_timestep)
77 
78  ! USE BLAS to perform
79  ! y = y - Hx
80  call daxpy(obs_dim,-1.0d0,hx,1,y,1)
81 
82  ! hx = R^{-1}(y-Hx)
83  call solve_r(obs_dim,1,y,hx,tsdata%current_timestep)
84 
85  ! compute the obs component of the objective function
86  ! f = 0.5 (y-Hx)^T R^{-1} (y-Hx)
87  ftemp = 0.5_rk*sum(y*hx)
88  if(comm_version .eq. 3) then
89  !need to perform the sum across all parts of the obs vector
90  ftemp3 = ftemp
91  call mpi_allreduce(ftemp3,ftemp,1,mpi_double_precision,mpi_sum&
92  &,pf_member_comm,mpi_err)
93  end if
94 
95  ! gtemp = H^TR^{-1}(y-Hx)
96  call ht(obs_dim,1,y,gtemp,tsdata%current_timestep)
97 
98 
99 
100  !now lets do the background component
101 
102  !x - xb
103  xtemp = x - xb
104 
105  !g = B^{1}(x-xb)
106  call solve_b(1,xtemp,g)
107 
108  ! compute the background component of the objective function
109  ! f = 0.5 (x-x_b)^T B^{-1} (x-x_b)
110  f = 0.5_rk*sum(xtemp*g)
111  if(comm_version .eq. 3) then
112  !need to perform the sum across all parts of the obs vector
113  ftemp3 = f
114  call mpi_allreduce(ftemp3,f,1,mpi_double_precision,mpi_sum&
115  &,pf_member_comm,mpi_err)
116  end if
117 
118  ! put the background and observation components of the objective
119  ! function together
120  f = f + ftemp
121 
122  ! put the background and observation components of the gradient
123  ! together. Note the minus sign in the obs component.
124  ! Use blas to perform g = g - gtemp
125  call daxpy(state_dim,-1.0d0,gtemp,1,g,1)
126 
127 end subroutine threedvar_fcn
subroutine solve_r(obsDim, nrhs, y, v, t)
subroutine to take an observation vector y and return v in observation space.
Module containing EMPIRE coupling data.
Definition: comms.f90:57
Module that stores the information about the timestepping process.
subroutine threedvar_fcn(n, x, f, g)
subroutine to provide the objective function and gradient for 3dvar
Module that stores the dimension of observation and state spaces.
Definition: sizes.f90:29
subroutine h(obsDim, nrhs, x, hx, t)
subroutine to take a full state vector x and return H(x) in observation space.
subroutine solve_b(nrhs, x, v)
subroutine to take a state vector x and return v in state space.
subroutine ht(obsDim, nrhs, y, x, t)
subroutine to take an observation vector y and return x in full state space.
module to store stuff for 3DVar
subroutine get_observation_data(y, t)
Subroutine to read observation from a file .