Skip to content
This repository was archived by the owner on Jan 2, 2023. It is now read-only.

Commit 0e520ae

Browse files
committed
feat: upgrade to Mun v0.2.0 and buoyancy example
1 parent 747c52a commit 0e520ae

File tree

5 files changed

+127
-22
lines changed

5 files changed

+127
-22
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ GitHub.sublime-settings
293293
tmtags
294294

295295
##### VisualStudioCode
296+
/.vscode
296297
.vscode/*
297298
!.vscode/settings.json
298299
!.vscode/tasks.json
@@ -912,3 +913,6 @@ _deps
912913
*.exe
913914
*.out
914915
*.app
916+
917+
# Artifacts
918+
*.munlib

resources/buoyancy.mun

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
extern fn log_f32(value: f32);
2+
3+
struct SimContext {
4+
sphere: Sphere,
5+
water: Water,
6+
gravity: f32,
7+
}
8+
9+
struct Sphere {
10+
radius: f32,
11+
mass: f32, // density: f32,
12+
height: f32,
13+
velocity: f32,
14+
}
15+
16+
struct Water {
17+
density: f32,
18+
}
19+
20+
pub fn new_sim() -> SimContext {
21+
SimContext {
22+
sphere: new_sphere(),
23+
water: new_water(),
24+
gravity: 9.81,
25+
}
26+
}
27+
28+
fn new_sphere() -> Sphere {
29+
let radius = 1.0;
30+
let density = 250.0;
31+
32+
let volume = calc_sphere_volume(radius);
33+
let mass = density * volume;
34+
35+
Sphere {
36+
radius,
37+
mass,
38+
height: 1.0,
39+
velocity: 0.0,
40+
}
41+
}
42+
43+
fn new_water() -> Water {
44+
Water {
45+
density: 1000.0,
46+
}
47+
}
48+
49+
fn calc_submerged_ratio(s: Sphere) -> f32 {
50+
let bottom = s.height - s.radius;
51+
let diameter = 2.0 * s.radius;
52+
if bottom >= 0.0 {
53+
0.0
54+
} else if bottom <= -diameter {
55+
1.0
56+
} else {
57+
-bottom / diameter
58+
}
59+
}
60+
61+
fn calc_sphere_volume(radius: f32) -> f32 {
62+
let pi = 3.1415926535897;
63+
let r = radius;
64+
65+
3.0/4.0 * pi * r * r * r
66+
}
67+
68+
fn calc_buoyancy_force(s: Sphere, w: Water, gravity: f32, submerged_ratio: f32) -> f32 {
69+
let volume = calc_sphere_volume(s.radius);
70+
volume * submerged_ratio * w.density * gravity
71+
}
72+
73+
pub fn sim_update(ctx: SimContext, elapsed_secs: f32) {
74+
let submerged_ratio = calc_submerged_ratio(ctx.sphere);
75+
if submerged_ratio > 0.0 {
76+
let buoyancy_force = calc_buoyancy_force(
77+
ctx.sphere,
78+
ctx.water,
79+
ctx.gravity,
80+
submerged_ratio
81+
);
82+
let buoyancy_acc = buoyancy_force / ctx.sphere.mass;
83+
ctx.sphere.velocity += buoyancy_acc * elapsed_secs;
84+
}
85+
86+
ctx.sphere.velocity -= ctx.gravity * elapsed_secs;
87+
88+
ctx.sphere.height += ctx.sphere.velocity * elapsed_secs;
89+
90+
log_f32(ctx.sphere.height);
91+
}

resources/fibonacci.mun

-11
This file was deleted.

src/main.cc

+31-10
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,55 @@
11
#include <iostream>
22
#include <string>
33

4-
#include "mun/runtime.h"
4+
#include "mun/mun.h"
5+
6+
extern "C" {
7+
void log_f32(float value) { std::cout << std::to_string(value) << std::endl; }
8+
}
59

610
// How to run?
711
// 1. On the CLI, navigate to the `example-cpp` directory.
8-
// 2. Run the compiler daemon from the CLI: `/path/to/mun build resources/fibonacci.mun --watch`
9-
// 3. Run the application from the CLI: `main /path/to/fibonacci.dll`
12+
// 2. Run the compiler daemon from the CLI:
13+
// `/path/to/mun build resources/buoyancy.mun --watch`
14+
// 3. Run the application from the CLI:
15+
// `main /path/to/buoyancy.munlib`
1016
int main(int argc, char* argv[]) {
1117
if (argc < 2) {
1218
return 1;
1319
}
14-
1520
std::cout << "lib: " << argv[1] << std::endl;
1621

22+
mun::RuntimeOptions options;
23+
options.functions.emplace_back(mun::RuntimeFunction("log_f32", log_f32));
24+
1725
mun::Error error;
18-
if (auto runtime = mun::make_runtime(argv[1], &error)) {
26+
if (auto runtime = mun::make_runtime(argv[1], options, &error)) {
27+
auto ctx = mun::invoke_fn<mun::StructRef>(*runtime, "new_sim").wait();
28+
29+
using clock_t = std::chrono::high_resolution_clock;
30+
using fsec_t = std::chrono::duration<float>;
31+
32+
auto previous = clock_t::now();
33+
constexpr auto FRAME_TIME = std::chrono::milliseconds(40);
1934
while (true) {
20-
auto n = mun::invoke_fn<int64_t>(*runtime, "nth").wait();
21-
auto result = mun::invoke_fn<int64_t>(*runtime, "fibonacci", n).wait();
35+
std::this_thread::sleep_until(previous + FRAME_TIME);
36+
37+
const auto now = clock_t::now();
38+
const auto elapsed =
39+
std::chrono::duration_cast<fsec_t>(now - previous);
2240

23-
std::cout << "fibonacci(" << std::to_string(n) << ") = " << result << std::endl;
41+
mun::invoke_fn<void>(*runtime, "sim_update", ctx, elapsed.count()).wait();
42+
previous = now;
2443

2544
mun::Error update_error;
2645
if (!runtime->update(&update_error) && update_error) {
27-
std::cerr << "Failed to update runtime due to error: " << update_error.message() << std::endl;
46+
std::cerr << "Failed to update runtime due to error: "
47+
<< update_error.message() << std::endl;
2848
}
2949
}
3050
}
3151

32-
std::cerr << "Failed to construct Mun runtime due to error: " << error.message() << std::endl;
52+
std::cerr << "Failed to construct Mun runtime due to error: "
53+
<< error.message() << std::endl;
3354
return 2;
3455
}

0 commit comments

Comments
 (0)