Created
June 19, 2022 03:25
-
-
Save JavascriptMick/a9bd01bfe9216074db664568863f970d to your computer and use it in GitHub Desktop.
Simple Kuramoto simulation in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
// The coupling constant, higher values tend to converge the model faster | |
const k_const float64 = .006 | |
// A simple struct to represent an oscillator with a phase | |
type Oscillator struct { | |
phase float64 | |
} | |
// Apply one phase delta to the oscillators in the model | |
func bump(oscillators []Oscillator) { | |
for i := 0; i < len(oscillators); i++ { | |
oscillators[i].phase = oscillators[i].phase + k_const*sum_of_phase_diffs(i, oscillators) | |
} | |
} | |
// Calculate value needed for coupling of sine functions. | |
// Return the sum of the sines of the differences between all other elements | |
// and the element to compare. | |
func sum_of_phase_diffs(compareToIndex int, oscillators []Oscillator) float64 { | |
res := 0.0 | |
target_value := oscillators[compareToIndex].phase | |
for i := 0; i < len(oscillators); i++ { | |
node := oscillators[i] | |
if i != compareToIndex { | |
res += math.Sin(node.phase - target_value) | |
} | |
} | |
return res | |
} | |
// Simple simulation with 3 oscillators and 1000 iterations | |
// | |
// Note that I am not actually oscillating the oscillators | |
func main() { | |
oscillators := []Oscillator{ | |
Oscillator{phase: 0 * math.Pi}, | |
Oscillator{phase: 0.5 * math.Pi}, | |
Oscillator{phase: 0.75 * math.Pi}, | |
} | |
fmt.Println(oscillators) | |
for j := 0; j < 1000; j++ { | |
bump(oscillators) | |
} | |
fmt.Println(oscillators) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment