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

added simplified histogram

parent d8407280
Loading
Loading
Loading
Loading
+29 −18
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);
n = DistributionArray{Float64}(0., 1., N);
f = FluxArray{Float64}(N);
```

%% Cell type:code id: tags:

``` julia
n[Integer(length(n)/2)] = 1
n.p[Integer(size(n)/2)] = 1;
n.p
```

%% Output

    1
    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_fr!(f);
fill_lr!(f, n);
fill_lr!(f, n.p);
```

%% Cell type:code id: tags:

``` julia
f.left
```

%% Output

    10-element Array{Float64,1}:
     0.0
     0.0
     0.0
     0.0
     0.13816584763737239
     0.0022742765607139948
     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.8618341523626276
     0.997725723439286
     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
      138.1658476373724
     -861.8341523626276
        2.2742765607139948
     -997.725723439286
        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}:
     -0.42054232430683963
     -0.0
      0.0
     -0.8640646960498399
      0.0
      0.9582299402193841
      9.427963410111976
      0.0
     -0.0
      0.3672085709169771
     -2.924360797089483
      0.0
      0.0
     -0.0
      0.6927609681908308
      0.0
      0.3838915655069409

%% 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
      0.9582299402193841
      9.427963410111976
      0.0
     -0.0
      0.3672085709169771
     -2.924360797089483
      0.0
      0.0
     -0.0
      0.0
      0.0

%% Cell type:code id: tags:

``` julia
```
+40 −9
Original line number Diff line number Diff line
module FP

export FluxArray, size, reset!, fill_fr!, fill_lr!, fill_df!, fill_sf!, apply_bc!
export DistributionArray
export size
export FluxArray, reset!, fill_fr!, fill_lr!, fill_df!, fill_sf!, apply_bc!

import Base.size
import Random.rand!
@@ -10,6 +12,35 @@ include("sample.jl")
import .Sample


"""
DistributionArrays store the distribution (probablity) `p` data, together with
the cell-center `xc` and face-center `xf` coodrdinates. This is essentially a
cheap histogram.
"""
mutable struct DistributionArray{T}

    xc::Array{T};
    xf::Array{T};
    p::Array{T};

    function DistributionArray{T}(xlo::T, xhi::T, n::Integer) where T
        p::Array{T} = zeros(n);

        xf::Array{T} = collect(range(xlo, stop = xhi, length = n+1));
        
        dx::T = (xhi-xlo)/n;
        xc::Array{T} = xf[1:n] .+ dx/2;
        
        new{T}(xc, xf, p)
    end
end

function size(d::DistributionArray{T}) where T
    return size(d.p, 1)
end



"""
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`
@@ -18,23 +49,23 @@ is the argument of the constructor).
mutable struct FluxArray{T}

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

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

    # Storage for the Dististribution object (used to sample Gaussian random variables)
    distribution  
    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
    function FluxArray{T}(n::Integer) where T
        total::Array{T}      = zeros(n + 1);
        diffusive::Array{T}  = zeros(n + 1);
        stochastic::Array{T} = zeros(n + 1);