Commit fecf6229 authored by Johannes Blaschke's avatar Johannes Blaschke
Browse files

add docstrings

parent 2dc2e0b8
Loading
Loading
Loading
Loading
+12 −12
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` julia
include("src/fp.jl")
```

%% Output

    Main.FP

%% Cell type:code id: tags:

``` julia
using .FP
```

%% Cell type:code id: tags:

``` julia
N = 10
D = 1.;
h = 1e-3;
k = 1.;
```

%% Cell type:code id: tags:

``` julia
n = zeros(N);
f = FluxArray{Float64}(N);
```

%% Cell type:code id: tags:

``` julia
n[Integer(length(n)/2)] = 1
```

%% Output

    1

%% Cell type:code id: tags:

``` julia
fill_fr!(f);
fill_lr!(f, n);
```

%% Cell type:code id: tags:

``` julia
f.left
```

%% Output

    10-element Array{Float64,1}:
     0.0
     0.0
     0.0
     0.0
     0.06446778552563037
     0.4618638805059607
     0.0
     0.0
     0.0
     0.0
     0.0

%% Cell type:code id: tags:

``` julia
f.right
```

%% Output

    10-element Array{Float64,1}:
     0.0
     0.0
     0.0
     0.0
     0.9355322144743696
     0.5381361194940393
     0.0
     0.0
     0.0
     0.0
     0.0

%% Cell type:code id: tags:

``` julia
f.left .+ f.right
```

%% Output

    10-element Array{Float64,1}:
     0.0
     0.0
     0.0
     0.0
     1.0
     0.0
     0.0
     0.0
     0.0
     0.0

%% Cell type:code id: tags:

``` julia
fill_df!(f, D, h);
```

%% Cell type:code id: tags:

``` julia
f.diffusive
```

%% Output

    11-element Array{Float64,1}:
        0.0
        0.0
        0.0
        0.0
       64.46778552563038
     -935.5322144743697
      461.8638805059607
     -538.1361194940392
        0.0
        0.0
        0.0
        0.0
        0.0

%% Cell type:code id: tags:

``` julia
fill_sf!(f, D, h, k);
```

%% Cell type:code id: tags:

``` julia
f.stochastic
```

%% Output

    11-element Array{Float64,1}:
      1.4002402012922117
     -0.5929904221281879
     -0.0
     -0.0
     -0.0
     -1.735176623280667
     -5.851512422602964
      0.0
     -4.280339552006784
      2.185681116243459
      0.0
     -0.0
      0.0
     -0.0
     -0.5199940022311099
     -0.31415706471809324

%% Cell type:code id: tags:

``` julia
apply_bc!(f);
```

%% Cell type:code id: tags:

``` julia
f.stochastic
```

%% Output

    11-element Array{Float64,1}:
      0.0
     -0.0
     -0.0
     -0.0
     -1.735176623280667
     -5.851512422602964
      0.0
     -4.280339552006784
      2.185681116243459
      0.0
     -0.0
      0.0
     -0.0
      0.0

%% Cell type:code id: tags:

``` julia
```
+44 −8
Original line number Diff line number Diff line
@@ -6,23 +6,30 @@ import Base.size
import Random.rand!
import Distributions.Normal

"""
FluxArrays store flux data as Struct-of-Arrays. These arrays are allocated and
initialized (to zero) by the constructor (each array has length `n+1` where `n`
is the argument of the constructor).
"""
mutable struct FluxArray{T}
    #
    # FluxArrays store flux data as Struct-of-Arrays. These arrays are allocated
    # and initialized (to zero) by the constructor (each array has length `n+1`
    # where `n` is the argument of the constructor).
    #

    # Fluxes
    total::Array{T}
    diffusive::Array{T}
    stochastic::Array{T}

    # Left and Right sampling
    fr::Array{T}
    left::Array{T}
    right::Array{T}

    # Storage for the Dististribution object (used to sample Gaussian random variables)
    distribution  

    """
    Constructor: allocates memory for all fluxes, and intializes the distribution
    object. Note that `n` is the length of the corresponding 1D data array.
    """
    function FluxArray{T}(n) where T
        total::Array{T}      = zeros(n + 1);
        diffusive::Array{T}  = zeros(n + 1);
@@ -38,15 +45,24 @@ mutable struct FluxArray{T}
    end
end

"""
Returns the size of the FluxArray's internal face-centered Array{T} containers
"""
function size(flx::FluxArray{T}) where T
    return size(flx.total, 1)
end

"""
Same as `size` but returns the FluxArray's internal cell-centered Array{T}
containers. Identical to `size(flx)-1`
"""
function size_cc(flx::FluxArray{T}) where T
    return size(flx) - 1
end


"""
Resets all fluxes to the specified value `zero::T`
"""
function reset!(flx::FluxArray{T}, zero::T) where T
    for i = 1:size(flx)
        flx.total[i]      = zero
@@ -55,21 +71,37 @@ function reset!(flx::FluxArray{T}, zero::T) where T
    end
end

"""
Fills the fr (Fraction Right) array with uniform random numbers
"""
function fill_fr!(flx::FluxArray{T}) where T
    rand!(flx.fr);
end

"""
Fills the `left` and `right` fields using the data `n` and the `fr` arrays.
Note that `fill_fr!` needs to be called first for new left and right data.
"""
function fill_lr!(flx::FluxArray{T}, n::Array{T}) where T
    flx.left  = n .* (1 .- flx.fr);
    flx.right = n .* flx.fr;
end

"""
Fills the diffusive fluxes using the `left` and `right` data. `D` is the
diffusion coefficient and `h` is the spatial discretization.
"""
function fill_df!(flx::FluxArray{T}, D::T, h::T) where T
    for i = 2:size_cc(flx)
        flx.diffusive[i] = D/h * (flx.left[i] - flx.right[i-1]);
    end
end

"""
Fills the stochstic fluxes using the `left` and `right` data, as well as the
normal random numbers generated by `distribution`. `D` is the diffusion
coefficient, `h` is the spatial discretization, and `k` is unknown (TODO).
"""
function fill_sf!(flx::FluxArray{T}, D::T, h::T, k::T) where T
    rand!(flx.distribution, flx.stochastic);
    for i = 2:size_cc(flx)
@@ -78,6 +110,10 @@ function fill_sf!(flx::FluxArray{T}, D::T, h::T, k::T) where T
    end
end

"""
Fills the stochastic and diffusive flux boundary conditions. Currently only
Dirichlet BCs are implemented.
"""
function apply_bc!(flx::FluxArray{T}, 
                   vd_lo::T=T(0), vd_hi::T=T(0), vs_lo::T=T(0), vs_hi::T=T(0)) where T
    flx.diffusive[1]    = vd_lo;