v / examples / pendulum-simulation / modules / sim / args / parser.v
169 lines · 143 sloc · 4.96 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1module args
2
3import flag
4import os
5import runtime
6import sim
7import math
8
9// customisable through setting VJOBS
10const max_parallel_workers = runtime.nr_jobs()
11
12@[params]
13pub struct ParserSettings {
14pub:
15 sequential bool
16 img bool
17 extra_workers int
18}
19
20pub struct SequentialArgs {
21pub:
22 params sim.SimParams
23 grid sim.GridSettings
24 filename string
25}
26
27pub struct ParallelArgs {
28 SequentialArgs
29pub:
30 workers int = max_parallel_workers
31}
32
33pub type SimArgs = ParallelArgs | SequentialArgs
34
35pub fn parse_args(config ParserSettings) !SimArgs {
36 if config.sequential {
37 args := parse_sequential_args()!
38 return SimArgs(args)
39 } else {
40 args := parse_parallel_args(config.extra_workers)!
41 return SimArgs(args)
42 }
43}
44
45fn parse_sequential_args() !SequentialArgs {
46 mut fp := flag.new_flag_parser(os.args)
47 fp.application('vps')
48 fp.version('v0.1.0')
49 fp.limit_free_args(0, 0)!
50 fp.description('This is a pendulum simulation written in pure V')
51 fp.skip_executable()
52
53 // output parameters
54 width := fp.int('width', `w`, sim.default_width,
55 'width of the image output. Defaults to ${sim.default_width}')
56 height := fp.int('height', `h`, sim.default_height,
57 'height of the image output. Defaults to ${sim.default_height}')
58 filename := fp.string('output', `o`, 'out.ppm', 'name of the image output. Defaults to out.ppm')
59
60 // simulation parameters
61 rope_length := fp.float('rope-length', 0, sim.default_rope_length,
62 'rope length to use on simulation. Defaults to ${sim.default_rope_length}')
63 bearing_mass := fp.float('bearing-mass', 0, sim.default_bearing_mass,
64 'bearing mass to use on simulation. Defaults to ${sim.default_bearing_mass}')
65 magnet_spacing := fp.float('magnet-spacing', 0, sim.default_magnet_spacing,
66 'magnet spacing to use on simulation. Defaults to ${sim.default_magnet_spacing}')
67 magnet_height := fp.float('magnet-height', 0, sim.default_magnet_height,
68 'magnet height to use on simulation. Defaults to ${sim.default_magnet_height}')
69 magnet_strength := fp.float('magnet-strength', 0, sim.default_magnet_strength,
70 'magnet strength to use on simulation. Defaults to ${sim.default_magnet_strength}')
71 gravity := fp.float('gravity', 0, sim.default_gravity,
72 'gravity to use on simulation. Defaults to ${sim.default_gravity}')
73
74 fp.finalize() or {
75 println(fp.usage())
76 return error('none')
77 }
78
79 params := sim.sim_params(
80 rope_length: rope_length
81 bearing_mass: bearing_mass
82 magnet_spacing: magnet_spacing
83 magnet_height: magnet_height
84 magnet_strength: magnet_strength
85 gravity: gravity
86 )
87
88 grid := sim.new_grid_settings(
89 width: width
90 height: height
91 )
92
93 args := SequentialArgs{
94 params: params
95 filename: filename
96 grid: grid
97 }
98
99 sim.log('${args}')
100
101 return args
102}
103
104fn parse_parallel_args(extra_workers int) !ParallelArgs {
105 mut fp := flag.new_flag_parser(os.args)
106 fp.application('vps')
107 fp.version('v0.1.0')
108 fp.limit_free_args(0, 0)!
109 fp.description('This is a pendulum simulation written in pure V')
110 fp.skip_executable()
111
112 workers := fp.int('workers', 0, max_parallel_workers,
113 'amount of workers to use on simulation. Defaults to ${max_parallel_workers}')
114
115 // output parameters
116 width := fp.int('width', `w`, sim.default_width,
117 'width of the image output. Defaults to ${sim.default_width}')
118 height := fp.int('height', `h`, sim.default_height,
119 'height of the image output. Defaults to ${sim.default_height}')
120 filename := fp.string('output', `o`, 'out.ppm', 'name of the image output. Defaults to out.ppm')
121
122 // simulation parameters
123 rope_length := fp.float('rope-length', 0, sim.default_rope_length,
124 'rope length to use on simulation. Defaults to ${sim.default_rope_length}')
125 bearing_mass := fp.float('bearing-mass', 0, sim.default_bearing_mass,
126 'bearing mass to use on simulation. Defaults to ${sim.default_bearing_mass}')
127 magnet_spacing := fp.float('magnet-spacing', 0, sim.default_magnet_spacing,
128 'magnet spacing to use on simulation. Defaults to ${sim.default_magnet_spacing}')
129 magnet_height := fp.float('magnet-height', 0, sim.default_magnet_height,
130 'magnet height to use on simulation. Defaults to ${sim.default_magnet_height}')
131 magnet_strength := fp.float('magnet-strength', 0, sim.default_magnet_strength,
132 'magnet strength to use on simulation. Defaults to ${sim.default_magnet_strength}')
133 gravity := fp.float('gravity', 0, sim.default_gravity,
134 'gravity to use on simulation. Defaults to ${sim.default_gravity}')
135
136 fp.finalize() or {
137 println(fp.usage())
138 return error('none')
139 }
140
141 params := sim.sim_params(
142 rope_length: rope_length
143 bearing_mass: bearing_mass
144 magnet_spacing: magnet_spacing
145 magnet_height: magnet_height
146 magnet_strength: magnet_strength
147 gravity: gravity
148 )
149
150 grid := sim.new_grid_settings(
151 width: width
152 height: height
153 )
154
155 args := ParallelArgs{
156 params: params
157 filename: filename
158 grid: grid
159 workers: get_workers(workers, extra_workers)
160 }
161 sim.log('${args}')
162
163 return args
164}
165
166@[inline]
167fn get_workers(workers int, extra_workers int) int {
168 return math.max(1, workers + extra_workers)
169}
170