Skip to content

Commit

Permalink
Merge branch 'ForceDescriptor<NodeID>'
Browse files Browse the repository at this point in the history
  • Loading branch information
li3zhen1 committed Dec 31, 2024
2 parents 5435b7b + 4a69b54 commit f74e180
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ struct MermaidVisualization: View {

} force: {
.manyBody()
.link(originalLength: 70.0)
.link(originalLength: 50.0)
.center()
} emittingNewNodesWithStates: { id in
KineticState(position: getInitialPosition(id: id, r: 100))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ struct MyRing: View {
} force: {
.manyBody(strength: -15)
.link(
originalLength: 20.0,
stiffness: .weightedByDegree { _, _ in 3.0}
originalLength: 30.0,
stiffness: .weightedByDegree { _, _ in 1.0 }
)
.center()
.collide()
// .collide()
}
.graphOverlay { proxy in
Rectangle().fill(.clear).contentShape(Rectangle())
Expand Down
21 changes: 8 additions & 13 deletions Sources/ForceSimulation/Forces/LinkForce.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Darwin

extension Kinetics {

public enum LinkStiffness {
Expand All @@ -15,12 +17,12 @@ extension Kinetics {
case .weightedByDegree(let k):
return link.map { l in
k(l, lookup)
/ Vector.Scalar(
/ (Vector.Scalar(
min(
lookup.count[l.source, default: 0],
lookup.count[l.target, default: 0]
)
)
)+1)
}
}
}
Expand Down Expand Up @@ -81,18 +83,14 @@ extension Kinetics {
assert(b != 0)

var vec =
(positionBufferPointer[t] + velocityBufferPointer[t]
- positionBufferPointer[s] - velocityBufferPointer[s])
// .jiggled()
(positionBufferPointer[t] - positionBufferPointer[s])
.jiggled(by: &kinetics.randomGenerator)

var l = vec.length()

l =
(l - self.calculatedLength[i]) / l * kinetics.alpha
* self.calculatedStiffness[i]
l = (l - self.calculatedLength[i]) / l * self.calculatedStiffness[i] * kinetics.velocityDecay * kinetics.alpha

vec *= l
vec *= l // * kinetics.velocityDecay

// same as d3
velocityBufferPointer[t] -= vec * b
Expand All @@ -101,7 +99,6 @@ extension Kinetics {
}
}


@usableFromInline
internal var links: [EdgeID<Int>]! = nil

Expand Down Expand Up @@ -144,7 +141,6 @@ extension Kinetics {

}


@inlinable
public func dispose() {}
}
Expand Down Expand Up @@ -173,7 +169,6 @@ public struct LinkLookup<NodeID: Hashable> {

}


extension Kinetics.LinkStiffness: Equatable {
@inlinable
public static func == (lhs: Kinetics.LinkStiffness, rhs: Kinetics.LinkStiffness) -> Bool {
Expand All @@ -196,4 +191,4 @@ extension Kinetics.LinkLength: Equatable {
return false
}
}
}
}
2 changes: 1 addition & 1 deletion Sources/ForceSimulation/Simulation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ where Vector: SimulatableVector & L2NormCalculatable, ForceField: ForceProtocol<
forceField: consuming ForceField,
initialAlpha: Vector.Scalar = 1,
alphaMin: Vector.Scalar = 1e-3,
alphaDecay: Vector.Scalar = 2e-3,
alphaDecay: Vector.Scalar = 1e-2,
alphaTarget: Vector.Scalar = 0.0,
velocityDecay: Vector.Scalar = 0.6,
position: [Vector]? = nil,
Expand Down

0 comments on commit f74e180

Please sign in to comment.