Skip to content

Commit

Permalink
feat: auto select iblksize based on simulation details
Browse files Browse the repository at this point in the history
  • Loading branch information
mchitre committed Oct 18, 2024
1 parent 44669cd commit 3bf7925
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "VirtualAcousticOcean"
uuid = "629e117c-1786-4222-ab4a-1acab234d923"
authors = ["Mandar Chitre <[email protected]>"]
version = "0.3.0"
version = "0.4.0"

[deps]
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Expand Down
2 changes: 2 additions & 0 deletions docs/uasp-protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,5 @@ DAC data is sent by the client to `uaspd`, and is appended to the DAC buffer pro

Multiple channels are interleaved, i.e., data is organized as:<br>
`[ch1_t1 ch2_t1 ch3_t1 ch4_t1 ch1_t2 ch2_t2 ch3_t2 ch4_t2 ...]`

Note: Since data PDUs are sent as UDP packets, it is recommended that `nsamples` is chosen such that the packet size does not exceed the supported UDP MTU (typically 1432 bytes). Modern systems support UDP packets larger than this, but they are often fragmented at the physical layer and may have poor performance, and so not recommended.
22 changes: 15 additions & 7 deletions src/sim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Base.@kwdef struct Simulation{T}
nodes::Vector{Node} = Node[]
frequency::Float64 # nominal frequency (Hz)
irate::Float64 = 96000.0 # ADC samples/s
iblksize::Int = 256 # ADC samples
iblksize::Int = 0 # ADC samples
orate::Float64 = 192000.0 # DAC samples/s
txref::Float64 = 185.0 # dB re µPa @ 1m
rxref::Float64 = -190.0 # dB re 1/µPa
Expand All @@ -58,7 +58,7 @@ with `UnderwaterAcoustics.jl`.
Optional parameters:
- `irate`: ADC frame rate for sampling aoustic signal (samples/s)
- `iblksize`: ADC block size for streaming acoustic signal (samples)
- `iblksize`: ADC block size for streaming acoustic signal (samples, 0 for auto)
- `orate`: DAC frame rate for transmitting acoustic signal (samples/s)
- `txref`: Conversion between DAC input and acoustic source level (dB re µPa @ 1m)
- `rxref`: Conversion between acoustic receive level and ADC output (dB re 1/µPa)
Expand Down Expand Up @@ -104,25 +104,33 @@ end

function _run(sim::Simulation, task::SimTask)
sf = 10 ^ (sim.rxref / 20)
iblksize = _iblksize(sim)
while task.t0 > 0
Δt = task.t0 + (task.t / sim.irate) - time()
Δt > 0 && sleep(Δt)
for node sim.nodes
x = Matrix{Float32}(undef, sim.iblksize, length(node.tapes))
x = Matrix{Float32}(undef, iblksize, length(node.tapes))
for i eachindex(node.tapes)
x[:,i] .= read(node.tapes[i], task.t, sim.iblksize)
x[:,i] .+= sf * real(record(noise(environment(sim.model)), sim.iblksize/sim.irate, sim.irate))
x[:,i] .= read(node.tapes[i], task.t, iblksize)
x[:,i] .+= sf * real(record(noise(environment(sim.model)), iblksize/sim.irate, sim.irate))
end
stream(sim, node, task.t, x)
end
task.t += sim.iblksize
task.t += iblksize
while !isempty(sim.timers) && sim.timers[1][1] task.t
_, callback = popfirst!(sim.timers)
@invokelatest callback(task.t)
end
end
end

# choose a block size that allows a data block to fit within an MTU of 1430 bytes or so
function _iblksize(sim::Simulation)
sim.iblksize > 0 && return sim.iblksize
maxch = maximum(node -> length(node.tapes), sim.nodes)
min(353 ÷ maxch, 256)
end

"""
close(sim::Simulation)
Expand Down Expand Up @@ -261,7 +269,7 @@ Get parameter `k`. Returns `nothing` is parameter is unknown.
function Base.get((sim, node)::Tuple{Simulation,Node}, k::Symbol)
k === :time && return round(Int, sim.task.t / sim.irate)
k === :iseqno && return node.seqno
k === :iblksize && return sim.iblksize
k === :iblksize && return _iblksize(sim)
k === :irate && return sim.irate
k === :irates && return [sim.irate]
k === :ichannels && return length(node.relpos)
Expand Down

2 comments on commit 3bf7925

@mchitre
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/117578

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.0 -m "<description of version>" 3bf7925240539b8ab50307df5e2a934225567f7b
git push origin v0.4.0

Please sign in to comment.