/* * Eliminate all dimensionless units from the given UnitDefinition. * A new UnitDefinition without the dimensionless units is returned. * The scale and multiplier are preserved. If the UnitDefinition has only * one unit the is a dimensionless, it is not deleted. * @param UnitDefinition* pUdef * @return UnitDefinition* result */ LIBSBML_EXTERN UnitDefinition* UnitConversionFactory::eliminateDimensionless(UnitDefinition* pUdef) { unsigned int maxUnits = pUdef->getNumUnits(); UnitDefinition* pTmpUdef = NULL; Unit* pU = NULL; if (maxUnits > 1) { int scale = 0; double multiplier = 1.0; unsigned int i = 0; pTmpUdef = new UnitDefinition(UnitConversionFactory::SBML_LEVEL, UnitConversionFactory::SBML_VERSION); while (i < maxUnits) { pU = new Unit(*(pUdef->getUnit(i))); if (pU->getKind() != UNIT_KIND_DIMENSIONLESS) { pTmpUdef->addUnit(pU); } else { // conserve scale and multiplier scale = scale + pU->getScale(); multiplier = multiplier * pU->getMultiplier(); } delete pU; ++i; } i = pTmpUdef->getNumUnits(); if (i > 0 && i < maxUnits) { pTmpUdef->setName(pUdef->getName()); pTmpUdef->setId(pUdef->getId()); pU = pTmpUdef->getUnit(0); pU->setScale(pU->getScale() + scale); pU->setMultiplier(pU->getMultiplier()*multiplier); } else { delete pTmpUdef; pTmpUdef = NULL; } } return pTmpUdef; }
/* * Converts a Unit into the corresponding UnitDefinition that consists * only of SI units. * Possible offsets are ignored. * Freeing the memory for the returned UnitDefinition is up to the * receiver. * On failure a NULL pointer is returned. * @param const Unit& unit * @return UnitDefinition* result */ LIBSBML_EXTERN UnitDefinition* UnitConversionFactory::convertToSI(const Unit& unit) { UnitDefinition* pUdef = NULL; Unit* pU = NULL; if (!unit.isSetKind()) return pUdef; UnitKind_t uKind = unit.getKind(); switch (uKind) { case UNIT_KIND_AMPERE: pUdef = convertAmpereToSI(unit); break; case UNIT_KIND_BECQUEREL: case UNIT_KIND_HERTZ: pUdef = convertFrequencyToSI(unit); break; case UNIT_KIND_CANDELA: pUdef = convertCandelaToSI(unit); break; case UNIT_KIND_CELSIUS: pUdef = convertCelsiusToSI(unit); break; case UNIT_KIND_COULOMB: pUdef = convertCoulombToSI(unit); break; case UNIT_KIND_DIMENSIONLESS: case UNIT_KIND_ITEM: case UNIT_KIND_RADIAN: case UNIT_KIND_STERADIAN: pUdef = convertDimensionlessToSI(unit); break; case UNIT_KIND_FARAD: pUdef = convertFaradToSI(unit); break; case UNIT_KIND_GRAM: pU = new Unit(unit); pU->setScale(pU->getScale() - 3); pU->setKind(UNIT_KIND_KILOGRAM); pUdef = convertKilogramToSI(*pU); delete pU; break; case UNIT_KIND_GRAY: case UNIT_KIND_SIEVERT: pUdef = convertDoseToSI(unit); break; case UNIT_KIND_HENRY: pUdef = convertHenryToSI(unit); break; case UNIT_KIND_JOULE: pUdef = convertJouleToSI(unit); break; case UNIT_KIND_KATAL: pUdef = convertKatalToSI(unit); break; case UNIT_KIND_KELVIN: pUdef = convertKelvinToSI(unit); break; case UNIT_KIND_KILOGRAM: pUdef = convertKilogramToSI(unit); break; case UNIT_KIND_LITER: case UNIT_KIND_LITRE: pU = new Unit(unit); pU->setKind(UNIT_KIND_METER); pU->setExponent(pU->getExponent()*3); pU->setScale(pU->getScale() - 3); pUdef = convertMeterToSI(*pU); delete pU; break; case UNIT_KIND_LUMEN: pUdef = convertLumenToSI(unit); break; case UNIT_KIND_LUX: pUdef = convertLuxToSI(unit); break; case UNIT_KIND_METER: case UNIT_KIND_METRE: pUdef = convertMeterToSI(unit); break; case UNIT_KIND_MOLE: #if LIBSBML_VERSION >= 40100 // this may be not totally correct, but this is the way we currently intend to // handle it in COPASI case UNIT_KIND_AVOGADRO: #endif // LIBSBML_VERSION pUdef = convertMoleToSI(unit); break; case UNIT_KIND_NEWTON: pUdef = convertNewtonToSI(unit); break; case UNIT_KIND_OHM: pUdef = convertOhmToSI(unit); break; case UNIT_KIND_PASCAL: pUdef = convertPascalToSI(unit); break; case UNIT_KIND_SECOND: pUdef = convertSecondToSI(unit); break; case UNIT_KIND_SIEMENS: pUdef = convertSiemensToSI(unit); break; case UNIT_KIND_TESLA: pUdef = convertTeslaToSI(unit); break; case UNIT_KIND_VOLT: pUdef = convertVoltToSI(unit); break; case UNIT_KIND_WATT: pUdef = convertWattToSI(unit); break; case UNIT_KIND_WEBER: pUdef = convertWeberToSI(unit); break; case UNIT_KIND_INVALID: delete pUdef; pUdef = NULL; break; } if (pUdef != NULL) { unsigned int num = 1; std::stringstream ss; ss << "UnitDefinition_" << num; while (!UnitConversionFactory::isIdUnused(ss.str())) { ++num; ss.str(""); ss << "UnitDefinition_" << num; } std::string id = ss.str(); usedIds.push_back(id); pUdef->setId(id); UnitKind_t uKind = unit.getKind(); pUdef->setName(UnitKind_toString(uKind)); } return pUdef; }