Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
General changes
Browse files Browse the repository at this point in the history
  • Loading branch information
f-ij committed Aug 10, 2022
1 parent cd2b8f0 commit c1fdf76
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 109 deletions.
Binary file removed .DS_Store
Binary file not shown.
52 changes: 1 addition & 51 deletions IsingGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using Random, Distributions, Observables, SquareAdj
using WeightFuncs

export AbstractIsingGraph, IsingGraph, CIsingGraph, reInitGraph!, coordToIdx, idxToCoord, ising_it, setSpins!, setSpin!, addDefects!, remDefects!, addDefect!, remDefect!,
connIdx, connW, initSqAdj, HFunc, HWeightedFunc, HMagFunc, HWMagFunc, setMIdxs!, setGHFunc!
connIdx, connW, initSqAdj, HFunc, HWeightedFunc, HMagFunc, HWMagFunc, setGHFunc!

# Aliases
const Edge = Pair{Int32,Int32}
Expand Down Expand Up @@ -348,56 +348,6 @@ function HWMagFunc(g::AbstractIsingGraph,idx,state = g.state[idx])::Float32
return efactor -g.d.mlist[idx]
end

function setGHFunc!(sim)
g = sim.g
if !g.d.weighted
if !g.d.mactive
g.d.hFuncRef = Ref(HFunc)
println("Set HFunc")
else
g.d.hFuncRef = Ref(HMagFunc)
println("Set HMagFunc")
end
else
if !g.d.mactive
g.d.hFuncRef = Ref(HWeightedFunc)
println("Set HWeightedFunc")
else
g.d.hFuncRef = Ref(HWMagFunc)
println("Set HWMagFunc")
end
end

branchSim(sim)
end

""" Changing E functions """

function setMIdxs!(sim,idxs,strengths)
g = sim.g
if length(idxs) != length(strengths)
error("Idxs and strengths lengths not the same")
return
end

sim.shouldRun[] = false
g.d.mactive = true
g.d.mlist[idxs] = strengths
# setGHFunc!(g)
g.d.hFuncRef = Ref(HWMagFunc)
while sim.isRunning[]
sleep(.1)
end
sim.shouldRun[] = true
end

function branchSim(sim)
sim.shouldRun[] = false
while sim.isRunning[]
sleep(.1)
end
sim.shouldRun[] = true
end


"""Helper Functions"""
Expand Down
Empty file added IsingMagneticFields.jl
Empty file.
147 changes: 92 additions & 55 deletions IsingSim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Plots as pl
using SquareAdj, WeightFuncs, IsingGraphs, Interaction, Analysis, IsingMetropolis, GPlotting
using IsingLearning

# For plotting
const img = Ref(zeros(RGB{Float64},1,1))

function showlatest(buffer::Array{UInt32, 1}, width32::Int32, height32::Int32)
Expand All @@ -34,6 +35,7 @@ end
export showlatest_cfunction


# Simulation struct
export Sim
mutable struct Sim
# Graph
Expand Down Expand Up @@ -151,33 +153,6 @@ function (sim::Sim)(start = true)
end


# """ Helper Functions """

# Insert a value in front of vector and push out last one.
function insertShift(vec::Vector{T}, el::T) where T
newVec = Vector{T}(undef, length(vec))
newVec[1:(end-1)] = vec[2:end]
newVec[end] = el
return newVec
end

# Spawn a new thread for a function, but only if no thread for that function was already created
# The function is "locked" using a reference to a Boolean value: spawned
function spawnOne(f::Function, spawned::Ref{Bool}, args...)
# Run function, when thread is finished mark it as not running
function runOne(func::Function, spawned::Ref{Bool}, args...)
func(args...)
spawned[] = false
GC.safepoint()
end

# Mark as running, then spawn thread
if !spawned[]
spawned[] = true
# Threads.@spawn runOne(f,spawned)
runOne(f,spawned, args...)
end
end

""" Persistent functions of the simulation """

Expand Down Expand Up @@ -410,34 +385,6 @@ end
""" Sim Functions"""

export runSim
# Cannot be inside qmlFunctions due to problem with closures and @cfunction
# const img = Ref(zeros(RGB,500,500))

# function showlatest(buffer::Array{UInt32, 1}, width32::Int32, height32::Int32)
# buffer = reshape(buffer, size(img))
# buffer = reinterpret(ARGB32, buffer)
# buffer .= img
# return
# end

# export showlatest_cfunction
# showlatest_cfunction = CxxWrap.@safe_cfunction(showlatest, Cvoid,
# (Array{UInt32,1}, Int32, Int32))


# function showlatesteval(sim)
# img = sim.img
# function showlatest(buffer::Array{UInt32, 1}, width32::Int32, height32::Int32)
# buffer = reshape(buffer, size(img[]))
# buffer = reinterpret(ARGB32, buffer)
# buffer .= img[]
# return
# end

# @eval $:(CxxWrap.@safe_cfunction($showlatest, Cvoid, (Array{UInt32,1}, Int32, Int32)))
# end


function runSim(sim)
# showlatest_cfunction = showlatesteval(sim)
Threads.@spawn updateGraph(sim)
Expand All @@ -451,6 +398,96 @@ function startSim(sim)
runSim(sim)
end


# """ Helper Functions """

# Insert a value in front of vector and push out last one.
function insertShift(vec::Vector{T}, el::T) where T
newVec = Vector{T}(undef, length(vec))
newVec[1:(end-1)] = vec[2:end]
newVec[end] = el
return newVec
end

# Spawn a new thread for a function, but only if no thread for that function was already created
# The function is "locked" using a reference to a Boolean value: spawned
function spawnOne(f::Function, spawned::Ref{Bool}, args...)
# Run function, when thread is finished mark it as not running
function runOne(func::Function, spawned::Ref{Bool}, args...)
func(args...)
spawned[] = false
GC.safepoint()
end

# Mark as running, then spawn thread
if !spawned[]
spawned[] = true
# Threads.@spawn runOne(f,spawned)
runOne(f,spawned, args...)
end
end

""" Magnetic field stuff """
function branchSim(sim)
sim.shouldRun[] = false
while sim.isRunning[]
sleep(.1)
end
sim.shouldRun[] = true
end

function setGHFunc!(sim)
g = sim.g
if !g.d.weighted
if !g.d.mactive
g.d.hFuncRef = Ref(HFunc)
println("Set HFunc")
else
g.d.hFuncRef = Ref(HMagFunc)
println("Set HMagFunc")
end
else
if !g.d.mactive
g.d.hFuncRef = Ref(HWeightedFunc)
println("Set HWeightedFunc")
else
g.d.hFuncRef = Ref(HWMagFunc)
println("Set HWMagFunc")
end
end

branchSim(sim)
end

export setMIdxs!
function setMIdxs!(sim,idxs,strengths)
g = sim.g
if length(idxs) != length(strengths)
error("Idxs and strengths lengths not the same")
return
end

g.d.mactive = true
g.d.mlist[idxs] = strengths
setGHFunc!(sim)
end

# Insert func(x,y)
export setMFunc!
function setMFunc!(sim,func::Function)
g = sim.g
m_matr = Matrix{Float32}(undef,g.N,g.N)
for i in 1:g.N
for j in 1:g.N
m_matr[i,j] = func(j,i)
end
end
setMIdxs!(sim,[1:g.size;],reshape(transpose(m_matr),g.size))
end




# """ REPL FUNCTIONS FOR DEBUGGING """

# # # Draw circle to state
Expand Down
6 changes: 3 additions & 3 deletions main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ using Distributions
include("WeightFuncsCustom.jl")

weightFunc = defaultIsingWF
setAddDist!(weightFunc, Normal(0,0.3))
# setAddDist!(weightFunc, Normal(0,0.5))

const sim = Sim(
continuous = false,
continuous = true,
graphSize = 512,
weighted = true;
weightFunc
)

g = sim(true)
g = sim(true);
Binary file removed qml/.DS_Store
Binary file not shown.

0 comments on commit c1fdf76

Please sign in to comment.