Functions in LearnConvection Module

LearnConvection.Main

LearnConvection.Main.plot_profile โ€” Method

plotprofile(gp::GP, data::ProfileData, Vname, timeindex, gprprediction)

Description

Used in interact.jl Plots the simulation profile at a single given index in the data timeseries along with the corresponding GP prediction (prediciting on the training data). i.e. produces a snapshot of the profile evolution. The gpr_prediction is computed outside this function for efficiency.

Arguments

  • โ„ณ (GP or NN). The model.
  • ๐’Ÿ (ProfileData). The ProfileData object used for training and testing.
  • V_name (ProfileData). The ProfileData object whose starting profile will be evolved forward using โ„ณ.
  • time_index (Int). The time index
  • gpr_prediction (Array). Output of getgprpred (which should only be computed once) on โ„ณ and ๐’Ÿ.
source
LearnConvection.Main.predict โ€” Method

Description

Predict profile across all time steps for the true check. - if the problem is sequential, predict profiles from start to finish without the training, using only the initial profile as the initial condition. - if the problem is residual, predict profiles at each timestep using model-predicted difference between truth and physics-based model (KPP or TKE) prediction

Returns an n-length array of D-length vectors, where n is the number of training points and D is the

Arguments

  • โ„ณ (GP or NN). The model you will use to approximate the mapping.
  • ๐’Ÿ (ProfileData). The ProfileData object whose data ๐’ข will "test on", not necessarily the object that was used to train ๐’ข. If the problem is sequential, ๐’Ÿ is the ProfileData object whose starting profile will be evolved forward using ๐’ข.

Keyword Arguments

  • postprocessed (bool or String). If false, return whatever the model predicts directly (direct model output) If true, return the full predicted temperature profile calculated from the model output. If "both", return both.
source

LearnConvection.GaussianProcess

LearnConvection.GaussianProcess โ€” Module

Includes all useful functions for applying GPR to T and wT profiles from Oceananigans.jl simulations. Uses ProfileData struct to store data and GP struct for performing GPR on the data in ProfileData object.

source
LearnConvection.GaussianProcess.GP โ€” Type

GP

Description

  • data structure for typical GPR computations

Data Structure and Description

kernel::โ„ฑ, a Kernel object
x_train::๐’ฎ , an array of vectors (n-length array of D-length vectors)
ฮฑ::๐’ฎ2 , an array
K::๐’ฐ , matrix or sparse matrix
CK::๐’ฑ, cholesky factorization of K
source
LearnConvection.GaussianProcess.GP_multiple โ€” Type

GP_multiple

Description

  • data structure for GPR computations where each gridpoint in the prediction has a different predictor

Data Structure and Description

GPs, Array of GP objects
kernel, Kernel object
x_train
source
LearnConvection.GaussianProcess.Kernel โ€” Type

Constructors for covariance (kernel) functions.

  Constructor                   Description                                Isotropic/Anisotropic
- SquaredExponentialI(ฮณ,ฯƒ):     squared exponential covariance function    isotropic
- ExponentialI(ฮณ,ฯƒ):            exponential covariance function            isotropic
- RationalQuadraticI(ฮณ,ฯƒ,ฮฑ):    rational quadratic covariance function     isotropic
- Matern12I(ฮณ,ฯƒ):               Matรฉrn covariance function with ส‹ = 1/2.   isotropic
- Matern32I(ฮณ,ฯƒ):               Matรฉrn covariance function with ส‹ = 3/2.   isotropic
- Matern52I(ฮณ,ฯƒ):               Matรฉrn covariance function with ส‹ = 5/2.   isotropic
- SMP(w,ฮผ,ฮณ):                   Spectral mixture product cov. function

Distance metrics

- euclidean_distance            lยฒ-norm:  d(x,x') = || x - x' ||",
- derivative_distance           Hยน-norm:  d(x,x') = || diff(x)./diff(z) - diff(x')./diff(z) ||",
- antiderivative_distance       Hโปยน-norm: d(x,x') = || diff(x).*diff(z) - diff(x').*diff(z) ||"
source
LearnConvection.GaussianProcess.compute_kernel_matrix โ€” Method

computekernelmatrix(kernel, x)

Description

  • Computes the kernel matrix for GPR

Arguments

  • k : (Kernel) kernel function k(a,b).
  • x : (array of predictors). x[1] is a vector

Return

  • sK: (symmetric matrix). A symmetric matrix with entries sK[i,j] = k(x[i], x[j]). This is only meaningful if k(x,y) = k(y,x) (it should)
source
LearnConvection.GaussianProcess.mean_log_marginal_loss โ€” Method

meanlogmarginalloss(ytrain, ๐’ข::GP; add_constant=false)

Description

Computes log marginal loss for each element in the output and averages the results. Assumes noise-free observations.

log(p(y|X)) = -(1/2) * (y'ฮฑ + 2sum(Diagonal(CK)) + nlog(2pi)) where n is the number of training points and

Arguments

  • y_train: (Array). training outputs (prediction), must have the same number as x_train
  • ๐’ข: (GP).

Keyword Arguments

  • add_constant: (bool). whether to give the exact value of the loss or leave out an added constant for efficiency.
source
LearnConvection.GaussianProcess.model โ€” Method

model(xtrain, ytrain; kernel; sparsitythreshold = 0.0, robust = true, entrythreshold = sqrt(eps(1.0)))

Description

Constructs the posterior distribution for a gp. In other words this does the 'training' automagically.

Arguments

  • x_train: (array). training inputs (predictors), must be an array of states. length-n array of D-length vectors, where D is the length of each input n is the number of training points.
  • y_train: (array). training outputs (prediction), must have the same number as x_train length-n array of D-length vectors.
  • kernel: (Kernel). Kernel object. See kernels.jl. kernel_function(kernel)(x,x') maps predictor x predictor to real numbers.

Keyword Arguments

  • z: (vector). values w.r.t. which to derivate the state (default none).
  • normalize: (bool). whether to normalize the data during preprocessing and reverse the scaling for postprocessing. Can lead to better performance.
  • hyperparameters: (array). default = []. hyperparameters that enter into the kernel
  • sparsity_threshold: (number). default = 0.0. a number between 0 and 1 that determines when to use sparse array format. The default is to never use it
  • robust: (bool). default = true. This decides whether to uniformly scale the diagonal entries of the Kernel Matrix. This sometimes helps with Cholesky factorizations.
  • entry_threshold: (number). default = sqrt(eps(1.0)). This decides whether an entry is "significant" or not. For typical machines this number will be about 10^(-8) * largest entry of kernel matrix.

Return

  • GP object
source
LearnConvection.GaussianProcess.model โ€” Method

model(๐’Ÿ::ProfileData; kernel::Kernel = Kernel(), stencil_size=nothing)

Description

Create an instance of GP using data from ProfileData object ๐’Ÿ.

Arguments

  • ๐’Ÿ::ProfileData, Data for training the GP

Keyword Arguments

  • kernel::Kernel,
  • stencil_size::Int64
source
LearnConvection.GaussianProcess.model_output โ€” Method

prediction(x, ๐’ข::GP_multiple)

Description

  • Given state x, GP_multiple ๐’ข, returns the mean GP prediction

Arguments

  • x: single scaled state
  • ๐’ข: GP_multiple object with which to make the prediction

Return

  • y: scaled prediction
source

LearnConvection.Data

LearnConvection.Data.ProfileData โ€” Type

ProfileData

Description

  • data structure for preparing profile data for analysis with gpr or nn.

Data Structure and Description

v::Array,           Nz x Nt array of T or wT values directly from the LES simulation, not preprocessed.
vavg::Array,        Nt-length array of Nz-length vectors from LES simulation, scaled and pre-processed
x::Array,           all simulation inputs, scaled and pre-processed
y::Array,           all simulation inputs, scaled and pre-processed
x_train::Array,     training inputs (predictors; array of states). (length-n array of D-length vectors, where D is the length of each input n is the number of training points)
y_train::Array,     training outputs (predictions) (length-n array of D-length vectors).
validation_set::Array, vector of indices corresponding to validation data
z::Array,           Nz-length vector of depth values
zavg::Array,        length-D vector; depth values averaged to D gridpoints
t::Array,           timeseries [seconds]
Nt::Int64,          length(timeseries)
state_variables
problem::Problem,   what mapping you wish to evaluate with the model. (Sequential("T"), Sequential("wT"), Residual("T"), Residual("KPP"), or Residual("TKE"))
source
LearnConvection.Data.ReadJLD2_LESbraryData โ€” Method

LESbraryData(filename)

Description

  • Constructor for Oceananigans data type. Loads data from LES

Fields for the output are

# initial conditions
Tโฐ::๐’ฎ
Sโฐ::๐’ฎ
Uโฐ::๐’ฎ
Vโฐ::๐’ฎ
# fields at each moment in time
T::๐’ฏ
S::๐’ฏ
U::๐’ฏ
V::๐’ฏ
# some second order statistics at each moment in time
wT::๐’ฏ
wS::๐’ฏ
uu::๐’ฏ
vv::๐’ฏ
ww::๐’ฏ
tke::๐’ฏ
# simulation constants
ฯ::๐’ฐ
ฮฑ::๐’ฐ
ฮฒ::๐’ฐ
cแต–::๐’ฐ
fโฐ::๐’ฐ
g::๐’ฐ
# time and grid
t::๐’ฎ
z::๐’ฎ
# #top boundary condition data, see string for type of boundary condition
# top_T::๐’ฐ
# top_S::๐’ฐ
# top_U::๐’ฐ
# top_V::๐’ฐ
# #bottom boundary condition data, see string for type of boundary condtion
# bottom_T::๐’ฐ
# bottom_S::๐’ฐ
# bottom_U::๐’ฐ
# bottom_V::๐’ฐ
#info about the simulation
info::๐’ฑ
source
LearnConvection.Data.ReadJLD2_OceananigansData โ€” Method

OceananigansData(filename)

Description

  • Constructor for Oceananigans data type. Loads data from LES

Fields for the output are

# initial conditions
Tโฐ::๐’ฎ
Sโฐ::๐’ฎ
Uโฐ::๐’ฎ
Vโฐ::๐’ฎ
# fields at each moment in time
T::๐’ฏ
S::๐’ฏ
U::๐’ฏ
V::๐’ฏ
# some second order statistics at each moment in time
wT::๐’ฏ
wS::๐’ฏ
uu::๐’ฏ
vv::๐’ฏ
ww::๐’ฏ
tke::๐’ฏ
# simulation constants
ฯ::๐’ฐ
ฮฑ::๐’ฐ
ฮฒ::๐’ฐ
cแต–::๐’ฐ
fโฐ::๐’ฐ
g::๐’ฐ
# time and grid
t::๐’ฎ
z::๐’ฎ
#top boundary condition data, see string for type of boundary condition
top_T::๐’ฐ
top_S::๐’ฐ
top_U::๐’ฐ
top_V::๐’ฐ
#bottom boundary condition data, see string for type of boundary condtion
bottom_T::๐’ฐ
bottom_S::๐’ฐ
bottom_U::๐’ฐ
bottom_V::๐’ฐ
#info about the simulation
info::๐’ฑ
source
LearnConvection.Data.ReadNetCDF_OceananigansData โ€” Method

OceananigansData(filename)

Description

  • Constructor for Oceananigans data type. Loads data from LES

Fields for the output are

# initial conditions
Tโฐ::๐’ฎ
Sโฐ::๐’ฎ
Uโฐ::๐’ฎ
Vโฐ::๐’ฎ
# fields at each moment in time
T::๐’ฏ
S::๐’ฏ
U::๐’ฏ
V::๐’ฏ
# some second order statistics at each moment in time
wT::๐’ฏ
wS::๐’ฏ
uu::๐’ฏ
vv::๐’ฏ
ww::๐’ฏ
# simulation constants
ฯ::๐’ฐ
ฮฑ::๐’ฐ
ฮฒ::๐’ฐ
cแต–::๐’ฐ
fโฐ::๐’ฐ
g::๐’ฐ
# time and grid
t::๐’ฎ
z::๐’ฎ
#top boundary condition data, see string for type of boundary condition
top_T::๐’ฐ
top_S::๐’ฐ
top_U::๐’ฐ
top_V::๐’ฐ
#bottom boundary condition data, see string for type of boundary condtion
bottom_T::๐’ฐ
bottom_S::๐’ฐ
bottom_U::๐’ฐ
bottom_V::๐’ฐ
#info about the simulation
info::๐’ฑ
source
LearnConvection.Data.closure_free_convection_flexible โ€” Method

closurefreeconvection_flexible(N, ฮ”t, les::OceananigansData; subsample = 1, grid = 1)

Description

  • constructs forward map. Assumes initial conditions and boundary conditions are taken from les data. Adds two more parameters compared to previous one

Arguments

  • N: number of gridpoints to output to
  • ฮ”t: time step size in seconds
  • les: les data of the OceananigansData type

Keyword Arguments

  • subsample: indices to subsample in time,
  • grid: in case one wants to save the model grid
  • power: a method of modifying the scaling law, default is 1

Output

  • The forward map. A function that takes parameters and outputs temperature profiles

  • ๐‘ช: parameters in KPP, assumes that

    ๐‘ช[1]: Surface Layer Fraction

    ๐‘ช[2]: Nonlocal Flux Amplitude

    ๐‘ช[3]: Diffusivity Amplitude

    ๐‘ช[4]: Shear Constant, w1 * w2

    ๐‘ช[5] = Shear Constant 2 ๐‘ช[6]: Shear Constant 3,

  • The extra parameters correspond to C[5] * w1^(1-power) * w2^(1+power)

  • where w1 = max(0, h * Qb)^(1/3), w2 = sqrt(๐‘ช[6] * h^2), are two velocity scales.

source
LearnConvection.Data.closure_free_convection_kpp_full_evolution โ€” Method

closurefreeconvection(N, ฮ”t, les::OceananigansData; subsample = 1, grid = 1)

Description

  • constructs forward map. Assumes initial conditions and boundary conditions are taken from les data.

Arguments

  • N: number of gridpoints to output to
  • ฮ”t: time step size in seconds
  • les: les data of the OceananigansData type

Keyword Arguments

  • subsample: indices to subsample in time,
  • grid: in case one wants to save the model grid

Output

  • The forward map. A function that takes parameters and outputs temperature profiles

  • ๐‘ช: parameters in KPP, assumes that

    ๐‘ช[1]: Surface Layer Fraction

    ๐‘ช[2]: Nonlocal Flux Amplitude

    ๐‘ช[3]: Diffusivity Amplitude

    ๐‘ช[4]: Shear Constant

source
LearnConvection.Data.custom_avg โ€” Method

From sandreza/Learning/sandbox/oceananigansconverter.jl https://github.com/sandreza/Learning/blob/master/sandbox/oceananigansconverter.jl

custom_avg(ฮฆ, n)

Description

  • Average a field down to n.
  • Requires field to have evenly spaced points. Size of N leq length(ฮฆ).
  • Furthermore requires

Arguments

  • ฮฆ :(vector) The field, an array
  • n :(Int) number of grid points to average down to.

Return

  • ฮฆ2 :(vector) The field with values averaged, an array
source
LearnConvection.Data.data โ€” Method

data(filename, D; N=4)

Description

Returns an instance of ProfileData containing training data from multiple simulations. *** Important: ONLY v, xtrain and ytrain contain data from all filenames; the remaining attributes are taken from the first filename in filenames

Arguments

  • filenames: (string) Vector of filenames (.nc or .jld2) to collect data from.
  • problem: (Problem). What mapping you wish to evaluate with the model. (Sequential("T"), Sequential("wT"), Residual("T"), Residual("KPP"), or Residual("TKE"))

Keyword Arguments

  • D: (integer) Number of gridpoints in the z direction to average the data to for training and prediction.
  • N: (integer) Interval between the timesteps to be reserved for training data (default 4). If N=4, the profile data for every 4 timesteps will be reserved for training (~25% training data); the rest will be used in the validation set.
source
LearnConvection.Data.data โ€” Method

data(filename, problem; D=16, N=4)

Description

Returns a ProfileData object based on data from filename

Arguments

  • filename: (string) Name of the NetCDF or JLD2 file containing the data from the Oceananigans simulation.
  • problem: (Problem). What mapping you wish to evaluate with the model.

Keyword Arguments

  • D: (integer) Number of gridpoints in the z direction to average the data to for training and prediction.
  • N: (integer) Interval between the timesteps to be reserved for training data (default 4). If N=4, the profile data for every 4 timesteps will be reserved for training (~25% training data); the rest will be used in the validation set.
source
LearnConvection.Data.get_predictors_targets โ€” Method

get_predictors_targets(vavg::Array, problem::Sequential_T)

Description

Returns x and y, the predictors and target predictions from which to extract the training and verification data for "T" profiles.

model( predictors ) -> targets
      model( T[i] ) -> T[i+1]

Arguments

  • vavg: (Array) Nt-length array of D-length vectors. Data from which to extract x and y, the predictors and corresponding predictions.
  • problem: (SequentialT) SequentialT object associated with the data (output of get_problem)
source
LearnConvection.Data.get_predictors_targets โ€” Method

get_predictors_targets(vavg, problem::Sequential_wT)

Description

Returns x and y, the predictors and targets from which to extract the training and verification data for "wT" profiles.

 model( predictor ) -> target
     model( wT[i] ) -> wT[i+1]

Arguments

  • vavg: (Array) Nt-length array of D-length vectors. Data from which to extract x and y, the predictors and corresponding predictions.
  • problem: (SequentialwT) SequentialwT object associated with the data (output of get_problem)
source
LearnConvection.Data.get_predictors_targets โ€” Method

get_predictors_targets(vavg::Array, problem::Sequential_T)

Description

Returns x and y, the scaled predictor and target pairs from which to extract the training and verification data sets for "T" profiles.

model( predictor ) -> target
     model( T[i] ) -> (T[i+1]-T[i])/ฮ”t โ‰ˆ โˆ‚t(T)

Arguments

  • vavg: (Array) Nt-length array of D-length vectors. Data from which to extract x and y, the predictors and corresponding predictions.
  • problem: (SequentialdT) SequentialT object associated with the data (output of get_problem)
source
LearnConvection.Data.get_predictors_targets โ€” Method

getpredictorstargets(vavg::Array, problem::Residual_KPP)

Description

Returns x and y, the scaled predictors and target predictions from which to extract the training and verification data for temperature profiles.
Scales the predictors and targets using min-max scaling based on the initial temperature profile from the les simulation.

model( predictors ) = targets
model( KPP(T[i])  ) = T[i] - KPP(T[i])

Arguments

  • vavg: (Array) Nt-length array of D-length vectors. Data from which to extract x and y, the predictors and corresponding predictions.
  • problem: (ResidualKPP) ResidualKPP object associated with the data (output of get_problem)
source
LearnConvection.Data.get_predictors_targets โ€” Method

getpredictorstargets(vavg::Array, problem::Residual_KPP)

Description

Returns x and y, the scaled predictors and target predictions from which to extract the training and verification data for temperature profiles.
Scales the predictors and targets using min-max scaling based on the initial temperature profile from the les simulation.

model( predictors ) = targets
model( KPP(T[i])  ) = T[i] - KPP(T[i])

Arguments

  • vavg: (Array) Nt-length array of D-length vectors. Data from which to extract x and y, the predictors and corresponding predictions.
  • problem: (ResidualKPP) ResidualKPP object associated with the data (output of get_problem)
source
LearnConvection.Data.get_predictors_targets โ€” Method

getpredictorstargets(vavg::Array, problem::Slack_KPP)

Description

Returns x and y, the scaled predictors and target predictions from which to extract the training and verification data for temperature profiles.
Scales the predictors and targets using min-max scaling based on the initial temperature profile from the les simulation.

model( predictors ) = targets
model( KPP(T[i])  ) = T[i] - KPP(T[i])

Arguments

  • vavg: (Array) Nt-length array of D-length vectors. Data from which to extract x and y, the predictors and corresponding predictions.
  • problem: (SlackKPP) SlackKPP object associated with the data (output of get_problem)
source
LearnConvection.Data.get_problem โ€” Method

get_problem(problem::Problem, data::OceananigansData, timeseries)

Description

Creates an instance of a Problem struct depending on the type of mapping.
Returns the problem and the correct data array from the OceananigansData objectssociated with the variable of interest.

Arguments

  • 'problem': (Problem). what mapping you wish to evaluate with the model. (Sequential("T"), Sequential("wT"))
  • 'v': (Array). Array of values
  • 'les': (OceananigansData). LES simulation output
  • 'Nยฒ': (Number) initial buoyancy stratification
  • 'D': (Number) number of grid points in the vertical
source
LearnConvection.Data.get_v โ€” Method

get_v(problem)

Description

Return data array from the OceananigansData object associated with the variable of interest.

Arguments

  • 'problem': (Problem). what mapping you wish to evaluate with the model. (Sequential("T"), Sequential("wT"))
source
LearnConvection.Data.postprocess_prediction โ€” Method

postprocess_prediction(predictor, prediction, problem::Sequential_T)

Description

Takes in a scaled predictor, T[i], the scaled GP prediction on the predictor, G(T[i]), and a Sequential_T object. Returns the predicted temperature profile, T[i+1], computed from T[i] and G(T[i]) by

       prediction = model( predictor )
 predicted T[i+1] = model( T[i] )

Arguments

  • predictor: (Array) T[i], the scaled predictor for a temperature profile
  • prediction: (Array) model(T[i), the scaled prediction for a temperature profile
  • problem: (Sequential_T)
source
LearnConvection.Data.postprocess_prediction โ€” Method

postprocess_prediction(predictor, model_output, problem::Sequential_dT)

Description

Takes in a scaled predictor, T[i], the scaled model output on the predictor, model(T[i]), and a Sequential_T object. Returns the unscaled prediction (predicted temperature profile), T[i+1], computed from T[i] and model(T[i]) by

predicted T[i+1] = model(T[i])ฮ”t + T[i]

Arguments

  • predictor: (Array) T[i], the scaled predictor for a temperature profile
  • model_output: (Array) model(T[i]), the scaled model output for a temperature profile
  • problem: (Sequential_dT)
source
LearnConvection.Data.postprocess_prediction โ€” Method

postprocess_prediction(predictor, prediction, problem::Sequential_wT)

Description

Takes in a scaled predictor, wT[i], the scaled model prediction on the predictor, model(wT[i]), and a Sequential_wT object. Returns the temperature profile, T[i+1], computed from model(T[i]) by

       prediction = model(predictor)
predicted wT[i+1] = model(wT[i])

Arguments

  • predictor: (Array) wT[i], the predictor for a wT profile
  • prediction: (Array) predicted wT[i+1], the model prediction for a wT profile
  • problem: (SequentialwT) SequentialwT object associated with the data
source
LearnConvection.Data.postprocess_prediction โ€” Method

Description

Takes in a predictor, T[i], the GP prediction on the predictor, G(T[i]), and a Residual_KPP object. Returns the predicted temperature profile, T[i+1], computed from T[i] and G(T[i]) by

       prediction = model(predictor) # residual
 predicted T[i+1] = model(KPP(T[i])) + KPP(T[i])

Arguments

  • predictor: (Array) T[i], the predictor for a temperature profile
  • prediction: (Array) model(T[i), the prediction for a temperature profile
  • problem: (Residual_KPP)
source
LearnConvection.Data.postprocess_prediction โ€” Method

Description

Takes in a predictor, T[i], the GP prediction on the predictor, G(T[i]), and a Residual_KPP object. Returns the predicted temperature profile, T[i+1], computed from T[i] and G(T[i]) by

       prediction = model(predictor) # residual
 predicted T[i+1] = model(KPP(T[i])) + KPP(T[i])

Arguments

  • predictor: (Array) T[i], the predictor for a temperature profile
  • prediction: (Array) model(T[i), the prediction for a temperature profile
  • problem: (Residual_KPP)
source
LearnConvection.Data.postprocess_prediction โ€” Method

Description

Takes in a predictor, T[i], the GP prediction on the predictor, G(T[i]), and a Slack_KPP object. Returns the predicted temperature profile, T[i+1], computed from T[i] and G(T[i]) by

       prediction = model(predictor) # residual
 predicted T[i+1] = model(KPP(T[i])) + KPP(T[i])

Arguments

  • predictor: (Array) T[i], the predictor for a temperature profile
  • prediction: (Array) model(T[i), the prediction for a temperature profile
  • problem: (Slack_KPP)
source