2022-02-12 20:38:07 +03:00
|
|
|
module args
|
|
|
|
|
|
|
|
import flag
|
|
|
|
import os
|
|
|
|
import runtime
|
|
|
|
import sim
|
|
|
|
import math
|
|
|
|
|
|
|
|
// customisable through setting VJOBS
|
|
|
|
const max_parallel_workers = runtime.nr_jobs()
|
|
|
|
|
|
|
|
[params]
|
|
|
|
pub struct ParserSettings {
|
|
|
|
sequential bool
|
|
|
|
img bool
|
|
|
|
extra_workers int
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SequentialArgs {
|
|
|
|
pub:
|
|
|
|
params sim.SimParams
|
|
|
|
grid sim.GridSettings
|
|
|
|
filename string
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ParallelArgs {
|
|
|
|
SequentialArgs
|
|
|
|
pub:
|
|
|
|
workers int = args.max_parallel_workers
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type SimArgs = ParallelArgs | SequentialArgs
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn parse_args(config ParserSettings) !SimArgs {
|
2022-02-12 20:38:07 +03:00
|
|
|
if config.sequential {
|
2022-10-16 09:28:57 +03:00
|
|
|
args := parse_sequential_args()!
|
2022-02-12 20:38:07 +03:00
|
|
|
return SimArgs(args)
|
|
|
|
} else {
|
2022-10-16 09:28:57 +03:00
|
|
|
args := parse_parallel_args(config.extra_workers)!
|
2022-02-12 20:38:07 +03:00
|
|
|
return SimArgs(args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
fn parse_sequential_args() !SequentialArgs {
|
2022-02-12 20:38:07 +03:00
|
|
|
mut fp := flag.new_flag_parser(os.args)
|
|
|
|
fp.application('vps')
|
|
|
|
fp.version('v0.1.0')
|
2022-10-16 09:28:57 +03:00
|
|
|
fp.limit_free_args(0, 0)!
|
2022-02-12 20:38:07 +03:00
|
|
|
fp.description('This is a pendulum simulation written in pure V')
|
|
|
|
fp.skip_executable()
|
|
|
|
|
|
|
|
// output parameters
|
2022-11-15 16:53:13 +03:00
|
|
|
width := fp.int('width', `w`, sim.default_width, 'width of the image output. Defaults to ${sim.default_width}')
|
|
|
|
height := fp.int('height', `h`, sim.default_height, 'height of the image output. Defaults to ${sim.default_height}')
|
2022-02-12 20:38:07 +03:00
|
|
|
filename := fp.string('output', `o`, 'out.ppm', 'name of the image output. Defaults to out.ppm')
|
|
|
|
|
|
|
|
// simulation parameters
|
2022-11-15 16:53:13 +03:00
|
|
|
rope_length := fp.float('rope-length', 0, sim.default_rope_length, 'rope length to use on simulation. Defaults to ${sim.default_rope_length}')
|
|
|
|
bearing_mass := fp.float('bearing-mass', 0, sim.default_bearing_mass, 'bearing mass to use on simulation. Defaults to ${sim.default_bearing_mass}')
|
|
|
|
magnet_spacing := fp.float('magnet-spacing', 0, sim.default_magnet_spacing, 'magnet spacing to use on simulation. Defaults to ${sim.default_magnet_spacing}')
|
|
|
|
magnet_height := fp.float('magnet-height', 0, sim.default_magnet_height, 'magnet height to use on simulation. Defaults to ${sim.default_magnet_height}')
|
|
|
|
magnet_strength := fp.float('magnet-strength', 0, sim.default_magnet_strength, 'magnet strength to use on simulation. Defaults to ${sim.default_magnet_strength}')
|
|
|
|
gravity := fp.float('gravity', 0, sim.default_gravity, 'gravity to use on simulation. Defaults to ${sim.default_gravity}')
|
2022-02-12 20:38:07 +03:00
|
|
|
|
|
|
|
fp.finalize() or {
|
|
|
|
println(fp.usage())
|
2022-10-16 09:28:57 +03:00
|
|
|
return error('none')
|
2022-02-12 20:38:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
params := sim.sim_params(
|
|
|
|
rope_length: rope_length
|
|
|
|
bearing_mass: bearing_mass
|
|
|
|
magnet_spacing: magnet_spacing
|
|
|
|
magnet_height: magnet_height
|
|
|
|
magnet_strength: magnet_strength
|
|
|
|
gravity: gravity
|
|
|
|
)
|
|
|
|
|
|
|
|
grid := sim.new_grid_settings(
|
|
|
|
width: width
|
|
|
|
height: height
|
|
|
|
)
|
|
|
|
|
|
|
|
args := SequentialArgs{
|
|
|
|
params: params
|
|
|
|
filename: filename
|
|
|
|
grid: grid
|
|
|
|
}
|
|
|
|
|
2022-11-15 16:53:13 +03:00
|
|
|
sim.log('${args}')
|
2022-02-12 20:38:07 +03:00
|
|
|
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
fn parse_parallel_args(extra_workers int) !ParallelArgs {
|
2022-02-12 20:38:07 +03:00
|
|
|
mut fp := flag.new_flag_parser(os.args)
|
|
|
|
fp.application('vps')
|
|
|
|
fp.version('v0.1.0')
|
2022-10-16 09:28:57 +03:00
|
|
|
fp.limit_free_args(0, 0)!
|
2022-02-12 20:38:07 +03:00
|
|
|
fp.description('This is a pendulum simulation written in pure V')
|
|
|
|
fp.skip_executable()
|
|
|
|
|
2022-11-15 16:53:13 +03:00
|
|
|
workers := fp.int('workers', 0, args.max_parallel_workers, 'amount of workers to use on simulation. Defaults to ${args.max_parallel_workers}')
|
2022-02-12 20:38:07 +03:00
|
|
|
|
|
|
|
// output parameters
|
2022-11-15 16:53:13 +03:00
|
|
|
width := fp.int('width', `w`, sim.default_width, 'width of the image output. Defaults to ${sim.default_width}')
|
|
|
|
height := fp.int('height', `h`, sim.default_height, 'height of the image output. Defaults to ${sim.default_height}')
|
2022-02-12 20:38:07 +03:00
|
|
|
filename := fp.string('output', `o`, 'out.ppm', 'name of the image output. Defaults to out.ppm')
|
|
|
|
|
|
|
|
// simulation parameters
|
2022-11-15 16:53:13 +03:00
|
|
|
rope_length := fp.float('rope-length', 0, sim.default_rope_length, 'rope length to use on simulation. Defaults to ${sim.default_rope_length}')
|
|
|
|
bearing_mass := fp.float('bearing-mass', 0, sim.default_bearing_mass, 'bearing mass to use on simulation. Defaults to ${sim.default_bearing_mass}')
|
|
|
|
magnet_spacing := fp.float('magnet-spacing', 0, sim.default_magnet_spacing, 'magnet spacing to use on simulation. Defaults to ${sim.default_magnet_spacing}')
|
|
|
|
magnet_height := fp.float('magnet-height', 0, sim.default_magnet_height, 'magnet height to use on simulation. Defaults to ${sim.default_magnet_height}')
|
|
|
|
magnet_strength := fp.float('magnet-strength', 0, sim.default_magnet_strength, 'magnet strength to use on simulation. Defaults to ${sim.default_magnet_strength}')
|
|
|
|
gravity := fp.float('gravity', 0, sim.default_gravity, 'gravity to use on simulation. Defaults to ${sim.default_gravity}')
|
2022-02-12 20:38:07 +03:00
|
|
|
|
|
|
|
fp.finalize() or {
|
|
|
|
println(fp.usage())
|
2022-10-16 09:28:57 +03:00
|
|
|
return error('none')
|
2022-02-12 20:38:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
params := sim.sim_params(
|
|
|
|
rope_length: rope_length
|
|
|
|
bearing_mass: bearing_mass
|
|
|
|
magnet_spacing: magnet_spacing
|
|
|
|
magnet_height: magnet_height
|
|
|
|
magnet_strength: magnet_strength
|
|
|
|
gravity: gravity
|
|
|
|
)
|
|
|
|
|
|
|
|
grid := sim.new_grid_settings(
|
|
|
|
width: width
|
|
|
|
height: height
|
|
|
|
)
|
|
|
|
|
|
|
|
args := ParallelArgs{
|
|
|
|
params: params
|
|
|
|
filename: filename
|
|
|
|
grid: grid
|
|
|
|
workers: get_workers(workers, extra_workers)
|
|
|
|
}
|
|
|
|
|
2022-11-15 16:53:13 +03:00
|
|
|
sim.log('${args}')
|
2022-02-12 20:38:07 +03:00
|
|
|
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
|
|
|
[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)
|
|
|
|
}
|