00001 // using_units.cc 00002 00003 // UNITS MUST BE PROPERLY USED TO GET THE RIGHT ANSWERS OUT 00004 // OF SUPERMIX !!! 00005 00006 // How to use units with numerical values in SuperMix. 00007 00008 #include "supermix.h" 00009 00010 // Numerical values with units must be stored and manipulated 00011 // in a consistent way by the many numerical routines in 00012 // SuperMix. To ensure that consistent results are obtained 00013 // no matter what units the user desires (e.g., Volts vs. 00014 // milliVolts), SuperMix uses the following conventions: 00015 // 00016 // (1) Electromagnetic formulas are expressed in their SI 00017 // form. 00018 // 00019 // (2) Amplitudes for sinusoids are RMS, not peak. 00020 // 00021 // (3) Phases are in radians. 00022 // 00023 // (4) Program results should not be dependent upon a user 00024 // knowing the actual numerical magnitude stored in a 00025 // variable by SuperMix for some physical quantity; it 00026 // should not be necessary for the user to understand 00027 // how SuperMix stores physical quantities. 00028 // 00029 // SuperMix accomplishes the 4th goal by using a defined set 00030 // of constants to scale numerical quantities in order to 00031 // convert them to its internal numerical representation. These 00032 // constants are defined in the Supermix header files "global.h" 00033 // and "units.h"; "units.h" also includes extensive comments 00034 // describing how the constants are to be used. 00035 00036 // This program gives a brief introduction to the use of units 00037 // in SuperMix. 00038 00039 int main() 00040 { 00041 // ----------------------------------------------------------------- 00042 // BASIC USE OF SUPERMIX UNITS 00043 00044 // Here we define two variables which must hold the numerical 00045 // values of some real physical quantities. WE MUST ASSIGN 00046 // VALUES TO THESE VARIABLES USING THE APPROPRIATE UNITS AS 00047 // MULTIPLIERS !!! 00048 00049 double v = 0.5*Volt; // v is now 0.5 Volts (RMS). 00050 double r = 100*Ohm; 00051 00052 // Now if we perform some calculations using these quantities, 00053 // we can be sure that the results will have the proper internal 00054 // numerical representation. 00055 00056 double i = v/r; // i holds the properly normalized current 00057 00058 // To see the result converted back into some meaningful unit, 00059 // we divide by the desired unit. We can use standard powers of 00060 // 10 multipliers as well: 00061 00062 cout << "v (volts) = " << v/(Volt) << endl; 00063 cout << "r (kohms) = " << r/(Kilo*Ohm) << endl; 00064 cout << "i (milliamps) = " << i/(Milli*Amp) << endl; 00065 00066 // Since amplitudes are RMS, getting the average power is easy: 00067 00068 cout << "P (milliwatts) = " << (v*i)/(Milli*Watt) << endl; 00069 00070 // Note the units conversion rules: Multiply by the unit to 00071 // convert a physical value into its internal representation; 00072 // divide by the unit to convert an internal value into a 00073 // physical quantity. 00074 00075 00076 // ----------------------------------------------------------------- 00077 // SOME USEFUL CONSTANTS 00078 00079 // Although not really units conversions, SuperMix also provides 00080 // some frequently-used constants for degree-radian and RMS-peak 00081 // conversions: 00082 00083 // RmsToPeak == sqrt(2.0) 00084 cout << "peak v (volts) = " << RmsToPeak*v/Volt << endl; 00085 00086 // Pi == 3.14159... 00087 cout << "sin(Pi/2) = " << sin(Pi/2) << endl; 00088 00089 // Degree == Pi/180; use to convert angles: 00090 cout << "sin(90*Degree) = " << sin(90*Degree) << endl; 00091 cout << "atan(1) = " << atan(1.0)/Degree << " degrees" << endl; 00092 00093 00094 // ----------------------------------------------------------------- 00095 // DEFINING NEW UNITS 00096 00097 // It is easy to create additional units as needed: 00098 const double Inch = 2.54*Centi*Meter; 00099 const double Mil = Inch/1000; 00100 00101 double x = 1*Mil; // this length has the correct internal form 00102 cout << x/Mil << " mil = " << x/Micron << " micron" << endl; 00103 00104 00105 // ----------------------------------------------------------------- 00106 // PREDEFINED PHYSICAL CONSTANTS IN SUPERMIX 00107 00108 // SuperMix includes several physical constants already in the 00109 // proper internal representation; see "units.h" for the complete 00110 // list. 00111 00112 // Speed of Light: 00113 const double Foot = 12*Inch; // using our unit we defined above 00114 cout << "c in Km/S = " << cLight/(Kilo*Meter/Second) << endl; 00115 cout << "c in ft/nS = " << cLight/(Foot/nSec) << endl; 00116 00117 // e/h (convert voltage to frequency using e*V == h*f): 00118 v = 1.0*Milli*Volt; 00119 double f = VoltToFreq * v; // f = (e/h)*v 00120 cout << v/(Milli*Volt) << " mV --> " << f/GHz << " gigaHertz" << endl; 00121 00122 }
Please direct comments and corrections to
supermix@submm.caltech.edu
Go to the supermix home page
Generated by
1.2.7