Ignore:
Timestamp:
Oct 5, 2005, 8:17:26 AM (19 years ago)
Author:
dkearney
Message:
  1. More cleaning of RpUnits and RpLibrary? code
  2. added rp_result code to c++/fortran/c code
  3. added rp_children, rp_lib_node[comp,type,id] for fortran code (need to test)
  4. adjusted convert function to recognize statements as follows:

convert("5J","neV")
convert("3.12075e+28neV","J")

  1. made joules a metric unit in RpUnits.cc
  2. tested examples/app-fermi/fortran/fermi.f with new rappture library.

added units conversion.

File:
1 edited

Legend:

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

    r76 r83  
     1/*
     2 * ----------------------------------------------------------------------
     3 *  RpUnits.h - Header file for Rappture Units
     4 *
     5 *   RpUnits Class Declaration / Definition
     6 *
     7 * ======================================================================
     8 *  AUTHOR:  Derrick Kearney, Purdue University
     9 *  Copyright (c) 2004-2005
     10 *  Purdue Research Foundation, West Lafayette, IN
     11 * ======================================================================
     12 */
     13
    114#include <iostream>
    215#include <string>
     
    1124#include "RpUnitsStd.h"
    1225
    13 #ifndef _RpUNITS_H 
     26#ifndef _RpUNITS_H
    1427#define _RpUNITS_H
    1528
    16 class RpUnits; 
     29class RpUnits;
    1730
    1831class unit
     
    3043        const std::string units;
    3144        double exponent;
    32         // RpUnits* basis[4];
    33         RpUnits* basis;
     45        RpUnits* basis;
    3446
    3547        unit* prev;
    3648        unit* next;
    3749
    38         unit (
    39                 const std::string& units,
    40                 double&            exponent,
    41                 // RpUnits**           basis,
    42                 RpUnits*           basis, 
     50        // private constructor
     51        unit (
     52                const std::string& units,
     53                double&            exponent,
     54                RpUnits*           basis,
    4355                unit*              next
    4456             )
    45             :   units    (units), 
    46                 exponent (exponent), 
    47                 basis    (basis), 
     57            :   units    (units),
     58                exponent (exponent),
     59                basis    (basis),
    4860                prev     (NULL),
    4961                next     (next)
    50             { };
    51 
     62            {};
     63
     64        /*
     65        // private copy constructor
     66        unit ( unit& other )
     67            :   units       (other.units),
     68                exponent    (other.exponent),
     69                basis       (other.basis),
     70                prev        (other.prev),
     71                next        (other.next)
     72            {};
     73
     74        // copy assignment operator
     75        unit& operator= (unit& other) {
     76            units       = other.units;
     77            exponent    = other.exponent;
     78            basis       = other.basis;
     79            prev        = other.prev;
     80            next        = other.next;
     81        }
     82
     83        // destructor (its not virtual yet, still testing)
     84        ~unit () {
     85
     86        }
     87        */
    5288
    5389        void newExponent(double newExponent) {exponent = newExponent;};
     
    6197// hold the pointer to a function to do the forward conversion (from->to)
    6298// hold the pointer to a function to do the backward conversion (to->from)
    63 // 
     99//
    64100class conversion
    65101{
     
    68104        const RpUnits* getFrom()    { return (const RpUnits*) fromPtr; };
    69105        const RpUnits* getTo()      { return (const RpUnits*) toPtr; };
    70        
     106
    71107        friend class RpUnits;
    72108
     
    86122
    87123        // constructor
    88         // private because i only want RpUnits to be able to 
     124        // private because i only want RpUnits to be able to
    89125        // create a conversion
    90         conversion ( 
    91                 RpUnits* fromPtr, 
    92                 RpUnits* toPtr, 
     126        conversion (
     127                RpUnits* fromPtr,
     128                RpUnits* toPtr,
    93129                double (*convForwFxnPtr)(double),
    94130                double (*convBackFxnPtr)(double),
     
    97133             )
    98134            :   fromPtr             (fromPtr),
    99                 toPtr               (toPtr), 
     135                toPtr               (toPtr),
    100136                convForwFxnPtr      (convForwFxnPtr),
    101137                convBackFxnPtr      (convBackFxnPtr),
     
    106142                prev                (prev),
    107143                next                (next)
    108             {
    109             };
    110 
    111         conversion (
     144            {};
     145
     146        conversion (
    112147                RpUnits* fromPtr,
    113148                RpUnits* toPtr,
     
    120155             )
    121156            :   fromPtr             (fromPtr),
    122                 toPtr               (toPtr), 
     157                toPtr               (toPtr),
    123158                convForwFxnPtr      (NULL),
    124159                convBackFxnPtr      (NULL),
     
    129164                prev                (prev),
    130165                next                (next)
    131             {
    132             };
     166            {};
    133167
    134168        // copy constructor
     
    139173// used by the RpUnits class to create a linked list of the conversions
    140174// associated with the specific unit.
    141 // 
     175//
    142176// we could templitize this and make a generic linked list
    143177// or could use generic linked list class from book programming with objects
     
    156190        convEntry*  next;
    157191
    158         convEntry ( 
    159                 conversion* conv, 
     192        convEntry (
     193                conversion* conv,
    160194                convEntry*  prev,
    161195                convEntry*  next
    162196             )
    163             :   conv    (conv), 
     197            :   conv    (conv),
    164198                prev    (prev),
    165199                next    (next)
    166             { };
     200            {};
    167201
    168202        /*
     
    176210class RpUnits
    177211{
    178     /* 
     212    /*
    179213     * helpful units site
    180214     * http://aurora.regenstrief.org/~gunther/units.html
     
    182216
    183217    public:
    184        
     218
    185219        // users member fxns
    186220        std::string getUnits();
     
    190224
    191225        // convert from one RpUnits to another if the conversion is defined
    192         double convert(RpUnits* toUnits, double val, int* result = NULL); 
     226        double convert(RpUnits* toUnits, double val, int* result = NULL);
    193227        // convert from one RpUnits to another if the conversion is defined
    194         void* convert(RpUnits* toUnits, void* val, int* result = NULL); 
     228        void* convert(RpUnits* toUnits, void* val, int* result = NULL);
    195229        // convert from one RpUnits to another if the conversion is defined
    196         double convert(std::string, double val); 
     230        double convert(std::string, double val);
    197231        // convert from one RpUnits to another if the conversion is defined
    198         std::string convert (   RpUnits* toUnits, 
    199                                 double val, 
    200                                 int showUnits = 0, 
    201                                 int* result = NULL  ); 
     232        std::string convert (   RpUnits* toUnits,
     233                                double val,
     234                                int showUnits = 0,
     235                                int* result = NULL  );
    202236
    203237        static std::string convert ( std::string val,
     
    210244
    211245        // turn the current unit to the metric system
    212         // this should only be used for units that are part of the 
     246        // this should only be used for units that are part of the
    213247        // metric system. doesnt deal with exponents, just prefixes
    214248        double makeBasis(double value, int* result = NULL);
    215249        RpUnits & makeBasis(double* value, int* result = NULL);
    216        
     250
    217251        static int makeMetric(RpUnits * basis);
    218252
     
    222256        static RpUnits* find(std::string key)
    223257        {
    224             // dict.find seems to return a (RpUnits* const) so i had to 
     258            // dict.find seems to return a (RpUnits* const) so i had to
    225259            // cast it as a (RpUnits*)
    226260
    227261            // dict pointer
    228              // RpUnits* unitEntry = (RpUnits*) *(dict.find(key).getValue());
    229              RpUnits* unitEntry = (RpUnits*) *(dict->find(key).getValue());
    230 
    231             // RpUnits* unitEntry = (RpUnits*) dEntr.getValue();
     262            RpUnits* unitEntry = (RpUnits*) *(dict->find(key).getValue());
    232263
    233264            // dict pointer
    234             // if (unitEntry == (RpUnits*)dict.getNullEntry().getValue()) {
    235265            if (unitEntry == (RpUnits*)dict->getNullEntry().getValue()) {
    236266                unitEntry = NULL;
     
    246276        static RpUnits * defineCmplx(const std::string units,RpUnits * basis);
    247277        //
    248         // add relation rule 
    249 
    250         static RpUnits * define(RpUnits* from, 
    251                                 RpUnits* to, 
     278        // add relation rule
     279
     280        static RpUnits * define(RpUnits* from,
     281                                RpUnits* to,
    252282                                double (*convForwFxnPtr)(double),
    253283                                double (*convBackFxnPtr)(double));
    254284
    255         static RpUnits * define(RpUnits* from, 
    256                                 RpUnits* to, 
     285        static RpUnits * define(RpUnits* from,
     286                                RpUnits* to,
    257287                                void* (*convForwFxnPtr)(void*, void*),
    258288                                void* convForwData,
     
    271301        static int addPresets (std::string group);
    272302
    273         // undefining a relation rule is probably not needed 
     303        // undefining a relation rule is probably not needed
    274304        // int undefine(); // delete a relation
    275305
     
    279309
    280310        // why are these functions friends...
    281         // probably need to find a better way to let RpUnits 
     311        // probably need to find a better way to let RpUnits
    282312        // use the RpDict and RpDictEntry fxns
    283313        friend class RpDict<std::string,RpUnits*>;
     
    286316        // copy constructor
    287317        RpUnits ( const RpUnits& myRpUnit )
    288         { 
     318        {
    289319
    290320            /*
     
    295325            */
    296326
    297             // copy constructor for unit 
     327            // copy constructor for unit
    298328            unit* tmp = NULL;
    299329            unit* newUnit = NULL;
     
    319349
    320350            head = tmp;
    321                
     351
    322352        };
    323        
     353
    324354        /*
     355        // copy assignment operator
    325356        RpUnits& operator= (const RpUnits& myRpUnit) {
    326357
     
    332363        }
    333364        */
    334        
     365
    335366    private:
    336367
     
    348379        // move through the linked list, only converting the metric elements.
    349380        //
    350        
     381
    351382        // used by the RpUnits when defining units elements
    352383        unit* head;
     
    371402
    372403
    373         RpUnits (
    374                     const std::string& units,
    375                     double& exponent,
    376                     // RpUnits* basis[]
     404        RpUnits (
     405                    const std::string& units,
     406                    double& exponent,
    377407                    RpUnits* basis
    378408                )
     
    386416
    387417
    388        
    389         RpUnits ( 
     418
     419        RpUnits (
    390420                    RpUnits* from,
    391421                    RpUnits* to,
     
    397427            :   head (NULL),
    398428                convList (NULL),
    399                 conv (new conversion 
     429                conv (new conversion
    400430                        (from,to,convForwFxnPtr,convBackFxnPtr,prev,next))
    401         { 
     431        {
    402432            connectConversion(from);
    403433            connectConversion(to);
     
    407437
    408438
    409        
    410         RpUnits ( 
     439
     440        RpUnits (
    411441                    RpUnits* from,
    412442                    RpUnits* to,
     
    426456                                     )
    427457                     )
    428         { 
     458        {
    429459            connectConversion(from);
    430460            connectConversion(to);
     
    457487        // returns 0 on success (object inserted or already exists)
    458488        // returns !0 on failure (object cannot be inserted or dne)
    459         int RpUnits::insert(std::string key);   
     489        int RpUnits::insert(std::string key);
    460490
    461491        // link two RpUnits objects that already exist in RpUnitsTable
Note: See TracChangeset for help on using the changeset viewer.