Add machine workshop, typed logic, mechanics and sensor-driven ECUs
This commit is contained in:
32
tools/machine_benchmark.cpp
Normal file
32
tools/machine_benchmark.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "MachineBench.hpp"
|
||||
#include "FluidSim/Machine.hpp"
|
||||
#include "FluidSim/Scenario.hpp"
|
||||
#include "FluidSim/ScenarioCatalog.hpp"
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
using namespace fluidsim::port;
|
||||
template<class Step> double measure(Step step,int frames){
|
||||
const auto begin=std::chrono::steady_clock::now();
|
||||
for(int i=0;i<frames;++i){const auto value=step();if(!std::isfinite(value.left)||!std::isfinite(value.right))throw std::runtime_error("Nonfinite benchmark audio");}
|
||||
return std::chrono::duration<double>(std::chrono::steady_clock::now()-begin).count();
|
||||
}
|
||||
int main(int argc,char** argv)try{
|
||||
const int frames=argc>1?std::stoi(argv[1]):32768;const float rpm=argc>2?std::stof(argv[2]):3000,pedal=argc>3?std::stof(argv[3]):.35f;
|
||||
if(frames<1||!std::isfinite(rpm)||rpm<0||rpm>50000||!std::isfinite(pedal)||pedal<0||pedal>1)throw std::invalid_argument("Usage: fluidsim_machine_benchmark [frames>0] [rpm 0..50000] [pedal 0..1]");
|
||||
std::cout<<"scenario,runtime,frames,rpm,pedal,cells,min_cell_mm,substeps_per_frame,us_per_frame,wall_per_sim,positivity_repairs\n";
|
||||
for(const char* id:{"single-450","twin","flat6","v8-h"}){
|
||||
const auto& preset=scenario_example(id);FourStrokeScenario native(44100,preset);native.set_controls(pedal,0,1);
|
||||
const auto step=[&]{native.crankshaft.omega=rpm*2*pi/60;return native.process();};
|
||||
for(int i=0;i<512;++i)step();const auto before=native.snapshot().solver_substeps;const double elapsed=measure(step,frames);const auto after=native.snapshot();
|
||||
float shortest=1e9f;unsigned cells=0;for(auto& p:native.pipes){cells+=unsigned(p.cells.size());shortest=std::min(shortest,p.minimum_cell_length());}
|
||||
std::cout<<id<<",native,"<<frames<<','<<rpm<<','<<pedal<<','<<cells<<','<<shortest*1000<<','<<double(after.solver_substeps-before)/frames<<','<<elapsed*1e6/frames<<','<<elapsed*44100/frames<<','<<after.pipe_positivity_repairs<<std::endl;
|
||||
auto design=machine_from_engine(preset);bench::set_rpm(design,rpm);MachineSimulation machine(design);machine.set_controls(pedal);
|
||||
for(int i=0;i<512;++i)machine.process();const auto start=machine.snapshot().substeps;const double wall=measure([&]{return machine.process();},frames);const auto state=machine.snapshot();
|
||||
std::cout<<id<<",workshop,"<<frames<<','<<rpm<<','<<pedal<<','<<state.cells<<','<<state.minimum_cell_length*1000<<','<<double(state.substeps-start)/frames<<','<<wall*1e6/frames<<','<<wall*44100/frames<<','<<state.positivity_repairs<<std::endl;
|
||||
if(state.positivity_repairs||after.pipe_positivity_repairs)throw std::runtime_error("Benchmark required positivity repairs");
|
||||
}
|
||||
return 0;
|
||||
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}
|
||||
Reference in New Issue
Block a user