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

Commit

Permalink
feat: upgrade to Mun v0.2.0 and buoyancy example
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed May 16, 2020
1 parent 747c52a commit 0e520ae
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 22 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ GitHub.sublime-settings
tmtags

##### VisualStudioCode
/.vscode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
Expand Down Expand Up @@ -912,3 +913,6 @@ _deps
*.exe
*.out
*.app

# Artifacts
*.munlib
91 changes: 91 additions & 0 deletions resources/buoyancy.mun
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);
}
11 changes: 0 additions & 11 deletions resources/fibonacci.mun

This file was deleted.

41 changes: 31 additions & 10 deletions src/main.cc
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;
}

0 comments on commit 0e520ae

Please sign in to comment.