Skip to content

Commit

Permalink
Added license
Browse files Browse the repository at this point in the history
  • Loading branch information
ange-yaghi committed Aug 9, 2022
1 parent c6902af commit caf1542
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 21 deletions.
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2022 AngeTheGreat (Ange Yaghi)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
# Engine Simulator
![Alt text](docs/public/screenshot_v01.png?raw=true)
---
## **Warning: code is in development and will change frequently**
---

To be honest, I don't even know if this project will work... but yeah it's an internal combustion engine simulator.
## What is this?

This is a real-time internal combustion engine simulation **designed specifically to produce engine audio and simulate engine response characteristics.** It is NOT a scientific tool and cannot be expected to provide accurate figures for the purposes of engineering or engine tuning.

## Why is the code so sloppy?

I wrote this to demo in a [YouTube video](https://youtu.be/RKT-sKtR970), not as a real product. If you would like it to become a usable product please reach out to me or join my Discord (link can be found in the description of the aforementioned YouTube video). I use this codebase for my own purposes and so it might change frequently and without warning.

## How do I use it?

The UI is extremely minimalistic and there are only a few controls used to interact with the engine:

| Key/Input | Action |
| :---: | :---: |
| A | Toggle ignition |
| S | Hold for starter |
| D | Enable dyno |
| F | Enter fullscreen mode |
| Escape | Exit the program |
| Q, W, E, R | Change throttle position |
| 1, 2, 3, 4, 5 | Simulation time warp |
| Tab | Change screen |

## How do I build it?
**Note: this project currently only builds on Windows!**

### Step 1 - Clone the repository
```git clone --recurse-submodules https://github.com/ange-yaghi/engine-sim```

### Step 2 - Install CMake
Install the latest version of CMake [here](https://cmake.org/) if it's not already installed.

### Step 3 - Install Dependencies
You will need to install the following dependencies and CMake will need to be able to locate them (ie. they need to be listed on your PATH):

1. SDL2
2. SDL2_image
3. Boost (make sure to build the optional dependencies)

### Step 4 - Build and Run
From the root directory of the project, run the following commands:

```
mkdir build
cd build
cmake ..
cmake --build .
```

If these steps are successful, a Visual Studio solution will be generated in ```build```. You can open this project with Visual Studio and then run the ```engine-sim-app``` project. If you encounter an error telling you that you're missing DLLs, you will have to copy those DLLs to your EXE's directory.
Binary file modified assets/assets.dia
Binary file not shown.
Binary file modified assets/assets.ysce
Binary file not shown.
2 changes: 1 addition & 1 deletion assets/part-library/engines/kohler_ch750.mr
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public node kohler_ch750 {
moment_of_inertia: 0.22986844776863666 * 0.5,
position_x: 0.0,
position_y: 0.0,
tdc: constants.pi / 4 - 2 * constants.pi
tdc: -constants.pi / 4
)

rod_journal rj0(angle: 0.0)
Expand Down
Binary file added assets/smooth_46.wav
Binary file not shown.
Binary file added assets/smooth_47.wav
Binary file not shown.
Binary file added assets/smooth_48.wav
Binary file not shown.
Binary file added assets/smooth_49.wav
Binary file not shown.
2 changes: 1 addition & 1 deletion assets/test.mr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "engine_sim.mr"
import "part-library/part_library.mr"
import "video-scripts/454-tuning/engine_03.mr"
import "video-scripts/454-tuning/engine_04.mr"

set_engine(
engine: kohler_ch750()
Expand Down
Binary file added docs/public/screenshot_v01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/combustion_chamber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ void CombustionChamber::ignite() {
units::pressure(160, units::psi));

if (rand() % 32 == 0) {
m_flameEvent.efficiency = maxBurningEfficiency;
//m_flameEvent.efficiency = maxBurningEfficiency;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/crankshaft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void Crankshaft::setRodJournalAngle(int i, double angle) {
}

double Crankshaft::getAngle() const {
return -(m_body.theta - m_tdc);
return -m_body.theta - m_tdc;
}

double Crankshaft::getCycleAngle(double offset) {
Expand Down
6 changes: 3 additions & 3 deletions src/crankshaft_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void CrankshaftObject::render(const ViewParameters *view) {

for (int i = 0; i < journalCount; ++i) {
const int layer = i;
if (layer > view->Layer1 || layer < view->Layer0) return;
if (layer > view->Layer1 || layer < view->Layer0) continue;

const ysVector col = tintByLayer(ysColor::srgbiToLinear(0xAAAAAA), i);

Expand All @@ -45,13 +45,13 @@ void CrankshaftObject::render(const ViewParameters *view) {
0.0f,
0.0f,
0.0f);
m_app->getShaders()->SetBaseColor(tintByLayer(ysColor::srgbiToLinear(0x999999), 0));
m_app->getShaders()->SetBaseColor(tintByLayer(ysColor::srgbiToLinear(0xFFFFFF), 1));
m_app->getEngine()->DrawModel(
m_app->getShaders()->GetRegularFlags(),
m_app->getAssetManager()->GetModelAsset("CrankSnout"),
0x10);

m_app->getShaders()->SetBaseColor(tintByLayer(ysColor::srgbiToLinear(0xFFFFFF), 1));
m_app->getShaders()->SetBaseColor(tintByLayer(ysColor::srgbiToLinear(0xAAAAAA), 1));
m_app->getEngine()->DrawModel(
m_app->getShaders()->GetRegularFlags(),
m_app->getAssetManager()->GetModelAsset("CrankSnoutThreads"),
Expand Down
14 changes: 10 additions & 4 deletions src/engine_sim_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ void EngineSimApplication::initialize() {
simulatorParams.Transmission = transmission;
simulatorParams.Vehicle = vehicle;
simulatorParams.SimulationFrequency = 10000;
simulatorParams.FluidSimulationSteps = 16;
m_simulator.initialize(simulatorParams);
m_simulator.startAudioRenderingThread();
createObjects(m_iceEngine);
Expand Down Expand Up @@ -729,7 +730,7 @@ void EngineSimApplication::process(float frame_dt) {
void EngineSimApplication::render() {
SimulationObject::ViewParameters view;
view.Layer0 = 0;
view.Layer1 = 3;
view.Layer1 = 10;

for (SimulationObject *object : m_objects) {
object->generateGeometry();
Expand Down Expand Up @@ -933,13 +934,18 @@ void EngineSimApplication::run() {
m_simulator.getTransmission()->changeGear(m_simulator.getTransmission()->getGear() + 1);

m_infoCluster->setLogMessage(
"UPSHIFTED TO " + std::to_string(m_simulator.getTransmission()->getGear()));
"UPSHIFTED TO " + std::to_string(m_simulator.getTransmission()->getGear() + 1));
}
else if (m_engine.ProcessKeyDown(ysKey::Code::Down)) {
m_simulator.getTransmission()->changeGear(m_simulator.getTransmission()->getGear() - 1);

m_infoCluster->setLogMessage(
"DOWNSHIFTED TO " + std::to_string(m_simulator.getTransmission()->getGear()));
if (m_simulator.getTransmission()->getGear() != -1) {
m_infoCluster->setLogMessage(
"DOWNSHIFTED TO " + std::to_string(m_simulator.getTransmission()->getGear() + 1));
}
else {
m_infoCluster->setLogMessage("SHIFTED TO NEUTRAL");
}
}

if (m_engine.IsKeyDown(ysKey::Code::Shift)) {
Expand Down
2 changes: 1 addition & 1 deletion src/oscilloscope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void Oscilloscope::render(const Bounds &bounds) {

const bool detached =
prev.x > p_i.x
|| std::abs(p_i.x - prev.x) > pixelsToUnits(0.5) * (m_xMax - m_xMin);
|| std::abs(p_i.x - prev.x) > pixelsToUnits(20.0f);
m_app->getGeometryGenerator()->generatePathSegment(
params,
(detached || lastDetached) && !m_drawReverse);
Expand Down
15 changes: 7 additions & 8 deletions src/simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ void Simulator::initialize(const Parameters &params) {
m_system->addForceGenerator(m_engine->getChamber(i));
}

atg_scs::GravityForceGenerator *gravity = new atg_scs::GravityForceGenerator;
//gravity->m_g = 10.0;
//m_system->addForceGenerator(gravity);

m_dyno.connectCrankshaft(m_engine->getOutputCrankshaft());
m_system->addConstraint(&m_dyno);

Expand Down Expand Up @@ -256,17 +260,12 @@ void Simulator::startFrame(double dt) {
const double timestep = getTimestep();
i_steps = (int)std::round((dt * m_simulationSpeed) / timestep);

const double targetLatency = getSynthesizerInputLatencyTarget();

const double targetLatency = getSynthesizerInputLatencyTarget();
if (m_synthesizer.getLatency() < targetLatency) {
++i_steps;
i_steps *= 1.1;
}
else if (m_synthesizer.getLatency() > targetLatency) {
if (m_synthesizer.getLatency() > 2 * targetLatency) i_steps -= 2;
else --i_steps;
if (i_steps < 0) {
i_steps = 0;
}
i_steps *= 0.1;
}

if (i_steps > 0) {
Expand Down

0 comments on commit caf1542

Please sign in to comment.