/*
 * Inductor.java - contains functions relating to inductance.
 * (This is a weak name, it does not represent an inductor itself.)
 *
 * I need a 'computeInductance()' function that works differently
 * for various types of timing. So we derive three different objects
 * from a base class.
 *
 * Created on November 1, 2003, 10:52 AM
 */

/**
 * contains functions relating to inductance.
 * @author  Barry barry@coilgun.info
 */
public class Inductor {
    
    /**
     * Public interface -- the One Big And Glorious Useful Function!!!
     */
    public double computeInductance(double time, double cap) {
        switch (inductanceType)
        {
            case ZERO_CROSSING:
                return inductanceZeroCrossing(time, cap);
            case HALF_POWER:
                return inductanceHalfPower(time, cap);
            case FULL_CYCLE:
                return inductanceFullCycle(time, cap);
            default:
                return -1;      // should not happen
        }
    }
    
    /**
     * Set type of inductance calculation
     */
    public void setInductorType(int type) {
        inductanceType = type;
    }
    
    /** Create new instance of Inductor */
    public Inductor(int type) {
        inductanceType = type;
    }
    
    /**
     * Compute inductance necessary for given half-cycle pulse width.
     *                                      2 * pi * SQRT(L*C)
     * Starting with the basic relation T = --------------
     *                                           2
     * Solve for L:
     *          L = T^2 / (pi^2 * C)
     * @parm time - seconds
     * @parm cap - farads
     * @return inductance in henries
     */
    private double inductanceZeroCrossing(double time, double cap) {
        double ind = time * time / (PI_SQUARED * cap);
        return ind;
    }
    
    /**
     * Compute inductance necessary for given full-cycle pulse width.
     * Starting with the basic relation T = 2 * pi * SQRT(L*C)
     * Solve for L:
     *          L = T^2 / (4 * pi^2 * C)
     */
    private double inductanceFullCycle(double time, double cap) {
        double ind = time * time / (FOUR_PI_SQUARED * cap);
        return ind;
    }
    
    /**
     * Compute inductance necessary for given half-power pulse width,
     * where 'half power' is defined as the point where current 
     * is 0.707 times the maximum. For sine waves, this is 1/4 of complete cycle.
     *
     *                                      2 * pi * SQRT(L*C)
     * Starting with the basic relation T = ------------------
     *                                             4
     * Solve for L:
     *          L = 4 * T^2 / (pi^2 * C)
     */
    private double inductanceHalfPower(double time, double cap) {
        double ind = 4 * time * time / (PI_SQUARED * cap);
        return ind;
    }
    
    // constants for how to compute inductance
    static public final int ZERO_CROSSING = 1;
    static public final int HALF_POWER = 2;
    static public final int FULL_CYCLE = 3;
    private int inductanceType;

    // constants (hopefully speeds up computations)
    private final double PI_SQUARED = Math.PI * Math.PI;
    private final double FOUR_PI_SQUARED = 4 * Math.PI * Math.PI;
    
}
