| 1 | | <? |
| 2 | | function db_get_plot_values($plottype, $log){ |
| 3 | | |
| 4 | | include("db.php.inc"); |
| | 1 | <?php |
| | 2 | class Unit |
| | 3 | { |
| | 4 | var $name; |
| | 5 | var $num_name; |
| | 6 | //Multiple existing value by scale to get num_name |
| | 7 | var $scale; |
| | 8 | function Unit($name,$num_name,$scale) |
| | 9 | { |
| | 10 | $this->name=$name; |
| | 11 | $this->num_name=$num_name; |
| | 12 | $this->scale=$scale; |
| | 13 | } |
| | 14 | function name_split() |
| | 15 | { |
| | 16 | $ret=explode("/",$this->name); |
| | 17 | if(!isset($ret[1])) |
| | 18 | $ret[1]=""; |
| | 19 | return $ret; |
| | 20 | } |
| | 21 | function div($other) |
| | 22 | { |
| | 23 | $mine=$this->name_split(); |
| | 24 | $eirs=$other->name_split(); |
| | 25 | if($mine[0]&&$eirs[1]) |
| | 26 | $mine[0].="-$eirs[1]"; |
| | 27 | else |
| | 28 | $mine[0].=$eirs[1]; |
| | 29 | if($mine[1]&&$eirs[0]) |
| | 30 | $mine[1].="-$eirs[0]"; |
| | 31 | else |
| | 32 | $mine[1].=$eirs[0]; |
| | 33 | if(!$mine[1]) |
| | 34 | $name=$mine[0]; |
| | 35 | else |
| | 36 | $name="$mine[0]/$mine[1]"; |
| | 37 | $ret=new Unit( |
| | 38 | $name, |
| | 39 | $this->num_name/$other->num_name, |
| | 40 | $this->scale/$other->scale |
| | 41 | ); |
| | 42 | return $ret; |
| | 43 | } |
| | 44 | function mul($other) |
| | 45 | { |
| | 46 | $mine=$this->name_split(); |
| | 47 | $eirs=$other->name_split(); |
| | 48 | if($mine[0]&&$eirs[0]) |
| | 49 | $mine[0].="-$eirs[0]"; |
| | 50 | else |
| | 51 | $mine[0].=$eirs[0]; |
| | 52 | if($mine[1]&&$eirs[1]) |
| | 53 | $mine[1].="-$eirs[1]"; |
| | 54 | else |
| | 55 | $mine[1].=$eirs[1]; |
| | 56 | if(!$mine[1]) |
| | 57 | $name=$mine[0]; |
| | 58 | else |
| | 59 | $name="$mine[0]/$mine[1]"; |
| | 60 | $ret=new Unit( |
| | 61 | "$name", |
| | 62 | $this->num_name*$other->num_name, |
| | 63 | $this->scale*$other->scale |
| | 64 | ); |
| | 65 | return $ret; |
| | 66 | } |
| | 67 | function equals($other) |
| | 68 | { |
| | 69 | return abs($this->num_name-$other)<.00000001; |
| | 70 | } |
| | 71 | } |
| | 72 | |
| | 73 | $unit_infos=array( |
| | 74 | "g"=>new Unit("g",2,1), |
| | 75 | "m"=>new Unit("m",3,1), |
| | 76 | "K"=>new Unit("K",5,1), |
| | 77 | "s"=>new Unit("s",7,1), |
| | 78 | "°C"=>new Unit("°C",11,1), |
| | 79 | "lb"=>new Unit("lb",13,1), |
| | 80 | "°F"=>new Unit("°F",17,1), |
| | 81 | //kg m s2 |
| | 82 | "N"=>new Unit("N",2*3/(7*7),1000), |
| | 83 | //1 pound = 4.44822162 Newtons |
| | 84 | "p"=>new Unit("p",2*3/(7*7),4.44822162*1000), |
| | 85 | "in"=>new Unit("in",3,0.0254), |
| | 86 | "cm"=>new Unit("cm",3,.01), |
| | 87 | //kg*m*m/(s^3) |
| | 88 | "W"=>new Unit("W",2*3*3/(7*7*7),1000), |
| | 89 | //N m-2 |
| | 90 | "Pa"=>new Unit("Pa",2*3/(7*7)/(3*3),1000), |
| | 91 | "MPa"=>new Unit("MPa",2*3/(7*7)/(3*3),1000000*1000), |
| | 92 | "cc"=>new Unit("cc",3*3*3,.01*.01*.01), |
| | 93 | //1 kg × 1 m2 × 1 s-2 |
| | 94 | "J"=>new Unit("J",2*3*3/(7*7),1000), |
| | 95 | "BTU"=>new Unit("BTU",2*3*3/(7*7),1055*1000), |
| | 96 | "hr"=>new Unit("hr",7,60*60), |
| | 97 | "ft"=>new Unit("ft",3,12*0.0254) |
| | 98 | ); |
| | 99 | |
| | 100 | class PropertyData |
| | 101 | { |
| | 102 | //average value out of all the matches |
| | 103 | var $avg_value; |
| | 104 | var $num_unit; |
| | 105 | //number of matches |
| | 106 | var $count; |
| | 107 | function PropertyData() |
| | 108 | { |
| | 109 | $this->avg_value=0; |
| | 110 | $this->num_unit=0; |
| | 111 | $this->count=0; |
| | 112 | } |
| | 113 | } |
| | 114 | |
| | 115 | function parse_units($units_element) |
| | 116 | { |
| | 117 | global $unit_infos; |
| | 118 | global $dom; |
| | 119 | $scale=1; |
| | 120 | if(get_class($units_element)=="DOMNodeList") |
| | 121 | $units_element=$units_element->item(0); |
| | 122 | if($units_element) |
| | 123 | { |
| | 124 | $num_unit=1; |
| | 125 | $factor=$units_element->getAttribute("factor"); |
| | 126 | if($factor) |
| | 127 | $scale*=$factor; |
| | 128 | //$units=$units_element->get_elements_by_tagname("Unit"); |
| | 129 | $units=$units_element->getElementsByTagName("Unit"); |
| | 130 | foreach($units as $unit) |
| | 131 | { |
| | 132 | $name=$unit->getElementsByTagName("Name"); |
| | 133 | $name=$name->item(0); |
| | 134 | $name=$name->nodeValue; |
| | 135 | $power=$unit->getAttribute("power"); |
| | 136 | if(!$power) |
| | 137 | $power=1; |
| | 138 | $unit_info=$unit_infos[$name]; |
| | 139 | if(!$unit_info) |
| | 140 | echo "Error: unknown unit $name\n"; |
| | 141 | $scale*=pow($unit_info->scale,$power); |
| | 142 | $num_unit*=pow($unit_info->num_name,$power); |
| | 143 | } |
| | 144 | } |
| | 145 | else |
| | 146 | $num_unit=0; |
| | 147 | return array($scale,$num_unit); |
| | 148 | } |
| | 149 | |
| | 150 | function get_property_data($filename,$property_names) |
| | 151 | { |
| | 152 | global $dom; |
| | 153 | $dom=new DOMDocument; |
| | 154 | $dom->loadXML(file_get_contents($filename)); |
| | 155 | $context=new DOMXPath($dom); |
| | 156 | |
| | 157 | $equivalencies=array( |
| | 158 | array_flip(array("Young's Modulus","Modulus of Elasticity","Tensile Modulus")), |
| | 159 | array_flip(array("Tensile Strength, Yield","Yield Strength","Tensile Strength @ Yield")), |
| | 160 | array_flip(array("Heat Capacity","Specific Heat")) |
| | 161 | ); |
| | 162 | $translation_string="\"abcdefghijklmnopqrstuvwxyz'\",'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"; |
| | 163 | $ret=array(); |
| | 164 | $details=$context->query("/MatML_Doc/Material/BulkDetails/Name/text()"); |
| | 165 | $item=$details->item(0); |
| | 166 | $Matid=$item->nodeValue; |
| | 167 | foreach($property_names as $key=>$property_name) |
| | 168 | { |
| | 169 | $found=false; |
| | 170 | foreach($equivalencies as $equivalency) |
| | 171 | { |
| | 172 | if(array_key_exists($property_name,$equivalency)) |
| | 173 | { |
| | 174 | $name_condition=""; |
| | 175 | foreach($equivalency as $equiv=>$value) |
| | 176 | { |
| | 177 | if($name_condition!="") |
| | 178 | $name_condition.=" or "; |
| | 179 | $name_condition.="translate(Name,$translation_string)='".strtoupper(strtr($equiv,array("'"=>"")))."'"; |
| | 180 | } |
| | 181 | $found=true; |
| | 182 | break; |
| | 183 | } |
| | 184 | } |
| | 185 | if($property_name=="Material Name") |
| | 186 | { |
| | 187 | $ret[$key]=$Matid; |
| | 188 | continue; |
| | 189 | } |
| | 190 | else if($property_name=="Material Categories") |
| | 191 | { |
| | 192 | $categories=array(); |
| | 193 | $queried=$context->query("/MatML_Doc/Material/BulkDetails/Class/Name/text()"); |
| | 194 | foreach($queried as $category) |
| | 195 | { |
| | 196 | $categories[]=$category->nodeValue; |
| | 197 | } |
| | 198 | $queried=$context->query("/MatML_Doc/Material/BulkDetails/Subclass/Name/text()"); |
| | 199 | foreach($queried as $category) |
| | 200 | { |
| | 201 | $categories[]=$category->nodeValue; |
| | 202 | } |
| | 203 | $ret[$key]=$categories; |
| | 204 | continue; |
| | 205 | } |
| | 206 | else if(!$found) |
| | 207 | { |
| | 208 | $name_condition="translate(Name,$translation_string)='".strtoupper($property_name)."'"; |
| | 209 | } |
| | 210 | $details=$context->query("/MatML_Doc/Metadata/PropertyDetails[$name_condition]"); |
| | 211 | $data=new PropertyData; |
| | 212 | if(!$details->length) |
| | 213 | continue; |
| | 214 | foreach($details as $detail) |
| | 215 | { |
| | 216 | $units=$detail->getElementsByTagName("Units"); |
| | 217 | list($scale,$num_unit)=parse_units($units); |
| | 218 | if(!$data->num_unit) |
| | 219 | $data->num_unit=$num_unit; |
| | 220 | else |
| | 221 | { |
| | 222 | force_unit_match($Matid,new Unit("unknown",$num_unit,$scale),$data); |
| | 223 | } |
| | 224 | |
| | 225 | $propid=$detail->getAttribute("id"); |
| | 226 | $property_datas=$context->query("/MatML_Doc/Material/BulkDetails/PropertyData[@property='$propid']/Data"); |
| | 227 | foreach($property_datas as $property_data) |
| | 228 | { |
| | 229 | $data->count++; |
| | 230 | $data->avg_value+=$property_data->nodeValue*$scale; |
| | 231 | } |
| | 232 | } |
| | 233 | if($data->count) |
| | 234 | $data->avg_value/=$data->count; |
| | 235 | $ret[$key]=$data; |
| | 236 | } |
| | 237 | return $ret; |
| | 238 | } |
| | 239 | |
| | 240 | function fractionalize($number) |
| | 241 | { |
| | 242 | $integer=floor($number); |
| | 243 | $decimal=$number-$integer; |
| | 244 | if($decimal<.00000001) |
| | 245 | return array($integer,1); |
| | 246 | if($decimal>.99999999) |
| | 247 | return array($integer+1,1); |
| | 248 | $denominator=fractionalize(1/$decimal); |
| | 249 | $denominator=$denominator[0]; |
| | 250 | return array(round($number*$denominator),$denominator); |
| | 251 | } |
| | 252 | |
| | 253 | function get_factor_power($number,$factor) |
| | 254 | { |
| | 255 | $power=0; |
| | 256 | list($numerator,$denominator)=fractionalize($number); |
| | 257 | if($numerator==0) |
| | 258 | return 0; |
| | 259 | list($fn,$fd)=fractionalize($factor); |
| | 260 | while(($numerator % $fn)==0 && ($denominator % $fd) == 0) |
| | 261 | { |
| | 262 | $numerator/=$fn; |
| | 263 | $denominator/=$fd; |
| | 264 | $power++; |
| | 265 | } |
| | 266 | if($power||$denominator==1) |
| | 267 | return $power; |
| | 268 | while(($numerator % $fd)==0 && ($denominator % $fn) == 0) |
| | 269 | { |
| | 270 | $numerator/=$fd; |
| | 271 | $denominator/=$fn; |
| | 272 | $power--; |
| | 273 | } |
| | 274 | return $power; |
| | 275 | } |
| | 276 | |
| | 277 | function force_unit_match($material,$unit,&$property_data) |
| | 278 | { |
| | 279 | //Convert BTUs per degree F to Joules per degree C |
| | 280 | $BTUs_per_degree_F=2*3*3/(7*7)/17; |
| | 281 | $power=get_factor_power($property_data->num_unit,$BTUs_per_degree_F); |
| | 282 | $property_data->num_unit*=pow($BTUs_per_degree_F,-$power)*pow( |
| | 283 | 2*3*3/(7*7)/11,$power); |
| | 284 | $property_data->avg_value*=pow( |
| | 285 | 1899.1009999999999/1000,$power); |
| | 286 | |
| | 287 | if(!$unit->equals($property_data->num_unit)) |
| | 288 | { |
| | 289 | $power=get_factor_power($property_data->num_unit,13); |
| | 290 | if($unit->equals($property_data->num_unit*pow(13,-$power)*pow(2*3/(7*7),$power))) |
| | 291 | { |
| | 292 | //it uses pound as a force |
| | 293 | $property_data->num_unit*=pow(13,-$power)*pow(2*3/(7*7),$power); |
| | 294 | $property_data->avg_value*=pow(4.44822162*1000,$power); |
| | 295 | return true; |
| | 296 | } |
| | 297 | if($unit->equals($property_data->num_unit*pow(13,-$power)*pow(2,$power))) |
| | 298 | { |
| | 299 | //it uses pound as a mass |
| | 300 | $property_data->num_unit*=pow(13,-$power)*pow(2,$power); |
| | 301 | $property_data->avg_value*=pow(453.59237,$power); |
| | 302 | return true; |
| | 303 | } |
| | 304 | echo "Error: unit mismatch ($unit->name $unit->num_name vs $property_data->num_unit) in $material\n"; |
| | 305 | return false; |
| | 306 | } |
| | 307 | return true; |
| | 308 | } |
| | 309 | |
| | 310 | function db_get_plot_values($plottype, $log, $category_options){ |
| | 311 | global $unit_infos; |
| 74 | | $keysY = array(); |
| 75 | | $keysX = array(); |
| 76 | | $unitsY = array(); |
| 77 | | $unitsX = array(); |
| 78 | | |
| 79 | | |
| 80 | | |
| 81 | | while( $rowx = pg_fetch_array ($Xquery) ){ |
| 82 | | |
| 83 | | //fetching matid, value, units fields |
| 84 | | |
| 85 | | array_push($keysX,$rowx["matid"]); |
| 86 | | array_push($valuesX,$rowx["value"]); |
| 87 | | array_push($unitsX,$rowx["units"]); |
| 88 | | |
| 89 | | } |
| 90 | | |
| 91 | | |
| 92 | | while( $rowy = pg_fetch_array($Yquery) ){ |
| 93 | | |
| 94 | | //fetching matid, value, units fields |
| 95 | | |
| 96 | | array_push($keysY,$rowy["matid"]); |
| 97 | | array_push($valuesY,$rowy["value"]); |
| 98 | | array_push($unitsY,$rowy["units"]); |
| 99 | | } |
| 100 | | |
| 101 | | |
| 102 | | |
| 103 | | // Non very pretty hack for getting the common keys due to the lack |
| 104 | | // of array_intersect_assoc prior PHP <= 4.3 |
| 105 | | // Hope this solves the problem for now, and don't slow down things too much |
| 106 | | |
| 107 | | $dataY = array_combine($keysY, $valuesY); |
| 108 | | $dataX = array_combine($keysX, $valuesX); |
| 109 | | |
| 110 | | $uniY = array_combine($keysY, $unitsY); |
| 111 | | $uniX = array_combine($keysX, $unitsX); |
| 112 | | |
| 113 | | |
| 114 | | $valuesX = array(); |
| 115 | | $valuesY = array(); |
| 116 | | $unitsX = array(); |
| 117 | | $unitsY = array(); |
| 118 | | |
| 119 | | |
| 120 | | $common_keys = array_intersect($keysX, $keysY); |
| 121 | | |
| 122 | | // for (i in array resultante) { $valores[$llaves[$i]] } |
| 123 | | |
| 124 | | foreach($common_keys as $key){ |
| 125 | | |
| 126 | | array_push($valuesY,$dataY[$key]); |
| 127 | | array_push($valuesX,$dataX[$key]); |
| 128 | | |
| 129 | | array_push($unitsY,$uniY[$key]); |
| 130 | | array_push($unitsX,$uniX[$key]); |
| 131 | | |
| 132 | | |
| 133 | | } |
| 134 | | |
| 135 | | } |
| 136 | | |
| 137 | | |
| 138 | | if($extra){ |
| 139 | | |
| 141 | | $keyse = array(); |
| 142 | | $unitse = array(); |
| 143 | | |
| 144 | | while($rowextra = pg_fetch_array ($extra) ){ |
| 145 | | //fetching matid, value, units fields |
| 146 | | |
| 147 | | array_push($keyse,$rowextra["matid"]); |
| 148 | | array_push($valuese,$rowextra["value"]); |
| 149 | | array_push($unitse,$rowextra["units"]); |
| 150 | | } |
| 151 | | |
| 152 | | |
| 153 | | $datae = array_combine($keyse, $valuese); |
| 154 | | $unie = array_combine($keyse, $unitse); |
| 155 | | |
| 156 | | $valuese = array(); |
| 157 | | $unitse = array(); |
| 158 | | |
| 159 | | $common_keys = array_intersect($common_keys, $keyse); |
| 160 | | |
| 161 | | |
| 162 | | // for (i in array resultante) { $valores[$llaves[$i]] } |
| 163 | | |
| 164 | | foreach($common_keys as $key){ |
| 165 | | |
| 166 | | array_push($valuesY,$dataY[$key]); |
| 167 | | array_push($valuesX,$dataX[$key]); |
| 168 | | array_push($valuese,$datae[$key]); |
| 169 | | |
| 170 | | array_push($unitsY,$uniY[$key]); |
| 171 | | array_push($unitsX,$uniX[$key]); |
| 172 | | array_push($unitse,$unie[$key]); |
| 173 | | } |
| 174 | | } |
| | 383 | $categories = array(); |
| | 384 | |
| | 385 | $handle = opendir('materials'); |
| | 386 | |
| | 387 | while (false !== ($file = readdir($handle))) { |
| | 388 | if(preg_match("/\.xml?$/",$file)==1) |
| | 389 | { |
| | 390 | // Fetch values from DB and sort them in an array (actually 3 arrays) |
| | 391 | $values=get_property_data("materials/$file",$query); |
| | 392 | |
| | 393 | if(isset($values["Xquery"])&& |
| | 394 | isset($values["Yquery"]) |
| | 395 | ) |
| | 396 | { |
| | 397 | $values_categories=$values["categories"]; |
| | 398 | $values_categories[]=""; |
| | 399 | foreach($values_categories as $category) |
| | 400 | { |
| | 401 | if(isset($category_options[$category])) |
| | 402 | break; |
| | 403 | } |
| | 404 | if(!isset($category_options[$category])) |
| | 405 | continue; |
| | 406 | $category=$category_options[$category]; |
| | 407 | force_unit_match($values["Matid"],$final_units["Xquery"],$values["Xquery"]); |
| | 408 | force_unit_match($values["Matid"],$final_units["Yquery"],$values["Yquery"]); |
| | 409 | if(isset($query["extra"])) |
| | 410 | { |
| | 411 | if(isset($values["extra"])) |
| | 412 | { |
| | 413 | force_unit_match($values["Matid"],$final_units["extra"],$values["extra"]); |
| | 414 | $valuesX[]=($values["Xquery"]->avg_value/$final_units["Xquery"]->scale); |
| | 415 | $valuesY[]=($values["Yquery"]->avg_value/$final_units["Yquery"]->scale); |
| | 416 | $valuese[]=$values["extra"]->avg_value/$final_units["extra"]->scale; |
| | 417 | $names[]=$values["Matid"]; |
| | 418 | $categories[]=$category; |
| | 419 | } |
| | 420 | } |
| | 421 | else |
| | 422 | { |
| | 423 | $valuesX[]=($values["Xquery"]->avg_value/$final_units["Xquery"]->scale); |
| | 424 | $valuesY[]=($values["Yquery"]->avg_value/$final_units["Yquery"]->scale); |
| | 425 | $names[]=$values["Matid"]; |
| | 426 | $categories[]=$category; |
| | 427 | } |
| | 428 | } |
| | 429 | } |
| | 430 | } |
| | 431 | closedir($handle); |