-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #146 from ggebbie/ggebbie/invert-model
Julia 1.10 plus initial code for circulation inversion
- Loading branch information
Showing
12 changed files
with
524 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
#=%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
% Under development: the goal is to invert model output | ||
% and get the transport matrix | ||
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% =# | ||
|
||
import Pkg; Pkg.activate(".") | ||
using Revise | ||
using TMI | ||
using Test | ||
using GGplot | ||
using LinearAlgebra | ||
using SparseArrays | ||
using Statistics | ||
#, Distributions, LinearAlgebra, Zygote, ForwardDiff, Optim | ||
|
||
TMIversion = "modern_90x45x33_GH10_GH12" | ||
A, Alu, γ, TMIfile, L, B = config_from_nc(TMIversion) | ||
|
||
|
||
|
||
pkgdir() = dirname(dirname(pathof(TMI))) | ||
pkgdir(args...) = joinpath(pkgdir(), args...) | ||
|
||
pkgdatadir() = joinpath(pkgdir(),"data") | ||
pkgdatadir(args...) = joinpath(pkgdatadir(), args...) | ||
|
||
TMIfile = pkgdatadir("TMI_"*TMIversion*".nc") | ||
θtrue = readfield(TMIfile,"θ",γ) | ||
|
||
ctrue = vec(θtrue) | ||
|
||
#An = A./sum(A;dims=1) | ||
|
||
q = A * ctrue | ||
|
||
# The first guess for the tracer concentration should be close to the actual tracer concentration | ||
# take first guess as θtrue+0.01 | ||
cvec=vec(θtrue).+ 0.1 | ||
θguess = unvec(θtrue,cvec) | ||
|
||
#first guess tracer control vector is near zero, and we want this to remain relatively small | ||
u = Field(-0.01.*ones(size(γ.wet)),γ,θtrue.name,θtrue.longname,θtrue.units) | ||
uvec = vec(u) | ||
|
||
|
||
|
||
#We need an error covariance matrix | ||
W⁻ = Diagonal(1 ./( ones(sum(γ.wet))).^2)#(1/sum(γ.wet)) | ||
|
||
#I want to allow a bunch of error in the surface part of the tracer conservation please | ||
Qerror = ones(size(γ.wet)) | ||
Qerror[:,:,1].=0 | ||
Qfield = Field(Qerror,γ,θtrue.name,θtrue.longname,θtrue.units) | ||
Qvec = vec(Qfield) | ||
|
||
|
||
Q⁻ = Diagonal(1 ./( ones(sum(γ.wet))).^2) | ||
A0= A .* 0.2 | ||
non_zero_indices1, non_zero_indices2, non_zero_values = findnz(A0) | ||
|
||
non_zero_indices = hcat(non_zero_indices1, non_zero_indices2) | ||
|
||
|
||
convec = [uvec; non_zero_values] | ||
ulength=length(uvec) | ||
|
||
# get sample J value | ||
F = costfunction_gridded_model(convec,non_zero_indices,u,A0,ctrue,cvec,q,W⁻,Q⁻,γ) | ||
fg!(F,G,x) = costfunction_gridded_model!(F,G,x,non_zero_indices,u,A0,ctrue,cvec,q,W⁻,Q⁻,γ) | ||
fg(x) = costfunction_gridded_model(x,non_zero_indices,u,A0,ctrue,cvec,q,W⁻,Q⁻,γ) | ||
f(x) = fg(x)[1] | ||
J₀,gJ₀ = fg(convec) | ||
|
||
#### gradient check ################### | ||
# check with forward differences | ||
ϵ = 1e-3 | ||
#ii = rand(1:sum(γ.wet[:,:,1])) | ||
println(size(length(convec))) | ||
ii = rand(1:length(convec)) | ||
println("Location for test =",ii) | ||
δu = copy(convec); δu[ii] += ϵ | ||
∇f_finite = (f(δu) - f(convec))/ϵ | ||
println(∇f_finite) | ||
|
||
fg!(J₀,gJ₀,(convec+δu)./2) # J̃₀ is not overwritten | ||
∇f = gJ₀[ii] | ||
println(∇f) | ||
|
||
# error less than 10 percent? | ||
println("Percent error ",100*abs(∇f - ∇f_finite)/abs(∇f + ∇f_finite)) | ||
#### end gradient check ################# | ||
|
||
|
||
#print(length(convec)) | ||
# filter the data with an Optim.jl method | ||
iterations = 5 | ||
out = steadyclimatology(convec,fg!,iterations) | ||
|
||
# reconstruct by hand to double-check. | ||
ũ = unvec((W⁻ * u),out.minimizer[begin:ulength]) | ||
|
||
|
||
# reconstruct tracer map | ||
c₀ = θguess | ||
c̃ = θguess+ũ | ||
|
||
Δc̃ = c̃ - θtrue | ||
Δc₀ = θguess - θtrue | ||
|
||
Anew = A0 + sparse(non_zero_indices[:, 1], non_zero_indices[:, 2], out.minimizer[ulength+1:end]) | ||
onesvec = ones(size(q)) | ||
|
||
Adiff1 = sum((A.-A0).^2) | ||
Adiff2 = sum((A.-Anew).^2) | ||
oldf = sum((non_zero_values).^2) | ||
newf = sum((out.minimizer[ulength+1:end]).^2) | ||
tracer_cons1 = sum((A0*cvec-q).^2) | ||
tracer_cons2 = sum((Anew*(cvec+out.minimizer[begin:ulength])-q).^2) | ||
mass_cons1 = sum((A0*onesvec-onesvec).^2) | ||
mass_cons2 = sum((Anew*onesvec-onesvec).^2) | ||
|
||
|
||
println("A difference before: $Adiff1") | ||
println("A difference after: $Adiff2") | ||
println("old tracer cons:$tracer_cons1") | ||
println("new tracer cons:$tracer_cons2") | ||
println("old mass cons:$mass_cons1") | ||
println("new mass cons:$mass_cons2") | ||
|
||
|
||
# plot the difference | ||
level = 15 # your choice 1-33 | ||
depth = γ.depth[level] | ||
|
||
cntrs = 0:0.5:15 | ||
label = "True θ" | ||
planviewplot(θtrue, depth, cntrs, titlelabel=label) | ||
readline() | ||
|
||
cntrs = 0:0.5:15 | ||
label = "Optimized θ" | ||
planviewplot(c̃, depth, cntrs, titlelabel=label) | ||
|
||
|
Oops, something went wrong.