Skip to content

Instantly share code, notes, and snippets.

@CryZe
Last active November 24, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CryZe/5295e8e5e777e0d88b17 to your computer and use it in GitHub Desktop.
Save CryZe/5295e8e5e777e0d88b17 to your computer and use it in GitHub Desktop.
Rust is cool
using System;
using System.IO;
namespace VeronikaMatrix
{
public static class Program
{
public static void Main(string[] args)
{
var countDroplets = 4;
var countPlug = 2;
var matrixN = countDroplets + countPlug + 2;
var matrix = new float[matrixN, matrixN];
Action<int, int, float> setValue = (x, y, v) => { if (x != y) { matrix[x, y] = v; matrix[y, x] = v; } };
var gas_ = 1.0f; // gas
var gpc = 3.0f; // plug-cont
var gdc = 3.0f; // droplet-cont
var gpp = 1.0f; // plug-plug
var gpd = 3.0f; // plug-droplet
var gdd = 1.0f; // droplet-droplet
// cont, gas, plug1, plug2, droplet...
var cont = 0;
var gas = 1;
var plugOffset = gas + 1;
var dropletOffset = plugOffset + countPlug;
for (int y = plugOffset; y < plugOffset + countPlug; ++y)
{
setValue(y, cont, gpc);
for (int x = plugOffset; x < plugOffset + countPlug; ++x)
{
setValue(x, y, gpp);
}
}
for (int y = 0; y < matrixN; ++y)
{
setValue(gas, y, gas_);
}
for (int y = dropletOffset; y < matrixN; ++y)
{
setValue(y, cont, gdc);
for (int x = plugOffset; x < plugOffset + countPlug; ++x)
{
setValue(x, y, gpd);
}
for (int x = dropletOffset; x < matrixN; ++x)
{
setValue(x, y, gdd);
}
}
for (int y = 0; y < matrixN; ++y)
{
Console.Write("(");
for (int x = 0; x < matrixN; ++x)
{
Console.Write("{0}", matrix[x, y]);
if (x + 1 < matrixN)
{
Console.Write(", ");
}
}
Console.WriteLine(") \\");
}
}
}
}
chris@Chris-Netbook ~/Projekte/C#/veronika_matrix
$ valgrind mono veronika_matrix.exe
==5557== Memcheck, a memory error detector
==5557== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==5557== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==5557== Command: mono veronika_matrix.exe
==5557==
(0, 1, 3, 3, 3, 3, 3, 3) \
(1, 0, 1, 1, 1, 1, 1, 1) \
(3, 1, 0, 1, 3, 3, 3, 3) \
(3, 1, 1, 0, 3, 3, 3, 3) \
(3, 1, 3, 3, 0, 1, 1, 1) \
(3, 1, 3, 3, 1, 0, 1, 1) \
(3, 1, 3, 3, 1, 1, 0, 1) \
(3, 1, 3, 3, 1, 1, 1, 0) \
==5557==
==5557== HEAP SUMMARY:
==5557== in use at exit: 347,887 bytes in 446 blocks
==5557== total heap usage: 14,708 allocs, 14,262 frees, 5,183,663 bytes allocated
==5557==
==5557== LEAK SUMMARY:
==5557== definitely lost: 782 bytes in 12 blocks
==5557== indirectly lost: 582 bytes in 17 blocks
==5557== possibly lost: 576 bytes in 2 blocks
==5557== still reachable: 345,947 bytes in 415 blocks
==5557== suppressed: 0 bytes in 0 blocks
==5557== Rerun with --leak-check=full to see details of leaked memory
==5557==
==5557== For counts of detected and suppressed errors, rerun with: -v
==5557== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
chris@Chris-Netbook ~/Projekte/C#/veronika_matrix
$ time mono veronika_matrix.exe
(0, 1, 3, 3, 3, 3, 3, 3) \
(1, 0, 1, 1, 1, 1, 1, 1) \
(3, 1, 0, 1, 3, 3, 3, 3) \
(3, 1, 1, 0, 3, 3, 3, 3) \
(3, 1, 3, 3, 0, 1, 1, 1) \
(3, 1, 3, 3, 1, 0, 1, 1) \
(3, 1, 3, 3, 1, 1, 0, 1) \
(3, 1, 3, 3, 1, 1, 1, 0) \
real 0m0.104s
user 0m0.068s
sys 0m0.028s
const COUNT_DROPLETS: usize = 4;
const COUNT_PLUG: usize = 2;
const GAS: f32 = 1.0;
const GPC: f32 = 3.0;
const GDC: f32 = 3.0;
const GPP: f32 = 1.0;
const GPD: f32 = 3.0;
const GDD: f32 = 1.0;
const MATRIX_N: usize = COUNT_DROPLETS + COUNT_PLUG + 2;
fn fill_matrix(matrix: &mut [[f32; MATRIX_N]; MATRIX_N]) {
let mut set_value = |x: usize, y: usize, v| {
if x != y {
matrix[x][y] = v;
matrix[y][x] = v;
}
};
let index_cont = 0;
let index_gas = index_cont + 1;
let index_plug = index_gas + 1;
let index_droplet = index_plug + COUNT_PLUG;
for y in index_plug..index_plug + COUNT_PLUG {
set_value(index_cont, y, GPC);
for x in index_plug..index_plug + COUNT_PLUG {
set_value(x, y, GPP);
}
}
for y in 0..MATRIX_N {
set_value(index_gas, y, GAS);
}
for y in index_droplet..MATRIX_N {
set_value(index_cont, y, GDC);
for x in index_plug..index_plug + COUNT_PLUG {
set_value(x, y, GPD);
}
for x in index_droplet..MATRIX_N {
set_value(x, y, GDD);
}
}
}
fn print_matrix(matrix: &[[f32; MATRIX_N]; MATRIX_N]) {
for row in matrix.iter() {
let cells = row.iter().map(|&x| x.to_string()).collect::<Vec<_>>().join(", ");
println!("({}) \\ ", cells);
}
}
fn main() {
let mut matrix: [[f32; MATRIX_N]; MATRIX_N] = [[0.0; MATRIX_N]; MATRIX_N];
fill_matrix(&mut matrix);
print_matrix(&matrix);
}
chris@Chris-Netbook ~/Projekte/Rust/veronika_matrix {master}
$ valgrind target/release/veronika_matrix
==5499== Memcheck, a memory error detector
==5499== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==5499== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==5499== Command: target/release/veronika_matrix
==5499==
(0, 1, 3, 3, 3, 3, 3, 3) \
(1, 0, 1, 1, 1, 1, 1, 1) \
(3, 1, 0, 1, 3, 3, 3, 3) \
(3, 1, 1, 0, 3, 3, 3, 3) \
(3, 1, 3, 3, 0, 1, 1, 1) \
(3, 1, 3, 3, 1, 0, 1, 1) \
(3, 1, 3, 3, 1, 1, 0, 1) \
(3, 1, 3, 3, 1, 1, 1, 0) \
==5499==
==5499== HEAP SUMMARY:
==5499== in use at exit: 0 bytes in 0 blocks
==5499== total heap usage: 6 allocs, 6 frees, 768 bytes allocated
==5499==
==5499== All heap blocks were freed -- no leaks are possible
==5499==
==5499== For counts of detected and suppressed errors, rerun with: -v
==5499== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
chris@Chris-Netbook ~/Projekte/Rust/veronika_matrix {master}
$ time target/release/veronika_matrix
(0, 1, 3, 3, 3, 3, 3, 3) \
(1, 0, 1, 1, 1, 1, 1, 1) \
(3, 1, 0, 1, 3, 3, 3, 3) \
(3, 1, 1, 0, 3, 3, 3, 3) \
(3, 1, 3, 3, 0, 1, 1, 1) \
(3, 1, 3, 3, 1, 0, 1, 1) \
(3, 1, 3, 3, 1, 1, 0, 1) \
(3, 1, 3, 3, 1, 1, 1, 0) \
real 0m0.013s
user 0m0.000s
sys 0m0.012s
#[derive(Clone, Copy, PartialEq)]
enum Phase {
Cont,
Gas,
Plug(usize),
Droplet(usize)
}
use Phase::*;
const PLUG_COUNT: usize = 2;
const DROPLET_COUNT: usize = 4;
const MATRIX_NAME: &'static str = "Gabd[vapor]";
fn get_surface_energy(a: Phase, b: Phase) -> f32 {
match (a, b) {
(x, y) if x == y => 0.0,
(Gas, _) => 1.0,
(Plug(_), Cont) => 3.0,
(Droplet(_), Cont) => 3.0,
(Plug(_), Plug(_)) => 1.0,
(Plug(_), Droplet(_)) => 3.0,
(Droplet(_), Droplet(_)) => 1.0,
_ => get_surface_energy(b, a)
}
}
fn main() {
let mut phases = vec![Cont, Gas];
let plugs = (0..PLUG_COUNT).map(|i| Plug(i));
let droplets = (0..DROPLET_COUNT).map(|i| Droplet(i));
phases.extend(plugs);
phases.extend(droplets);
let phase_matrix: Vec<_> = phases.iter().map(
|&phase| phases.iter().map(
|&inner_phase| get_surface_energy(phase, inner_phase))
.collect::<Vec<_>>()).collect();
let output = phase_matrix.into_iter().map(
|row| format!(" ({})", row.into_iter().map(
|x| x.to_string()).collect::<Vec<_>>().join(", ")))
.collect::<Vec<_>>().join(", \\ \n");
println!("Phasefield.{}.Matrix=[ \\ \n{} \\ \n]", MATRIX_NAME, output);
}
type Phase =
| Cont
| Gas
| Plug of int
| Droplet of int
let PlugCount = 2
let DropletCount = 4
let MatrixName = "Gabd[vapor]"
let rec getSurfaceEnergy a b =
match a, b with
| x, y when x.Equals(y) -> 0.0
| Gas, _ -> 1.0
| Plug _, Cont -> 3.0
| Droplet _, Cont -> 3.0
| Plug _, Plug _ -> 1.0
| Plug _, Droplet _ -> 3.0
| Droplet _, Droplet _ -> 1.0
| _ -> getSurfaceEnergy b a
let plugs = List.map (fun i -> Plug i) [1 .. PlugCount]
let droplets = List.map (fun i -> Droplet i) [1 .. DropletCount]
let phases = Seq.concat [[Cont; Gas]; plugs; droplets]
let phaseMatrix = Seq.map (fun phase ->
Seq.map (fun innerPhase ->
getSurfaceEnergy phase innerPhase) phases) phases
let output = String.concat ", \\ \n" <| Seq.map (fun row ->
sprintf " (%s)" <| String.concat ", "
(Seq.map (fun i -> i.ToString()) row)) phaseMatrix
printfn "Phasefield.%s.Matrix=[ \\ \n%s \\ \n]" MatrixName output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment