This repository has been archived by the owner on Jan 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: upgrade to Mun v0.2.0 and buoyancy example
- Loading branch information
Showing
5 changed files
with
127 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule runtime
updated
31 files
+3 −0 | .clang-format | |
+6 −0 | .gitmodules | |
+26 −20 | CMakeLists.txt | |
+9 −0 | README.md | |
+ − | bin/linux64/libmun_runtime.so | |
+ − | bin/osx64/libmun_runtime.dylib | |
+ − | bin/win64/mun_runtime.dll | |
+ − | bin/win64/mun_runtime.dll.lib | |
+1 −0 | external/catch2 | |
+1 −0 | external/md5 | |
+55 −0 | include/mun/error.h | |
+38 −0 | include/mun/function.h | |
+88 −0 | include/mun/gc.h | |
+102 −0 | include/mun/invoke_fn.h | |
+192 −201 | include/mun/invoke_result.h | |
+58 −0 | include/mun/marshal.h | |
+9 −0 | include/mun/mun.h | |
+89 −0 | include/mun/reflection.h | |
+168 −266 | include/mun/runtime.h | |
+262 −29 | include/mun/runtime_capi.h | |
+305 −0 | include/mun/struct_ref.h | |
+78 −0 | include/mun/type_info.h | |
+10 −0 | include/mun/util.h | |
+74 −0 | tests/CMakeLists.txt | |
+2 −0 | tests/catch_main.cc | |
+48 −0 | tests/extern.cc | |
+228 −0 | tests/marshal.cc | |
+72 −0 | tests/runtime.cc | |
+5 −0 | tests/sources/extern.mun | |
+16 −0 | tests/sources/fibonacci.mun | |
+103 −0 | tests/sources/marshal.mun |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
extern fn log_f32(value: f32); | ||
|
||
struct SimContext { | ||
sphere: Sphere, | ||
water: Water, | ||
gravity: f32, | ||
} | ||
|
||
struct Sphere { | ||
radius: f32, | ||
mass: f32, // density: f32, | ||
height: f32, | ||
velocity: f32, | ||
} | ||
|
||
struct Water { | ||
density: f32, | ||
} | ||
|
||
pub fn new_sim() -> SimContext { | ||
SimContext { | ||
sphere: new_sphere(), | ||
water: new_water(), | ||
gravity: 9.81, | ||
} | ||
} | ||
|
||
fn new_sphere() -> Sphere { | ||
let radius = 1.0; | ||
let density = 250.0; | ||
|
||
let volume = calc_sphere_volume(radius); | ||
let mass = density * volume; | ||
|
||
Sphere { | ||
radius, | ||
mass, | ||
height: 1.0, | ||
velocity: 0.0, | ||
} | ||
} | ||
|
||
fn new_water() -> Water { | ||
Water { | ||
density: 1000.0, | ||
} | ||
} | ||
|
||
fn calc_submerged_ratio(s: Sphere) -> f32 { | ||
let bottom = s.height - s.radius; | ||
let diameter = 2.0 * s.radius; | ||
if bottom >= 0.0 { | ||
0.0 | ||
} else if bottom <= -diameter { | ||
1.0 | ||
} else { | ||
-bottom / diameter | ||
} | ||
} | ||
|
||
fn calc_sphere_volume(radius: f32) -> f32 { | ||
let pi = 3.1415926535897; | ||
let r = radius; | ||
|
||
3.0/4.0 * pi * r * r * r | ||
} | ||
|
||
fn calc_buoyancy_force(s: Sphere, w: Water, gravity: f32, submerged_ratio: f32) -> f32 { | ||
let volume = calc_sphere_volume(s.radius); | ||
volume * submerged_ratio * w.density * gravity | ||
} | ||
|
||
pub fn sim_update(ctx: SimContext, elapsed_secs: f32) { | ||
let submerged_ratio = calc_submerged_ratio(ctx.sphere); | ||
if submerged_ratio > 0.0 { | ||
let buoyancy_force = calc_buoyancy_force( | ||
ctx.sphere, | ||
ctx.water, | ||
ctx.gravity, | ||
submerged_ratio | ||
); | ||
let buoyancy_acc = buoyancy_force / ctx.sphere.mass; | ||
ctx.sphere.velocity += buoyancy_acc * elapsed_secs; | ||
} | ||
|
||
ctx.sphere.velocity -= ctx.gravity * elapsed_secs; | ||
|
||
ctx.sphere.height += ctx.sphere.velocity * elapsed_secs; | ||
|
||
log_f32(ctx.sphere.height); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,55 @@ | ||
#include <iostream> | ||
#include <string> | ||
|
||
#include "mun/runtime.h" | ||
#include "mun/mun.h" | ||
|
||
extern "C" { | ||
void log_f32(float value) { std::cout << std::to_string(value) << std::endl; } | ||
} | ||
|
||
// How to run? | ||
// 1. On the CLI, navigate to the `example-cpp` directory. | ||
// 2. Run the compiler daemon from the CLI: `/path/to/mun build resources/fibonacci.mun --watch` | ||
// 3. Run the application from the CLI: `main /path/to/fibonacci.dll` | ||
// 2. Run the compiler daemon from the CLI: | ||
// `/path/to/mun build resources/buoyancy.mun --watch` | ||
// 3. Run the application from the CLI: | ||
// `main /path/to/buoyancy.munlib` | ||
int main(int argc, char* argv[]) { | ||
if (argc < 2) { | ||
return 1; | ||
} | ||
|
||
std::cout << "lib: " << argv[1] << std::endl; | ||
|
||
mun::RuntimeOptions options; | ||
options.functions.emplace_back(mun::RuntimeFunction("log_f32", log_f32)); | ||
|
||
mun::Error error; | ||
if (auto runtime = mun::make_runtime(argv[1], &error)) { | ||
if (auto runtime = mun::make_runtime(argv[1], options, &error)) { | ||
auto ctx = mun::invoke_fn<mun::StructRef>(*runtime, "new_sim").wait(); | ||
|
||
using clock_t = std::chrono::high_resolution_clock; | ||
using fsec_t = std::chrono::duration<float>; | ||
|
||
auto previous = clock_t::now(); | ||
constexpr auto FRAME_TIME = std::chrono::milliseconds(40); | ||
while (true) { | ||
auto n = mun::invoke_fn<int64_t>(*runtime, "nth").wait(); | ||
auto result = mun::invoke_fn<int64_t>(*runtime, "fibonacci", n).wait(); | ||
std::this_thread::sleep_until(previous + FRAME_TIME); | ||
|
||
const auto now = clock_t::now(); | ||
const auto elapsed = | ||
std::chrono::duration_cast<fsec_t>(now - previous); | ||
|
||
std::cout << "fibonacci(" << std::to_string(n) << ") = " << result << std::endl; | ||
mun::invoke_fn<void>(*runtime, "sim_update", ctx, elapsed.count()).wait(); | ||
previous = now; | ||
|
||
mun::Error update_error; | ||
if (!runtime->update(&update_error) && update_error) { | ||
std::cerr << "Failed to update runtime due to error: " << update_error.message() << std::endl; | ||
std::cerr << "Failed to update runtime due to error: " | ||
<< update_error.message() << std::endl; | ||
} | ||
} | ||
} | ||
|
||
std::cerr << "Failed to construct Mun runtime due to error: " << error.message() << std::endl; | ||
std::cerr << "Failed to construct Mun runtime due to error: " | ||
<< error.message() << std::endl; | ||
return 2; | ||
} |