Commit eeae95d6 authored by Johannes Blaschke's avatar Johannes Blaschke

create flux array object to store fluxes

parent 61e1a244
......@@ -29,6 +29,177 @@
"using .FP"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FluxArray{Float64}([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = FluxArray{Float64}(10)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f.total[1] = 2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FluxArray{Float64}([2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FluxArray{Float64}([2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"size(f)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11-element Array{Float64,1}:\n",
" 2.0\n",
" 0.0\n",
" 0.0\n",
" 0.0\n",
" 0.0\n",
" 0.0\n",
" 0.0\n",
" 0.0\n",
" 0.0\n",
" 0.0\n",
" 0.0"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f.total"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"reset!(f, 0.)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11-element Array{Float64,1}:\n",
" 0.0\n",
" 0.0\n",
" 0.0\n",
" 0.0\n",
" 0.0\n",
" 0.0\n",
" 0.0\n",
" 0.0\n",
" 0.0\n",
" 0.0\n",
" 0.0"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f.total"
]
},
{
"cell_type": "code",
"execution_count": null,
......
module FP
export FluxArray, size, reset!
import Base.size
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).
#
total::Array{T}
diffusive::Array{T}
stochastic::Array{T}
function FluxArray{T}(n) where T
total::Array{T} = zeros(n + 1);
diffusive::Array{T} = zeros(n + 1);
stochastic::Array{T} = zeros(n + 1);
new{T}(total, diffusive, stochastic)
end
end
function size(flx::FluxArray{T}) where T
return size(flx.total, 1)
end
function reset!(flx::FluxArray{T}, zero::T) where T
for i = 1:size(flx)
flx.total[i] = zero
flx.diffusive[i] = zero
flx.stochastic[i] = zero
end
end
#function getFlux(n, h, N, k, D, FP, time)
# #get the diffusive flux for FV solver
#
# #create flux vectors
# Ftot = zeros(1,N+1); #total flux
# Fd = zeros(1,N+1); #diffusive flux
# Fs = zeros(1,N+1); #stochastic flux
#
# #get the fraction of particles going in each direction
# fr = rand(1,N); #fraction going right, 1-fr is fraction going left
# left = n.*(1-fr);right = n.*fr; #define left and right particle numbers
#
# #compute the diffusive flux
# for i = 2:N
# Fd(i) = D/h * (left(i) - right(i-1)); #random fractions
# #Fd(i) = D/h * (n(i) - n(i-1)); #usual fv
# end
#
# #compute the stochastic flux - assume gaussian
# R = randn(1,N+1); #standard normals
# for i = 2:N
# avg = left(i) + right(i-1); #average over cell, ignore factor of 2
# Fs(i) = R(i)*sqrt(D*avg/(k*sqrt(h))); #set correct variance, 2 cancels
# end
#
# #check if each box has a physical flux
# nu = k/h; #ratio of time to space step to propogate soln
# Ftot = Fd + Fs; #set total flux
# Q = -Ftot*nu; #take time step to get real fluxes
# for i=1:N-1
# P = right(i)+left(i+1); #total particle number
# x0 = left(i+1); #number in right box
# lower = -x0; upper = P-x0; #valid range of fluxes
# if (Q(i+1)>upper || Q(i+1) < lower)
# #if particle num is low enough, sample uniform - approx FP
# if (P < 1e-3)
# Ftot(i+1) = -1/nu*(P*rand()-x0);
# else
# #do a fokker planck solve - sample flux
# #divide by nu to remain consistent.
# flux = FPsample(P,x0,D,h,k);
# Ftot(i+1) = -1/nu*flux;
# FP{i}(time) = 1;
# end
# end
# end
#
# return Ftot, FP
#end
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment