src/main/java/com/equinor/neqsim/parameterfitting/thermo/binaryInteractionParameterFitting/EosInteractionParameterFitting/TestCPAParameterFittingToSolubilityData.java
package com.equinor.neqsim.parameterfitting.thermo.binaryInteractionParameterFitting.EosInteractionParameterFitting;

import java.sql.ResultSet;
import java.util.ArrayList;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import neqsim.statistics.parameterFitting.SampleSet;
import neqsim.statistics.parameterFitting.SampleValue;
import neqsim.statistics.parameterFitting.nonLinearParameterFitting.LevenbergMarquardt;
import neqsim.thermo.system.SystemInterface;
import neqsim.thermo.system.SystemSrkCPAstatoil;
import neqsim.util.database.NeqSimExperimentDatabase;

/**
 * <p>
 * TestCPAParameterFittingToSolubilityData class.
 * </p>
 *
 * @author Even Solbraa
 * @version $Id: $Id
 */
public class TestCPAParameterFittingToSolubilityData {
    static Logger logger = LogManager.getLogger(TestCPAParameterFittingToSolubilityData.class);

    /**
     * <p>
     * main.
     * </p>
     *
     * @param args an array of {@link java.lang.String} objects
     */
    public static void main(String[] args) {
        LevenbergMarquardt optim = new LevenbergMarquardt();
        ArrayList<SampleValue> sampleList = new ArrayList<SampleValue>();

        // inserting samples from database
        NeqSimExperimentDatabase database = new NeqSimExperimentDatabase();
        // ResultSet dataSet = database.getResultSet( "SELECT * FROM
        // binarysolubilitydata WHERE ComponentSolute='methane' AND
        // ComponentSolvent='water' AND Temperature>278.0 AND Temperature<350.0");
        double parameterGuess[] = {-0.27686, 0.001121}; // , 0.000117974}; //cpa

        try (ResultSet dataSet = database.getResultSet(
                "SELECT * FROM binarySolubilityData WHERE ComponentSolute='CO2' AND ComponentSolvent='water' AND Reference='Houghton1957' AND Reference<>'Nighswander1989' AND Temperature>283.15 AND Temperature<373.15 AND Pressure<60.01325 ORDER BY Temperature")) {
            int p = 0;
            logger.info("adding....");
            while (dataSet.next() && p < 200) {
                p++;
                CPAParameterFittingToSolubilityData function =
                        new CPAParameterFittingToSolubilityData(1, 0);

                SystemInterface testSystem = new SystemSrkCPAstatoil(290, 1.0);
                // SystemInterface testSystem = new SystemSrkEos(290, 1.0);
                testSystem.addComponent("CO2", 1.0);
                testSystem.addComponent("water", 10.0);
                testSystem.createDatabase(true);
                testSystem.setMixingRule(10);
                testSystem.setMultiPhaseCheck(true);
                testSystem.setTemperature(Double.parseDouble(dataSet.getString("Temperature")));
                testSystem.setPressure(Double.parseDouble(dataSet.getString("Pressure")));

                testSystem.init(0);
                double sample1[] = {testSystem.getPressure(), testSystem.getTemperature()};
                double standardDeviation1[] = {0.13, 0.12};
                double expVal = Double.parseDouble(dataSet.getString("x1"));
                SampleValue sample = new SampleValue(expVal,
                        Double.parseDouble(dataSet.getString("StandardDeviation")), sample1,
                        standardDeviation1);
                function.setInitialGuess(parameterGuess);
                sample.setFunction(function);
                sample.setThermodynamicSystem(testSystem);
                sample.setReference(Double.toString(testSystem.getTemperature()));
                // double parameterGuess[] = {-0.130}; //srk
                // double parameterGuess[] = {-0.0668706940}; //cpa
                sampleList.add(sample);
            }
        } catch (Exception ex) {
            logger.error("database error", ex);
        }
        /*
         * 
         * dataSet = database.getResultSet(
         * "SELECT * FROM BinaryEquilibriumData WHERE Component1='methane' AND Component2='MEG'" );
         * try { int p = 0; logger.info("adding...."); while (!dataSet.next() && p < 0) { p++;
         * CPAParameterFittingToSolubilityData function = new
         * CPAParameterFittingToSolubilityData(0,1);
         * 
         * SystemInterface testSystem = new SystemSrkCPAstatoil(290, 1.0); //SystemInterface
         * testSystem = new SystemSrkEos(290, 1.0); testSystem.addComponent("methane", 1.0);
         * testSystem.addComponent("MEG", 1.0); //testSystem.createDatabase(true);
         * testSystem.setTemperature(Double.parseDouble(dataSet.getString("Temperature") ));
         * testSystem.setPressure(Double.parseDouble(dataSet.getString("Pressure")));
         * testSystem.setMixingRule(10); testSystem.init(0); double sample1[] =
         * {testSystem.getPressure(), testSystem.getTemperature()}; double standardDeviation1[] =
         * {0.13}; pressure double value = Double.parseDouble(dataSet.getString("y2")); SampleValue
         * sample = new SampleValue(value, value/100.0, sample1, standardDeviation1);
         * sample.setFunction(function); sample.setThermodynamicSystem(testSystem);
         * sample.setReference(Double.toString(testSystem.getTemperature())); //double
         * parameterGuess[] = {-0.130}; //srk // double parameterGuess[] = {-0.0668706940}; //cpa
         * function.setInitialGuess(parameterGuess); sampleList.add(sample); } } catch (Exception
         * ex) { logger.error("database error", ex); }
         */

        SampleSet sampleSet = new SampleSet(sampleList);
        optim.setSampleSet(sampleSet);

        // do simulations
        // optim.solve();
        // optim.runMonteCarloSimulation();
        optim.calcDeviation();
        optim.displayResult();
        optim.displayCurveFit();
        // optim.writeToTextFile("c:/testFit.txt");
    }
}
