<?php 
error_reporting(0);
//error_reporting(E_ALL);
//define("GRAPH_ROUND",5);
define("RESULT_ROUND",10);
define("GRAPH_SAMPLE_POINT_INTERVAL",0.001);
define("POISSON_INPUT_INTERVAL_CONTROL",true);

define("GRAPH_SAMPLE_POINT_COUNT",(int)2/GRAPH_SAMPLE_POINT_INTERVAL);

require_once 'Math_Complex/Math/ComplexOp.php';

$warning = '';
if(isset($_POST['submit'])){ 
	$ResidualStress 		= isset($_POST['ResidualStress'])	? (double) $_POST['ResidualStress'] * pow(10,6) : 1;  // Pascal
	$MaterialType 			= isset($_POST['MaterialType'])		? (int) $_POST['MaterialType'] : 0 ;
	$Radius					= isset($_POST['Radius'])			? (double) $_POST['Radius'] * pow(10,-6)	: 1 ; // meter
	if($Radius<=0)
		$warning.=" Check your radius value! ";
	$Thickness				= isset($_POST['Thickness'])		? (double) $_POST['Thickness'] * pow(10,-6)	: 1 ; // meter
	if($Thickness<=0)
		$warning.=" Check your thickness value! ";
	$Pressure				= isset($_POST['Pressure'])			? (double) $_POST['Pressure']	* pow(10,6) : 1 ;  // pascal

	switch($MaterialType){
		case 1:
		  $Young=850;
		  $Poisson=0.07;
		  $Density=3520; // kg/m^3
		  
		break;
		case 2:
		  $Young=1000;
		  $Poisson=0.07;
		  $Density=3520; // kg/m^3
		break;
		case 3:
		  $Young=1200;
		  $Poisson=0.2;
		  $Density=3520; // kg/m^3
		break;
		case 4:
		  $Young=160;
		  $Poisson=0.29;
		  $Density=2332; // kg/m^3
		break;
		case 5:
		  $Young=73;
		  $Poisson=0.17;
		  $Density=2200; // kg/m^3
		break;
		case 6:
		  $Young = 300;
		  $Poisson=0.24;
		  $Density=2400; // kg/m^3
		break;
		case 0:
		default:
			if(isset($_POST['Young']) && $_POST['Young']>0)
				$Young = (double)$_POST['Young'];
		  	else
				$warning.=" Check your young value! ";

			$Poisson				= isset($_POST['Poisson'])			?(double)$_POST['Poisson'] : 0;
		  	$Density				= isset($_POST['Density'])			?(double)$_POST['Density'] : 1;	// kg/m^3
		  	if(POISSON_INPUT_INTERVAL_CONTROL){
		  		if(!($Poisson>0 && $Poisson<=0.5)){
		  			$warning.=" Check your poission value! ";
		  		}
		  	}
		  	if($Density<=0)
				$warning.=" Check your density value! ";
		break;
	}

	if(strlen($warning)<=1){
		$Young *= pow(10,9); // pascal

		$FlexuralRigidity		= ($Young*pow($Thickness,3))/(12*(1-pow($Poisson,2)));   // Pa*m^3 = Nm
		$MembraneRadiusAxis 	= range(-1,1,GRAPH_SAMPLE_POINT_INTERVAL);                   // defines the membrane radius.
		reset($MembraneRadiusAxis);
		while (list($key, $val) = each($MembraneRadiusAxis)) {
	    	$MembraneRadiusAxis[$key]=$Radius*$val;
		}
		$Deflection = array();

		reset($MembraneRadiusAxis);
		$cons1 = (($Pressure*pow($Radius,4))/(64*$FlexuralRigidity));
		while (list($key, $val) = each($MembraneRadiusAxis)) {
	    	$Deflection[$key] = $cons1*pow((1-(pow($val,2)/pow($Radius,2))),2); // Eq. 1
		}	
		
		$Beta = 
		new Math_Complex(
			( 7 * ( $Pressure ) * pow ( $Radius, 4) * pow ( $Thickness , 2) )
			/
			( 8 * ($FlexuralRigidity) * ( 1 + $Poisson ) * ( 23 - 9 * $Poisson ) )
			,0.0
		);
	    $Alfa = new Math_Complex(
	    	( ( 56 * pow( $Thickness , 2 ) ) + 42 * pow ( $Radius, 2) * ($ResidualStress / $Young) * ( 1 + $Poisson ) )
	    	/
	    	( ( 1 + $Poisson ) * ( 23-9 * ( $Poisson ) ) )
	    	,0.0
	    );
	    $Gamma = Math_ComplexOp::sqrt(
	    			Math_ComplexOp::add(
		    			Math_ComplexOp::div(
		    				Math_ComplexOp::pow($Alfa, new Math_Complex(3.0,0.0)),
		    				new Math_Complex(27.0,0.0)
		    			),
    					Math_ComplexOp::div(
    						Math_ComplexOp::pow($Beta,new Math_Complex(2.0,0.0)),
    						new Math_Complex(4.0,0.0)
    					)
    				)
	    		);
		//$Gamma = sqrt(pow($Alfa,3)/27+pow($Beta,2)/4);
		$base = Math_ComplexOp::add(
					Math_ComplexOp::div(
						$Beta,
						new Math_Complex(2.0,0.0)
					),
					$Gamma
				);
		//$base=(($Beta/2)+$Gamma);
		$A = Math_ComplexOp::pow(
				$base,
				new Math_Complex(1.0/3,0.0)
			);
		$f = Math_ComplexOp::sub(
				$A,
				Math_ComplexOp::div(
					$Alfa,
					Math_ComplexOp::mult(
						new Math_Complex(3.0,0.0),
						$A
					)
				)
			);

		
		$LargeDeflection = array();

		reset($MembraneRadiusAxis);
		
		while (list($key, $val) = each($MembraneRadiusAxis)) {
			$resultNumber = Math_ComplexOp::mult(
	    								$f,
	    								new Math_Complex(
	    									pow((1-pow(($val/$Radius),2)),2),
	    									0.0
	    								)
	    							);
			$sign = ($resultNumber->getReal()>0.0) ? 1 : -1 ;
			$LargeDeflection[$key] = $sign*$resultNumber->abs();
	    	//$LargeDeflection[$key] = $f*pow((1-pow(($val/$Radius),2)),2);
		}
		$Plot1=array();
		for($i=0;$i<GRAPH_SAMPLE_POINT_COUNT;$i++){
				array_push($Plot1,
					array(
						$MembraneRadiusAxis[$i]*pow(10,6),
						(-1*$Deflection[$i]*pow(10,6))
					)
				);
		}

		$Plot2=array();
		for($i=0;$i<GRAPH_SAMPLE_POINT_COUNT;$i++){
				array_push($Plot2,
					array(
						$MembraneRadiusAxis[$i]*pow(10,6),
						(-1*$LargeDeflection[$i]*pow(10,6))
					)
				);
		}
		$value = array(array('name' =>'Small Deflection','data'=>$Plot1 ),array('name'=>'Large Deflection','data'=>$Plot2));

		if(($maxSmallDef = max($Deflection)*pow(10,6))==0.0){
			if(($maxSmallDef = min($Deflection)*pow(10,6))==0.0){
				$warning .= ' Calculation may be incorrect. Please, be careful at your inputs!';
			}
		}

		if(($maxLargeDef = max($LargeDeflection)*pow(10,6))==0.0){
			if(($maxLargeDef = min($LargeDeflection)*pow(10,6))==0.0){
				$warning .= ' Calculation may be incorrect. Please, be careful at your inputs!';
			}
		}

		if(strlen($warning)<=1){
			$Tolerance   = abs(abs($maxSmallDef)-abs($maxLargeDef))/abs($maxSmallDef)*100;
			//$Tolerance   = abs(($maxSmallDef-$maxLargeDef)/$maxSmallDef)*100;
/*			$a = (string) $maxSmallDef;
			$b = (string) $maxLargeDef;

			
			echo bcdiv(bcsub($a, $b, 500),$b,500); 
			echo (abs($maxSmallDef)-abs($maxLargeDef));
*/			
			 $Tolerance=round($Tolerance,RESULT_ROUND);

		}
	}
}
?>
<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>Circular Membrane Deflection Calculator</title>
	<meta name="description" content="Circular Membrane Deflection Calculator">
	<meta name="author" content="Ultramems">
	<meta name="viewport" content="width=device-width">
	<link rel="stylesheet" href="css/style.css">
</head>
<body>
	<?php if(strlen($warning)>1): ?>
	<div id="warning">
		<?php echo $warning; ?>
	</div>
	<?php endif;?>
	<header>
		<h1>Circular Membrane Deflection Calculator</h1>
	</header>
	<div id="main" role="main">
		<form action="?" method="post">
			<fieldset>
				<legend>Input Parameters: </legend>
				<div class=half>
					<label for="Radius">Radius <input name="Radius" min="0" step="any" type="number" id="Radius" value="<?php if(isset($_POST['Radius'])) echo $_POST['Radius']; else echo "0"; ?>"> <span>µm</span></label>
					<label for="MaterialType">Material Type <select name="MaterialType" id="MaterialType">
						<?php $val = isset($_POST['MaterialType'])?$_POST['MaterialType']:"0"; ?>
							<option value="1" <?php if($val=="1") echo "selected";?>>UNCD</option>
							<option value="2" <?php if($val=="2") echo "selected";?>>NCD</option>
							<option value="3" <?php if($val=="3") echo "selected";?>>Diamond</option>
							<option value="4" <?php if($val=="4") echo "selected";?>>Silicon</option>
							<option value="5" <?php if($val=="5") echo "selected";?>>Silicon Dioxide</option>
							<option value="6" <?php if($val=="6") echo "selected";?>>Silicon Nitride</option>
							<option value="0" <?php if($val=="0") echo "selected";?>>Custom</option>
						</select>
					</label>
					<label for="Thickness">Thickness <input type="number" min="0" step="any" name="Thickness" id="Thickness" value="<?php if(isset($_POST['Thickness'])) echo $_POST['Thickness'];  else echo "0";?>"><span>µm</span></label>
					<label for="ResidualStress">Residual Stress <input type="number" step="any" name="ResidualStress" id="ResidualStress" value="<?php if(isset($_POST['ResidualStress'])) echo $_POST['ResidualStress'];  else echo "0";?>"><span>MPa</span></label>					
					<label for="Pressure">Pressure <input type="number" step="any" name="Pressure" id="Pressure" value="<?php if(isset($_POST['Pressure'])) echo $_POST['Pressure'];  else echo "0"; ?>"><span>MPa</span></label>
				</div>
				<div class=half>
					<label for="Young">Elastic Modulus <input type="number" min="0" step="any" name="Young" id="Young" <?php if($val>0) echo "disabled"; ?> value="<?php if(isset($Young)) print($Young*pow(10,-9));  else echo "0"; ?>"><span>GPa</span></label>
					<label for="Poisson">Poisson Value<input type="number" name="Poisson" step="any" id="Poisson" <?php if($val>0) echo "disabled"; ?> value="<?php if(isset($Poisson)) echo $Poisson;  else echo "0"; ?>"></label>
					<label for="Density">Density <input type="number" min="0" name="Density" step="any" id="Density" <?php if($val>0) echo "disabled"; ?> value="<?php if(isset($Density)) echo $Density;  else echo "0"; ?>"><span>kg/m<sup>3</sup></span></label>
					<button type="submit" name="submit"> Plot Deflection </button>
				</div>
				<div class="clear"></div>
			</fieldset>


			<fieldset>
				<legend>Output Parameters: </legend>
				<div class="half">
					<label for="Tolerance">Tolerance <input type="text" readonly name="Tolerance" id="Tolerance" value="<?php if(isset($Tolerance)) echo $Tolerance;?>"> <span>%</span></label>

					<label for="maxSmallDef">Small Def. Value<input type="text" readonly name="maxSmallDef" id="maxSmallDef" value="<?php if(isset($maxSmallDef)) printf("%e", abs($maxSmallDef)); ?>"> <span>µm</span></label>
					<label for="maxLargeDef">Large Def. Value<input type="text" readonly name="maxLargeDef" id="maxLargeDef" value="<?php if(isset($maxLargeDef)) printf("%e", abs($maxLargeDef)); ?>"> <span>µm</span></label>
					<a href="http://www.ultramems.com" id="logo"><img src="img/ultramems.png" alt="Ultramems Logo"></a>
				</div>
				<div class="half" id="graph">Graphing...</div>
				<div class="clearfix"></div>
				<div id=last>
				
				<span class=left><a href="#references" id="reference-link">References</a></span>	<span class=right>Calculator Code by <a href="mailto:sgroumlu@gmail.com">Serdar Görümlü</a>, 
	 	PHP Convertion by <a href="mailto:caglaryesilyurt@gmail.com">Çağlar Yeşilyurt</a>. 
		© 2012. All Rights Reserved.</span>

				<span class="clearfix"></span>
					<ul id="references">
						<li>W.P. Eaton, F. Bitsie, J.H. Smith, D.W. Plummer, "A New Analytical Solution for Diaphragm Deflection and its Application to a Surface-Micromachined Pressure Sensor",International Conference on Modeling and Simulation of Microsystems,MSM,1999. </li>
						<li>S. Timoshenko, S.Woinosky-Krieger, Theory of Plates and Shells, McGraw Hill Classic Textbook Reissue,1987.</li>
					</ul>
				
				</div>
			</fieldset>
			<input type="hidden" name="graphType" id="graphType" value="<?php if(isset($_POST['graphType'])) echo $_POST['graphType']; else echo "0";?>">
		</form>
		<footer>
		
		</footer>
	
	</div>

	<script type="text/javascript">
	value = <?php echo (!empty($value))?json_encode($value):'[{"name":"Small Deflection","data":[[0,0]]},{"name":"Large Deflection","data":[[0,0]]}]'; ?>
	</script>

	<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
	<script>window.jQuery || document.write('<script src="js/jquery-1.7.2.min.js"><\/script>')</script>
	<script src="js/highcharts.js"></script>
	<script src="js/script.js"></script>
</body>
</html>