From db61723206b9b2d8268a9e3f2e8bf6a91d49a115 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 29 Jan 2024 04:37:46 +0200 Subject: [PATCH] examples: fix notices for pendulum-simulation, simplify --workers option handling, fix assertions for `v test examples/pendulum-simulation` --- examples/pendulum-simulation/animation.v | 15 +++++-------- .../modules/sim/args/parser.v | 12 +++++----- .../modules/sim/params_test.v | 10 ++++----- .../pendulum-simulation/modules/sim/runner.v | 6 ++--- .../modules/sim/sim_test.v | 4 ++-- .../modules/sim/worker_test.v | 22 +++++++++---------- 6 files changed, 31 insertions(+), 38 deletions(-) diff --git a/examples/pendulum-simulation/animation.v b/examples/pendulum-simulation/animation.v index 4fd84ef8664022..1d93764eb6982e 100644 --- a/examples/pendulum-simulation/animation.v +++ b/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)) } diff --git a/examples/pendulum-simulation/modules/sim/args/parser.v b/examples/pendulum-simulation/modules/sim/args/parser.v index ed2008e74fc43f..3424bb9fa652d5 100644 --- a/examples/pendulum-simulation/modules/sim/args/parser.v +++ b/examples/pendulum-simulation/modules/sim/args/parser.v @@ -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}') @@ -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}') @@ -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) } diff --git a/examples/pendulum-simulation/modules/sim/params_test.v b/examples/pendulum-simulation/modules/sim/params_test.v index 9e205774c2ab9b..c6d2a22669ba76 100644 --- a/examples/pendulum-simulation/modules/sim/params_test.v +++ b/examples/pendulum-simulation/modules/sim/params_test.v @@ -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 ) @@ -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 @@ -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 ) @@ -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 ) diff --git a/examples/pendulum-simulation/modules/sim/runner.v b/examples/pendulum-simulation/modules/sim/runner.v index 326f82643a3e52..da6c725d12b17d 100644 --- a/examples/pendulum-simulation/modules/sim/runner.v +++ b/examples/pendulum-simulation/modules/sim/runner.v @@ -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) { diff --git a/examples/pendulum-simulation/modules/sim/sim_test.v b/examples/pendulum-simulation/modules/sim/sim_test.v index c1ea2c8b1febeb..221812bc113162 100644 --- a/examples/pendulum-simulation/modules/sim/sim_test.v +++ b/examples/pendulum-simulation/modules/sim/sim_test.v @@ -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 } diff --git a/examples/pendulum-simulation/modules/sim/worker_test.v b/examples/pendulum-simulation/modules/sim/worker_test.v index d7ff9b12fe824f..d82450a6bd1852 100644 --- a/examples/pendulum-simulation/modules/sim/worker_test.v +++ b/examples/pendulum-simulation/modules/sim/worker_test.v @@ -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