/*
*   Classes Donnan
*
*   Class Donnan contains the primary methods for
*   calculating a Donnan Potential between two
*   partitions of different electrical permittivity
*   (dielectric constant) between which any number of
*   ionic species may be partitioned and, in one
*   partition only, may bind to a neutral ionophore.
*
*   Class Donnan requires Class DonnanMinim and Class DonnanConcn
*   that implement interfaces to the required minimisation methods
*
*   WRITTEN BY: Michael Thomas Flanagan
*
*   DATE:           November 2004
*   LAST UPDATE:    1 December 2004, 5-7 July 2008
*
*   DOCUMENTATION:
*   See Michael Thomas Flanagan's JAVA library on-line web page:
*   http://www.ee.ucl.ac.uk/~mflanaga/java/Donnan.html
*   http://www.ee.ucl.ac.uk/~mflanaga/java/
*
*   Copyright (c) November 2004
*
*   PERMISSION TO COPY:
*   Permission to use, copy and modify this software and its documentation for
*   NON-COMMERCIAL purposes is granted, without fee, provided that an acknowledgement
*   to the author, Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies.
*
*   Dr Michael Thomas Flanagan makes no representations about the suitability
*   or fitness of the software for any or for a particular purpose.
*   Michael Thomas Flanagan shall not be liable for any damages suffered
*   as a result of using, modifying or distributing this software or its derivatives.
*
***************************************************************************************/

package flanagan.physchem;

import java.util.ArrayList;
import flanagan.physprop.IonicRadii;
import flanagan.io.*;
import flanagan.math.*;
import javax.swing.JOptionPane;

public class Donnan{


    private ArrayList arrayl = new ArrayList();      //  vector holding:
                                            //  element 0:  name of first ion
                                            //  element 1:  initial concentration of first ion in partition A (enter as M)
                                            //  element 2:  initial concentration of first ion in partition B (enter as M)
                                            //  element 3:  association constant with ionophore (enter as M^-1)
                                            //  element 4:  radius of first ion (metres)
                                            //  element 5:  charge of first ion
                                            //  element 6:  partition coefficient if provided by the user
                                            //              -1 if not; partition coefficient to be calculated later
                                            //  element 7:  name of second ion
                                            //  etc
    private int numOfIons = 0;              //  number of ionic species
    private int numOfAnions = 0;            //  number of anionic species
    private int numOfCations = 0;           //  number of cationic species
    private String[] ionNames = null;       //  names of the ions
    private double[] concnA = null;         //  concentration of ions in partition A (moles per cubic metre)
    private double[] concnB = null;         //  concentration of ions in partition B (moles per cubic metre)
    private double[] molesT = null;         //  total moles of an ion
    private double[] complex = null;        //  concentration of complex in partition B (moles per cubic metre)
    private double[] excessConcnA = null;   //  excess concentration of ions in partition A in interfacial charge (moles per cubic metre)
    private double[] excessConcnB = null;   //  excess concentration of ions in partition B in interfacial charge (moles per cubic metre)
    private double[] excessComplex = null;  //  excess concentration of complex in partition B in interfacial charge (moles per cubic metre)
    private int[] indexC = null;            //  ion index of ionic species with non zero total concentrations
    private int nonZeroConcns = 0;          //  number of ionic species with a non-zero total concentration
    private double[] assocConsts = null;    //  association constants of the ions with the ionophore (cubic metres per mole)
    private int[] indexK = null;            //  ion index of ionic species with non zero assocConsts
    private int nonZeroAssocK = 0;          //  number of ionic species with affinity for the ionophore
    private double[] radii = null;          //  radii of ions
    private boolean radiusType = true;      //  if = true - hydrated radii are taken from Class IonicRadii
                                            //  if = false - bare radii are taken from Class IonicRadii
    private double[] charges = null;        //  charge of ions
    private double tol = 1e-6;              //  fractional tolerance in checking for overall charge neutrality
                                            //  when multiplied by total concentration of species of lowest concentration
                                            //  gives limit for considering overall neurtality achieved
    private String ionophore = "ionophore"; //  name ionophore for output texts
    private double ionophoreConcn = 0.0D;   //  ionophore concentration - entered as molar, converted to moles per cubic metre
    private double freeIonophoreConcn = 0.0D;   // ionophore concentration - all complex concentrations
    private double ionophoreRad = 0.0D;     //  ionophore radius (metres)
    private boolean ionophoreSet = false;   //  = true when ionophore concentration and radius have been set
    private double volumeA = 0.0D;          //  volume of partition A (cubic metres)
    private double volumeB = 0.0D;          //  volume of partition B (cubic metres)
    private double interfacialArea = 0.0D;  //  onterfacial area between Partitions A and B(square metres)
    private boolean volumeSet = false;      //  = true when volumeA and volumeB have been set
    private double epsilonA = 0.0D;         //  relative electrical permittivity of partition A
    private double epsilonB = 0.0D;         //  relative electrical permittivity of partition B
    private double epsilonSternA = 0.0D;    //  relative electrical permittivity of partition A Stern layer
    private double epsilonSternB = 0.0D;    //  relative electrical permittivity of partition B SternLayer
    private boolean epsilonSet = false;     //  = true when epsilonA and epsilonB have been set
    private double temp = 25.0-Fmath.T_ABS; //  Temperature (degrees Kelvin) [Enter temperature in degrees Celsius]
    private boolean tempSet = false;        //  = true when temperature has been set
    private double[] deltaMu0 = null;       //  difference in Born charging energy (partitionB - partitionA)
    private double[] partCoeff = null;      //  partition coefficients in absence of inter-partition, e.g. Donnan, potential
    private double[] partCoeffPot = null;   //  partition coefficients in presence of inter-partition, e.g. Donnan, potential
    private boolean[] indexPC = null;       //  partition coefficient (pc) index
                                            //  = true if pc provided by user
                                            //  = false if pc calculated from Born charging
    private double donnanPotential = 0.0D;  //  Donanan potential with respect to partition A (psiB - psiA) [volts]
    private double diffPotentialA = 0.0D;   //  Double layer potential difference - compartment A [volts]
    private double diffPotentialB = 0.0D;   //  Double layer potential difference - compartment B [volts]
    private double sternPotential = 0.0D;   //  Stern potential difference  [volts]
    private double estimate = 0.0D;         //  Initial Estimate of the Donanan potential with respect to partition A (psiB - psiA) [volts]
    private double step = 0.0D;             //  Initial step size in the estimation of the Donanan potential with respect to partition A (psiB - psiA) [volts]
    private double tolerance = 1.0e-20;     //  tolerance in exiting estimation of the Donanan potential with respect to partition A (psiB - psiA)
    private int nMaxIter = 10000;           //  maximum number of iterations allowed in estimation of the Donanan potential with respect to partition A (psiB - psiA)
    private int numIterations = 0;          //  number of iterations taken
    private double minimum = 1.0e300;       //  value of function to be minimised at the minimum
    private double sternCap = 0.0D;         //  Stern layer capacitance [F]
    private double diffCapA = 0.0D;         //  diffuse double layer capacitance in partition A [F]
    private double diffCapB = 0.0D;         //  diffuse double layer capacitance in partition B [F]
    private double donnanCap = 0.0D;        //  total interfacial capacitance [F]
    private double sternDeltaA = 0.0D;      //  Stern layer thickness in partition A [m]
    private double sternDeltaB = 0.0D;      //  Stern layer thickness in partition B [m]
    private double chargeValue = 0;         //  Absolute value of the charge valency if all are the same, i.e. symmetric electrolytes of the same valency
    private boolean chargeSame = true;      //  = false if chargeValue not the same for all ions
    private double interfacialChargeDensity = 0.0D;// interface Charge Density [C per square metre]
    private double interfacialCharge = 0.0D;       // interface Charge [C]
    private boolean includeIc = true;       //  = true - interface charge included in the calculation of the Donnan potential
                                            //  = false - interface charge ignored in the calculation of the Donnan Poptential
    private double[] ratioA = null;         //  ratio of excess to bulk concentrations - compartment A
    private double[] ratioB = null;         //  ratio of excess to bulk concentrations - compartment B
    private double[] ratioC = null;         //  ratio of excess to bulk concentrations - complex
    private double recipKappaA = 0.0D;      //  Debye length - compartment A
    private double recipKappaB = 0.0D;      //  Debye length - compartment B

    // Constructor
    public Donnan(){
    }

    // Method for setting ionic radii taken from Class IonicRadii to hydrated radii
    // This is the default option
    public void setHydratedRadii(){
        this.radiusType = true;
    }

    // Method for setting ionic radii taken from Class IonicRadii to bare radii
    public void setBareRadii(){
        this.radiusType = false;
    }

    // Method for setting Donnan Potential calculatiom method to ignore interfacial charge
    public void ignoreInterfaceCharge(){
        this.includeIc = false;
    }

     // Method for setting Donnan Potential calculatiom method to include interfacial charge
     // This is the default option
    public void includeInterfaceCharge(){
        this.includeIc = true;
    }


    // Method to add an ionic species to the  Donnan Equilibrium
    // Partition coefficient to be calculated from Born charging equation
    // Concentrations - Molar,  assocK - M^-1, radius - metres, charge - valency e.g. +1
    public void setIon(String ion, double concnA, double concnB, double assocK, double radius, int charge){
        this.arrayl.add(ion);
        this.arrayl.add(new Double(concnA));
        this.arrayl.add(new Double(concnB));
        if(concnA>0.0D || concnB>0.0)this.nonZeroConcns++;
        this.arrayl.add(new Double(assocK));
        if(assocK!=0.0D)this.nonZeroAssocK++;
        this.arrayl.add(new Double(radius));
        this.arrayl.add(new Integer(charge));
        this.arrayl.add(new Double(-1.0D));
        this.numOfIons++;
    }

    // Method to add an ionic species to the  Donnan Equilibrium
    // Partition coefficient supplied by user
    // Concentrations - Molar,  assocK - M^-1, radius - metres, charge - valency e.g. +1
    public void setIon(double partCoeff, String ion, double concnA, double concnB, double assocK, double radius, int charge){
        this.arrayl.add(ion);
        this.arrayl.add(new Double(concnA));
        this.arrayl.add(new Double(concnB));
        if(concnA>0.0D || concnB>0.0)this.nonZeroConcns++;
        this.arrayl.add(new Double(assocK));
        if(assocK!=0.0D)this.nonZeroAssocK++;
        this.arrayl.add(new Double(radius));
        this.arrayl.add(new Integer(charge));
        this.arrayl.add(new Double(partCoeff));
        this.numOfIons++;
    }

    // Method to add an ionic species to the  Donnan Equilibrium
    // Partition coefficient to be calculated from Born charging equation
    // Concentrations - Molar,  radius - metres, charge - valency e.g. +1
    // association constant = 0.0D
    public void setIon(String ion, double concnA, double concnB, double radius, int charge){
        this.arrayl.add(ion);
        this.arrayl.add(new Double(concnA));
        this.arrayl.add(new Double(concnB));
        if(concnA>0.0D || concnB>0.0)this.nonZeroConcns++;
        this.arrayl.add(new Double(0.0D));
        this.arrayl.add(new Double(radius));
        this.arrayl.add(new Integer(charge));
        this.arrayl.add(new Double(-1.0D));
        this.numOfIons++;
    }

    // Method to add an ionic species to the  Donnan Equilibrium
    // Partition coefficient to be supplied by user
    // Concentrations - Molar,  radius - metres, charge - valency e.g. +1
    // association constant = 0.0D
    public void setIon(double partCoeff, String ion, double concnA, double concnB, double radius, int charge){
        this.arrayl.add(ion);
        this.arrayl.add(new Double(concnA));
        this.arrayl.add(new Double(concnB));
        if(concnA>0.0D || concnB>0.0)this.nonZeroConcns++;
        this.arrayl.add(new Double(0.0D));
        this.arrayl.add(new Double(radius));
        this.arrayl.add(new Integer(charge));
        this.arrayl.add(new Double (partCoeff));
        this.numOfIons++;
    }

    // Method to add an ionic species to the  Donnan Equilibrium
    // Partition coefficient to be calculated from Born charging equation
    // default radii and charge taken from class IonicRadii
    // if radii not in Ionic Radii, Donnan potential calculated with interface charge neglected
    // Concentrations - Molar
    public void setIon(String ion, double concnA, double concnB, double assocK){
        IonicRadii ir = new IonicRadii();
        this.arrayl.add(ion);
        this.arrayl.add(new Double(concnA));
        this.arrayl.add(new Double(concnB));
        if(concnA>0.0D || concnB>0.0)this.nonZeroConcns++;
        this.arrayl.add(new Double(assocK));
        if(assocK!=0.0D)this.nonZeroAssocK++;
        double rad = 0.0D;
        if(this.radiusType){
            rad = ir.hydratedRadius(ion);
        }
        else{
            rad = ir.radius(ion);
        }

        if(rad==0.0D){
            String mess1 = ion + " radius is not in the IonicRadii list\n";
            String mess2 = "Please enter radius in metres\n";
            rad = Db.readDouble(mess1+mess2);
        }
        this.arrayl.add(new Double(rad));
        int charg = 0;
        charg = ir.charge(ion);
        if(charg==0){
            String mess1 = ion + " charge is not in the IonicRadii list\n";
            String mess2 = "Please enter charge, e.g +2";
            charg = Db.readInt(mess1+mess2);
        }
        this.arrayl.add(new Integer(charg));
        this.arrayl.add(new Double(-1.0D));
        this.numOfIons++;
    }

   // Method to add an ionic species to the  Donnan Equilibrium
    // Partition coefficient to be supplied by user
    // default radii and charge taken from class IonicRadii
    // Concentrations - Molar
    public void setIon(double partCoeff, String ion, double concnA, double concnB, double assocK){
        IonicRadii ir = new IonicRadii();
        this.arrayl.add(ion);
        this.arrayl.add(new Double(concnA));
        this.arrayl.add(new Double(concnB));
        if(concnA>0.0D || concnB>0.0)this.nonZeroConcns++;
        this.arrayl.add(new Double(assocK));
        if(assocK!=0.0D)this.nonZeroAssocK++;
        double rad = 0.0D;
        if(this.includeIc){
            if(this.radiusType){
                rad = ir.hydratedRadius(ion);
            }
            else{
                rad = ir.radius(ion);
            }

            if(rad==0.0D){
                String mess1 = ion + " radius is not in the IonicRadii list\n";
                String mess2 = "Please enter radius in metres\n";
                String mess3 = "Enter 0.0 if you wish interfacial charge to be neglected";
                rad = Db.readDouble(mess1+mess2+mess3);
                if(rad==0.0D)this.includeIc = false;
            }
        }
        this.arrayl.add(new Double(rad));
        int charg = 0;
        charg = ir.charge(ion);
        if(charg==0){
            String mess1 = ion + " charge is not in the IonicRadii list\n";
            String mess2 = "Please enter charge, e.g +2";
            charg = Db.readInt(mess1+mess2);
        }
        this.arrayl.add(new Integer(charg));
        this.arrayl.add(new Double(partCoeff));
        this.numOfIons++;
    }

    // Method to add an ionic species to the  Donnan Equilibrium
    // Partition coefficients to be calculated from Born charging equation
    // default radii and charge taken from class IonicRadii
    // association constant = 0.0D
    // Concentrations - Molar
    public void setIon(String ion, double concnA, double concnB){
        IonicRadii ir = new IonicRadii();
        this.arrayl.add(ion);
        this.arrayl.add(new Double(concnA));
        this.arrayl.add(new Double(concnB));
        if(concnA>0.0D || concnB>0.0)this.nonZeroConcns++;
        this.arrayl.add(new Double(0.0D));
        double rad = 0.0D;
        if(this.radiusType){
            rad = ir.hydratedRadius(ion);
        }
        else{
            rad = ir.radius(ion);
        }
        if(rad==0.0D){
            String mess1 = ion + " radius is not in the IonicRadii list\n";
            String mess2 = "Please enter radius in metres\n";
            rad = Db.readDouble(mess1+mess2);
            if(rad==0.0D)this.includeIc = false;
        }
        this.arrayl.add(new Double(rad));
        int charg = 0;
        charg = ir.charge(ion);
        if(charg==0){
            String mess1 = ion + " charge is not in the IonicRadii list\n";
            String mess2 = "Please enter charge, e.g +2";
            charg = Db.readInt(mess1+mess2);
        }
        this.arrayl.add(new Integer(charg));
        this.arrayl.add(new Double(-1.0D));
        this.numOfIons++;
    }

    // Method to add an ionic species to the  Donnan Equilibrium
    // Partition coefficients to be supplied by the user
    // default radii and charge taken from class IonicRadii
    // association constant = 0.0D
    // Concentrations - Molar
    public void setIon(double partCoeff, String ion, double concnA, double concnB){
        IonicRadii ir = new IonicRadii();
        this.arrayl.add(ion);
        this.arrayl.add(new Double(concnA));
        this.arrayl.add(new Double(concnB));
        if(concnA>0.0D || concnB>0.0)this.nonZeroConcns++;
        this.arrayl.add(new Double(0.0D));
        double rad = 0.0D;
        if(this.includeIc){
            if(this.radiusType){
                rad = ir.hydratedRadius(ion);
            }
            else{
                rad = ir.radius(ion);
            }

            if(rad==0.0D){
                String mess1 = ion + " radius is not in the IonicRadii list\n";
                String mess2 = "Please enter radius in metres\n";
                String mess3 = "Enter 0.0 if you wish interfacial charge to be neglected";
                rad = Db.readDouble(mess1+mess2+mess3);
                if(rad==0.0D)this.includeIc = false;
            }
        }
        this.arrayl.add(new Double(rad));
        int charg = 0;
        charg = ir.charge(ion);
        if(charg==0){
            String mess1 = ion + " charge is not in the IonicRadii list\n";
            String mess2 = "Please enter charge, e.g +2";
            charg = Db.readInt(mess1+mess2);
        }
        this.arrayl.add(new Integer(charg));
        this.arrayl.add(new Double(partCoeff));
        this.numOfIons++;
    }

    // Method to add an ionophore to the partition B
    // Concentration = Molar, radius = metres
    public void setIonophore(double concn, double radius){
        this.ionophoreConcn = concn*1e3;
        this.ionophoreRad = radius;
        this.ionophoreSet = true;
    }

    // Method to add an ionophore to the partition B
    // Concentration = Molar, radius = metres
    // Ionophore name can be added for output text
    public void setIonophore(String ionophore, double concn, double radius){
        this.ionophore = ionophore;
        this.ionophoreConcn = concn*1e3;
        this.ionophoreRad = radius;
        this.ionophoreSet = true;
    }

    // Method to add an ionophore to the partition B
    // No radius added - calculation of Donnan potential will neglect interface charge
    // Concentration = Molar
    // Ionophore name can be added for output text
    public void setIonophore(String ionophore, double concn){
        this.ionophore = ionophore;
        this.ionophoreConcn = concn*1e3;
        this.includeIc = false;
        this.ionophoreSet = true;
    }

    // Method to add an ionophore to the partition B
    // No radius added - calculation of Donnan potential will neglect interface charge
    // Concentration = Molar
    public void setIonophore(double concn){
        this.ionophoreConcn = concn*1e3;
        this.includeIc = false;
        this.ionophoreSet = true;
    }

    // Method to set partition volumes (m^3) and interfacial area
    public void setVolumes(double volA, double volB, double area){
        this.volumeA = volA;
        this.volumeB = volB;
        this.interfacialArea = area;
        this.volumeSet = true;
    }

    // Method to set partition volumes (m^3)
    // No interfacial area entered - calculation of Donnan potential will neglect interface charge
    public void setVolumes(double volA, double volB){
        this.volumeA = volA;
        this.volumeB = volB;
        this.includeIc = false;
        this.volumeSet = true;
    }

    // Method to set partition relative permittivities
    public void setRelPerm(double epsA, double epsB, double epsSternA, double epsSternB){
        this.epsilonA = epsA;
        this.epsilonB = epsB;
        this.epsilonSternA = epsSternA;
        this.epsilonSternB = epsSternB;
        this.epsilonSet = true;
    }

    // Method to set partition relative permittivities
    // No Stern layer permittivities included - interface charge cannot be calculated
    public void setRelPerm(double epsA, double epsB){
        this.epsilonA = epsA;
        this.epsilonB = epsB;
        this.includeIc = false;
        this.epsilonSet = true;
    }

    // Method to set temperature (enter as degrees Celsius)
    public void setTemp(double temp){
        this.temp = temp - Fmath.T_ABS;
        this.tempSet = true;
    }

    // Method to set initial estimate of the Donnan potential (V)
    public void setEstimate(double pot){
        this.estimate = pot;
    }

    // Method to set initial step size of the Donnan potential Estimations (V)
    public void setStep(double step){
        this.step = step;
    }

    // Method to set tolerance in exiting the Donnan potential calculation
    public void setTolerance(double tol){
        this.tolerance = tol;
    }

    // Method to set maximum number of iterations in calculating the Donnan potential calculation
    public void setMaxIterations(int nMax){
        this.nMaxIter = nMax;
    }

    // Method to get the Donnan potential [volts]
    public double getDonnanPotential(){
        return this.donnanPotential;
    }

    // Method to get the diffuse double layer potential in partition A [volts]
    public double getDiffuseLayerPotentialA(){
        return this.diffPotentialA;
    }

    // Method to get the diffuse double layer potential in partition B [volts]
    public double getDiffuseLayerPotentialB(){
        return this.diffPotentialB;
    }

    // Method to get the Stern layer potential [volts]
    public double getSternLayerPotential(){
        return this.sternPotential;
    }

    // Method to get the bulk concentrations of the ionic species, in partition A, at the minimum [M]
    public double[] getConcnA(){
        double[] concn = Conv.copy(this.concnA);
        for(int i=0; i0.0D){
                indexC[i] = 1;
            }
            else{
                indexC[i] = 0;
            }
            // association constant
            hold            = (Double)this.arrayl.get(3+i*7);
            this.assocConsts[i] = hold.doubleValue()*1e-3;
            if(this.assocConsts[i]>0.0D){
                indexK[ii] = i;
                ii++;
            }
            // ion radius
            hold            = (Double)this.arrayl.get(4+i*7);
            this.radii[i]   = hold.doubleValue();
            // ion charge
            holi            = (Integer)this.arrayl.get(5+i*7);
            this.charges[i] = holi.intValue();
            // running check for all ions having same absolute charge
            if(i==0){
                this.chargeValue = Math.abs(this.charges[0]);
            }
            else{
                if(Math.abs(this.charges[i])!=this.chargeValue)this.chargeSame=false;
            }

            // partition coefficient
            hold        = (Double)this.arrayl.get(6+i*7);
            this.partCoeff[i]= hold.doubleValue();
            this.indexPC[i] = true;
            if(this.partCoeff[i]==-1.0D){
                this.indexPC[i] = false;
                // calculate partition coefficient from Born charging equation
                // calculate differences in Born charging energies between the two partitions
                this.deltaMu0[i] = this.BornChargingEnergy(this.radii[i], this.charges[i], this.epsilonB) - this.BornChargingEnergy(this.radii[i], this.charges[i], this.epsilonA);

                // calculate partition coefficients
                this.partCoeff[i] = Math.exp((this.deltaMu0[i])/(Fmath.K_BOLTZMANN*this.temp));
            }
            // calculate number of anionic and cationic species
            if(charges[i]<0){
                numOfAnions++;
            }
            else{
                numOfCations++;
            }
            if(this.ionophoreConcn==0.0D)this.nonZeroAssocK = 0;
        }


        // Calculate overall charge
        double overallCharge = 0.0D;
        double positives = 0.0D;
        double negatives = 0.0D;
        for(int i=0; i0.0D){
                positives += this.molesT[i]*charges[i];
            }
            else{
                negatives += this.molesT[i]*charges[i];
            }
            overallCharge = positives + negatives;
        }
        if(Math.abs(overallCharge)>positives*this.tol){
            String quest0 = "Class: Donnan, method: unpack()\n";
            String quest1 = "Total charge = " + overallCharge + " mol/dm, i.e. is not equal to zero\n";
            String quest2 = "Positive charge = " + positives + " mol/dm\n";
            String quest3 = "Do you wish to continue?";
            String quest = quest0 + quest1 + quest2 + quest3;
            int res = JOptionPane.showConfirmDialog(null, quest, "Neutrality check", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if(res==1)System.exit(0);
        }
    }

    // Calculate Born charging energy
    public double BornChargingEnergy(double radius, double charge, double epsilon){
        return Fmath.square(Fmath.Q_ELECTRON*charge)/(8.0*Math.PI*radius*Fmath.EPSILON_0*epsilon);
    }

    // Method that calculate partitions A and B and ionophore complex concentrations
    // of the iith ion for a given trans partition, e.g. Donnan potential
    public void ionConcns(double potential){

        // calculate partition coefficients for given potential
        for(int ii=0; ii=0.0D && root1<=limit){
                    if(root2<0.0D || root2>limit){
                        this.concnB[ii] = root1;
                        this.concnA[ii] = this.concnB[ii]*this.partCoeffPot[ii];
                        this.complex[ii] = this.assocConsts[ii]*this.ionophoreConcn*this.concnB[ii]/(1.0D + this.assocConsts[ii]*this.concnB[ii]);
                        //this.complex[ii] = (this.molesT[ii] - this.concnA[ii]*this.volumeA - this.concnB[ii]*this.volumeB)/this.volumeB;
                    }
                    else{
                        System.out.println("Class: DonnanMinim\nMethod: ionConcns");
                        System.out.println("error1: no physically meaningfull root");
                        System.out.println("root1 = " + root1 + " root2 = " + root2 + " limit = " + limit);
                        System.exit(0);
                    }
                }
                else{
                    if(root2>=0.0D && root2<=limit){
                        if(root1<0.0D || root1>limit){
                            this.concnB[ii] = root2;
                            this.concnA[ii] = this.concnB[ii]*this.partCoeffPot[ii];
                            //this.complex[ii] = (this.molesT[ii] - this.concnA[ii]*this.volumeA - this.concnB[ii]*this.volumeB)/this.volumeB;
                            this.complex[ii] = this.assocConsts[ii]*this.ionophoreConcn*this.concnB[ii]/(1.0D + this.assocConsts[ii]*this.concnB[ii]);
                        }
                        else{
                            System.out.println("Class: DonnanMinim\nMethod: ionConcns");
                            System.out.println("error2: no physically meaningfull root");
                            System.out.println("root1 = " + root1 + " root2 = " + root2 + " limit = " + limit);
                            System.exit(0);
                        }
                    }
                    else{
                        System.out.println("Class: DonnanMinim\nMethod: ionConcns");
                        System.out.println("error3: no physically meaningfull root");
                        System.out.println("root1 = " + root1 + " root2 = " + root2 + " limit = " + limit);
                        System.exit(0);
                    }
                }
            }
        }
    }

    // Method to obtain ionic and complex concentrations for a given Donnan potential
    // when more than one ion competes for the ionophore
    // by miminimising the sum of the squares of the equations describing each equilibrium
    public void calcConcnsMultiK(double potential){


        // calculate initial estimates of ionic concentration
        double[] start = new double[this.numOfIons];
        double[] step = new double[this.numOfIons];


        for(int ii=0; ii   ");
            fout.printsp(Fmath.truncate(this.interfacialChargeDensity/Math.abs(Fmath.Q_ELECTRON),7));
            fout.println("unit charges per square metre ");
            fout.print("Total interface charge = ");
            fout.printsp(Fmath.truncate(this.interfacialCharge,7));
            fout.printsp("unit charges   ->   ");
            fout.printsp(Fmath.truncate(this.interfacialCharge/Math.abs(Fmath.Q_ELECTRON),7));
            fout.println("unit charges ");

            fout.print("Overall interfacial capacitance = ");
            fout.printsp(Fmath.truncate(this.donnanCap*this.interfacialArea,7));
            fout.printsp("F ");
            fout.print("  ->  ");
            fout.printsp(Fmath.truncate(this.donnanCap,7));
            fout.println("Farads per square metre");
            fout.print("Diffuse double layer capacitance (Compartment A) = ");
            fout.printsp(Fmath.truncate(this.diffCapA*this.interfacialArea,7));
            fout.printsp("F ");
            fout.print("  ->  ");
            fout.printsp(Fmath.truncate(this.diffCapA,7));
            fout.println("Farads per square metre");
            fout.print("Diffuse double layer capacitance (Compartment B) = ");
            fout.printsp(Fmath.truncate(this.diffCapB*this.interfacialArea,7));
            fout.printsp("F ");
            fout.print("  ->  ");
            fout.printsp(Fmath.truncate(this.diffCapB,7));
            fout.println("Farads per square metre");
            fout.print("Stern capacitance = ");
            fout.printsp(Fmath.truncate(this.sternCap*this.interfacialArea,7));
            fout.printsp("F ");
            fout.print("  ->  ");
            fout.printsp(Fmath.truncate(this.sternCap,7));
            fout.println("Farads per square metre");
            fout.print("Stern thickness (Compartment A) = ");
            fout.printsp(Fmath.truncate(this.sternDeltaA,7));
            fout.println("m");
            fout.print("Stern thickness (Compartment B) = ");
            fout.printsp(Fmath.truncate(this.sternDeltaB,7));
            fout.println("m");
            fout.print("Debye length (Compartment A) = ");
            fout.printsp(Fmath.truncate(this.recipKappaA,7));
            fout.println("m");
            fout.print("Debye length (Compartment B) = ");
            fout.printsp(Fmath.truncate(this.recipKappaB,7));
            fout.println("m");
            fout.println("Compartment thicknesses assuming cubes with one side equal to the interfacial area");
            fout.print("Compartment A thickness = ");
            fout.printsp(Fmath.truncate(this.volumeA/this.interfacialArea,7));
            fout.println("m");
            fout.print("Compartment B thickness = ");
            fout.printsp(Fmath.truncate(this.volumeB/this.interfacialArea,7));
            fout.println("m");
            fout.println();
        }
        fout.print("Volume of compartment A = ");
        fout.printsp(this.volumeA);
        fout.println("cubic metres");
        fout.print("Volume of compartment B = ");
        fout.printsp(this.volumeB);
        fout.println("cubic metres");
        fout.print("Interfacial area = ");
        fout.printsp(this.interfacialArea);
        fout.println("square metres");
        fout.print("Relative electrical permittivity of compartment A = ");
        fout.println(this.epsilonA);
        fout.print("Relative electrical permittivity of compartment B = ");
        fout.println(this.epsilonB);
        fout.print("Relative electrical permittivity of compartment A Stern layer= ");
        fout.println(this.epsilonSternA);
        fout.print("Relative electrical permittivity of compartment B Stern layer= ");
        fout.println(this.epsilonSternB);
        fout.print("Temperature= ");
        fout.printsp(this.temp+Fmath.T_ABS);
        fout.println("degrees Celsius");
        fout.println();

        fout.printtab("Ion");
        fout.printtab("Radius   ");
        fout.printtab("Charge");
        fout.printtab("Partition");
        fout.printtab("Partition");
        fout.printtab("Delta(mu0)");
        fout.println("Ion-Ionophore ");

        fout.printtab("  ");
        fout.printtab(" m       ");
        fout.printtab(" ");
        fout.printtab("Coefficient ");
        fout.printtab("Coefficient ");
        fout.printtab("/ J per mol");
        fout.println("associaion ");

        fout.printtab("  ");
        fout.printtab("         ");
        fout.printtab(" ");
        fout.printtab("at      ");
        fout.printtab("at zero ");
        fout.printtab("       ");
        fout.println("constant");

        fout.printtab("  ");
        fout.printtab("          ");
        fout.printtab(" ");
        fout.printtab("equilibrium ");
        fout.printtab("potential ");
        fout.printtab("          ");
        fout.println("mol per cubic dm");

        for(int i=0; i 
 

Ads help maintain this website.