Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: BathCalc

by SciDude (Friar)
on May 30, 2004 at 04:43 UTC ( #357558=note: print w/ replies, xml ) Need Help??


in reply to BathCalc

Finally!

Some real work is being done with perl!

Unfortunately, this code has some problems. See my partial log here for a 100L bath:

4. H202 2. HF 1. DI What is the solvent: 1 What is the final concentration of each chemical you desire (wt %): H202 (in %): 30 HF (in %): 10 Here are the volumes you need to fill: H202: 102.94 L HF: 20.1 L DI: -23.04 L

I suggest examination of this code carefully before mixing chemicals via this calculation. The effort is well appreciated however.

One more suggestion: Use IUPAC names please. I once ordered "IPA" from a major chemical manufacturer only to receive isopropyl acetate instead of the isopropanol delivery expected.

SciDude


Comment on Re: BathCalc
Download Code
Re^2: BathCalc
by ~~David~~ (Hermit) on Jun 03, 2004 at 13:51 UTC
    Thank you for your reply. I've fixed the problem. I have the updated code at work and will post the fixed code when I get a chance. I forgot to undef a variable that I was incrementing, resulting in a problem... Sorry and I will update ASAP. Thanks again.
Re^2: BathCalc
by ~~David~~ (Hermit) on Jun 04, 2004 at 20:35 UTC
    Actually, to make another reply... The reason you are getting the negative number is because peroxide is only sold industrially as a 30% solution. Therefore, it is impossible to add HF (which comes as a 49% solution max) to any amount of peroxide, and get a 30% solution. If you look at the assumptions made in the program, you can see the concentrations that were assumed. If you have access to a higher concentration of peroxide, than you will have to add it as an "Other" or modify the code. Thanks again, David
Re^2: BathCalc
by ~~David~~ (Hermit) on Jun 07, 2004 at 03:09 UTC
    Here is the updated code with some fixed problems...
    #!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 <<DONE ****************************************************** * * * * * Chemical Bath Calculation Utility * * ver. 1.0.3 * * * * by: * * David Daycock * * * * * * * ****************************************************** DONE } sub CHOICE { print("\nMake a Selection: \n"); print("1. Calculate amount of material to make the bath.\n"); print("2. Convert chemical ratio to mass percent.\n"); print("3. Display a list of assumptions used in the calculatio +ns.\n"); print("4. Quit\n\n"); print("Choice: "); $where = <STDIN>; 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 pou +r into a bath... { print <<DONE ******************************************************************* * * * This Utility will give you an estimate of the amount of * * material you need to pour into your bath to achieve * * a desired weight percent based concentration * * * * * ******************************************************************* + DONE ;&BASIC_QUESTIONS; print("\n\n"); foreach (@choices) { print("$_\. $because{$_}\n"); } print ("\nWhat is the solvent: "); $solvent = <STDIN>; chomp($solvent); print("\nWhat is the final concentration of each chemical you desi +re \(wt \%\):\n"); $vDI = 0; foreach (@choices) { if ($_ != $solvent) { while ($testSix !=1) # this will make sure that a numeri +cal answer is given { print("\n$because{$_} \(in \%\): "); $answer2 = <STDIN>; 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{$chemN +ame3}) * $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 <<DONE ******************************************************************* * * * This Utility will give you an estimate of the concentration * * of material in your bath given a known pour up ratio. * * * * * ******************************************************************* + DONE ;$true = 1; #this is for the BASIC_QUESTION sub, to know whethe +r or not to ask for total volume... &BASIC_QUESTIONS; # At this point, we have all the chemical choices in the @choices +array using the keys of %because # Now we need to take the chemicals we have and find out the ratio + of chemicals in the final bath... while ($testOne != 1){ print("\nAre you using mass or volume ratios (m or v): "); $mORv = <STDIN>; chomp($mORv); if ($mORv =~ m/[mM]/ || $mORv =~ m/[vV]/){$testOne = 1;} #lets m +ake sure we have m's and v's as answers... }; print("\nPlease provide the ratio of the chemicals you have select +ed: \n"); foreach (@choices) { while ($testTwo !=1) # this will make sure that a numerical +answer is given { print("\n$because{$_}: "); $answer = <STDIN>; 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 a +re 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 displa +y 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 s +cript 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 = <STDIN>; chomp($totalsize); if ($totalsize =~ m/\d/) {$fourTest = 1;} } } print ("\nHow many chemicals in the mixture (include DI as a chemi +cal): "); $numChems = <STDIN>; chomp($numChems); # Now we need to find which chemicals are going to be in t +he 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 = <STDIN>; 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 = <STDIN>; chomp($othName); print ("\nChemical Concentration of Pure M +aterial\(in percent\): "); $othConc = <STDIN>; chomp($othConc); $othConc = $othConc * 0.01; print ("\nChemical Density of Pure Materia +l(g/mL): "); $othDen = <STDIN>; 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 = <STDIN>; 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 percen +t weighted average of the components."); &ASK; }

      deleted someone took offense to the bad humor.


      SciDude
      The first dog barks... all other dogs bark at the first dog.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://357558]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2015-07-06 19:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (81 votes), past polls