Search in sources :

Example 1 with UnitImpl

use of ucar.units_vcell.UnitImpl in project vcell by virtualcell.

the class VCUnitTranslator method expandUcarCellML.

// time mult not included.
// first call: (1.0, 0, unit, new ArrayList(), transType)
// Problem: units override each other's mult/multiplier before getting to the level of derived unit.
// To avoid that, add a dimensionless unit for cases where
protected static ArrayList<Element> expandUcarCellML(double mult, double offset, Unit unit, ArrayList<Element> transUnits) {
    if (unit instanceof UnitImpl) {
        UnitImpl unitImpl = (UnitImpl) unit;
        if (unitImpl instanceof DerivedUnitImpl) {
            DerivedUnitImpl baseUnit = (DerivedUnitImpl) unitImpl;
            // String symbol = baseUnit.getID();
            // String name = baseUnit.getName();
            Factor[] factors = baseUnit.getDimension().getFactors();
            for (int i = 0; i < factors.length; i++) {
                RationalNumber exponent = factors[i].getExponent();
                // String baseSymbol  = factors[i].getBase().getID();
                String baseName = ((BaseUnit) factors[i].getBase()).getName();
                // System.out.println(baseName + " " + exponent);
                if (factors.length > 1) {
                    if (i == 0) {
                        transUnits.add(getCellMLTransUnit(new RationalNumber(1), mult, offset, DerivedUnitImpl.DIMENSIONLESS.getName()));
                    }
                    transUnits.add(getCellMLTransUnit(exponent, 1.0, 0.0, baseName));
                } else {
                    transUnits.add(getCellMLTransUnit(exponent, mult, offset, baseName));
                }
            }
            return transUnits;
        } else if (unitImpl instanceof OffsetUnit) {
            OffsetUnit offsetUnit = (OffsetUnit) unitImpl;
            offset += offsetUnit.getOffset();
            // offset = offsetUnit.getOffset();
            if (offsetUnit.getUnit() != offsetUnit.getDerivedUnit()) {
                return expandUcarCellML(mult, offset, offsetUnit.getUnit(), transUnits);
            }
        } else if (unitImpl instanceof ScaledUnit) {
            ScaledUnit multdUnit = (ScaledUnit) unitImpl;
            mult *= multdUnit.getScale();
            System.out.println("mult: " + mult);
            if (multdUnit.getUnit() != multdUnit.getDerivedUnit()) {
                return expandUcarCellML(mult, offset, multdUnit.getUnit(), transUnits);
            }
        }
        if (unitImpl.getDerivedUnit() != unit) {
            // i.e. we have not reached the base unit, yet
            return expandUcarCellML(mult, offset, unitImpl.getDerivedUnit(), transUnits);
        }
    } else {
        System.err.println("Unable to process unit translation for CellML: " + " " + unit.getSymbol());
    }
    return null;
}
Also used : DerivedUnitImpl(ucar.units_vcell.DerivedUnitImpl) OffsetUnit(ucar.units_vcell.OffsetUnit) ScaledUnit(ucar.units_vcell.ScaledUnit) UnitImpl(ucar.units_vcell.UnitImpl) DerivedUnitImpl(ucar.units_vcell.DerivedUnitImpl) Factor(ucar.units_vcell.Factor) RationalNumber(ucar.units_vcell.RationalNumber) BaseUnit(ucar.units_vcell.BaseUnit)

Aggregations

BaseUnit (ucar.units_vcell.BaseUnit)1 DerivedUnitImpl (ucar.units_vcell.DerivedUnitImpl)1 Factor (ucar.units_vcell.Factor)1 OffsetUnit (ucar.units_vcell.OffsetUnit)1 RationalNumber (ucar.units_vcell.RationalNumber)1 ScaledUnit (ucar.units_vcell.ScaledUnit)1 UnitImpl (ucar.units_vcell.UnitImpl)1