Search in sources :

Example 1 with ScaledUnit

use of ucar.units_vcell.ScaledUnit 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 ScaledUnit

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

the class InternalUnitDefinition method getInstance.

/**
 * Creation date: (4/15/2004 1:39:26 PM)
 * @return cbit.vcell.units.InternalUnitDefinition
 * @param InternalUnitDefinition cbit.vcell.units.InternalUnitDefinition
 * @deprecated please don't use, we should hide the underlying implementation (CellML translator needs it now)
 */
static InternalUnitDefinition getInstance(Unit ucarUnit) {
    // 
    for (int i = 0; i < defs.size(); i++) {
        InternalUnitDefinition temp = (InternalUnitDefinition) defs.get(i);
        if (equivalent(ucarUnit, temp.getUcarUnit())) {
            return temp;
        }
    }
    // 
    try {
        if (ucarUnit instanceof ScaledUnit) {
            java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(ucarUnit.toString(), " ");
            double scale = 1;
            int cnt = 0;
            String nonNum = null;
            while (tokenizer.hasMoreTokens()) {
                String token = tokenizer.nextToken();
                try {
                    scale *= Double.parseDouble(token);
                    cnt++;
                } catch (NumberFormatException e) {
                    nonNum = token;
                    break;
                }
            }
            double roundedScale = round(scale);
            if (roundedScale != scale || cnt > 1) {
                String newSymbol = String.valueOf(roundedScale);
                if (nonNum != null) {
                    newSymbol += " " + nonNum;
                }
                StandardUnitFormat standardUnitFormat = new StandardUnitFormat(new java.io.ByteArrayInputStream(newSymbol.trim().getBytes()));
                ucar.units_vcell.UnitDB unitDB = ucar.units_vcell.UnitSystemManager.instance().getUnitDB();
                ucarUnit = standardUnitFormat.unitSpec(unitDB);
            }
        }
    } catch (ParseException e) {
        e.printStackTrace();
        throw new VCUnitException("Unable to get unit: " + ucarUnit.toString() + ": " + e.getMessage());
    } catch (UnitException e) {
        e.printStackTrace();
        throw new VCUnitException("Unable to get unit: " + ucarUnit.toString() + ": " + e.getMessage());
    }
    // 
    return new InternalUnitDefinition(ucarUnit);
}
Also used : ScaledUnit(ucar.units_vcell.ScaledUnit) UnitException(ucar.units_vcell.UnitException) StandardUnitFormat(ucar.units_vcell.StandardUnitFormat) ParseException(ucar.units_vcell.ParseException)

Example 3 with ScaledUnit

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

the class InternalUnitDefinition method showUChar.

/**
 * Insert the method's description here.
 * Creation date: (4/14/2004 5:48:29 PM)
 * @param obj java.lang.Object
 */
private static void showUChar(String pad, String attribute, Object obj) {
    if (obj == null || obj instanceof String || obj instanceof Boolean || obj instanceof Double || obj instanceof Number || // to make much less verbose
    obj instanceof Factor || obj instanceof QuantityDimension) {
        // to make much less verbose
        System.out.println(pad + attribute + " = " + obj);
        return;
    } else {
        System.out.println(pad + attribute + " = " + ((obj != null) ? (obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode()) + " '" + obj + "'") : ("null")));
    }
    pad += "   ";
    if (obj instanceof ucar.units_vcell.UnitImpl) {
        ucar.units_vcell.UnitImpl unitImpl = (ucar.units_vcell.UnitImpl) obj;
        if (unitImpl.getDerivedUnit() != obj) {
            // stops infinite recursion
            showUChar(pad, "derivedUnit", unitImpl.getDerivedUnit());
        }
        showUChar(pad, "name", unitImpl.getName());
        // showUChar(pad,"plural",unitImpl.getPlural());
        showUChar(pad, "symbol", unitImpl.getSymbol());
        showUChar(pad, "unitname", unitImpl.getUnitName());
        showUChar(pad, "isDimensionless", new Boolean(unitImpl.isDimensionless()));
        if (unitImpl instanceof DerivedUnitImpl) {
            DerivedUnitImpl derivedUnitImpl = (DerivedUnitImpl) unitImpl;
            if (!(derivedUnitImpl instanceof BaseUnit)) {
                // stops infinite recursion
                showUChar(pad, "unitDimension", derivedUnitImpl.getDimension());
            }
            showUChar(pad, "quantityDimension", derivedUnitImpl.getQuantityDimension());
            if (derivedUnitImpl instanceof ucar.units_vcell.BaseUnit) {
                ucar.units_vcell.BaseUnit baseUnit = (ucar.units_vcell.BaseUnit) derivedUnitImpl;
                showUChar(pad, "baseQuantity", baseUnit.getBaseQuantity());
                showUChar(pad, "id", baseUnit.getID());
            }
        } else if (unitImpl instanceof OffsetUnit) {
            OffsetUnit offsetUnit = (OffsetUnit) unitImpl;
            showUChar(pad, "offset", new Double(offsetUnit.getOffset()));
            if (offsetUnit.getUnit() == offsetUnit.getDerivedUnit()) {
                showUChar(pad, "unit", offsetUnit.getUnit().getClass().getName() + "@" + Integer.toHexString(offsetUnit.hashCode()) + "  '" + obj + "'  <<<<REPEATED>>>>");
            } else {
                showUChar(pad, "unit", offsetUnit.getUnit());
            }
        } else if (unitImpl instanceof ScaledUnit) {
            ScaledUnit scaledUnit = (ScaledUnit) unitImpl;
            showUChar(pad, "scale", new Double(scaledUnit.getScale()));
            if (scaledUnit.getUnit() == scaledUnit.getDerivedUnit()) {
                showUChar(pad, "unit", scaledUnit.getUnit().getClass().getName() + "@" + Integer.toHexString(scaledUnit.hashCode()) + "  '" + obj + "'  <<<<REPEATED>>>>");
            } else {
                showUChar(pad, "unit", scaledUnit.getUnit());
            }
        } else if (unitImpl instanceof TimeScaleUnit) {
            TimeScaleUnit timeScaleUnit = (TimeScaleUnit) unitImpl;
            showUChar(pad, "origin", timeScaleUnit.getOrigin());
            if (timeScaleUnit.getUnit() == timeScaleUnit.getDerivedUnit()) {
                showUChar(pad, "unit", timeScaleUnit.getUnit().getClass().getName() + "@" + Integer.toHexString(timeScaleUnit.hashCode()) + "  '" + obj + "'  <<<<REPEATED>>>>");
            } else {
                showUChar(pad, "unit", timeScaleUnit.getUnit());
            }
        }
    } else if (obj instanceof UnitName) {
        UnitName unitName = (UnitName) obj;
        showUChar(pad, "name", unitName.getName());
        // showUChar(pad,"plural",unitName.getPlural());
        showUChar(pad, "symbol", unitName.getSymbol());
    } else if (obj instanceof Dimension) {
        Dimension dimension = (Dimension) obj;
        Factor[] factors = dimension.getFactors();
        if (factors == null || factors.length == 0) {
            showUChar(pad, "factors", null);
        } else {
            for (int i = 0; i < factors.length; i++) {
                showUChar(pad, "factors[" + i + "]", factors[i]);
            }
        }
        // showUChar(pad,"rank",new Integer(dimension.getRank()));  // adds nothing
        showUChar(pad, "isDimensionless", new Boolean(dimension.isDimensionless()));
        if (dimension instanceof UnitDimension) {
            UnitDimension unitDimension = (UnitDimension) dimension;
            showUChar(pad, "quantityDimension", unitDimension.getQuantityDimension());
        }
    } else if (obj instanceof Factor) {
        Factor factor = (Factor) obj;
        // to make much less verbose
        showUChar(pad, "base", factor.getBase().toString());
        showUChar(pad, "exponent", factor.getExponent());
        showUChar(pad, "ID", factor.getID());
        showUChar(pad, "isDimensionless", new Boolean(factor.isDimensionless()));
    } else if (obj instanceof Base) {
        Base base = (Base) obj;
        showUChar(pad, "ID", base.getID());
        showUChar(pad, "isDimensionless", new Boolean(base.isDimensionless()));
        if (base instanceof BaseQuantity) {
            BaseQuantity baseQuantity = (BaseQuantity) base;
            showUChar(pad, "name", baseQuantity.getName());
            showUChar(pad, "symbol", baseQuantity.getSymbol());
        }
    }
}
Also used : DerivedUnitImpl(ucar.units_vcell.DerivedUnitImpl) OffsetUnit(ucar.units_vcell.OffsetUnit) QuantityDimension(ucar.units_vcell.QuantityDimension) ScaledUnit(ucar.units_vcell.ScaledUnit) BaseUnit(ucar.units_vcell.BaseUnit) DerivedUnitImpl(ucar.units_vcell.DerivedUnitImpl) BaseQuantity(ucar.units_vcell.BaseQuantity) UnitName(ucar.units_vcell.UnitName) BaseUnit(ucar.units_vcell.BaseUnit) QuantityDimension(ucar.units_vcell.QuantityDimension) Dimension(ucar.units_vcell.Dimension) UnitDimension(ucar.units_vcell.UnitDimension) Base(ucar.units_vcell.Base) RationalNumber(ucar.units_vcell.RationalNumber) Factor(ucar.units_vcell.Factor) TimeScaleUnit(ucar.units_vcell.TimeScaleUnit) UnitDimension(ucar.units_vcell.UnitDimension)

Example 4 with ScaledUnit

use of ucar.units_vcell.ScaledUnit 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)

Example 5 with ScaledUnit

use of ucar.units_vcell.ScaledUnit 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

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