#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <stdint.h>

int main(int argc, char **argv){
	int32_t target_rpm = 1800;
	double total_displacement = 3.70;
	int32_t cyclinders = 6;
	double VE = 0.95;
	double tmp1 = 0;
	double tmp2 = 0;
	double runner_area = 0;
	double runner_len = 0;
	double runner_dia = 0;
	double runner_rad = 0;
	double cyl_disp = 0;
	double compression_ratio = 9.7;

	//runner diameter
	tmp1 = sqrt(   (target_rpm*(total_displacement/cyclinders)*VE)/3330  );

	//runner length
	tmp2 = 17.8 + (4.3*((10000-target_rpm)/1000));
	//convert to inches
	tmp2 /= 2.54;

	printf("ideal diameter: %lf in.\n", tmp1);
	printf("ideal length: %lf in.\n", tmp2);


	//Helmholtz formula
	//cyl_disp = (total_displacement/cyclinders)*61.0237;

	cyl_disp = (total_displacement/cyclinders)*1000;
//	tmp1 += 1.0;
	runner_dia = tmp1*2.54/100;
	runner_len = tmp2*2.54/100;

//runner_dia = 2.54/100;
runner_dia = 1.905/100;
runner_len = 53.34/100;

runner_rad = runner_dia/2;

	runner_area = (6.28318530718*runner_len*runner_rad) + (6.28318530718*runner_rad*runner_rad);

//39.3701
	tmp1 = 218280 * sqrt(runner_area/(runner_len*cyl_disp)) * sqrt((compression_ratio-1)/(compression_ratio+1));
	//tmp1 /= 2.54;
	printf("helmholtz calculated rpm: %lf\n", tmp1);

/*
L = Primary runner length: 14 cm

A = Primary runner area: 5.8 cm2

V = Displacement per cylinder: 150 cc

C = Compression ratio: 13.0:1

c = Speed of sound is taken as 340 m/s.

The Helmholtz peak, in RPM, will be given by the following equation:



642 * c * sqrt(A/L * V) * sqrt(C-1/C+1)


III. b/ LENGTH OF RAM INTAKE TUBE

A suggested starting point for the length of a tube with peak torque at 6000 rpm is 13 in.

You add 1.7 in. for every 1000 rpm that you want to move the peak torque below 6000.

Or subtract 1.7 in. for every 1000 rpm you want to move the peak torque above 6000.


solve for V
642 x c x [ SQRT (S/[L x V] ) ] x [ SQRT { (CR-1)/ (CR+1) } ]

R = 642*c*sqrt(S/L*V)*sqrt((CR-1)/(CR+1))
R/sqrt((CR-1)/(CR+1)) = 642*c*sqrt(S/L*V)

1800/sqrt((8.7)/(10.7)) = 642*340*sqrt(S/L*V)

sqrt(S/L*V) = 1800/sqrt((8.7)/(10.7)) / 642*340

S/L*V = (1800/sqrt((8.7)/(10.7)) / 642*340) * (1800/sqrt((8.7)/(10.7)) / 642*340)

V = ((1800/sqrt((8.7)/(10.7)) / 642*340) * (1800/sqrt((8.7)/(10.7)) / 642*340) * L)  / S

where f = the rpm at which you get peak torque ( the natural frequency of pressure
oscillations in the acoustic chamber ) , c = the speed of sound (= 340 m/sec.) , S = runner
area, L = runner length, V = displacement per cylinder

V = ((1800/sqrt((8.7)/(10.7)) / 642*340) * (1800/sqrt((8.7)/(10.7)) / 642*340) * 0.508) / 0.031

*/


	return 0;
}
