Boost Converter


Circuit Solver Desktop Interface:
Save this file to your local hard drive. Once we run the java engine it will request a file. This is the file we would like to choose.


The Schematic: The highlighted boxes are the references we will use when we load the circuit into the Java Simulation Engine.

The Source Code:

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import com.phasorsystems.circuitsolver.*;

public class BoostConverter {
    /* Essential Variables */
    private static double START_TIME = 0.0;
    private static double STOP_TIME = 10.0;
    private static double TIME_STEP = 1;
    private static int SIM_LENGTH = 0;
    private static String FILE_DIRECTORY = "";
    private static String FILE_PATH = "";

    private static int NODE_SIZE = 625;
    private static int index = 0;
    private static int width = 1280;
    private static int height = 640;
    private static Graphics2D g2d;
    private static BufferedImage surface;

    private static String FILE_STORAGE = "\\lfile.txt";
    private static int MaxScope = 0;

    /* Displaying Outputs */
    private static double[] time_axis;
    private static double[] VOLTAGE_SIGNAL;
    private static double[] CURRENT_SIGNAL;
    private static double[] MOSFET_CURRENT;
    private static double[] SOURCE_CURRENT;
    private static double[] PWM_SIGNAL;

    private static int S1 = -1;
    private static int v_out = -1;
    private static int i_out = -1;
    private static int i_mos = -1;
    private static int i_src = -1;
    private static int v_control = -1;
    private static double f_switching = 20e3;
    
    private final static double CYCLES = 50.0;
    private final static double SUB_DIVISIONS = 100.0;

    public static void main(String[] args) {
        System.out.println("-------- STARTING ---------");

        /* initialize the Simulation Engine */
        initialize();
        
        /* Running the variable Simulation Engine */
        while (Engine.simulationTime < STOP_TIME) {

            process();

            /* Safety checks */
            if (!Engine.isSimulating) {
                System.out.println("ABORTED: SIMULATION ERROR");
                break;
            }

            if (error()) {
                break;
            }
        }

        plot();
    }

    /* Initialization */
    public static void init() {

        TIME_STEP = (1.0 / (SUB_DIVISIONS * f_switching));
        STOP_TIME = (1.0 / f_switching) * CYCLES;
        
        /* Load a file created by Circuit Solver Desktop Version */
        File file = new File(FILE_PATH);

        /* Remove any extra elements inside the Engine */
        Engine.clearElements();

        /* Load your file into the Engine */
        Engine.loadFile(file);

        /* Setting Engine Parameters */
        Engine.simulationTime = START_TIME;
        Engine.isSimulating = true;

        /* Check the length of the simulation. */
        simlength();

        Engine.timeStep = TIME_STEP;
        
        S1 = Engine.getSPST(0);

        v_out = Engine.getVoltMeter(0);
        i_out = Engine.getAmMeter(0);
        i_mos = Engine.getAmMeter(1);
        i_src = Engine.getAmMeter(2);
        v_control = Engine.getVoltMeter(3);
        
        /* Creating vectors for our data */
        SIM_LENGTH = (int) Math.round((STOP_TIME - START_TIME) / TIME_STEP);
        time_axis = tvector(SIM_LENGTH, TIME_STEP);
        
        VOLTAGE_SIGNAL = new double[SIM_LENGTH];
        CURRENT_SIGNAL = new double[SIM_LENGTH];
        MOSFET_CURRENT = new double[SIM_LENGTH];
        SOURCE_CURRENT = new double[SIM_LENGTH];
        PWM_SIGNAL = new double[SIM_LENGTH];
    }

    /* Insert your logic here for setting variables. */
    public static void logic_loop() {
        if (!error()) {
            /* Write your custom loop logic here. */
            
            if (index < SIM_LENGTH)
            {    
                if (v_control != -1)
                {
                    PWM_SIGNAL[index] = Engine.voltmeter.get(v_control).getComputedVoltage();
                }
                
                if (S1 != -1)
                {
                    Engine.spst.get(S1).setParams((int)Math.round(PWM_SIGNAL[index]));
                }
            }
        }
    }

    /* Process the information from the engine here. */
    public static void output_loop() {
        if (!error()) {
            /* Grab simulation data here. */
            
            if (index < SIM_LENGTH)
            {
                if (v_out != -1)
                {
                    VOLTAGE_SIGNAL[index] = Engine.voltmeter.get(v_out).getComputedVoltage();
                }
                
                if (i_out != -1)
                {
                    CURRENT_SIGNAL[index] = Engine.ammeter.get(i_out).getComputedCurrent();
                }
                
                if (i_mos != -1)
                {
                    MOSFET_CURRENT[index] = Engine.ammeter.get(i_mos).getComputedCurrent();
                }
                
                if (i_src != -1)
                {
                    SOURCE_CURRENT[index] = Engine.ammeter.get(i_src).getComputedCurrent();
                }
            }
        }
    }

    /* Plot data here. */
    public static void plot() {
        double[][] plots = to2dArray(VOLTAGE_SIGNAL, CURRENT_SIGNAL, MOSFET_CURRENT, SOURCE_CURRENT, PWM_SIGNAL);
        String[] legends = legend("Output Voltage""Output Current""Mosfet Current""Source Current""Pwm Signal");

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new PlotlyWrapper(800, 480, time_axis, plots, "IV Vs Time""Time""Amplitude", legends, "-line");
            }
        });
    }

    /* General function to walk the engine through the simulation */
    public static void process() {
        logic_loop();
        Engine.compute();
        output_loop();
        index++;
        visual_indicator(index);
    }

    /* Combine Vectors */
    public static double[][] to2dArray(double[]... array) {
        return array;
    }

    /* Create String Vectors */
    public static String[] legend(String... str) {
        return str;
    }

    /* check if the simulation is within bounds. */
    public static boolean error() {
        return (!(index < SIM_LENGTH));
    }

    /* Visual Indicator for the Percentage of Completed Computations */
    public static void visual_indicator(int index) {
        if (index == (int) Math.round(0.25 * (SIM_LENGTH - 1))) {
            System.out.println("-[-----     25%          ]-");
        } else if (index == (int) Math.round(0.50 * (SIM_LENGTH - 1))) {
            System.out.println("-[----------50%          ]-");
        } else if (index == (int) Math.round(0.75 * (SIM_LENGTH - 1))) {
            System.out.println("-[----------75%-----     ]-");
        } else if (index == (int) Math.round(1.00 * (SIM_LENGTH - 1))) {
            System.out.println("-[----------100%---------]-");
        }
    }

    /* Determine if the simulation length makes sense */
    public static void simlength() {
        MaxScope = Integer.MAX_VALUE/2;

        if (MaxScope < (STOP_TIME - START_TIME)/TIME_STEP) {
            System.out.println("(STOP_TIME - START_TIME) is too large for the given TIME_STEP");
            System.exit(0);
        }
    }

    /*
     * This function creates a uniformly spaced time vector of the specified
     * length and time step
     */

    public static double[] tvector(int length, double delta) {
        double[] output = new double[length];

        for (int i = 0; i < output.length; i++) {
            output[i] = i * delta;
        }

        return output;
    }

    /* returns a vector of the input variable "a" */
    public static double[] vector(double a) {
        double[] out = new double[1];

        out[0] = a;

        return out;
    }

    /* returns a vector of the input variables "a" and "b" */
    public static double[] vector2(double a, double b) {
        double[] out = new double[2];

        out[0] = a;
        out[1] = b;

        return out;
    }

    /* returns a vector of the input variables "a", "b", and "c" */
    public static double[] vector3(double a, double b, double c) {
        double[] out = new double[3];

        out[0] = a;
        out[1] = b;
        out[2] = c;

        return out;
    }

    /* returns a vector of the input variables "a", "b", "c", and "d" */
    public static double[] vector4(double a, double b, double c, double d) {
        double[] out = new double[4];

        out[0] = a;
        out[1] = b;
        out[2] = c;
        out[3] = d;

        return out;
    }

    /* Initialization function for the Simulation Engine */
    public static void initialize() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        String last_file = ".";
        File l_file = new File(Paths.get("").toAbsolutePath().toString() + FILE_STORAGE);
        if (l_file.exists()) {
            last_file = read_file(Paths.get("").toAbsolutePath().toString() + FILE_STORAGE);
        } else {
            write_file(Paths.get("").toAbsolutePath().toString() + FILE_STORAGE, ".");
        }

        JFrame frame = new JFrame();
        frame.setAlwaysOnTop(true);
        frame.setLocation(0, 0);
        frame.setResizable(false);
        frame.pack();
        
        JFileChooser file_selector = new JFileChooser(new File(last_file).getAbsolutePath());
        file_selector.setDialogTitle("Select Simulation File");
        file_selector.setFileSelectionMode(JFileChooser.FILES_ONLY);
        
        if (file_selector.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
            FILE_PATH = file_selector.getSelectedFile().toString();
            FILE_DIRECTORY = file_selector.getCurrentDirectory().toString();
        } else {
            System.out.println("SIMULATION ENGINE: SELECT A FILE.");
            System.exit(0);
        }
        
        frame.add(file_selector);

        System.out.println("Loaded: " + FILE_PATH);

        write_file(Paths.get("").toAbsolutePath().toString() + FILE_STORAGE,
                file_selector.getSelectedFile().getParent());

        surface = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Global.surface = new GraphicsEngine();
        g2d = surface.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        Global.surface.setGraphics(g2d);
        try {
            Engine.initialize(width, height, NODE_SIZE, Private.LICENSE);
        } catch (Exception e) {
            e.printStackTrace();
        }

        while (Global.state != Engine.MAIN) {
            Engine.update();
        }

        Global.SAVEIMGPATH = FILE_DIRECTORY;

        Global.state = Engine.DESIGN;
        Global.lastState = Engine.MAIN;
        
        init();
    }

    public static String read_file(String filename) {
        String str = "";
        Path path = Paths.get(filename);

        Charset charset = Charset.forName("ISO-8859-1");
        try {
            List lines = Files.readAllLines(path, charset);
            str = String.join(System.lineSeparator(), lines);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return str;
    }

    public static void write_file(String filename, String content) {
        try {
            FileWriter fw = new FileWriter(filename);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Note: A valid license is needed for simulation.

Structure of the file:
The basic structure of the simulation file is as follows:
(1) Initialize the Engine.
(2) Load a Simulation File.
(3) Perform dynamic analysis on the circuit.
(4) Collect the outputs from the system.
(5) Plot the outputs from the engine.

Loading the File: Output of the Boost Converter:


Copyright (c) 2016 - 2017 Phasor Systems