<?php session_start();

	include('../../libs/uam/core/config.php'); 
	include('../../libs/cooltool/class.coolTool.php'); 
	
	//If user is not signed in redirect
	if(!$user->signed) {
		
		//$_SESSION['ErrorMsgs'][] = "Please login or register to access this page.";
		redirect("../../?page=login");	
		
	}
	$ctool = new coolTool($user->data['user_id']);  //initiate cooltool
	$results = $ctool->getSimResults(); // retrieve sim results
	
  	if (!$results) {
		
		//$_SESSION['ErrorMsgs'][] = "There are no results to display. Please try running your simulation again.";
		redirect("../../?page=results");
	  
  	}

	function coldplate_TR($data) {
		return floordec( ($data["pjt"] - @$_SESSION['sim_data']['bdry']["inlet_temperature"])/@$_SESSION['sim_data']['bdry']["heat_power"],3); 
	}
	function coldplate_TR_wTIM($data) {
		return floordec( ($data["mjtt"] - @$_SESSION['sim_data']['bdry']["inlet_temperature"])/@$_SESSION['sim_data']['bdry']["heat_power"],3); 
	}
	function overall_TR($data) {
	 	return floordec( ($data["pjt"] - @$_SESSION['sim_data']['bdry']["inlet_temperature"])/@$_SESSION['sim_data']['bdry']["heat_power"],3); 

	}
	function tim_TR($data) {
		return overall_TR($data) - coldplate_TR_wTIM($data);
	}	
	function floordec($d,$decimals=2) {    
	
		$decimals = ($d <= 0.01 ? 4:$decimals);
	
     return floor($d*pow(10,$decimals))/pow(10,$decimals);
	}
	
	function hasTIM() {
		return ( $_SESSION['sim_data']['geom']['tim_thickness'] == '0' ? false: true );
	}
	function thermalParams($data) {
		$html = '';
		if (hasTIM()) {
			$html.='<p>Max Coldplate Surface temp: '.floordec($data["mjtt"]).' &deg;C </p>'; 
        	$html.='<p>Max TIM Surface temp: '.floordec($data["pjt"]).' &deg;C </p>';
			$html.='<p>Overall Thermal Resistance<sup>1</sup>: '.overall_TR($data).' k/W </p>';
			$html.='<p>Coldplate Thermal Resistance<sup>2</sup>: '.coldplate_TR_wTIM($data).' k/W </p>';
			$html.='<p>TIM Thermal Resistance<sup>3</sup>: '.tim_TR($data).' k/W </p>';
			$html.='<p>Presssure Drop [inlet to outlet]: '.floordec($data["pd"]/100,3).' bar </p>';
			$html.='<p>Outlet Fluid Temp: '.floordec($data["tfo"]).'  &deg;C </p>';
			$html.='<p>Fluid Temp Rise [inlet to outlet]: '.floordec($data["dtf"]).' &deg;C </p>';
        	$html.='<p>Heat Transfer Coeff. (Average): '.floordec($data["mbf"]).'  kW/m<sup>2</sup>K </p>';
        	$html.='<p>Heat Transfer Coeff. (Wetted Area): '.floordec($data["htc"]).'  kW/m<sup>2</sup>K </p>';
        	$html.='<p>Fluid Velocity<sup>4</sup>: '.floordec($data["iv"],3).' m/s</p>';
        	$html.='<p>Reynolds Number: '.floordec($data["ir"]).' </p>';
        	$html.='<p>Flow Regime<sup>5</sup>: '.$data["fr"].'</p>';
        
        } else {
            $html.='<p>Max Coldplate Surface temp: '.floordec($data["pjt"]).' &deg;C </p>';    
			$html.='<p>Thermal Resistance<sup>1</sup>: '.coldplate_TR($data).' k/W </p>';
			$html.='<p>Presssure Drop [inlet to outlet]: '.floordec($data["pd"]/100,3).' bar </p>';
			$html.='<p>Outlet Fluid Temp: '.floordec($data["tfo"]).'  &deg;C </p>';
			$html.='<p>Fluid Temp Rise [inlet to outlet]: '.floordec($data["dtf"]).' &deg;C </p>';
        	$html.='<p>Heat Transfer Coeff. (Average): '.floordec($data["mbf"]).'  kW/m<sup>2</sup>K </p>';
        	$html.='<p>Heat Transfer Coeff. (Wetted Area): '.floordec($data["htc"]).'  kW/m<sup>2</sup>K </p>';
        	$html.='<p>Fluid Velocity<sup>2</sup>: '.floordec($data["iv"],3).' m/s</p>';
        	$html.='<p>Reynolds Number: '.floordec($data["ir"]).' </p>';
        	$html.='<p>Flow Regime<sup>3</sup>: '.$data["fr"].'</p>';
        
		}
		return $html;
		
	}
	function thermalParamNotes() {
		
		$html = '';
    	if (hasTIM()) { 
    
            $html.='<p><sup>1</sup>Overall Thermal Resistance = (Max TIM Surface Temp – Inlet Fluid Temp) / Heat Power</p>';
            $html.='<p><sup>2</sup>Coldplate Thermal Resistance = (Max Coldplate Surface Temp – Inlet Fluid Temp) / Heat Power</p>';
            $html.='<p><sup>3</sup>TIM Thermal Resistance = Overall Thermal Resistance - Coldplate Thermal Resistance</p>';
          	$html.='<p><sup>4</sup>Fluid Velocity is taken at minimum cross sectional area of flow.</p>';
          	$html.='<p><sup>5</sup>Laminar Flow < 2300, Transition 2,300 – 4,000, Turbulent > 4,000.</p>';
            
		 } else { 
            
            $html.='<p><sup>1</sup>Thermal Resistance = (Max Coldplate Surface Temp – Inlet Fluid Temp) / Heat Power</p>';
          	$html.='<p><sup>2</sup>Fluid Velocity is taken at minimum cross sectional area of flow.</p>';
          	$html.='<p><sup>3</sup>Laminar Flow < 2300, Transition 2,300 – 4,000, Turbulent > 4,000.</p>';
            
		}
		return $html;	
		
	}
	
	function returnFinType() {
		
		switch(@$_SESSION["sim_data"]["geom"]['fin_type']) {
			case "0":
				return '<option value="0">Straight Fin (User Defined)</option>';
				break;
			case "1":
				return '<option value="1">Straight Fin (WLV)</option>';
				break;
			case "2":
				return '<option value="2">In-Line Pin Fin (WLV)</option>';
				break;
			case "3":
				return '<option value="3">Staggered Pin Fin (WLV)</option>';
				break;
			default:
			return '<option value="-1"></option>';
										
										
		}
		
	}
	function returnFinOptions() {
		
		if(@$_SESSION["sim_data"]["geom"]['fin_type'] == "0") return '<option value="-1">N/A</option>';
		
		switch(@$_SESSION["sim_data"]["geom"]['fin_options']) {
			case "1":
				return '<option value="1">12 FPI (Copper)</option>';
				break;
			case "2":
				return '<option value="2">25 FPI (Copper)</option>';
				break;
			case "3":
				return '<option value="3">40 FPI (Copper)</option>';
				break;
			case "4":
				return '<option value="4">12 FPI (Aluminium)</option>';
				break;
			case "5":
				return '<option value="5">25 FPI (Aluminium)</option>';
				break;	
			default:
				return '<option value="-1"></option>';
										
										
		}
		
	}
	function returnFinWidth() {
		
		if(@$_SESSION["sim_data"]["geom"]['fin_type'] != "0") return 'N/A'; 
		
		return @$_SESSION["sim_data"]["geom"]['fin_width'];
		
	}
	function returnMaterialColdPlate() {
						
		switch(@$_SESSION["sim_data"]["mat"]['material_cold_plate']) {
			case "aluminium":
				return '<option value="aluminium">Aluminium 1100</option>';
				break;
			case "copper":
				return '<option value="copper">Copper 110</option>';
				break;
			default:
				return '<option value="-1"></option>';
														
		}
		
	}
	function returnMaterialTim() {
			
		switch(@$_SESSION["sim_data"]["mat"]['material_tim']) {
			case "siliconPaste":
				return '<option value="siliconPaste">Silicon Paste</option>';
				break;
			case "liquidMetal":
				return '<option value="liquidMetal">Liquid Metal</option>';
				break;
			case "other":
				return ' <option value="other">Other</option>';
				break;	
			default:
				return '<option value="-1"></option>';
														
		}
		
	}
	function returnMaterialFluid() {
			
		switch(@$_SESSION["sim_data"]["mat"]['material_fluid']) {
			case "water_corr":
				return '<option value="water_corr">Water</option>';
				break;
			case "eg":
				return '<option value="eg">Water/EG</option>';
				break;
			case "pg":
				return '<option value="pg">Water/PG</option>';
				break;	
			case "Dexron":
				return '<option value="Dexron">Dexron VI ATF</option>';
				break;	
			case "OptiCoolPH5":
				return '<option value="OptiCoolPH5">OptiCool PH5</option>';
				break;			
			case "KeroseneD60":
				return '<option value="KeroseneD60">Kerosene D60</option>';
				break;				
			default:
				return '<option value="-1"></option>';
														
		}
		
	}	
	
                      
// Studio Green Test for Base64 to Image                 				
$imgstring='data:image/jpeg;base64,'.$results["webfig"];
                                	
                                	
	


$html = '<div>
        <div id="report_container">
            <div id="report_main">
                <div id="report_banner"><a href="/"> &nbsp; </a></div>
                <div id="report_content" style="margin-top:10px;">
                    <div class="left">
                        <hr class="breakblue" />
                        <div class="main_display">
                           <form id="geom" action="#" enctype="application/x-www-form-urlencoded" class="sim-form">  
							<div class="report-subcontent" id="geom">
    						<h3>GEOMETRY</h3>
    						<hr  class="breaklightblue" />
               				<div class="leftcol">
                	
                			<h4>Cold plate</h4>
							<p><label for="heated_width" class="formLabel">Width [Xc] </label>
                       			<input type="text" id="heated_width" name="heated_width" size="10" value="'.$_SESSION["sim_data"]["geom"]['heated_width'].'"/> (mm)
                            	<label for="heated_width" class="error" generated="true"></label>
                    		</p>
                			<p><label for="heated_length" class="formLabel">Length [Yc] </label>
                       	   		<input type="text" id="heated_length" name="heated_length" size="10" value="'.$_SESSION["sim_data"]["geom"]['heated_length'].'" /> (mm)
                           		<label for="heated_length" class="error" generated="true"></label>
                    		</p>
                        	<p><label for="base_thickness" class="formLabel">Base Thickness [Zc]</label>
                	   	   		<input type="text" name="base_thickness" size="10" value="'.$_SESSION["sim_data"]["geom"]['base_thickness'].'"/> (mm)
                           		<label for="base_thickness" class="error" generated="true"></label>
          					</p>
                	
                	
                    	<br />
                
                			<h4>MDT<sup>®</sup> Geometry</h4>
                         	<p><label class="formLabel" style="vertical-align:top;">Type</label>
      							<select name="fin_type" id="fin_type" style="display:inline-block; height:150px; padding-top:5px; padding-bottom:5px; width:150px;">'.returnFinType().'
                				</select>
                             	<label for="fin_type" class="error" generated="true"></label>
                        	</p>
                        	<p id="fin_options_p"><label class="formLabel" style="vertical-align:top;">Fin Options</label>
      							<select name="fin_options" id="fin_options" style="display:inline-block; height:auto; padding-top:5px; padding-bottom:5px; width:150px;">'.returnFinOptions().'
              					</select>
                            	<label for="fin_options_p" class="error" generated="true"></label>
                        	</p>
                        
                        	<p id="fin_width_p"><label class="formLabel">Fin Width</label>
      							<input type="text" id="fin_width" name="fin_width" size="10" value = "'.returnfinWidth().'"/> (mm)
                           	 	<label for="fin_width" class="error" generated="true"></label>
                       	 	</p>
                        	<p><label class="formLabel">Fin Height</label>
      							 <input type="text" name="fin_height" size="10" value="'.$_SESSION["sim_data"]["geom"]['fin_height'].'"/> (mm)
                           	 	 <label for="fin_height" class="error" generated="true"></label>
                        	</p>
                        	<p><label class="formLabel">Tim Thickness</label>
      							<input type="text" name="tim_thickness" size="10" value="'.$_SESSION["sim_data"]["geom"]['tim_thickness'].'"/> (microns)
                            	<label for="tim_thickness" class="error" generated="true"></label>
                        	</p>
                        	<p>
                           		<input type="hidden" name="channel_roughness" size="10" value="'.$_SESSION["sim_data"]["geom"]['channel_roughness'].'" />
                           		<label for="channel_roughness" class="error" generated="true"></label>
                           		<input type="hidden" name="channel_width" id="channel_width" size="10" value="'.$_SESSION["sim_data"]["geom"]['channel_width'].'"/>
                           		<label for="channel_width" class="error" generated="true"></label>
                           		<input type="hidden" name="heated_thickness" size="10" value = "'.$_SESSION["sim_data"]["geom"]['heated_thickness'].'"/>
                           		<label for="heated_thickness" class="error" generated="true"></label>
                        	</p>
                
                	</div><!-- End .leftcol -->
                	<div class = "rightcol">
                		<img alt="Cold Plate Geometry" src="../images/geometry.png" width="300" style="margin-top:0px; margin-bottom:20%;"/>
						
                    	<p class="notes"><b>Special Notes:</b></p>
                    	<ul class="notes">
                        	<li><b>Cold Plate Length [Yc] must be <= 500 mm</b></li>
                    	 	<li><b>Cold Plate Thickness [Zc] must be between 1-12 mm</b></li>
                         	<li><b>Heat Source is uniformly distributed across top of cold plate surface</b></li>
                         	<li><b>MDT Fin Height must be between 0.5 - 6.0 mm</b></li>
                         	<li><b>MDT Fin Width must be between 0.1 - 1.0 mm</b></li>
                         	<li><b>Fin width = Channel width</b></li>
                         	<li><b>TIM thickness must be between 0 - 200 microns.</b></li>
                         	<li><b>Zero TIM thickness => No TIM</b></li>
                    	</ul>
                    
                	</div><!-- End .rightcol -->
            	</div><!-- end .subcontent-->
				</form>
				<form id="bdry" action="#" enctype="application/x-www-form-urlencoded" class="sim-form"> 
					<div class="report_subcontent" id="bdry" style="margin-top:15px;">
				    	<h3>BOUNDARY CONDITIONS</h3>
						<hr  class="breaklightblue" />
						<div class="leftcol">
                			<p> <label class="formLabel">Flow Rate [V]</label>
                	   			<input type="text" name="inlet_vol_flow" size="10" value="'.$_SESSION["sim_data"]["bdry"
								]['inlet_vol_flow'].'"/> (Liters/min)
          					</p>
                        	<p> <label class="formLabel">Inlet Temp [T]</label>
                	   	   	    <input type="text" name="inlet_temperature" size="10" value="'.$_SESSION["sim_data"][
								"bdry"]['inlet_temperature'].'"/> (&deg;C)
          					</p>
                        	<p> <label class="formLabel">Power on Heat Source [Q]</label>
                	   	   		<input type="text" id="heat_power" name="heat_power" size="10" value="'.$_SESSION[
								"sim_data"]["bdry"]['heat_power'].'"/> (W)
          					</p>  
                        	<p>
								<input type="hidden" id = "bc_val_heat_footprint" name="bc_val_heat_footprint" size="5" 
								value="0"/>
                            	<label for="bc_val_heat_footprint" class="error" generated="true"></label>
                            	<input type="hidden" name="bc_type_heat_footprint" size="5" value="HF"/>
                            	<label for="bc_type_heat_footprint" class="error" generated="true"></label>
                            	<input type="hidden" name="outlet_drop_coeff" value="0" size="5"/>   
                            	<label for="outlet_drop_coeff" class="error" generated="true"></label>
                            	<input type="hidden" name="inlet_drop_coeff" value="0" size="5" /> 
                            	<label for="inlet_drop_coeff" class="error" generated="true"></label>
                            	<input type="hidden" name="inlet_pressure" size="5" value="3"/>
                            	<label for="inlet_pressure" class="error" generated="true"></label>
                        	</p> 
                		</div><!-- End .leftcol -->
                		<div class = "rightcol">
                			<p><img alt="Cold Plate Geometry" src="../images/boundary_conditions.png" width="250"/>
							</p>
 							<p style="float:right;"></p>    
                		</div><!-- End .rightcol -->
            		</div><!-- end .subcontent-->   
				</form>
		   	
				<form id="mat" action="#" enctype="application/x-www-form-urlencoded" class="sim-form"> 
				<div class="report_subcontent" id="mat" style="padding-top:20px;">
    			<h3>MATERIALS</h3>
    			<hr  class="breaklightblue" />
                 <div class="leftcol" style="width:45%;">
				 <p><label class="formLabel" style="vertical-align:top;">Coldplate Material</label>
                	   		<select name="material_cold_plate" id="material_cold_plate"style="display:inline-block; height:45px; padding-top:5px; padding-bottom:5px;">'.returnMaterialColdPlate().'
                    			
                			</select>
                            <label for="material_cold_plate" class="error" generated="true"></label>
                 </p>
<p><label class="formLabel" style="vertical-align:top;">TIM Material</label>
                	   		<select name="material_tim" id="material_tim" style="display:inline-block; height:55px; padding-top:5px; padding-bottom:5px;">'.returnMaterialTim().'
                    			
                			</select>
                            <label for="material_tim" class="error" generated="true"></label>
                       </p>
					   <p><label class="formLabel">TIM Thermal Conductivity*</label>
      							 <input type="text" name="material_tim_conductivity" size="8" value="'.$_SESSION["sim_data"]["mat"]['material_tim_conductivity'].'" />(W m<sup>-1</sup>K<sup>-1</sup>)
                           	 	 <label for="material_tim_conductivity" class="error" generated="true"></label>
                       </p>
						<p><label class="formLabel" style="vertical-align:top;">Working Fluid**</label>
                	   		<select name="material_fluid" id="material_fluid" style="display:inline-block; height:auto; padding-top:5px; padding-bottom:5px; width:100px;">'.returnMaterialFluid().'
                    			
                			</select>
                             <label for="material_fluid" class="error" generated="true"></label>
                       </p>	
						<p>
                			<label class="formLabel">Water (% Mass)</label>
      							<input id="mixture_ratio" type="slider" name="mixture_ratio" value="0" size="10" value="'.$_SESSION["sim_data"]["mat"]['mixture_ratio'].'"/>
    					        <label for="mixture_ratio" class="error" generated="true"></label>
                	   </p>					   				   					   
                               
                                    <br />
                                  
                                </div><!-- End .leftcol -->
				                                <div class="rightcol" style="width:50%; padding: 0 5px 0 0;">
                                    <table class="gridtable">
                                        <caption style="font-size:10px;">Fluid Properties Temperature: 30 °C</caption>
                                        <tbody>
                                            <tr>
                                                <th width="85"></th>
                                                <th width="61">Density <br />[kg m<sup>-3</sup>]</th>
                                                <th width="73">Specific heat <br />[J kg<sup>-1</sup> K<sup>-1</sup>]</th>
                                                <th width="57">Dynamic viscosity <br />[µPa s]</th>
                                                <th width="80">Thermal conductivity <br />[mW m<sup>-1</sup> K<sup>-1</sup>]</th>
                                            </tr>
                                            <tr>
                        	<td>Water</td>
                        	<td>995.7</td>
                            <td>4179.8</td>
                            <td>797.4</td>
                            <td>615.5</td>
                       </tr>
					    <tr>
                        	<td>Water/EG (50/50)</td>
                        	<td>1069.5</td>
                            <td>3408.1</td>
                            <td>2785.1</td>
                            <td>395.7</td>
                       </tr>
					    <tr>
                        	<td>Water/EG (85/15)</td>
                        	<td>1016.4</td>
                            <td>3948.4</td>
                            <td>1128.7</td>
                            <td>540.5</td>
                       </tr>
					    <tr>
                        	<td>Water/PG (50/50)</td>
                        	<td>1030.3</td>
                            <td>3651.8</td>
                            <td>3954.2</td>
                            <td>382.1</td>
                       </tr>
					    <tr>
                        	<td>Water/PG (85/15)</td>
                        	<td>1005.7</td>
                            <td>4050.2</td>
                            <td>1301.8</td>
                            <td>545.6</td>
                       </tr>
                       <tr>
                        	<td>Dexron VI ATF</td>
                        	<td>836.1</td>
                            <td>1942.2</td>
                            <td>37122.8</td>
                            <td>136.2</td>
                       </tr>   
                       <tr>
                        	<td>OptiCool PH5</td>
                        	<td>823.5</td>
                            <td>2166.3</td>
                            <td>6250.4</td>
                            <td>137.1</td>
                       </tr>                                                                     
						</table>
				
				
                
                    <p class="notes"><b>Special Notes:</b></p>
                    <ul class="notes">
                         <li><b>*Thermal conductivities for Silicon Paste and Liquid Metal &nbsp;&nbsp;are 0.3 W m<sup>-1</sup>K<sup>-1</sup> and 23.7 W m<sup>-1</sup>K<sup>-1</sup>respectively.</b></li>
                    	 <li><b>*Thermal conductivity must be between 0.1 - 450 W m<sup>-1</sup>K<sup>-1</sup>.</b></li>
                         <li><b>**EG = Ethylene Glycol</b></li>
                         <li><b>**PG = Propylene Glycol</b></li>
                    </ul>
                </div><!-- End .rightcol -->
            </div><!-- end .subcontent-->    
</form>            	
                            <div class="report_subcontent" style="padding-top:40px;">
                                <h3>Results</h3>
                                <hr class="breaklightblue" />
                                <div class="leftcol" id="report_result_container" style="width:95%">
	<div style="float:left; width:52%;" class="ptight">'.thermalParams($results).'</div>
          
    <div align="center" style="width:47%; float:left; padding-left:3px;">   
    	<table cellpadding="0" cellspacing="0" style="text-align:center; font-size:11px;">
        	<tr><td><img style="margin-top:-15px;" width="380" src="'. $imgstring .'" /></td></tr>
            <tr><td>Fig. 1. Temperature (&deg;C) map of base plate surface</td></tr>
        </table>
    </div>
    <div style="float:left; width:90%; clear:both;" class="ptightextra">'.thermalParamNotes().'</div>
                                </div><!-- end .leftcol -->   
                            </div><!-- end .report_subcontent -->
                        </div><!-- end .main_display DO NOT REMOVE THIS-->
                    </div><!-- end #left -->
                </div><!-- end #content -->
                <div id="report_clearbox"></div>
				<br /><br /><br />
                <div id="report_footer"><p><span></span> Published by MicroCoolTool on '.date('l jS \of F Y h:i A').'</p><div class="custom">
                    <p>Copyright © Wolverine Tube, Inc. All Rights Reserved. <a href="https://www.microcooling.com/microcool-tool/2-uncategorised/55-privacy-statement" target="_blank">Privacy Statement</a>. <a href="https://www.microcooling.com/microcool-tool/2-uncategorised/56-return-policy">Return Policy</a>.</p></div>
                </div><!--end #report_footer-->
            </div><!-- end #main -->
        </div><!-- end #container -->
    </div><!-- end th container -->';



//==============================================================
//==============================================================
//==============================================================

include("../../libs/MPDF57/mpdf.php");
$mpdf=new mPDF('c');

$mpdf->SetDisplayMode('fullpage');

// LOAD a stylesheet
$stylesheet = file_get_contents('../../css/cooltoolreport.css');
$mpdf->WriteHTML($stylesheet,1);	// The parameter 1 tells that this is css/style only and no body/html/text

$mpdf->WriteHTML($html);

$mpdf->Output('CoolToolSimulation.pdf', 'I');

exit;
//==============================================================
//==============================================================
//==============================================================

?>