{
pH.vec();//Make pH a column vector.
pH.show("pH");
DblMatrix H = DblMatrix.pow(10,pH.times(-1));
//Set Names = this.bufferMap.keySet();
String[] Names = this.bufferPredictor.getComponents();
int num_bufs = Names.length; //Number of buffers in this solution.
HashMap OUT = new HashMap();
// DblMatrix[] OUT;
// if (do_partial)
// {
// OUT = new DblMatrix[num_bufs];
// for (int j=0;j<num_bufs;j++)
// {
// OUT[j] = new DblMatrix(H.getN());
// }
// }
// else
// {
// OUT = new DblMatrix[1];
// OUT[0] = new DblMatrix(H.getN());
// }
//
//Iterator iter = Names.iterator();
String buffname;
//BufferDOM buffer;
ComplexBufferComponent buffer;
DblMatrix C;
DblMatrix IonicStr=new DblMatrix(H.getN());
HashMap Coefs;
DblMatrix[] KKa;
DblMatrix[] KKb;
DblMatrix[] SSaltType;
DblMatrix Kw;
BufferPredictor complex;
//Calculate ion product constant of water Kw.
//buffer = (BufferDOM)this.bufferMap.get("com.mockturtlesolutions.water");
buffer = this.bufferPredictor.getComponent("com.mockturtlesolutions.water");
if (buffer == null)
{
throw new IllegalArgumentException("Water must be a component of the solution.");
}
//Coefs = getAverageKa(buffer);
Coefs = this.bufferPredictor.getEstimatedKa("com.mockturtlesolutions.water");
//C = (DblMatrix)this.concentrationMap.get("com.mockturtlesolutions.water");
C = this.bufferPredictor.getEstimatedConcentration("com.mockturtlesolutions.water");
KKa = (DblMatrix[])Coefs.get("Ka");
Kw = KKa[0].times(C);
///
//while (iter.hasNext()) //For each named buffer in the solution...
for (int j=0;j<Names.length;j++)
{
IonicStr=new DblMatrix(H.getN());
System.out.println("Right Here");
//buffname = (String)iter.next();
buffname = Names[j];
buffer = this.bufferPredictor.getComponent(buffname);
//buffer = (BufferDOM)this.bufferMap.get(buffname);
//C = (DblMatrix)this.concentrationMap.get(buffname);
C = this.bufferPredictor.getEstimatedConcentration(buffname);
complex = buffer.getSmooth();
if (complex != null) //This is a COMPLEX buffer.
{
System.out.println("This is a complex buffer!:");
// if (do_partial)
// {
//
// OUT.put(buffname,C*feval(SOL(i).Func.EvalFun,SOL(i).Func.Smooth,pH,25,SOL(i).Contains));
// }
// else
// {
// OUT = OUT + SOL(i).Conc*feval(SOL(i).Func.EvalFun,SOL(i).Func.Smooth,pH,25,SOL(i).Contains);
// }
}
else
{
//Coefs = getAverageKa(buffer);
Coefs = this.bufferPredictor.getEstimatedKa(buffname);
KKa = (DblMatrix[])Coefs.get("Ka");
KKb = (DblMatrix[])Coefs.get("Kb");
SSaltType = (DblMatrix[])Coefs.get("SaltType");
if (buffname.endsWith(".water"))
{
IonicStr = Kw.divideBy(H).plus(H);
}
else
{
int num_pka,num_pkb,N,n;
DblMatrix Ka;
DblMatrix Numerator;
DblMatrix Denominator;
DblMatrix IonicStrtemp;
for (int k=0;k<KKa.length;k++) //For each ion...
{
Numerator = new DblMatrix(H.getN());
Denominator = new DblMatrix(H.getN());
num_pka = KKa[k].getN();
num_pkb = KKb[k].getN();
if ((num_pka == 0) && (num_pkb ==0))
{
if (SSaltType[k].getN() == 1)
{
//System.out.println("Electrolyte"+k);
//SSaltType[k].show("SaltType[k]");
IonicStrtemp = SSaltType[k].pow(2);
}
else
{
if (SSaltType[k].getN() > 1)
{
throw new RuntimeException("SaltType of an electrolyte can not consist of more than one integer value.");
}
else
{
System.out.println("Inert thing"+k);
//Would be something completely inert like glucose.
IonicStrtemp = new DblMatrix(0.0);
}
}
}
else
{
N = num_pka + num_pkb;
n = num_pka;
Ka = KKa[k];
Ka.show("Ka");
if (num_pkb > 0)
{
DblMatrix transKb = Kw.divideBy(KKb[k]);
Ka = Ka.concat(transKb,2);
}
DblSort Sort = new DblSort(Ka,2);
Sort.sort();
Ka = Sort.getSorted();
//Calculate numerator
DblMatrix cumProdKa = Ka.cumProd(1);
cumProdKa = cumProdKa.fliplr();
DblMatrix temp;
Ka.show("Final Ka");
for (int i=1;i<=N;i++)
{
temp = H.pow(i-1).times(cumProdKa.getDoubleAt(i-1));
Denominator = Denominator.plus(temp);
temp = temp.times(n+1-i);
temp = temp.times(n+1-i);
Numerator = Numerator.plus(temp);
}
//Denominator gets one more term than numerator...
Denominator = Denominator.plus(H.pow(N));
Denominator.show("Denominator");
IonicStrtemp = Numerator.divideBy(Denominator);
}
IonicStr = IonicStr.plus(C.times(IonicStrtemp));
}
}
}
IonicStr = IonicStr.times(0.5);
IonicStr.show(buffname);
//Enter latest IonicStr calculation into the results for output.
if (do_partial)
{
OUT.put(buffname,IonicStr);
}
if (OUT.containsKey("total"))
{
OUT.put("total",IonicStr.plus((DblMatrix)OUT.get("total")));
}
else
{
OUT.put("total",IonicStr);
}
}
//Put in the contribution to ionic strength from the titrant!
//Enter latest IonicStr calculation into the results for output.
//DefaultCbAlgorithm CbAlg = new DefaultCbAlgorithm(this.bufferMap,this.concentrationMap);
DefaultCbAlgorithm CbAlg = new DefaultCbAlgorithm(this.bufferPredictor);
HashMap titrant = CbAlg.Cb(pH);
DblMatrix Cb = (DblMatrix)titrant.get("total");
IonicStr =DblMatrix.abs(Cb).times(N_titrant);
//Note that this is really N_titrant^(2)*Cb/N_titrant simplified!!
IonicStr = IonicStr.times(0.5);
if (do_partial)