#!c:\perl\bin\perl.exe; #Calculating Information about chemical baths #Author = David Daycock #Date = April, 2004 # Revision 1.0.3 added IPA and is fixing a problem with undefing some of the variables. #First, lets initilize the hashs we will need and fill them with data... %density = ( DI => "1", HF => "1.16", NH4OH => "0.9", H202 => "1.11", HCl => "1.18", H2SO4 => "1.84", Citric_Acid => "1", NH4F => "1.01", TMAH => "1", NITRIC => "1.415", ACETIC => "1.05", PHOS => "1.7", IPA => "0.79" ); %concentration =( DI => "1", HF => "0.49", NH4OH => "0.29", H202 => "0.3", HCl => "0.37", H2SO4 => ".98", Citric_Acid => "0.5", NH4F => "0.4", TMAH => "0.25", NITRIC => "0.7", ACETIC => "0.8", PHOS => "0.85", IPA => "1.0" ); %because = ( 1 => "DI", 2 => "HF", 3 => "NH4OH", 4 => "H202", 5 => "HCl", 6 => "H2SO4", 7 => "Citric_Acid", 8 => "NH4F", 9 => "TMAH", 10 => "NITRIC", 11 => "ACETIC", 12 => "PHOS", 13 => "IPA", 14 => "Other" ); #Overall Script Flow below: &INTRODUCTION; $true = 0; &CHOICE; sub INTRODUCTION { # print("\n\nChemical Bath Calculation Utility\n"); print <; chomp($where); if ($where == 1) { &MAKE_BATH; } elsif ($where == 2) { &RATIO; } elsif ($where == 4) { &QUIT; } elsif ($where == 3) { &ASSUME; } else { print ("\nThat is not an option...try again.\n"); &CHOICE; }; } sub MAKE_BATH #subroutine to calculate how much chemical to pour into a bath... { print <; chomp($solvent); print("\nWhat is the final concentration of each chemical you desire \(wt \%\):\n"); $vDI = 0; foreach (@choices) { if ($_ != $solvent) { while ($testSix !=1) # this will make sure that a numerical answer is given { print("\n$because{$_} \(in \%\): "); $answer2 = ; chomp($answer2); if ($answer2 =~ m/\d/ || $answer2 =~ m/\d/){$testSix = 1;} # searching for a numererical answer } $answer2 = $answer2 / 100; $testSix = 0; $c2i{$_} = $answer2; } } $percentsolvent = 1; foreach (@choices) { $foo = $because{$_}; $percentsolvent = $percentsolvent - ($c2i{$_} / $concentration{$foo}); } foreach (@choices) { $chemName3 = $because{$_}; $estdensity = $estdensity + (($c2i{$_} / $concentration{$chemName3}) * $density{$chemName3}); } $solvName = $because{$solvent}; $estdensity = $estdensity + ($percentsolvent * $density{$solvName}); # print("\nestdensity = $estdensity\n"); $volSolv = $totalsize; foreach (@choices) { if ($_ != $solvent) { $chemName2 = $because{$_}; $concRatio = $c2i{$_} / $concentration{$chemName2}; $totalmass = $totalsize * $estdensity; $mass{$_} = $concRatio * $totalmass; $volume{$_} = $mass{$_} / $density{$chemName2}; $volSolv = $volSolv - $volume{$_}; } } print("\nHere are the volumes you need to fill:\n\n"); foreach (@choices) { if ($_ != $solvent) { $volume{$_} = $volume{$_}; # $volume{$_} = $volume{$_} * 100; # $volume{$_} = int($volume{$_}); # $volume{$_} = $volume{$_} / 100; print("$because{$_}:\t\t$volume{$_} L\n"); } } # $volSolv = $volSolv * 100; # $volSolv = int($volSolv); # $volSolv = $volSolv / 100; print("$because{$solvent}:\t\t$volSolv L\n"); &ASK; } sub RATIO { print <; chomp($mORv); if ($mORv =~ m/[mM]/ || $mORv =~ m/[vV]/){$testOne = 1;} #lets make sure we have m's and v's as answers... }; print("\nPlease provide the ratio of the chemicals you have selected: \n"); foreach (@choices) { while ($testTwo !=1) # this will make sure that a numerical answer is given { print("\n$because{$_}: "); $answer = ; chomp($answer); if ($answer =~ m/\d/ || $answer =~ m/\d/){$testTwo = 1;} # searching for a numererical answer } $testTwo = 0; # need to reset the test or else goodbye! $ratio{$_} = $answer; #Put all answers into the ratio hash for later use $chemName = $because{$_}; if ($mORv eq "v" || $mORv eq "V") #i like to work in weight percent so this does the ratio conversion { $ratio{$_} = $ratio{$_} * $density{$chemName}; } $wtPureAmt{$_} = $ratio{$_} * $concentration{$chemName}; $A = $ratio{$_}; $B = 1 - $concentration{$chemName}; $C = $A * $B; $residualWater = $residualWater + $C; #becasue most chemicals have water in them, lets add the residual water together } $sumTotal = 0; foreach (@choices) { $sumTotal = $sumTotal + $ratio{$_}; #calculate the total mass of chemical in the bath } print("\nHere are the weight percents:\n\n"); foreach (@choices) { if ($_ == 1) { $wtPureAmt{$_} = $wtPureAmt{$_} + $residualWater; } $chemName2 = $because{$_}; $wtPercent{$_} = $wtPureAmt{$_} / $sumTotal; # print("WtPercent = $wtPercent{$_}\n"); # print("PureAmt = $wtPureAmt{$_}\n"); #print $sumTotal; $percent = $wtPercent{$_}*100000; #these next three lines are for displaying to two sig figs, since I don't know another way to do this... $percent = int($percent); $percent = $percent / 1000; print("$because{$_}:\t\t$percent \%\n"); # $volPercent{$_} = $wtPercent{$_} / } $DI_PRESENT = 0; foreach (@choices) { if ($_ == 1) {$DI_PRESENT = 1}; } if ($residualWater != 0 && $DI_PRESENT != 1) { #I want to display the residual water if I haven't already accounted for it in the DI water answer $DI_Percent = $residualWater / $sumTotal; $DI_Percent = $DI_Percent * 10000; $DI_Percent = int($DI_Percent); $DI_Percent = $DI_Percent / 100; print("Water:\t\t$DI_Percent\%\n"); } &ASK; } sub QUIT #This is for GOODBYE and GOOD RIDANCE! {print("\nHope this helped! See you next time...\n");} sub sort_by_number #This is for sorting the hashes / arrays in the script in numerical order... { if ($a < $b) { return -1; } elsif ($a == $b) { return 0; } else { return 1; } } sub BASIC_QUESTIONS { if ($true == 0) { while ($fourTest != 1) { print("\nWhat is the size of your bath in liters: "); $totalsize = ; chomp($totalsize); if ($totalsize =~ m/\d/) {$fourTest = 1;} } } print ("\nHow many chemicals in the mixture (include DI as a chemical): "); $numChems = ; chomp($numChems); # Now we need to find which chemicals are going to be in the bath... print("\n\n\tCHEMICAL LIST\n"); foreach (sort sort_by_number keys(%because)) { print("$_\.\t$because{$_}\n"); }; $selection = 1; #this section puts all the chemicals into the @choices array for ($i = 0; $i < $numChems; $i++) { print("\nChemical #$selection: "); $selection++; while ($fiveTest != 1) { $chemchoice = ; chomp($chemchoice); if ($chemchoice =~ m/\d/) {$fiveTest = 1;} if ($fiveTest == 0){print ("I need a numerical answer: ");} } $fiveTest = 0; push (@choices, $chemchoice); }; #if we don't have the chemical already, we need to add them to the existing hash $inc = 0; foreach(@choices) { if ($_ == 14) { #no error checking in here, if you are adding your own stuff, you better be sure it is right! print("\nChemical Name: "); $othName = ; chomp($othName); print ("\nChemical Concentration of Pure Material\(in percent\): "); $othConc = ; chomp($othConc); $othConc = $othConc * 0.01; print ("\nChemical Density of Pure Material(g/mL): "); $othDen = ; chomp($othDen); $density{$othName} = $othDen; $concentration{$othName} = $othConc; $othIndex = 14 + $inc; $because{$othIndex} = "$othName"; $choices[$inc] = $othIndex; } $inc++; } } sub ASK { print("\nAre you finished (y or n): "); $quitornot = ; chomp($quitornot); undef (@choices); undef ($fiveTest); undef ($fourTest); undef ($DI_PRESENT); undef ($true); undef ($vDI); undef ($testSix); undef ($testOne); undef ($testTwo); undef ($residualWater); undef ($volSolve); undef ($c2i); undef ($percentsolvent); undef ($foo); undef (@ratio); undef (@wtPercent); undef (@wtPureAmt); undef ($estdensity); if ($quitornot eq "y" || $quitornot eq "Y") { &QUIT; } elsif ($quitornot eq "n" || $quitornot eq "N") { &CHOICE; } else { print ("\nThat is not an option...try again.\n"); &ASK; }; } sub ASSUME { print("\n\nHere are the assumptions used in the calculations:\n"); print("\n\nThe following densities were used \(g/mL\):\n"); foreach (sort keys(%density)) { print("$_\t$density{$_}\n"); } print("\n\nThe following concentrations were used:\n"); foreach (sort keys(%concentration)) { print("$_\t$concentration{$_}\n"); } print("\nWhen calculating the amount of material to pour up a bath, the density of the entire solution is assumed to be the mass percent weighted average of the components."); &ASK; }