Skip to content

Instantly share code, notes, and snippets.

@sholtomaud
Last active January 3, 2019 09:51
Show Gist options
  • Save sholtomaud/04118c55076f2ded72e6c99fbb5273e1 to your computer and use it in GitHub Desktop.
Save sholtomaud/04118c55076f2ded72e6c99fbb5273e1 to your computer and use it in GitHub Desktop.
node_modules
#!/usr/bin/env node
const sprintf = require('sprintf-js').sprintf;
const asciichart = require ('asciichart')
// TANK
// Small Lateral Aperture Simulation
const parameters = {
'apertureHeight': 0.001, // height of ap
'density': 1000, //water density (kg/m3)
'diameter': 0.1, // 0.01 m diameter of outlet meters
'volumetricFlow': 0,
'gravity': 9.81, // acceleration of gravity (9.81 m/s2)
'k' : 0.97 * 0.97, // discharge coefficient: velocity coefficient (water 0.97) * contraction coefficient (well rounded aperture 0.97)
'maxTime': 60,
'waterLevel': 1, // 1 meter height of tank
aperture() {
return Math.PI * Math.pow((this.diameter/2),2);
},
shoot(priorHeight){
return 2 * Math.pow(priorHeight * this.apertureHeight,0.5);
},
newVolumetricFlow(priorLevel){
let flow = this.k * this.aperture() * Math.pow(2 * this.gravity * priorLevel,0.5);
this.waterLevel =( priorLevel - flow < 0 )? 0 : priorLevel - flow ;
this.volumetricFlow = flow;
return flow;
},
newWaterLevel(timestep,outFlow){
let wl = (this.waterLevel - outFlow);
if ( timestep > 0) return wl;
if ( wl < 0) return 0;
return this.waterLevel_0 ;
},
backForce(){
return this.density * this.volumetricFlow * this.velocity;
}
}
const s0 = new Array (parameters.maxTime);
process.stdout.write("\u001b[2J");
process.stdout.write("\u001b[0;0H\n");
process.stdout.write(sprintf('%s: %s: %s:\n', 'TIME', 'LEVEL', 'FLOW'));
process.stdout.write(sprintf('%s\n', '-------------------------'));
for (let timestep = 0; timestep < parameters.maxTime || this.waterLevel === 0 ; timestep++) {
s0[timestep] = parameters.waterLevel * 10;
const params = sprintf('%03f %03.4f %04.4f\n',
`${timestep}`,
`${parameters.waterLevel}`,
`${parameters.volumetricFlow}`
)
const ellipsis = sprintf(' %s %s %s\n',
`⋮`,
`⋮`,
`⋮`
)
if (timestep < 3 || timestep > parameters.maxTime - 4 ) {process.stdout.write( params )}
else if (timestep < 4 || timestep > parameters.maxTime - 4 ) {process.stdout.write(ellipsis)}
parameters.newVolumetricFlow(parameters.waterLevel)
parameters.newWaterLevel(timestep,parameters.volumetricFlow)
}
process.stdout.write(sprintf('%s\n', '-------------------------'));
process.stdout.write('\n');
console.log (asciichart.plot (s0),'\n')
{
"name": "sim-tank",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"bin": "./index.js",
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"asciichart": "^1.5.7",
"sprintf-js": "^1.1.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment