00001 // parameters2.cc 00002 00003 // Illustrates how we can have a parameter maintain a value which 00004 // is the result of a complicated calculation. 00005 00006 #include "supermix.h" 00007 00008 // The shadowing capability of the parameter classes provides a 00009 // powerful and flexible functionality. 00010 // 00011 // Suppose the program defines some parameter which we must set. 00012 // Unfortunately, the value must be calculated from some other 00013 // values which may be changing under control of an optimizer or 00014 // read in from a file, etc. How do we ensure that the parameter 00015 // dynamically maintains the proper calculated value during 00016 // program execution? 00017 // 00018 // Solution: we can have the parameter shadow an object which 00019 // performs the calculation whenever it is asked to provide its 00020 // value. Here's a simple example: 00021 // we have a parameter which must be kept equal to the normal 00022 // resistance (Rn) of an SIS junction, but we must calculate 00023 // Rn from two variables we actually control: the SIS junction 00024 // area (A) and normal resistance-area product (RnA), so that 00025 // Rn = RnA/A. 00026 // 00027 // We derive a class from abstract_real_parameter which holds 00028 // pointers to two parameter values (RnA and Area) and can perform 00029 // the required calculation. We decide to call this new class 00030 // "function": 00031 00032 class function : public abstract_real_parameter 00033 { 00034 public: // DON'T FORGET THE "public:" LABEL !!! 00035 00036 // We decide that we want pointers to the argument variables, which are 00037 // to be held in parameters: 00038 abstract_real_parameter *p1; 00039 abstract_real_parameter *p2; 00040 00041 // We must put the required calulation in the body of a member function 00042 // named get() as follows (ensure you declare get() EXACTLY as shown): 00043 double get() const 00044 { return (*p1) / (*p2); } // (*p1 and *p2 behave like doubles) 00045 00046 // To make things really clever, we also define a constructor which 00047 // simply initializes our pointers from two provided arguments: 00048 function(abstract_real_parameter * num, abstract_real_parameter * denom) 00049 : p1(num), p2(denom) 00050 { } 00051 00052 }; // DON'T FORGET THE SEMICOLON AFTER THE CLOSING BRACE !!! 00053 00054 00055 // Now we can use our new class as follows: 00056 00057 int main() 00058 { 00059 parameter Rn; // here's the parameter we must calculate 00060 00061 parameter RnA, Area; // the values we actually control 00062 00063 // Now we create an object of our new class. The constructor we 00064 // defined requires that two pointer arguments be provided when 00065 // we create an object of class "function". We name our object "f": 00066 function f(& RnA, & Area); 00067 00068 // And have Rn shadow this new object 00069 Rn = & f; 00070 00071 // Here's what happens when we assign values to RnA and Area: 00072 RnA = 20 * Ohm*Micron*Micron; 00073 Area = 1.4*Micron * 1.4*Micron; 00074 00075 cout << "RnA = " << RnA/(Ohm*Micron*Micron) << " Ohm uM^2" 00076 << ", Area = " << Area/(Micron*Micron) << " uM^2" 00077 << endl; 00078 00079 // Rn gets its value from f, which uses the values in RnA and Area: 00080 cout << "Rn = " << Rn/Ohm << " Ohm" << endl; 00081 00082 // Changing RnA automatically changes Rn 00083 RnA = 10*Ohm; 00084 cout << endl 00085 << "RnA = " << RnA/(Ohm*Micron*Micron) << " Ohm uM^2" 00086 << ", Area = " << Area/(Micron*Micron) << " uM^2" 00087 << endl; 00088 cout << "Rn = " << Rn/Ohm << " Ohm" << endl; 00089 00090 // Ditto for changes to Area 00091 Area /= 2; 00092 cout << endl 00093 << "RnA = " << RnA/(Ohm*Micron*Micron) << " Ohm uM^2" 00094 << ", Area = " << Area/(Micron*Micron) << " uM^2" 00095 << endl; 00096 cout << "Rn = " << Rn/Ohm << " Ohm" << endl; 00097 00098 } 00099 00100 /* Here's the program output: 00101 00102 RnA = 20 Ohm uM^2, Area = 1.96 uM^2 00103 Rn = 10.2041 Ohm 00104 00105 RnA = 10 Ohm uM^2, Area = 1.96 uM^2 00106 Rn = 5.10204 Ohm 00107 00108 RnA = 10 Ohm uM^2, Area = 0.98 uM^2 00109 Rn = 10.2041 Ohm 00110 00111 */
Please direct comments and corrections to
supermix@submm.caltech.edu
Go to the supermix home page
Generated by
1.2.7