/*      Class OpenLoop
*
*       This class supports the creation of a path of Black Boxes
*       i.e. of instances of BlackBox and of any of its subclasses,
*       e.g. PropIntDeriv, FirstOrder, and the methods to combine
*       these into both a single instance of BlackBox and a Vector
*       of analogue segments, digital segments and converters.
*
*       Author:  Michael Thomas Flanagan.
*
*       Created: August 2002
*	    Updated: 12 July 2003, 10 May 2005, 2 July 2006, 7 June 2007, 6 April 2008, 5 July 2008, 2-7 November 2009
*
*       DOCUMENTATION:
*       See Michael T Flanagan's JAVA library on-line web page:
*       http://www.ee.ucl.ac.uk/~mflanaga/java/OpenLoop.html
*       http://www.ee.ucl.ac.uk/~mflanaga/java/
*
*
*   Copyright (c) 2002 - 2009   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.control;

import java.util.ArrayList;
import java.util.Vector;
import flanagan.complex.Complex;
import flanagan.complex.ComplexPoly;

public class OpenLoop extends BlackBox{
    private ArrayList openPath = new ArrayList(); // open path boxes
    private ArrayList segments = new ArrayList();     // start of segment, end of segment, type of each segment, i.e. analogue, digital, AtoD, DtoA, ZOH

    private int nBoxes = 0;                     // number of boxes in original path
    private int nSeg = 0;                       // number of analogue, digital, AtoD, ZOH segments

    private boolean checkPath = false;          // true if segment has been called
    private boolean checkNoMix = true;          // true - no ADC or DAC
    private boolean checkConsolidate = false;   // true if consolidate has been called

    private boolean[] adcs = null;              // true if box = ADC
    private boolean[] dacs = null;              // true ifbox = DAC
    private boolean[] zeroHolds = null;         // true ifbox = Zero Order Hold

    // Constructor
    public OpenLoop(){
        super("OpenLoop");
    }

    // Add box to the open path
    public void addBoxToPath(BlackBox box){
        this.openPath.add(box);
        this.nBoxes++;
    }

    // Consolidate all boxes into appropriate segments and combine all boxes into one box
    public void consolidate(){
        // Empty segments ArrayList if openPath ArrayList has been updated
        if(!segments.isEmpty()){
            segments.clear();
            this.nBoxes = 0;
            this.nSeg = 0;
            this.checkNoMix = true;
            this.checkPath = false;
        }

        // Find analogue, digital and conversion segments in OpenLoop
        this.segment();

        // Combine all boxes into a single box and make this instance that combined box
         BlackBox aa = null;
        if(this.nSeg==1){
            if(this.nBoxes==1){
                aa = (BlackBox) this.openPath.get(0);
            }
            else{
                aa = (BlackBox) this.segments.get(3);
            }
        }
        else{
            aa = this.combineSegment(0, this.nBoxes);
        }
        super.sNumer = aa.sNumer.copy();
        super.sDenom = aa.sDenom.copy();
        super.sNumerPade = aa.sNumerPade.copy();
        super.sDenomPade = aa.sDenomPade.copy();
        super.sNumerDeg = aa.sNumerDeg;
        super.sDenomDeg = aa.sDenomDeg;
        super.sNumerDegPade = aa.sNumerDegPade;
        super.sDenomDegPade = aa.sDenomDegPade;
        super.sNumerSet = true;
        super.sDenomSet = true;
        super.deadTime = aa.deadTime;
        super.sZeros = Complex.copy(aa.sZeros);
        super.sPoles = Complex.copy(aa.sPoles);
        super.sZerosPade = Complex.copy(aa.sZerosPade);
        super.sPolesPade = Complex.copy(aa.sPolesPade);
        super.padeAdded=true;
        if(super.sNumerDeg==0){
            super.sNumerScaleFactor = super.sNumer.coeffCopy(0);
        }
        else{
            super.sNumerScaleFactor = BlackBox.scaleFactor(super.sNumer, super.sZeros);
        }
        if(super.sDenomDeg==0){
            super.sDenomScaleFactor = super.sDenom.coeffCopy(0);
        }
        else{
            super.sDenomScaleFactor = BlackBox.scaleFactor(super.sDenom, super.sPoles);
        }
        this.checkConsolidate = true;
    }

        // Find analogue and digital segments
    public void segment(){
        // Find ADCs, DACs and ZeroOrderHolds
        this.adcs = new boolean[nBoxes];
        int nADCs = 0;
        this.dacs = new boolean[nBoxes];
        int nDACs = 0;
        this.zeroHolds = new boolean[nBoxes];
        int nZeroHolds = 0;
        String thisName = null;
        for(int i=0; i0 && nDACs>0){
                // first adc or dac
                boolean test0 = true;
                adc0 = 0;
                while(test0){
                    if(adcs[adc0]){
                        test0 = false;
                    }
                    else{
                        adc0++;
                        if(adc0>=nBoxes){
                            test0 = false;
                        }
                    }
                }
                test0 = true;
                while(test0){
                    if(dacs[dac0]){
                        test0 = false;
                    }
                    else{
                        dac0++;
                        if(dac0>=nBoxes){
                            test0 = false;
                        }
                    }
                }
                if(adc00)adcFirst=true;
            }
            boolean adswitch = adcFirst;
            this.nSeg++;

            int nextStart = 0;
            if(adcFirst){
                this.segments.add(new Integer(0));
                this.segments.add(new Integer(adc0));
                this.segments.add("digital");
                BlackBox bb = this.combineSegment(0, adc0);
                this.segments.add(bb);
                nextStart = adc0+1;
            }
            else{
                this.segments.add(new Integer(0));
                this.segments.add(new Integer(dac0));
                this.segments.add("analogue");
                BlackBox bb = this.combineSegment(0, dac0);
                this.segments.add(bb);
                nextStart = dac0+1;
            }

            // Find all analogue and digital segments
            boolean test1 = true;
            if(nextStart>=this.nBoxes)test1 = false;
            while(test1){
                if(adswitch){
                    nextStart = nextDigitalSegment(nextStart);
                    adswitch = false;
                }
                else{
                   nextStart = nextAnalogueSegment(nextStart);
                   adswitch = true;
                }
                if(nextStart>=this.nBoxes)test1 = false;
            }

        }



    }

    // Find next digital segment
    private int nextDigitalSegment(int box0){
            // next adc
            int nextAdc = nBoxes;
            boolean endFound = false;
            boolean test = true;
            int ii = box0;
            while(test){
                if(this.adcs[ii]){
                    nextAdc = ii;
                    test = false;
                 }
                 else{
                    ii++;
                    if(ii>=nBoxes)test = false;
                }
            }

            // next dac
            int nextDac = nBoxes;
            test = true;
            ii = box0;
            while(test){
                if(dacs[ii]){
                    nextDac = ii;
                    test = false;
                }
                else{
                    ii++;
                    if(ii>=nBoxes){
                        test = false;
                        endFound = true;
                    }
                }
            }
            if(endFound)nextDac = nBoxes-1;
            if(nextAdc=nBoxes){
                        test = false;
                        endFound = true;
                    }
                }
            }

            // next dac
            int nextDac = nBoxes;
            test = true;
            ii = box0;
            while(test){
                if(dacs[ii]){
                    nextDac = ii;
                    test = false;
                }
                else{
                    ii++;
                    if(ii>=nBoxes){
                        test = false;
                    }
                }
            }
            if(endFound)nextAdc = nBoxes-1;
            if(nextDac zeros = new ArrayList();
        ArrayList poles = new ArrayList();
        ArrayList zerosPade = new ArrayList();
        ArrayList polesPade = new ArrayList();


        BlackBox aa = new BlackBox();           // Black Box to be returned

        int nBoxSeg = iHigh - iLow + 1;         // number of boxes in segment

        BlackBox bb = openPath.get(iLow);       // first box in segment
        if(!bb.padeAdded)bb.transferPolesZeros();

        aa.sNumerPade = bb.sNumerPade.copy();
        aa.sDenomPade = bb.sDenomPade.copy();
        aa.sNumer = bb.sNumer.copy();
        aa.sDenom = bb.sDenom.copy();

        aa.sNumerDegPade = bb.sNumerDegPade;
        aa.sDenomDegPade = bb.sDenomDegPade;
        aa.sNumerDeg = bb.sNumerDeg;
        aa.sDenomDeg = bb.sDenomDeg;

        if(aa.sNumerDegPade>0){
            Complex[] bbsZerosPade = Complex.copy(bb.sZerosPade);
            for(int i=0; i0){
            Complex[] bbsPolesPade = Complex.copy(bb.sPolesPade);
            for(int i=0; i0){
            Complex[] bbsZeros = Complex.copy(bb.sZeros);
            for(int i=0; i0){
            Complex[] bbsPoles = Complex.copy(bb.sPoles);
            for(int i=0; i0){
                Complex[] bbsZerosPade = Complex.copy(bb.sZerosPade);
                for(int ii=0; ii0){
                Complex[] bbsPolesPade = Complex.copy(bb.sPolesPade);
                for(int ii=0; ii0){
                Complex[] bbsZeros = Complex.copy(bb.sZeros);
                for(int ii=0; ii0){
                Complex[] bbsPoles = Complex.copy(bb.sPoles);
                for(int ii=0; ii0){
            aa.sZerosPade = Complex.oneDarray(aa.sNumerDegPade);
            for(int ii=0; ii0){
            aa.sPolesPade = Complex.oneDarray(aa.sDenomDegPade);
            for(int ii=0; ii0){
            aa.sZeros = Complex.oneDarray(aa.sNumerDeg);
            for(int ii=0; ii0){
            aa.sPoles = Complex.oneDarray(aa.sDenomDeg);
            for(int ii=0; ii getSegmentsArrayList(){
        if(!checkConsolidate)this.consolidate();
        return this.segments;
    }

    // Return segment Vector
    public Vector getSegmentsVector(){
        if(!checkConsolidate)this.consolidate();
        ArrayList seg = this.segments;
        Vector ret = null;
        if(seg!=null){
            int n = seg.size();
            ret = new Vector(n);
            for(int i=0; i();
            }
            else{
                for(int i=0; i();
            }
            else{
                int j=0;
                for(int i=0; i 
 

Ads help maintain this website.