Basics of Engine Creation
Here im just going to show the basics. An engine file might look like this:
import "engine_sim.mr"
units units()
constants constants()
impulse_response_library ir_lib()
label cycle(2 * 360 * units.deg)
public node saab_distributor {
input wires;
input timing_curve;
input rev_limit: 8500 * units.rpm;
alias output __out:
ignition_module(timing_curve: timing_curve, rev_limit: rev_limit)
.connect_wire(wires.wire1, (0.0/4.0) * cycle)
.connect_wire(wires.wire2, (3.0/4.0) * cycle)
.connect_wire(wires.wire3, (1.0/4.0) * cycle)
.connect_wire(wires.wire4, (2.0/4.0) * cycle);
}
private node wires {
output wire1: ignition_wire();
output wire2: ignition_wire();
output wire3: ignition_wire();
output wire4: ignition_wire();
}
private node add_sym_sample {
input angle;
input lift;
input this;
alias output __out: this;
this.add_sample(angle * units.deg, lift * units.thou)
this.add_sample(-angle * units.deg, lift * units.thou)
}
public node b201_lobe_profile_int {
alias output __out:
harmonic_cam_lobe(
duration_at_50_thou: 230 * units.deg,
gamma: 0.8,
lift: 13.3 * units.mm,
steps: 100
);
}
public node b201_lobe_profile_exh {
alias output __out:
harmonic_cam_lobe(
duration_at_50_thou: 230 * units.deg,
gamma: 0.8,
lift: 13.3 * units.mm,
steps: 100
);
}
public node saab_camshaft_builder {
input lobe_profile: b201_lobe_profile_int();
input ex_lobe_profile: b201_lobe_profile_exh();
input intake_lobe_profile: lobe_profile;
input exhaust_lobe_profile: ex_lobe_profile;
input lobe_separation: 106.0 * units.deg;
input intake_lobe_center: lobe_separation;
input exhaust_lobe_center: lobe_separation;
input advance: 0.0 * units.deg;
input base_radius: 0.6 * units.inch;
output intake_cam_0: _intake_cam_0;
output exhaust_cam_0: _exhaust_cam_0;
camshaft_parameters params(
advance: advance,
base_radius: base_radius
)
camshaft _intake_cam_0(params, lobe_profile: intake_lobe_profile)
camshaft _exhaust_cam_0(params, lobe_profile: exhaust_lobe_profile)
label rot360(360 * units.deg)
label cycle(2 * 360 * units.deg)
_intake_cam_0
.add_lobe(rot360 + lobe_separation + 0.0/4 * cycle)
.add_lobe(rot360 + lobe_separation + 3.0/4 * cycle)
.add_lobe(rot360 + lobe_separation + 1.0/4 * cycle)
.add_lobe(rot360 + lobe_separation + 2.0/4 * cycle)
_exhaust_cam_0
.add_lobe(rot360 - lobe_separation + 0.0/4 * cycle)
.add_lobe(rot360 - lobe_separation + 3.0/4 * cycle)
.add_lobe(rot360 - lobe_separation + 1.0/4 * cycle)
.add_lobe(rot360 - lobe_separation + 2.0/4 * cycle)
}
private node add_flow_sample {
input lift;
input flow;
input this;
alias output __out: this;
this.add_sample(lift * units.mm, k_28inH2O(flow))
}
public node saab_b201_head {
input intake_camshaft;
input exhaust_camshaft;
input chamber_volume: 34.0 * units.cc;
input flip_display: false;
input flow_attenuation: 1.0;
input lift_scale: 1.0;
alias output __out: head;
function intake_flow(1 * units.mm)
intake_flow
.add_flow_sample(0 * lift_scale, 0 * flow_attenuation)
.add_flow_sample(1 * lift_scale, 35 * flow_attenuation)
.add_flow_sample(2 * lift_scale, 60 * flow_attenuation)
.add_flow_sample(3 * lift_scale, 90 * flow_attenuation)
.add_flow_sample(4 * lift_scale, 125 * flow_attenuation)
.add_flow_sample(5 * lift_scale, 150 * flow_attenuation)
.add_flow_sample(6 * lift_scale, 175 * flow_attenuation)
.add_flow_sample(7 * lift_scale, 200 * flow_attenuation)
.add_flow_sample(8 * lift_scale, 215 * flow_attenuation)
.add_flow_sample(9 * lift_scale, 230 * flow_attenuation)
.add_flow_sample(10 * lift_scale, 235 * flow_attenuation)
.add_flow_sample(11 * lift_scale, 235 * flow_attenuation)
.add_flow_sample(12 * lift_scale, 238 * flow_attenuation)
function exhaust_flow(1 * units.mm)
exhaust_flow
.add_flow_sample(0 * lift_scale, 0 * flow_attenuation)
.add_flow_sample(1 * lift_scale, 35 * flow_attenuation)
.add_flow_sample(2 * lift_scale, 55 * flow_attenuation)
.add_flow_sample(3 * lift_scale, 85 * flow_attenuation)
.add_flow_sample(4 * lift_scale, 105 * flow_attenuation)
.add_flow_sample(5 * lift_scale, 120 * flow_attenuation)
.add_flow_sample(6 * lift_scale, 140 * flow_attenuation)
.add_flow_sample(7 * lift_scale, 150 * flow_attenuation)
.add_flow_sample(8 * lift_scale, 155 * flow_attenuation)
.add_flow_sample(9 * lift_scale, 160 * flow_attenuation)
.add_flow_sample(10 * lift_scale, 165 * flow_attenuation)
.add_flow_sample(11 * lift_scale, 165 * flow_attenuation)
.add_flow_sample(12 * lift_scale, 165 * flow_attenuation)
cylinder_head head(
chamber_volume: chamber_volume,
intake_runner_volume: 100.0 * units.cc,
intake_runner_cross_section_area: 2 * 12.4087 * units.cm2,
intake_port_flow: intake_flow,
exhaust_port_flow: exhaust_flow,
intake_camshaft: intake_camshaft,
exhaust_camshaft: exhaust_camshaft,
flip_display: flip_display
)
}
public node b201 {
alias output __out: engine;
engine engine(
name: "SAAB B201",
starter_torque: 150 * units.lb_ft,
starter_speed: 1000 * units.rpm,
redline: 7000 * units.rpm,
fuel: fuel(
max_turbulence_effect: 4.0
)
)
wires wires()
crankshaft c0(
throw: 80 * units.mm / 2,
flywheel_mass: 5.9 * units.kg,
mass: 5 * units.kg,
friction_torque: 10.0 * units.lb_ft,
moment_of_inertia: 0.22986844776863666 * 0.9,
position_x: 0.0,
position_y: 0.0,
tdc: 135 * units.deg
)
label cycle(720 * units.deg)
rod_journal rj0(angle: 0 * cycle/4)
rod_journal rj1(angle: 3 * cycle/4)
rod_journal rj2(angle: 1 * cycle/4)
rod_journal rj3(angle: 2 * cycle/4)
c0
.add_rod_journal(rj0)
.add_rod_journal(rj1)
.add_rod_journal(rj2)
.add_rod_journal(rj3)
piston_parameters piston_params(
mass: 280 * units.g,
//blowby: k_28inH2O(0.1),
compression_height: 11.2 * units.mm,
wrist_pin_position: 0.0,
displacement: 0.0
)
connecting_rod_parameters cr_params(
mass: 300.0 * units.g,
moment_of_inertia: 0.0015884918028487504,
center_of_mass: 0.0,
length: 153 * units.mm
)
cylinder_bank_parameters bank_params(
bore: 90 * units.mm,
deck_height: 203.2 * units.mm
)
performer_rpm_intake intake(
carburetor_cfm: 500.0,
idle_flow_rate_cfm: 0.1,
idle_throttle_plate_position: 0.94, // start at 0.963, go to -> 0.97
throttle_gamma: 2.0
)
exhaust_system_parameters es_params(
outlet_flow_rate: k_carb(1000.0),
primary_tube_length: 20.0 * units.inch,
primary_flow_rate: k_carb(200.0),
velocity_decay: 1.0, //0.5
volume: 50.0 * units.L
)
exhaust_system exhaust0(es_params, audio_volume: 0.5, impulse_response: ir_lib.default_0)
exhaust_system exhaust1(es_params, audio_volume: 1.0, impulse_response: ir_lib.default_0)
cylinder_bank b0(bank_params, angle: 45 * units.deg)
b0
.add_cylinder(
piston: piston(piston_params, blowby: k_28inH2O(0.1)),
connecting_rod: connecting_rod(cr_params),
rod_journal: rj0,
intake: intake,
exhaust_system: exhaust0,
ignition_wire: wires.wire1
)
.add_cylinder(
piston: piston(piston_params, blowby: k_28inH2O(0.1)),
connecting_rod: connecting_rod(cr_params),
rod_journal: rj1,
intake: intake,
exhaust_system: exhaust1,
ignition_wire: wires.wire2
)
.add_cylinder(
piston: piston(piston_params, blowby: k_28inH2O(0.1)),
connecting_rod: connecting_rod(cr_params),
rod_journal: rj2,
intake: intake,
exhaust_system: exhaust0,
ignition_wire: wires.wire3
)
.add_cylinder(
piston: piston(piston_params, blowby: k_28inH2O(0.1)),
connecting_rod: connecting_rod(cr_params),
rod_journal: rj3,
intake: intake,
exhaust_system: exhaust1,
ignition_wire: wires.wire4
)
engine
.add_cylinder_bank(b0)
engine.add_crankshaft(c0)
harmonic_cam_lobe lobe(
duration_at_50_thou: 256 * units.deg,
gamma: 1.1,
lift: 10.2 * units.mm,
steps: 100
)
saab_camshaft_builder camshaft(
lobe_profile: b201_lobe_profile_int(),
ex_lobe_profile: b201_lobe_profile_exh()
)
b0.set_cylinder_head (
saab_b201_head(
intake_camshaft: camshaft.intake_cam_0,
exhaust_camshaft: camshaft.exhaust_cam_0
)
)
function timing_curve(1000 * units.rpm)
timing_curve
.add_sample(0000 * units.rpm, 10 * units.deg)
.add_sample(1000 * units.rpm, 10 * units.deg)
.add_sample(2000 * units.rpm, 30 * units.deg)
.add_sample(3000 * units.rpm, 30 * units.deg)
.add_sample(4000 * units.rpm, 30 * units.deg)
.add_sample(5000 * units.rpm, 30 * units.deg)
.add_sample(6000 * units.rpm, 30 * units.deg)
.add_sample(7000 * units.rpm, 30 * units.deg)
engine.add_ignition_module(
saab_distributor(
wires: wires,
timing_curve: timing_curve,
rev_limit: 8000 * units.rpm
))
}
This is my SAAB B201 engine and it might look difficult right, but in reality it isnt.
What makes a functional engine?
A functional engine needs
- A distributor:
- Wires
- A camshaft
- An engine block head
- A Crankshaft
- Crankshaft journals
- Pistons
- Cylinder banks
- An intake
- An exhaust system
- And an ignition module
All this is needed or the engine sim WILL crash
I also want to point out that indentation, closing and opening brackets are is a must just like any programming language u have to follow syntax.
Failing to do so will result in instant crash.