Skip to content

Commit

Permalink
examples: fix notices for pendulum-simulation, simplify --workers opt…
Browse files Browse the repository at this point in the history
…ion handling, fix assertions for `v test examples/pendulum-simulation`
  • Loading branch information
spytheman committed Jan 29, 2024
1 parent 77b8097 commit db61723
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 38 deletions.
15 changes: 5 additions & 10 deletions examples/pendulum-simulation/animation.v
@@ -1,37 +1,32 @@
module main

import log
import benchmark
import sim
import sim.anim
import sim.args as simargs

fn main() {
unbuffer_stdout()
args := simargs.parse_args(extra_workers: 1)! as simargs.ParallelArgs

mut app := anim.new_app(args)
mut workers := []thread{cap: args.workers}

mut bmark := benchmark.start()

defer {
app.request_chan.close()
sim.log('Waiting for workers to finish')
log.info('Waiting for workers to finish')
workers.wait()
app.result_chan.close()
sim.log('Workers finished!')
log.info('Workers finished!')
bmark.measure(@FN)
sim.log('Done!')
log.info('Done!')
}

for id in 0 .. args.workers {
workers << spawn sim.sim_worker(id, app.request_chan, [app.result_chan])
}

handle_request := fn [app] (request &sim.SimRequest) ! {
app.request_chan <- request
}

spawn app.gg.run()

sim.run(args.params, grid: args.grid, on_request: sim.SimRequestHandler(handle_request))
}
12 changes: 5 additions & 7 deletions examples/pendulum-simulation/modules/sim/args/parser.v
Expand Up @@ -101,6 +101,7 @@ fn parse_parallel_args(extra_workers int) !ParallelArgs {
fp.skip_executable()

workers := fp.int('workers', 0, args.max_parallel_workers, 'amount of workers to use on simulation. Defaults to ${args.max_parallel_workers}')
dump(workers)

// output parameters
width := fp.int('width', `w`, sim.default_width, 'width of the image output. Defaults to ${sim.default_width}')
Expand Down Expand Up @@ -140,6 +141,9 @@ fn parse_parallel_args(extra_workers int) !ParallelArgs {
grid: grid
workers: get_workers(workers, extra_workers)
}
dump(extra_workers)
dump(args.max_parallel_workers)
dump(args)

sim.log('${args}')

Expand All @@ -148,11 +152,5 @@ fn parse_parallel_args(extra_workers int) !ParallelArgs {

@[inline]
fn get_workers(workers int, extra_workers int) int {
result := if workers + extra_workers <= args.max_parallel_workers {
workers
} else {
args.max_parallel_workers - extra_workers
}

return math.max(1, result)
return math.max(1, workers + extra_workers)
}
10 changes: 5 additions & 5 deletions examples/pendulum-simulation/modules/sim/params_test.v
Expand Up @@ -42,7 +42,7 @@ pub fn test_get_rope_vector() {
pub fn test_get_forces_sum() {
result := sim.params_test_mock_params.get_forces_sum(sim.params_test_mock_state)
expected := vector(
x: 3.637978807091713e-12
x: 3.410605131648481e-12
y: 5.229594535194337e-12
z: 9.094947017729282e-13
)
Expand All @@ -60,8 +60,8 @@ pub fn test_get_grav_force() {
pub fn test_get_magnet_position() {
result := sim.params_test_mock_params.get_magnet_position(sim.params_test_mock_tetha)
expected := vector(
x: -0.024999999999999988
y: 0.043301270189221946
x: -0.02499999999999999
y: 0.04330127018922194
z: -0.03
)
assert result == expected
Expand All @@ -71,7 +71,7 @@ pub fn test_get_magnet_force() {
result := sim.params_test_mock_params.get_magnet_force(sim.params_test_mock_tetha,
sim.params_test_mock_state)
expected := vector(
x: -157.45722976925555
x: -157.4572297692556
y: 1422.736432604726
z: -632.5695169850264
)
Expand All @@ -98,7 +98,7 @@ pub fn test_get_magnet1_force() {
pub fn test_get_magnet2_force() {
result := sim.params_test_mock_params.get_magnet2_force(sim.params_test_mock_state)
expected := vector(
x: -157.45722976925555
x: -157.4572297692556
y: 1422.736432604726
z: -632.5695169850264
)
Expand Down
6 changes: 3 additions & 3 deletions examples/pendulum-simulation/modules/sim/runner.v
Expand Up @@ -29,9 +29,9 @@ pub fn new_grid_settings(settings GridSettings) GridSettings {
pub struct RunnerSettings {
pub:
grid GridSettings
on_request SimRequestHandler
on_start SimStartHandler
on_finish SimFinishHandler
on_request SimRequestHandler = unsafe { nil }
on_start SimStartHandler = unsafe { nil }
on_finish SimFinishHandler = unsafe { nil }
}

pub fn run(params SimParams, settings RunnerSettings) {
Expand Down
4 changes: 2 additions & 2 deletions examples/pendulum-simulation/modules/sim/sim_test.v
Expand Up @@ -53,10 +53,10 @@ pub fn test_increment() {
assert state.position.x == -0.016957230930171364
assert state.position.y == -0.02937078552673524
assert state.position.z == 0.0023110634753272796
assert state.velocity.x == -7.251158929833044
assert state.velocity.x == -7.251158929833047
assert state.velocity.y == -12.559375680227637
assert state.velocity.z == -105.9153968768638
assert state.accel.x == 1.2126596023639044e-10
assert state.accel.x == 1.1368683772161604e-10
assert state.accel.y == 1.7431981783981126e-10
assert state.accel.z == 3.031649005909761e-11
}
22 changes: 11 additions & 11 deletions examples/pendulum-simulation/modules/sim/worker_test.v
Expand Up @@ -34,27 +34,27 @@ fn test_compute_result() {
}
expected_state := SimState{
position: vector(
x: -0.01695723093017133
y: -0.02937078552673517
x: -0.01695723093017135
y: -0.029370785526735183
z: 0.002311063475327252
)
velocity: vector(
x: -7.251158929832518
y: -12.559375680226692
z: -105.91539687685668
x: -7.251158929831753
y: -12.55937568022536
z: -105.91539687684447
)
accel: vector(
x: -3.789561257387201e-12
y: 3.410605131648481e-11
z: 3.031649005909761e-11
x: -1.1368683772161603e-11
y: -1.5916157281026244e-10
z: 0
)
}
expected := &SimResult{
state: expected_state
id: 0
magnet1_distance: 0.07993696666249224
magnet2_distance: 0.07993696666249223
magnet3_distance: 0.03609361938278009
magnet1_distance: 0.07993696666249225
magnet2_distance: 0.07993696666249224
magnet3_distance: 0.03609361938278008
}
result := compute_result(request)
assert result == expected
Expand Down

0 comments on commit db61723

Please sign in to comment.