Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Subframes #47

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sailsim/simulation/Frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Frame():

def __init__(self):
self.frameNr = self.time = None
self.subframe = None

self.windTable = []

Expand All @@ -28,6 +29,7 @@ def __init__(self):
def collectSimulation(self, simulation):
self.frameNr = simulation.frame
self.time = simulation.getTime()
self.subframe = simulation.subframe

def collectBoat(self, boat):
"""Collect and save all information about the boat."""
Expand Down Expand Up @@ -72,7 +74,7 @@ def getWindList(self):
def getCSVLine(self):
"""Return string that contains all data about this frame."""
data = [
self.frameNr, self.time,
self.frameNr, self.time, self.subframe,
self.boatPosX, self.boatPosY, self.boatSpeedX, self.boatSpeedY, # self.boatDirection,
self.boatApparentWindX, self.boatApparentWindY, self.boatApparentWindAngle, self.boatLeewayAngle, self.boatAngleOfAttack,
self.boatForceX, self.boatForceY,
Expand Down
2 changes: 1 addition & 1 deletion sailsim/simulation/FrameList.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def getCSV(self):
def getCSVHeader(self):
"""Generate head of .csv file."""
headers = [
"frame", "time",
"frame", "time", "subframe",
"boatPosX", "boatPosY", "boatSpeedX", "boatSpeedY", # "boatDirection",
"boatApparentWindX", "boatApparentWindY", "boatApparentWindAngle", "boatLeewayAngle", "boatAngleOfAttack",
"boatForceX", "boatForceY",
Expand Down
28 changes: 24 additions & 4 deletions sailsim/simulation/Simulation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from copy import deepcopy
from math import ceil, log2

from sailsim.utils.coordconversion import cartToRadius

from sailsim.simulation.FrameList import FrameList

Expand All @@ -22,6 +25,7 @@ def __init__(self, world, timestep, lastFrame=None):
# Timing
self.timestep = timestep
self.frame = 0
self.subframe = 0
self.lastFrame = lastFrame

def run(self):
Expand All @@ -40,15 +44,31 @@ def step(self):
# Calculate Forces on boat
(boatX, boatY) = self.world.boat.getPos() # Fetch boat position
(windX, windY) = self.world.wind.getWindCart(boatX, boatY, time) # Get wind
(forceX, forceY) = self.world.boat.resultingForce(windX, windY)
(forceX, forceY) = self.world.boat.resultingForce(windX, windY) # Get resulting Force

# Subframing
oldSubFrame = self.subframe # Save old subframe
subframeThreshold = 2 ** 4 # 1 / maxValue until new substep
deltav = cartToRadius(forceX, forceY) * self.timestep / self.world.boat.mass # calculate change of speed of boat
subframe = ceil(log2(deltav * subframeThreshold)) # calculate ideal subframe
self.subframe = min(8, max(0, subframe)) # keep subframe in interval

# Bring frame to integer values (get all binary decimal places to 0)
if oldSubFrame > self.subframe: # Only run this part when simulation wants to run on a lower subframe value
while oldSubFrame > self.subframe and self.frame % 2**(-oldSubFrame + 1) == 0: # If ... and the oldSubFrame'th place in binary is 0
oldSubFrame -= 1 # decrease subframe by one
self.subframe = oldSubFrame # Use calculated subframe as subframe

subfrFactor = 1 / (2 ** self.subframe)
# print(deltav, deltav * subfrFactor, subframe, self.subframe, sep="\t")

# Save frame
self.frameList.grabFrame(self)
self.frame += 1
self.frame += 1 * subfrFactor

# Move Boat
self.world.boat.applyForce(forceX, forceY, self.timestep)
self.world.boat.moveInterval(self.timestep)
self.world.boat.applyForce(forceX, forceY, self.timestep * subfrFactor)
self.world.boat.moveInterval(self.timestep * subfrFactor)


def getTime(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/basictest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

# Create world and simulation
w = World(b, wind, None)
s = Simulation(w, 0.01, 1024)
s = Simulation(w, .03125, 256)

# Simulate 1 step
s.step()
Expand Down