Changeset 697

Show
Ignore:
Timestamp:
01/29/10 02:19:16 (2 years ago)
Author:
BCS
Message:

finished adding support for the 7 base unit version of SI.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/units/si2.d

    r690 r697  
    1414import rational; 
    1515 
     16template T(Tp...){alias Tp T;} 
     17 
    1618version(FullSI) 
    1719{ 
    18     template SI(int LengthN, int MassN, int TimeN, int TempN, int CurrentN, int MoleN, int LumiN, T = real) 
    19     { 
    20         alias SIB!(LengthN, 1, MassN, 1, TimeN, 1, TempN, 1, CurrentN, 1, MoleN, 1, LumiN, 1, T) SI; 
     20 
     21    private alias T!(0,0) extra; 
     22    private alias T!(0,1,0,1) extra2; 
     23 
     24    template Batch( 
     25                int LengthN,  int LengthD, 
     26                int MassN,    int MassD, 
     27                int TimeN,    int TimeD, 
     28                int TempN,    int TempD, 
     29                int CurrentN, int CurrentD, 
     30                int LumiN,    int LumiD, 
     31                int MoleN,    int MoleD 
     32                ) 
     33    { 
     34        // sanity check 
     35        static assert (Reduce!(LengthN,  LengthD).Reduced, "Length must be in reduced form"); 
     36        static assert (Reduce!(MassN,    MassD).Reduced,   "Mass must be in reduced form"); 
     37        static assert (Reduce!(TimeN,    TimeD).Reduced,   "Time must be in reduced form"); 
     38        static assert (Reduce!(TempN,    TempD).Reduced,   "Temp must be in reduced form"); 
     39        static assert (Reduce!(CurrentN, CurrentD).Reduced,"Current must be in reduced form"); 
     40        static assert (Reduce!(LumiN,    LumiD).Reduced,   "Luminous intensity must be in reduced form"); 
     41        static assert (Reduce!(MoleN,    MoleD).Reduced,   "Quantity must be in reduced form"); 
     42 
     43        /// visable units 
     44        enum LenN = LengthN,  LenD = LengthD; 
     45        enum MasN = MassN,    MasD = MassD;     /// 
     46        enum TimN = TimeN,    TimD = TimeD;     /// 
     47        enum TmpN = TempN,    TmpD = TempD;     /// 
     48        enum CurN = CurrentN, CurD = CurrentD;  /// 
     49        enum LumN = LumiN,    LumD = LumiD;     /// 
     50        enum MolN = MoleN,    MolD = MoleD;     /// 
     51    } 
     52 
     53    template SI(int LengthN, int MassN, int TimeN, int TempN, int CurrentN, int LumiN, int MoleN, T = real) 
     54    { 
     55        alias SIB!(Batch!( 
     56                /// visable units 
     57                LengthN,  1, 
     58                MassN,    1, 
     59                TimeN,    1, 
     60                TempN,    1, 
     61                CurrentN, 1, 
     62                LumiN,    1, 
     63                MoleN,    1 
     64                ), T) SI; 
    2165    } 
    2266    template SI( 
     
    2670                int TempN,    int TempD, 
    2771                int CurrentN, int CurrentD, 
     72                int LuminN,   int LuminD, 
    2873                int MoleN,    int MoleD, 
    29                 int LuminN,   int LuminD, 
    3074                T = real 
    3175                ) 
    3276    { 
    33         alias SIB!( LengthN, LengthD, MassN, MassD, TimeN, TimeD, TempN, TempD, CurrentN, CurrentD, MoleN, MoleD, LuminN, LuminD, T ) SI; 
     77        alias SIB!(Batch!( 
     78                /// visable units 
     79                LengthN,  LengthD,  
     80                MassN,    MassD, 
     81                TimeN,    TimeD, 
     82                TempN,    TempD, 
     83                CurrentN, CurrentD, 
     84                LuminN,   LuminD, 
     85                MoleN,    MoleD 
     86                ), T ) SI; 
    3487    } 
    3588} 
    3689else 
    3790{ 
     91    private alias T!() extra; 
     92    private alias T!() extra2; 
    3893    template Batch( 
    3994                int LengthN,  int LengthD, 
     
    103158    static if(Exp.CurD == 1) private enum sCur = Exp.CurN.stringof; else private enum sCur = Exp.CurN.stringof~"/"~Exp.CurD.stringof; 
    104159    /// The type name 
    105     public enum Stringof = "SI!("~sLen~","~sMas~","~sTim~","~sTmp~","~sCur~","~U.stringof~")"; 
     160    version(FullSI)  
     161    { 
     162    static if(Exp.LumD == 1) private enum sLum = Exp.LumN.stringof; else private enum sLum = Exp.LumN.stringof~"/"~Exp.LumD.stringof; 
     163    static if(Exp.MolD == 1) private enum sMol = Exp.MolN.stringof; else private enum sMol = Exp.MolN.stringof~"/"~Exp.MolD.stringof; 
     164    public enum Stringof = "SI!("~sLen~","~sMas~","~sTim~","~sTmp~","~sCur~","~sLum~","~sMol~","~U.stringof~")"; 
     165    } 
     166    else public enum Stringof = "SI!("~sLen~","~sMas~","~sTim~","~sTmp~","~sCur~","~U.stringof~")"; 
    106167 
    107168    /// the value 
     
    114175    static assert(is(SIB!(Exp,U) == This)); 
    115176 
    116     template Using(V) 
    117     { 
    118         alias SIB!(Exp,V) Using; 
    119     } 
    120  
     177    template Using(V) { alias SIB!(Exp,V) Using; } 
     178 
     179    template Alike2(V) 
     180    { 
     181        version(FullSI) enum bool Alike2 =  
     182                    Exp.LumN == V.Exp.LumN && Exp.LumD == V.Exp.LumD && 
     183                    Exp.MolN == V.Exp.MolN && Exp.MolD == V.Exp.MolD; 
     184        else enum bool Alike2 = true; 
     185    } 
    121186    template Alike(V) 
    122187    { 
    123         enum bool Alike =  
     188        enum bool Alike = Alike2!(V) && 
    124189            Exp.LenN == V.Exp.LenN && Exp.LenD == V.Exp.LenD && 
    125190            Exp.MasN == V.Exp.MasN && Exp.MasD == V.Exp.MasD && 
     
    131196 
    132197    /// find the type of "This * T" 
     198    private template Mul2(U) 
     199    { 
     200        version(FullSI) alias T!( 
     201                Reduce!(Exp.LumN*U.Exp.LumD + Exp.LumD*U.Exp.LumN, Exp.LumD*U.Exp.LumD).V, 
     202                Reduce!(Exp.MolN*U.Exp.MolD + Exp.MolD*U.Exp.MolN, Exp.MolD*U.Exp.MolD).V) Mul2; 
     203        else alias T!() Mul2; 
     204    } 
    133205    private template Mul(T) 
    134206    { 
     
    141213                        Reduce!(Exp.TimN*T.Exp.TimD + Exp.TimD*T.Exp.TimN, Exp.TimD*T.Exp.TimD).V, 
    142214                        Reduce!(Exp.TmpN*T.Exp.TmpD + Exp.TmpD*T.Exp.TmpN, Exp.TmpD*T.Exp.TmpD).V, 
    143                         Reduce!(Exp.CurN*T.Exp.CurD + Exp.CurD*T.Exp.CurN, Exp.CurD*T.Exp.CurD).V), 
     215                        Reduce!(Exp.CurN*T.Exp.CurD + Exp.CurD*T.Exp.CurN, Exp.CurD*T.Exp.CurD).V, 
     216                        Mul2!(T)), 
    144217                        U 
    145218                    ) Mul; 
     
    147220 
    148221    /// find the type of "This / T" 
    149     public template Div(T) 
     222    private template Div2(U) 
     223    { 
     224        version(FullSI) alias T!( 
     225                Reduce!(Exp.LumN*U.Exp.LumD - Exp.LumD*U.Exp.LumN, Exp.LumD*U.Exp.LumD).V, 
     226                Reduce!(Exp.MolN*U.Exp.MolD - Exp.MolD*U.Exp.MolN, Exp.MolD*U.Exp.MolD).V) Div2; 
     227        else alias T!() Div2; 
     228    } 
     229    private template Div(T) 
    150230    { 
    151231        static if(is(T : U)) 
     
    157237                        Reduce!(Exp.TimN*T.Exp.TimD - Exp.TimD*T.Exp.TimN, Exp.TimD*T.Exp.TimD).V, 
    158238                        Reduce!(Exp.TmpN*T.Exp.TmpD - Exp.TmpD*T.Exp.TmpN, Exp.TmpD*T.Exp.TmpD).V, 
    159                         Reduce!(Exp.CurN*T.Exp.CurD - Exp.CurD*T.Exp.CurN, Exp.CurD*T.Exp.CurD).V), 
     239                        Reduce!(Exp.CurN*T.Exp.CurD - Exp.CurD*T.Exp.CurN, Exp.CurD*T.Exp.CurD).V, 
     240                        Div2!(T)), 
    160241                        U 
    161242                    ) Div; 
     
    163244 
    164245    /// find the type of "This ^ 1/i" 
    165     static public template RootT(int i) 
     246    private template Root2(int i) 
     247    { 
     248        version(FullSI) alias T!( 
     249                Reduce!(Exp.LumN,Exp.LumD*i).V, 
     250                Reduce!(Exp.MolN,Exp.MolD*i).V) Root2; 
     251        else alias T!() Root2; 
     252    } 
     253    private static template RootT(int i) 
    166254    { 
    167255        alias SIB!(Batch!( 
     
    170258            Reduce!(Exp.TimN, Exp.TimD*i).V, 
    171259            Reduce!(Exp.TmpN, Exp.TmpD*i).V, 
    172             Reduce!(Exp.CurN, Exp.CurD*i).V 
     260            Reduce!(Exp.CurN, Exp.CurD*i).V, 
     261            Root2!(i) 
    173262            ), 
    174263            U) RootT; 
     
    176265 
    177266    /// find the type of "This ^ i" 
    178     public template Power(int i) 
     267    private template Power2(int i) 
     268    { 
     269        version(FullSI) alias T!( 
     270                Reduce!(Exp.LumN*i,Exp.LumD).V, 
     271                Reduce!(Exp.MolN*i,Exp.MolD).V) Power2; 
     272        else alias T!() Power2; 
     273    } 
     274    private template Power(int i) 
    179275    { 
    180276        alias SIB!(Batch!( 
     
    183279            Reduce!(Exp.TimN*i, Exp.TimD).V, 
    184280            Reduce!(Exp.TmpN*i, Exp.TmpD).V, 
    185             Reduce!(Exp.CurN*i, Exp.CurD).V 
     281            Reduce!(Exp.CurN*i, Exp.CurD).V, 
     282            Power2!(i) 
    186283            ), 
    187284            U) Power; 
    188285    } 
    189286 
    190     // I don;t think I need this anymore 
     287    // I don't think I need this anymore 
    191288    //This opCall(This ret) { return ret; } 
    192289 
     
    225322    auto opDiv_r(U that) { return Power!(-1)(that / this.value); }  /// 
    226323 
    227     static if(Exp.LenN == 0 && Exp.MasN == 0 && Exp.TimN == 0 && Exp.TmpN == 0 && Exp.CurN == 0) 
     324    version(FullSI) private enum unitless2 = Exp.LumN == 0 && Exp.MolN == 0; 
     325    else private enum unitless2 = true; 
     326    static if(Exp.LenN == 0 && Exp.MasN == 0 && Exp.TimN == 0 && Exp.TmpN == 0 && Exp.CurN == 0 && unitless2) 
    228327        alias value this; 
    229328    else 
     
    286385public struct Types 
    287386{ 
    288     alias SI!( 0, 0, 0, 0, 0) Value;        /// 
    289     alias SI!( 1, 0, 0, 0, 0) Distance;     /// 
    290     alias SI!( 0, 1, 0, 0, 0) Mass;         /// 
    291     alias SI!( 0, 0, 1, 0, 0) Time;         /// 
    292     alias SI!( 0, 0, 0, 1, 0) Temp;         /// 
    293     alias SI!( 0, 0, 0, 0, 1) Current;      /// 
    294  
    295     alias SI!(-3, 1, 0, 0, 0) MassDensity;  /// 
    296  
    297     alias SI!(-2,-1, 3, 0, 2) Resistance;   /// 
    298     alias SI!(-2,-1, 4, 0, 2) Capacitance;  /// 
    299  
    300     alias SI!(-1, 1,-2, 0, 0) Pressure;     /// 
    301     alias SI!(-1, 1,-1, 0, 0) Viscosity;    /// 
    302  
    303     alias SI!( 0, 0,-1, 0, 0) Frequency;    /// 
    304     alias SI!( 0, 0, 1, 0, 1) Charge;       /// 
    305     alias SI!( 0, 1,-1, 0, 0) MassFlow;     /// 
    306     alias SI!( 0, 1,-2, 0,-1) MagneticFluxD;/// 
    307  
    308     alias SI!( 1, 1,-2, 0, 0) Force;        /// 
    309     alias SI!( 1, 0,-2, 0, 0) Acceleration; /// 
    310     alias SI!( 1, 0,-1, 0, 0) Speed;        /// 
    311  
    312  
    313     alias SI!( 2, 1,-3, 0,-1) Voltage;      /// 
    314     alias SI!( 2, 1,-3, 0, 0) Power;        /// 
    315     alias SI!( 2, 1,-2, 0,-2) Inductance;   /// 
    316     alias SI!( 2, 1,-2, 0, 0) Energy;       /// 
    317     alias SI!( 2, 0, 0, 0, 0) Area;         /// 
    318     alias SI!( 2, 0,-1, 0, 0) KViscosity;   /// 
    319  
    320     alias SI!( 3, 0,-1, 0, 0) VolumeFlow;   /// 
    321     alias SI!( 3, 0, 0, 0, 0) Volume;       /// 
     387    alias SI!( 0, 0, 0, 0, 0, extra) Value;         /// 
     388    alias SI!( 1, 0, 0, 0, 0, extra) Distance;      /// 
     389    alias SI!( 0, 1, 0, 0, 0, extra) Mass;          /// 
     390    alias SI!( 0, 0, 1, 0, 0, extra) Time;          /// 
     391    alias SI!( 0, 0, 0, 1, 0, extra) Temp;          /// 
     392    alias SI!( 0, 0, 0, 0, 1, extra) Current;       /// 
     393    version(FullSI) 
     394    { 
     395    alias SI!( 0, 0, 0, 0, 0, 1, 0) Luminosity;     /// 
     396    alias SI!( 0, 0, 0, 0, 0, 0, 1) Quantity;       /// 
     397    } 
     398 
     399    alias SI!(-3, 1, 0, 0, 0, extra) MassDensity;   /// 
     400 
     401    alias SI!(-2,-1, 3, 0, 2, extra) Resistance;    /// 
     402    alias SI!(-2,-1, 4, 0, 2, extra) Capacitance;   /// 
     403 
     404    alias SI!(-1, 1,-2, 0, 0, extra) Pressure;      /// 
     405    alias SI!(-1, 1,-1, 0, 0, extra) Viscosity;     /// 
     406 
     407    alias SI!( 0, 0,-1, 0, 0, extra) Frequency;     /// 
     408    alias SI!( 0, 0, 1, 0, 1, extra) Charge;        /// 
     409    alias SI!( 0, 1,-1, 0, 0, extra) MassFlow;      /// 
     410    alias SI!( 0, 1,-2, 0,-1, extra) MagneticFluxD; /// 
     411 
     412    alias SI!( 1, 1,-2, 0, 0, extra) Force;         /// 
     413    alias SI!( 1, 0,-2, 0, 0, extra) Acceleration;  /// 
     414    alias SI!( 1, 0,-1, 0, 0, extra) Speed;         /// 
     415 
     416 
     417    alias SI!( 2, 1,-3, 0,-1, extra) Voltage;       /// 
     418    alias SI!( 2, 1,-3, 0, 0, extra) Power;         /// 
     419    alias SI!( 2, 1,-2, 0,-2, extra) Inductance;    /// 
     420    alias SI!( 2, 1,-2, 0, 0, extra) Energy;        /// 
     421    alias SI!( 2, 0, 0, 0, 0, extra) Area;          /// 
     422    alias SI!( 2, 0,-1, 0, 0, extra) KViscosity;    /// 
     423 
     424    alias SI!( 3, 0,-1, 0, 0, extra) VolumeFlow;    /// 
     425    alias SI!( 3, 0, 0, 0, 0, extra) Volume;        /// 
    322426} 
    323427 
     
    328432    public struct Thermal 
    329433    { 
    330         alias SI!( 2, 0,-2,-1, 0) ThermalCap;     /// 
    331         alias SI!( 1, 1,-3,-1, 0) ThermalCond;        /// 
    332         alias SI!( 0, 1,-3,-1, 0) ConvectionCoeff;    /// 
     434        alias SI!( 2, 0,-2,-1, 0, extra) ThermalCap;      /// 
     435        alias SI!( 1, 1,-3,-1, 0, extra) ThermalCond;     /// 
     436        alias SI!( 0, 1,-3,-1, 0, extra) ConvectionCoeff; /// 
    333437    } 
    334438    /// 
    335439    public struct FracturMechanics 
    336440    { 
    337         alias SI!(-1,2, 1,1,-2,1, 0,1, 0,1) StressIntensity;/// 
     441        alias SI!(-1,2, 1,1,-2,1, 0,1, 0,1, extra2) StressIntensity;/// 
    338442    } 
    339443} 
     
    346450template Unit(string name : "kelvin")   { enum mul = 1.0;               alias Types.Temp     type; }    /// 
    347451template Unit(string name : "ampere")   { enum mul = 1.0;               alias Types.Current  type; }    /// 
     452version(FullSI) 
     453{ 
     454//template Unit(string name : "candela")    { enum mul = 1.0;               alias Types.Luminosity type; }  /// This one is named wrong 
     455template Unit(string name : "mole")     { enum mul = 1.0;               alias Types.Quantity   type; }  /// 
     456} 
    348457 
    349458/// Distance Unit Types 
     
    465574template Alt(string name : "Wb") { enum Alt = "weber"; } 
    466575template Alt(string name : "T")  { enum Alt = "tesla"; } 
     576version(FullSI) 
     577{ 
     578template Alt(string name : "mol"){ enum Alt = "mole"; } 
     579template Alt(string name : "cd") { enum Alt = "candela"; } 
     580} 
    467581 
    468582public: 
     
    475589    writef("%s\n", OfType.foot(3.0).meter); 
    476590 
    477     SI!(1,1, 0,1, 0,1, 0,1, 0,1) v;  //pragma(msg,"v:\t"~typeof(v).stringof); 
     591    Types.Distance v;  //pragma(msg,"v:\t"~typeof(v).stringof); 
    478592 
    479593    auto v2 = v * v;  //pragma(msg,"v2:\t"~typeof(v2).stringof);