/*      Class ImpedSpecSimulation
*
*        Calculates and displays the impedance spectra
*        for a user supplied circuit or for one of a
*        range of class supplied circuits
*
*       User supplied circuit models require the interface ImpedSpecModel
*
*       WRITTEN BY: Dr Michael Thomas Flanagan
*
*       DATE:    25 May 2007  (Derived from impedance spectroscopy programs, 2004 - 2007)
*       UPDATED: 1 June 2007, 7 June 2007, 8 June 2007, 5 July 2008
*
*       DOCUMENTATION:
*       See Michael T Flanagan's Java library on-line web pages:
*       http://www.ee.ucl.ac.uk/~mflanaga/java/
*       http://www.ee.ucl.ac.uk/~mflanaga/java/ImpedSpecSimulation.html
*
*       Copyright (c) May 2007    Michael Thomas Flanagan
*
*       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.circuits;

import flanagan.circuits.Impedance;
import flanagan.io.*;
import flanagan.complex.Complex;
import flanagan.math.Fmath;
import flanagan.math.Conv;
import flanagan.plot.*;

import java.text.*;
import java.util.*;


public class ImpedSpecSimulation{

    private double lowestFrequency = 0.0D;              // lowest frequency (Hz)
    private double lowestOmega = 0.0D;                  // lowest frequency (radians)
    private boolean lowestSet = false;                  // = true when lowest frequency entered
    private double highestFrequency = 0.0D;             // highest frequency (Hz)
    private double highestOmega = 0.0D;                 // highest frequency (radians)
    private boolean highestSet = false;                 // = true when highest frequency entered
    private boolean logOrLinear = true;                 // = true;  log plot
                                                        // = false; linear plot
    private double increment = 0.0D;                    // plotting increment
                                                        // if logOrLinear = true  - log10[Hz] increment
                                                        // if logOrLinear = false - linear [Hz] increment
    private double[] frequencies = null;                // frequencies [Hz]
    private double[] omegas = null;                     // radial frequencies
    private double[] log10frequencies = null;           // log10[frequencies/Hz]
    private double[] log10omegas = null;                // log10[radial frequencies]

    private int numberOfFrequencies = 800;              // number of points in the simulation
    private boolean numberSet = true;                   // = true when number of points entered
    private boolean frequenciesSet = false;             // = true when frequencies entered

    private int modelNumber = 0;                        // model number
    private double[] parameters = null;                 // model parameters
    private int numberOfParameters = 0;                 // number of model parameters
    private String[] modelParameterSymbols = null;      // model parameter symbols
    private boolean parametersSet = false;              // = true when parameters entered
    private boolean modelSet = false;                   // = true when a model number is set

    private Complex[] impedances = null;                // impedances
    private double[] magnitudesZ = null;                // magnitudes
    private double[] phasesRadZ = null;                 // impedance phases [radians]
    private double[] phasesDegZ = null;                 // impedance phases [degrees]
    private double[] realZ = null;                      // real part of the impedance
    private double[] imagZ = null;                      // imaginary part of the impedance

    private boolean impedancesSet = false;              // = true when impedances calculated

    private Complex[] voltages = null;                  // voltages across test circuit
    private double[] magnitudesV = null;                // voltage magnitudes
    private double[] phasesRadV = null;                 // voltage  phases [radians]
    private double[] phasesDegV = null;                 // voltage phases [degrees]
    private double[] realV = null;                      // real part of the voltage
    private double[] imagV = null;                      // imaginary part of the voltage

    private ImpedSpecModel userModel = null;            // user supplied model

    private String simulationTitle = null;              // Title for output graphs and text file
    private boolean fileType = false;                   // = true if 'n' number to be added to file name

    private Complex appliedVoltage = null;              // magnitude of the applied voltage as complex
    private boolean voltageSet = false;                 // = true when applied voltage entered

    private Complex referenceImpedance = null;          // reference impedance
    private boolean referenceSet = false;               // = true when reference impedance entered


    // Constructor
    public ImpedSpecSimulation(){
        this.simulationTitle = "  ";
    }

    // Constructor
    public ImpedSpecSimulation(String simulationTitle){
        this.simulationTitle = simulationTitle;
    }

    // Set scan range in Hz
    public void setScanRangeHz(double low, double high){
        this.lowestFrequency = low;
        this.lowestOmega = 2.0D*Math.PI*low;
        this.highestFrequency = high;
        this.highestOmega = 2.0D*Math.PI*high;
        this.calculateFrequencies();
    }

    // Set scan range in radians
    public void setScanRangeRadians(double low, double high){
        this.lowestFrequency = low/(2.0D*Math.PI);
        this.lowestOmega = low;
        this.highestFrequency = high/(2.0D*Math.PI);
        this.highestOmega = high;
        this.calculateFrequencies();
    }

    // Set lowest frequency in Hz
    public void setLowFrequency(double low){
        this.lowestFrequency = low;
        this.lowestOmega = 2.0D*Math.PI*low;
        this.lowestSet = true;
        if(this.highestSet && this.numberSet)this.calculateFrequencies();
    }

    // Set lowest radial frequency
    public void setLowRadialFrequency(double low){
        this.lowestOmega = low;
        this.lowestFrequency = low/(2.0D*Math.PI);
        this.lowestSet = true;
        if(this.highestSet && this.numberSet)this.calculateFrequencies();
    }

    // Set highest frequency in Hz
    public void setHighFrequency(double high){
        this.highestFrequency = high;
        this.highestOmega = 2.0D*Math.PI*high;
        this.highestSet = true;
        if(this.lowestSet && this.numberSet)this.calculateFrequencies();
    }

    // Set highest radial frequency
    public void setHighRadialFrequency(double high){
        this.highestOmega = high;
        this.highestFrequency = high/(2.0D*Math.PI);
        this.highestSet = true;
        if(this.lowestSet && this.numberSet)this.calculateFrequencies();
    }


    // Calculate frequencies
    private void calculateFrequencies(){
        if(this.logOrLinear){
            double logLow = Fmath.log10(this.lowestFrequency);
            double logHigh = Fmath.log10(this.highestFrequency);
            this.increment = (logHigh - logLow)/(this.numberOfFrequencies - 1);
            this.frequencies = new double[this.numberOfFrequencies];
            this.log10frequencies = new double[this.numberOfFrequencies];
            this.omegas = new double[this.numberOfFrequencies];
            this.log10omegas = new double[this.numberOfFrequencies];
            this.log10frequencies[0] = logLow;
            this.log10frequencies[this.numberOfFrequencies-1] = logHigh;
            for(int i=1; iImpedance.numberOfModels)throw new IllegalArgumentException("The model number, " + modelNumber + ", must lie between 1 and " + Impedance.numberOfModels + " inclusive");
        this.modelNumber = modelNumber;
        this.parameters = parameters;
        this.modelParameterSymbols = Impedance.modelComponents(modelNumber);
        this.numberOfParameters = modelParameterSymbols.length;
        if(this.numberOfParameters != this.parameters.length)throw new IllegalArgumentException("The number of model parametes passed, " + parameters.length + ", does not match the number required, " + this.numberOfParameters + ", by model number " + modelNumber);
        this.parametersSet = true;
        this.modelSet = true;
    }

    // set model number - parameters supplied as an array - parameter symbols also supplied
    public void setModel(int modelNumber, double[] parameters, String[] symbols){
        if(modelNumber == 0 || modelNumber>Impedance.numberOfModels)throw new IllegalArgumentException("The model number, " + modelNumber + ", must lie between 1 and " + Impedance.numberOfModels + " inclusive");
        this.modelNumber = modelNumber;
        this.parameters = parameters;
        this.modelParameterSymbols = Impedance.modelComponents(modelNumber);
        this.numberOfParameters = modelParameterSymbols.length;
        if(this.numberOfParameters != this.parameters.length)throw new IllegalArgumentException("The number of model parametes passed, " + parameters.length + ", does not match the numbber required, " + this.numberOfParameters + ", by model number " + modelNumber);
        if(this.numberOfParameters != symbols.length)throw new IllegalArgumentException("The number of model symbols passed, " + symbols.length + ", does not match the number required, " + this.numberOfParameters + ", by model number " + modelNumber);
        this.modelParameterSymbols = symbols;
        this.parametersSet = true;
        this.modelSet = true;
    }

    // set model number  - parameters read in through a dialog box
    public void setModel(int modelNumber){
        if(modelNumber == 0 || modelNumber>Impedance.numberOfModels)throw new IllegalArgumentException("The model number, " + modelNumber + ", must lie between 1 and " + Impedance.numberOfModels + " inclusive");
        this.modelNumber = modelNumber;
        this.modelSet = true;
        this.modelParameterSymbols = Impedance.modelComponents(modelNumber);
        this.numberOfParameters = modelParameterSymbols.length;
        this.parameters = new double[this.numberOfParameters];

        // Read in parameter values
        int ii = 0; // counter
        String symbol = null;
        while(ii getSimulationResultsAsArrayList(int nPoints){

        if(!this.impedancesSet)calculateImpedances();

        // determine points to be printed
        if(nPoints>this.numberOfFrequencies)nPoints = this.numberOfFrequencies;
        int increment = (int)Math.round((double)this.numberOfFrequencies/(double)nPoints);
        int[] points = new int[nPoints];
        points[0] = 0;
        for(int i=1; i selectedData = new ArrayList();

        Complex[] imp = Complex.oneDarray(nPoints);
        for(int i=0; i getSimulationResultsAsVector(int nPoints){

        if(!this.impedancesSet)calculateImpedances();

        // determine points to be printed
        if(nPoints>this.numberOfFrequencies)nPoints = this.numberOfFrequencies;
        int increment = (int)Math.round((double)this.numberOfFrequencies/(double)nPoints);
        int[] points = new int[nPoints];
        points[0] = 0;
        for(int i=1; i vec = new Vector();

        Complex[] imp = Complex.oneDarray(nPoints);
        for(int i=0; i getSimulationResults(int nPoints){
        return this.getSimulationResultsAsVector(nPoints);
    }

    // Plot impedance magnitude versus frequency
    public void plotImpedanceMagnitudes(){
        this.plotImpedanceMagnitudeVersusFrequency();
    }

    // Plot impedance magnitude versus frequency
    public void plotImpedanceMagnitudeVersusFrequency(){
        String[] dAndT= this.dateAndTime();
        String graphTitle1 = "ImpedSpecSimulation program:  Impedance Magnitude versus Frequency   [" + dAndT[0] + "    " + dAndT[1] + "]";
        String graphTitle2 = this.simulationTitle;
        if(logOrLinear){
            this.impedanceMagnitudeVersusLogFrequencyPlot(graphTitle1, graphTitle2);
        }
        else{
            this.impedanceMagnitudeVersusFrequencyPlot(graphTitle1, graphTitle2);
        }
    }

    // Plot impedance magnitude versus frequency (old signature)
    public void plotMagnitudeVersusFrequency(){
        this.plotImpedanceMagnitudeVersusFrequency();
    }

    // Plot magnitude versus log10 frequency
    private void impedanceMagnitudeVersusLogFrequencyPlot(String graphTitle1, String graphTitle2){

        if(!this.impedancesSet)calculateImpedances();

        double[][] data = new double[2][this.numberOfFrequencies];
        data[0] = this.log10frequencies;
        data[1] = this.magnitudesZ;
        PlotGraph pg = new PlotGraph(data);
        pg.setLine(3);
        pg.setPoint(0);
        pg.setGraphTitle(graphTitle1);
        pg.setGraphTitle2(graphTitle2);
        pg.setXaxisLegend("Log10[Frequency / Hz]");
        pg.setYaxisLegend("Impedance Magnitude");
        pg.plot();
    }

    // Plot magnitude versus frequency
    private void impedanceMagnitudeVersusFrequencyPlot(String graphTitle1, String graphTitle2){

        if(!this.impedancesSet)calculateImpedances();

        double[][] data = new double[2][this.numberOfFrequencies];
        data[0] = this.frequencies;
        data[1] = this.magnitudesZ;
        PlotGraph pg = new PlotGraph(data);
        pg.setLine(3);
        pg.setPoint(0);
        pg.setGraphTitle(graphTitle1);
        pg.setGraphTitle2(graphTitle2);
        pg.setXaxisLegend("Frequency");
        pg.setXaxisUnitsName("Hz");
        pg.setYaxisLegend("Impedance Magnitude");
        pg.plot();
    }

    // Plot impedance phase versus frequency
    public void plotImpedancePhases(){
        this.plotImpedancePhaseVersusFrequency();
    }

    // Plot impedance phase versus frequency
    public void plotImpedancePhaseVersusFrequency(){
        String[] dAndT= this.dateAndTime();
        String graphTitle1 = "ImpedSpecSimulation program:  Impedance Phase versus Frequency   [" + dAndT[0] + "    " + dAndT[1] + "]";
        String graphTitle2 = this.simulationTitle;
        if(logOrLinear){
            this.impedancePhaseVersusLogFrequencyPlot(graphTitle1, graphTitle2);
        }
        else{
            this.impedancePhaseVersusFrequencyPlot(graphTitle1, graphTitle2);
        }
    }

    // Plot impedance phase versus frequency (old signature)
    public void plotPhaseVersusFrequency(){
        this.plotImpedancePhaseVersusFrequency();
    }

    // Plot phase versus log10 frequency
    private void impedancePhaseVersusLogFrequencyPlot(String graphTitle1, String graphTitle2){

        if(!this.impedancesSet)calculateImpedances();

        double[][] data = new double[2][this.numberOfFrequencies];
        data[0] = this.log10frequencies;
        data[1] = this.phasesDegZ;
        PlotGraph pg = new PlotGraph(data);
        pg.setLine(3);
        pg.setPoint(0);
        pg.setGraphTitle(graphTitle1);
        pg.setGraphTitle2(graphTitle2);
        pg.setXaxisLegend("Log10[Frequency / Hz]");
        pg.setYaxisLegend("Impedance Phase");
        pg.setYaxisUnitsName("degrees");
        pg.plot();
    }

    // Plot phase versus frequency
    private void impedancePhaseVersusFrequencyPlot(String graphTitle1, String graphTitle2){

        if(!this.impedancesSet)calculateImpedances();

        double[][] data = new double[2][this.numberOfFrequencies];
        data[0] = this.frequencies;
        data[1] = this.phasesDegZ;
        PlotGraph pg = new PlotGraph(data);
        pg.setLine(3);
        pg.setPoint(0);
        pg.setGraphTitle(graphTitle1);
        pg.setGraphTitle2(graphTitle2);
        pg.setXaxisLegend("Frequency");
        pg.setXaxisUnitsName("Hz");
        pg.setYaxisLegend("Impedance Phase");
        pg.setYaxisUnitsName("degrees");
        pg.plot();
    }

    // Cole-Cole Plot
    public void plotColeCole(){
        String[] dAndT= this.dateAndTime();
        String graphTitle1 = "ImpedSpecSimulation program:  Cole - Cole plot   [" + dAndT[0] + "    " + dAndT[1] + "]";
        String graphTitle2 = this.simulationTitle;
        this.coleColePlot(graphTitle1, graphTitle2);
    }


    // Cole-Cole Plot
    private void coleColePlot(String graphTitle1, String graphTitle2){

        if(!this.impedancesSet)calculateImpedances();

        double[][] data = new double[2][this.numberOfFrequencies];
        for(int i=0; ithis.numberOfFrequencies)nPoints = this.numberOfFrequencies;
        int increment = (int)Math.round((double)this.numberOfFrequencies/(double)nPoints);
        int[] points = new int[nPoints];
        points[0] = 0;
        for(int i=1; ithis.numberOfFrequencies)nPoints = this.numberOfFrequencies;
        int increment = (int)Math.round((double)this.numberOfFrequencies/(double)nPoints);
        int[] points = new int[nPoints];
        points[0] = 0;
        for(int i=1; i 
 

Ads help maintain this website.