Search in sources :

Example 1 with Unit

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

the class VCUnitTranslator method processCellMLUnit.

// derives the ucar unit equivalent to a single cellml 'unit'
// part of the recursive retrieval of CellML units.
private static Unit processCellMLUnit(Element cellUnit, Namespace sNamespace, Namespace sAttNamespace, VCUnitSystem vcUnitSystem) {
    Unit unit = null;
    UnitName uName;
    String kind = cellUnit.getAttributeValue(CELLMLTags.units, sAttNamespace);
    if (kind == null || kind.length() == 0) {
        throw new RuntimeException("No kind specified for the CellML unit >>> 2");
    }
    String exp = cellUnit.getAttributeValue(CELLMLTags.exponent, sAttNamespace);
    String scaleStr = cellUnit.getAttributeValue(CELLMLTags.prefix, sAttNamespace);
    int scale;
    try {
        if (scaleStr == null || scaleStr.length() == 0) {
            scale = 0;
        } else {
            scale = Integer.parseInt(scaleStr);
        }
    } catch (NumberFormatException e) {
        // bad practice but no better way.
        scale = CELLMLTags.prefixToScale(scaleStr);
    }
    String multiplier = cellUnit.getAttributeValue(CELLMLTags.mult, sAttNamespace);
    String offset = cellUnit.getAttributeValue(CELLMLTags.offset, sAttNamespace);
    try {
        StandardUnitDB unitDB = StandardUnitDB.instance();
        if (kind.equals(CELLMLTags.noDimUnit)) {
            // special treatment. ignore all the other attributes. Not used in computing total unit.
            unit = DerivedUnitImpl.DIMENSIONLESS;
            uName = unit.getUnitName();
        } else if (CELLMLTags.isCellBaseUnit(kind)) {
            unit = unitDB.get(kind);
            uName = UnitName.newUnitName(kind);
        } else {
            Element owner = (Element) cellUnit.getParent().getParent();
            String ownerName = owner.getAttributeValue(CELLMLTags.name, sAttNamespace);
            // check if already added.
            VCUnitDefinition unitDef = getMatchingCellMLUnitDef(owner, sAttNamespace, kind, vcUnitSystem);
            if (unitDef != null) {
                unit = unitDef.getUcarUnit();
            } else {
                // recursive block. assumes model level units are added before component level ones,
                // so no need to recurse through those as well.
                @SuppressWarnings("unchecked") ArrayList<Element> siblings = new ArrayList<Element>(owner.getChildren(CELLMLTags.UNITS, sNamespace));
                Element cellUnitDef = null;
                for (Element cuElement : siblings) {
                    cellUnitDef = cuElement;
                    if (cellUnitDef.getAttributeValue(CELLMLTags.name, sAttNamespace).equals(kind)) {
                        break;
                    } else {
                        cellUnitDef = null;
                    }
                }
                if (cellUnitDef != null) {
                    unitDef = CellMLToVCUnit(cellUnitDef, sNamespace, sAttNamespace, vcUnitSystem);
                } else {
                    System.err.println("Unit definition: " + kind + " is missing. >>> 3");
                    return null;
                }
            }
            uName = UnitName.newUnitName(kind);
        }
        if (exp != null && Integer.parseInt(exp) != 1) {
            unit = unit.raiseTo(new RationalNumber(Integer.parseInt(exp)));
        }
        if (scale != 0) {
            unit = new ScaledUnit(Double.parseDouble("1.0e" + scale), unit, uName);
        }
        if (multiplier != null && Double.parseDouble(multiplier) != 1) {
            unit = new ScaledUnit(Double.parseDouble(multiplier), unit, uName);
        }
        if (offset != null && Double.parseDouble(offset) != 0) {
            unit = new OffsetUnit(unit, Double.parseDouble(offset), uName);
        }
    } catch (UnitException e) {
        e.printStackTrace();
        throw new cbit.vcell.units.VCUnitException("Unable to set unit value: " + e.getMessage());
    }
    return unit;
}
Also used : OffsetUnit(ucar.units_vcell.OffsetUnit) ScaledUnit(ucar.units_vcell.ScaledUnit) StandardUnitDB(ucar.units_vcell.StandardUnitDB) Element(org.jdom.Element) UnitException(ucar.units_vcell.UnitException) ArrayList(java.util.ArrayList) UnitName(ucar.units_vcell.UnitName) BaseUnit(ucar.units_vcell.BaseUnit) ScaledUnit(ucar.units_vcell.ScaledUnit) OffsetUnit(ucar.units_vcell.OffsetUnit) Unit(ucar.units_vcell.Unit) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) RationalNumber(ucar.units_vcell.RationalNumber)

Example 2 with Unit

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

the class VCUnitTranslator method CellMLToVCUnit.

// takes a CellML units jdom element.Similar to its SBML counterpart.
// part of the recursive retrieval of CellML units.
public static VCUnitDefinition CellMLToVCUnit(Element source, Namespace sNamespace, Namespace sAttNamespace, VCUnitSystem vcUnitSystem) {
    @SuppressWarnings("unchecked") ArrayList<Element> list = new ArrayList<Element>(source.getChildren(CELLMLTags.UNIT, sNamespace));
    if (list.size() == 0) {
        throw new RuntimeException("No units found for CellML unit definition: " + source.getAttributeValue(CELLMLTags.name, sAttNamespace) + " >>> 1");
    }
    VCUnitDefinition totalUnit = null;
    Unit unit = null;
    for (Element temp : list) {
        unit = processCellMLUnit(temp, sNamespace, sAttNamespace, vcUnitSystem);
        if (unit == null) {
            // for dimensionless and unresolved?
            continue;
        }
        if (totalUnit == null) {
            totalUnit = vcUnitSystem.getInstance(unit.getSymbol());
        } else {
            // ?
            totalUnit = totalUnit.multiplyBy(vcUnitSystem.getInstance(unit.getSymbol()));
        }
    }
    if (totalUnit != null) {
        String unitName = source.getAttributeValue(CELLMLTags.name, sAttNamespace);
        String ownerName = ((Element) source.getParent()).getAttributeValue(CELLMLTags.name, sAttNamespace);
        storeCellMLUnit(ownerName, unitName, totalUnit);
    }
    return totalUnit;
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Element(org.jdom.Element) ArrayList(java.util.ArrayList) BaseUnit(ucar.units_vcell.BaseUnit) ScaledUnit(ucar.units_vcell.ScaledUnit) OffsetUnit(ucar.units_vcell.OffsetUnit) Unit(ucar.units_vcell.Unit)

Example 3 with Unit

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

the class InternalUnitDefinition method getInstance.

static InternalUnitDefinition getInstance(String symbol) {
    if (symbol == null) {
        throw new IllegalArgumentException("symbol was null");
    }
    if (TBD_SYMBOL.equals(symbol)) {
        return UNIT_TBD;
    }
    if (symbol.equalsIgnoreCase("dimensionless")) {
        return UNIT_DIMENSIONLESS;
    }
    Unit dUnit;
    try {
        StandardUnitFormat standardUnitFormat = new StandardUnitFormat(new java.io.ByteArrayInputStream(symbol.trim().getBytes()));
        ucar.units_vcell.UnitDB unitDB = ucar.units_vcell.UnitSystemManager.instance().getUnitDB();
        dUnit = standardUnitFormat.unitSpec(unitDB);
        ucar.units_vcell.Factor[] factors = dUnit.getDerivedUnit().getDimension().getFactors();
        for (int i = 0; i < factors.length; i++) {
            if (factors[i].getBase() instanceof ucar.units_vcell.UnknownUnit) {
                throw new VCUnitException("invalid symbol '" + factors[i].getBase() + "'");
            }
        }
    } catch (ucar.units_vcell.ParseException e) {
        e.printStackTrace();
        throw new VCUnitException("Unable to get unit: " + symbol + ": " + e.getMessage());
    } catch (UnitException e) {
        e.printStackTrace();
        throw new VCUnitException("Unable to get unit: " + symbol + ": " + e.getMessage());
    }
    return getInstance(dUnit);
}
Also used : UnitException(ucar.units_vcell.UnitException) BaseUnit(ucar.units_vcell.BaseUnit) TimeScaleUnit(ucar.units_vcell.TimeScaleUnit) ScaledUnit(ucar.units_vcell.ScaledUnit) OffsetUnit(ucar.units_vcell.OffsetUnit) Unit(ucar.units_vcell.Unit) StandardUnitFormat(ucar.units_vcell.StandardUnitFormat) Factor(ucar.units_vcell.Factor) ParseException(ucar.units_vcell.ParseException)

Example 4 with Unit

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

the class InternalUnitDefinition method isCompatible.

// allows a milder approach to unit conversion.
public boolean isCompatible(InternalUnitDefinition anotherUnit) {
    Unit ucarUnit1 = getUcarUnit();
    Unit ucarUnit2 = anotherUnit.getUcarUnit();
    if (isTBD() || anotherUnit.isTBD()) {
        return false;
    }
    if (ucarUnit2.isDimensionless() && ucarUnit1.isDimensionless()) {
        return true;
    }
    if (ucarUnit2.isDimensionless() || ucarUnit1.isDimensionless()) {
        return false;
    }
    if (ucarUnit2.isCompatible(ucarUnit1)) {
        // 
        if (ucarUnit1.getDerivedUnit().getQuantityDimension().isReciprocalOf(ucarUnit2.getDerivedUnit().getQuantityDimension())) {
            return false;
        }
        return true;
    } else {
        return false;
    }
}
Also used : BaseUnit(ucar.units_vcell.BaseUnit) TimeScaleUnit(ucar.units_vcell.TimeScaleUnit) ScaledUnit(ucar.units_vcell.ScaledUnit) OffsetUnit(ucar.units_vcell.OffsetUnit) Unit(ucar.units_vcell.Unit)

Example 5 with Unit

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

the class InternalUnitDefinition method equivalent.

/**
 * Insert the method's description here.
 * Creation date: (4/15/2004 1:54:00 PM)
 * @return boolean
 * @param ucarUnit1 ucar.units_vcell.Unit
 * @param ucarUnit2 ucar.units_vcell.Unit
 */
private static boolean equivalent(Unit ucarUnit1, Unit ucarUnit2) {
    if (ucarUnit2.isDimensionless() && ucarUnit1.isDimensionless()) {
        double scale1 = 1.0;
        Unit temp1 = ucarUnit1;
        while (temp1 instanceof ScaledUnit) {
            scale1 *= ((ScaledUnit) temp1).getScale();
            temp1 = ((ScaledUnit) temp1).getUnit();
        }
        double scale2 = 1.0;
        Unit temp2 = ucarUnit2;
        while (temp2 instanceof ScaledUnit) {
            scale2 *= ((ScaledUnit) temp2).getScale();
            temp2 = ((ScaledUnit) temp2).getUnit();
        }
        if (scale1 == scale2) {
            return true;
        } else {
            double maxAbs = Math.max(Math.abs(scale1), Math.abs(scale2));
            if (Math.abs(scale1 - scale2) / maxAbs > 1e-10) {
                return false;
            } else {
                return true;
            }
        }
    }
    if (ucarUnit2.isCompatible(ucarUnit1)) {
        // 
        try {
            float mult = ucarUnit2.convertTo(2.0f, ucarUnit1);
            if (mult != 2.0) {
                return false;
            }
        } catch (UnitException e) {
            e.printStackTrace();
            throw new VCUnitException("Unable to convert units: " + ucarUnit2 + " to " + ucarUnit1.getSymbol() + ": " + e.getMessage());
        }
        // 
        if (ucarUnit1.getDerivedUnit().getQuantityDimension().isReciprocalOf(ucarUnit2.getDerivedUnit().getQuantityDimension())) {
            return false;
        }
        return true;
    } else {
        return false;
    }
}
Also used : ScaledUnit(ucar.units_vcell.ScaledUnit) UnitException(ucar.units_vcell.UnitException) BaseUnit(ucar.units_vcell.BaseUnit) TimeScaleUnit(ucar.units_vcell.TimeScaleUnit) ScaledUnit(ucar.units_vcell.ScaledUnit) OffsetUnit(ucar.units_vcell.OffsetUnit) Unit(ucar.units_vcell.Unit)

Aggregations

BaseUnit (ucar.units_vcell.BaseUnit)5 OffsetUnit (ucar.units_vcell.OffsetUnit)5 ScaledUnit (ucar.units_vcell.ScaledUnit)5 Unit (ucar.units_vcell.Unit)5 TimeScaleUnit (ucar.units_vcell.TimeScaleUnit)3 UnitException (ucar.units_vcell.UnitException)3 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)2 ArrayList (java.util.ArrayList)2 Element (org.jdom.Element)2 Factor (ucar.units_vcell.Factor)1 ParseException (ucar.units_vcell.ParseException)1 RationalNumber (ucar.units_vcell.RationalNumber)1 StandardUnitDB (ucar.units_vcell.StandardUnitDB)1 StandardUnitFormat (ucar.units_vcell.StandardUnitFormat)1 UnitName (ucar.units_vcell.UnitName)1