Changeset 116 for trunk/include/core


Ignore:
Timestamp:
Oct 26, 2005, 11:32:52 PM (19 years ago)
Author:
dkearney
Message:
  1. rewrote RpUnits::define(...) and RpUnits::convert(...) fxns.
  2. added functionality so you no longer need to call add_presets(...)
  3. RpUnits can now handle conversions as follows 1cm2/Vs -> 1e-7m2/kVus
  4. Cannot handle conversions dealing with Temperature very well because

Temperature conversions have offsets (+/- 32...). you can still do
F->C and Fs->Cs, but Fms->Cs provides unreliable results. (not to
mention that i'm still unsure how to do a conversion like this.

  1. still need to add Fo (delta fahrenheit) and Co (delta celcius)

units and conversions. These should not be effected by the notorious
temperature

  1. adjusted RpUnits_test.cc for testing. python.fortran and matlab

bindings have not been tested yet.

Location:
trunk/include/core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/core/RpUnits.h

    r115 r116  
    1515#include <iostream>
    1616#include <string>
     17#include <list>
    1718#include <sstream>
    1819#include <stdlib.h>
    1920#include <errno.h>
    20 
    21 #ifndef _RpDICT_H
    22     #include "RpDict.h"
    23 #endif
    24 
     21#include <math.h>
     22
     23#include "RpDict.h"
    2524#include "RpUnitsStd.h"
    2625
     
    2827#define _RpUNITS_H
    2928
     29#define LIST_TEMPLATE RpUnitsListEntry
     30
     31// should the define function:
     32// 1. compare portions of inStr to unit names that have previously
     33//    been defined (you must parse in order for this option to work)
     34#define COMPARE_MASK    1
     35// 2. create a unit if no unit by the name exists.
     36#define CREATE_MASK     2
     37// 3. parse the unit name to try to find previously defined units.
     38#define PARSE_MASK      4
     39
    3040class RpUnits;
    3141
    32 class unit
    33 {
    34 
     42class RpUnitsPreset {
    3543    public:
    36 
    37         const std::string getUnits(){ return units; };
    38         double getExponent() {return exponent;};
    39         const RpUnits * getBasis() {return basis;};
    40 
    41         friend class RpUnits;
    42 
    43     private:
    44         const std::string units;
    45         double exponent;
    46         const RpUnits* basis;
    47 
    48         unit* prev;
    49         unit* next;
    50 
    51         // private constructor
    52         unit (
    53                 const std::string& units,
    54                 double&            exponent,
    55                 const RpUnits*     basis,
    56                 unit*              prev,
    57                 unit*              next
    58              )
    59             :   units    (units),
    60                 exponent (exponent),
    61                 basis    (basis),
    62                 prev     (prev),
    63                 next     (next)
    64             {};
    65 
    66         /*
    67         // private copy constructor
    68         unit ( const unit& other )
    69             :   units        other.units,
    70                 exponent     other.exponent,
    71                 basis        other.basis,
    72                 prev         unit(other.prev),
    73                 next         unit(other.next)
    74             {};
    75 
    76         // copy assignment operator
    77         unit& operator= (unit& other)
    78             :   units        other.units,
    79                 exponent     other.exponent,
    80                 basis        RpUnits(other.basis),
    81                 prev         unit(other.prev),
    82                 next         unit(other.next)
    83         {}
    84         */
    85 
    86         /*
    87         // destructor (its not virtual yet, still testing)
    88         ~unit () {
    89 
    90         }
    91         */
    92 
    93         void newExponent(double newExponent) {exponent = newExponent;};
    94 
    95 
     44        RpUnitsPreset () {
     45            addPresetAll();
     46        };
     47
     48        static int addPresetAll();
     49        static int addPresetEnergy();
     50        static int addPresetLength();
     51        static int addPresetTemp();
     52        static int addPresetTime();
     53        static int addPresetVolume();
    9654};
    9755
     
    256214};
    257215
     216class RpUnitsListEntry
     217{
     218
     219    public:
     220
     221        // constructor
     222        RpUnitsListEntry (const RpUnits* unit, double exponent)
     223            : unit     (unit),
     224              exponent (exponent)
     225        {};
     226
     227        // copy constructor
     228        RpUnitsListEntry (const RpUnitsListEntry& other)
     229            : unit     (other.unit),
     230              exponent (other.exponent)
     231        {};
     232
     233        // copy assignment operator
     234        RpUnitsListEntry& operator= (const RpUnitsListEntry& other) {
     235            unit = other.unit;
     236            exponent = other.exponent;
     237            return *this;
     238        }
     239
     240        // negate the exponent
     241        void negateExponent() const;
     242
     243        // print the name of the object
     244        std::string name() const;
     245
     246        // report the basis of the RpUnits object being stored.
     247        const RpUnits* getBasis() const;
     248
     249        // return the RpUnits*
     250        const RpUnits* getUnitsObj() const;
     251
     252        // return the exponent
     253        double getExponent() const;
     254
     255        // destructor
     256        virtual ~RpUnitsListEntry ()
     257        {}
     258
     259    private:
     260
     261        const RpUnits* unit;
     262        mutable double exponent;
     263};
     264
    258265class RpUnits
    259266{
     
    261268     * helpful units site
    262269     * http://aurora.regenstrief.org/~gunther/units.html
     270     * http://www.nodc.noaa.gov/dsdt/ucg/
     271     * http://www.cellml.org/meeting_minutes/archive/
     272     *        20010110_meeting_minutes.html
    263273     */
    264274
     
    272282
    273283        // convert from one RpUnits to another if the conversion is defined
    274         double convert(         const RpUnits* toUnits, 
    275                                 double val, 
     284        double convert(         const RpUnits* toUnits,
     285                                double val,
    276286                                int* result=NULL    ) const;
    277287        // convert from one RpUnits to another if the conversion is defined
     
    292302                                     int* result = NULL );
    293303
     304        /*
     305        static std::string convert ( std::string val,
     306                                     std::string toUnits,
     307                                     int showUnits,
     308                                     int* result = NULL );
     309        */
     310
    294311        // dictionary to store the units.
    295312        // static RpDict<std::string,RpUnits> dict;
     
    312329        // add RpUnits Object
    313330        static RpUnits * define(const std::string units,
    314                                 const RpUnits* basis);
    315                                 // unsigned int create=0);
    316         static RpUnits * defineCmplx(   const std::string units,
    317                                         const RpUnits* basis);
     331                                const RpUnits* basis=NULL   );
     332        // static RpUnits * defineCmplx(   const std::string units,
     333        //                                 const RpUnits* basis);
    318334        //
    319335        // add relation rule
     
    357373        friend class RpDictEntry<std::string,RpUnits*>;
    358374
     375        friend int insert(std::string key,RpUnits* val);
     376
     377
    359378        // copy constructor
    360379        RpUnits (RpUnits &other)
    361             : head (NULL),
     380            : units    (other.units),
     381              exponent (other.exponent),
     382              basis    (other.basis),
    362383              convList (NULL)
    363384        {
    364             unit* p = NULL;
    365             unit* current = NULL;
    366385            convEntry* q = NULL;
    367             convEntry* curr2 = NULL;
     386            convEntry* curr = NULL;
    368387
    369388            dict = other.dict;
    370389
    371             if (other.head) {
    372                 p = other.head;
    373                 head = new unit(p->units, p->exponent, p->basis, NULL, NULL);
    374                 current = head;
    375 
    376                 while (p->next) {
    377                     p = p->next;
    378                     current->next = new unit( p->units,
    379                                               p->exponent,
    380                                               p->basis,
    381                                               current,
    382                                               NULL        );
    383                     current = current->next;
    384                 }
    385             }
    386390            if (other.convList) {
    387391                q = other.convList;
    388392                convList = new convEntry (q->conv,NULL,NULL);
    389                 curr2 = convList;
     393                curr = convList;
    390394                while (q->next) {
    391395                    q = q->next;
    392                     curr2->next = new convEntry (q->conv,curr2,NULL);
    393                     curr2 = curr2->next;
     396                    curr->next = new convEntry (q->conv,curr,NULL);
     397                    curr = curr->next;
    394398                }
    395399            }
     
    399403        RpUnits& operator= (const RpUnits& other) {
    400404
    401             unit* p = NULL;
    402             unit* current = NULL;
    403405            convEntry* q = NULL;
    404             convEntry* curr2 = NULL;
     406            convEntry* curr = NULL;
    405407
    406408            if ( this != &other ) {
    407                 delete head;
    408409                delete convList;
    409410            }
    410411
    411412            dict = other.dict;
    412 
    413             if (other.head) {
    414                 p = other.head;
    415                 head = new unit(p->units, p->exponent, p->basis, NULL, NULL);
    416                 current = head;
    417 
    418                 while (p->next) {
    419                     p = p->next;
    420                     current->next = new unit( p->units,
    421                                               p->exponent,
    422                                               p->basis,
    423                                               current,
    424                                               NULL        );
    425                     current = current->next;
    426                 }
    427             }
     413            units = other.units;
     414            exponent = other.exponent;
     415            basis = other.basis;
     416
    428417            if (other.convList) {
    429418                q = other.convList;
    430419                convList = new convEntry (q->conv,NULL,NULL);
    431                 curr2 = convList;
     420                curr = convList;
    432421                while (q->next) {
    433422                    q = q->next;
    434                     curr2->next = new convEntry (q->conv,curr2,NULL);
    435                     curr2 = curr2->next;
     423                    curr->next = new convEntry (q->conv,curr,NULL);
     424                    curr = curr->next;
    436425                }
    437426            }
     
    446435            // clean up dynamic memory
    447436
    448             unit* p = head;
    449             unit* tmp = p;
    450             convEntry* p2 = convList;
    451             convEntry* tmp2 = p2;
     437            convEntry* p = convList;
     438            convEntry* tmp = p;
    452439
    453440            while (p != NULL) {
     
    455442                p = p->next;
    456443                delete tmp;
    457             }
    458 
    459             while (p2 != NULL) {
    460                 tmp2 = p2;
    461                 p2 = p2->next;
    462                 delete tmp2;
    463444            }
    464445        }
     
    480461        //
    481462
    482         // used by the RpUnits when defining units elements
    483         unit* head;
     463        std::string units;
     464        double exponent;
     465        const RpUnits* basis;
    484466
    485467        // linked list of units this RpUnit can convert to
     
    501483        //
    502484
    503 
    504 
    505485        RpUnits (
    506486                    const std::string& units,
     
    508488                    const RpUnits* basis
    509489                )
    510             :   head        ( new unit( units, exponent, basis, NULL, NULL) ),
     490            :   units       (units),
     491                exponent    (exponent),
     492                basis       (basis),
    511493                convList    (NULL)
    512494        {};
    513 
    514         void RpUnits::connectConversion(conversion* conv) const;
    515495
    516496        // insert new RpUnits object into RpUnitsTable
    517497        // returns 0 on success (object inserted or already exists)
    518498        // returns !0 on failure (object cannot be inserted or dne)
    519         int RpUnits::insert(std::string key);
    520 
    521         // link two RpUnits objects that already exist in RpUnitsTable
    522         // returns 0 on success (linking within table was successful)
    523         // returns !0 on failure (linking was not successful)
    524         // int RpUnits::link(RpUnits * unitA);
    525 
    526 
    527         static int pre_compare( std::string& units,
    528                                 const RpUnits* basis = NULL);
    529 
    530         void addUnit( const std::string& units,
    531                       double &  exponent,
    532                       const RpUnits * basis
    533                      );
    534 
    535         static int addPresetAll();
    536         static int addPresetEnergy();
    537         static int addPresetLength();
    538         static int addPresetTemp();
    539         static int addPresetTime();
    540         static int addPresetVolume();
    541 
     499        // int RpUnits::insert(std::string key) const;
     500
     501        typedef std::list<LIST_TEMPLATE> RpUnitsList;
     502        typedef RpUnitsList::iterator RpUnitsListIter;
     503
     504        void newExponent(double newExponent) {exponent = newExponent;};
     505
     506        static int units2list( const std::string& inUnits,
     507                               RpUnitsList& outList         );
     508        static int grabExponent(const std::string& inStr, double* exp);
     509        static int grabUnitString( const std::string& inStr);
     510        static const RpUnits* grabUnits (std::string inStr, int* offset);
     511        static int negateListExponents(RpUnitsList& unitsList);
     512        static int RpUnits::printList(RpUnitsList& unitsList);
     513
     514        static int RpUnits::compareListEntryBasis( RpUnitsList& fromList,
     515                                                   RpUnitsListIter& fromIter,
     516                                                   RpUnitsListIter& toIter);
     517
     518        static int RpUnits::compareListEntrySearch( RpUnitsList& fromList,
     519                                                    RpUnitsListIter& fromIter,
     520                                                    RpUnitsListIter& toIter);
     521
     522        void RpUnits::connectConversion(conversion* conv) const;
    542523
    543524};
  • trunk/include/core/RpUnitsStd.h

    r115 r116  
    1212#endif
    1313
    14 double centi2base (double centi, double power);
    15 double milli2base (double milli, double power);
    16 double micro2base (double micro, double power);
    17 double nano2base (double nano, double power);
    18 double pico2base (double pico, double power);
    19 double femto2base (double femto, double power);
    20 double atto2base (double atto, double power);
    21 double kilo2base (double kilo, double power);
    22 double mega2base (double mega, double power);
    23 double giga2base (double giga, double power);
    24 double tera2base (double tera, double power);
    25 double peta2base (double peta, double power);
     14double centi2base (double centi);
     15double milli2base (double milli);
     16double micro2base (double micro);
     17double nano2base (double nano);
     18double pico2base (double pico);
     19double femto2base (double femto);
     20double atto2base (double atto);
     21double kilo2base (double kilo);
     22double mega2base (double mega);
     23double giga2base (double giga);
     24double tera2base (double tera);
     25double peta2base (double peta);
    2626
    2727
    2828
    29 double base2centi (double base, double power);
    30 double base2milli (double base, double power);
    31 double base2micro (double base, double power);
    32 double base2nano (double base, double power);
    33 double base2pico (double base, double power);
    34 double base2femto (double base, double power);
    35 double base2atto (double base, double power);
    36 double base2kilo (double base, double power);
    37 double base2mega (double base, double power);
    38 double base2giga (double base, double power);
    39 double base2tera (double base, double power);
    40 double base2peta (double base, double power);
     29double base2centi (double base);
     30double base2milli (double base);
     31double base2micro (double base);
     32double base2nano (double base);
     33double base2pico (double base);
     34double base2femto (double base);
     35double base2atto (double base);
     36double base2kilo (double base);
     37double base2mega (double base);
     38double base2giga (double base);
     39double base2tera (double base);
     40double base2peta (double base);
    4141
    4242
    4343
    44 double angstrom2meter (double angstrom, double power);
    45 double meter2angstrom (double meters, double power);
    46 double meter2inch (double m, double power);
    47 double inch2meter (double in, double power);
    48 double meter2feet (double m, double power);
    49 double feet2meter (double ft, double power);
    50 double meter2yard (double m, double power);
    51 double yard2meter (double yd, double power);
     44double angstrom2meter (double angstrom);
     45double meter2angstrom (double meter);
     46double meter2inch (double meter);
     47double inch2meter (double in);
     48double meter2feet (double meter);
     49double feet2meter (double ft);
     50double meter2yard (double meter);
     51double yard2meter (double yd);
    5252
    5353
     
    7171
    7272
    73 double cubicMeter2usGallon (double m3, double none);
    74 double usGallon2cubicMeter (double g, double none);
    75 double cubicFeet2usGallon (double ft3, double none);
    76 double usGallon2cubicFeet (double g, double none);
     73double cubicMeter2usGallon (double m3);
     74double usGallon2cubicMeter (double gal);
     75double cubicFeet2usGallon (double ft3);
     76double usGallon2cubicFeet (double gal);
    7777
    7878
Note: See TracChangeset for help on using the changeset viewer.