lambert_w_diode.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright Paul A. Bristow 2016
  2. // Copyright John Z. Maddock 2016
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or
  5. // copy at http ://www.boost.org/LICENSE_1_0.txt).
  6. /*! brief Example of using Lambert W function to compute current through a diode connected transistor with preset series resistance.
  7. \details T. C. Banwell and A. Jayakumar,
  8. Exact analytical solution of current flow through diode with series resistance,
  9. Electron Letters, 36(4):291-2 (2000)
  10. DOI: doi.org/10.1049/el:20000301
  11. The current through a diode connected NPN bipolar junction transistor (BJT)
  12. type 2N2222 (See https://en.wikipedia.org/wiki/2N2222 and
  13. https://www.fairchildsemi.com/datasheets/PN/PN2222.pdf Datasheet)
  14. was measured, for a voltage between 0.3 to 1 volt, see Fig 2 for a log plot,
  15. showing a knee visible at about 0.6 V.
  16. The transistor parameter isat was estimated to be 25 fA and the ideality factor = 1.0.
  17. The intrinsic emitter resistance re was estimated from the rsat = 0 data to be 0.3 ohm.
  18. The solid curves in Figure 2 are calculated using equation 5 with rsat included with re.
  19. http://www3.imperial.ac.uk/pls/portallive/docs/1/7292572.PDF
  20. */
  21. #include <boost/math/special_functions/lambert_w.hpp>
  22. using boost::math::lambert_w0;
  23. #include <iostream>
  24. // using std::cout;
  25. // using std::endl;
  26. #include <exception>
  27. #include <stdexcept>
  28. #include <string>
  29. #include <array>
  30. #include <vector>
  31. /*!
  32. Compute thermal voltage as a function of temperature,
  33. about 25 mV at room temperature.
  34. https://en.wikipedia.org/wiki/Boltzmann_constant#Role_in_semiconductor_physics:_the_thermal_voltage
  35. \param temperature Temperature (degrees centigrade).
  36. */
  37. const double v_thermal(double temperature)
  38. {
  39. constexpr const double boltzmann_k = 1.38e-23; // joules/kelvin.
  40. const double charge_q = 1.6021766208e-19; // Charge of an electron (columb).
  41. double temp =+ 273; // Degrees C to K.
  42. return boltzmann_k * temp / charge_q;
  43. } // v_thermal
  44. /*!
  45. Banwell & Jayakumar, equation 2
  46. */
  47. double i(double isat, double vd, double vt, double nu)
  48. {
  49. double i = isat * (exp(vd / (nu * vt)) - 1);
  50. return i;
  51. } //
  52. /*!
  53. Banwell & Jayakumar, Equation 4.
  54. i current flow = isat
  55. v voltage source.
  56. isat reverse saturation current in equation 4.
  57. (might implement equation 4 instead of simpler equation 5?).
  58. vd voltage drop = v - i* rs (equation 1).
  59. vt thermal voltage, 0.0257025 = 25 mV.
  60. nu junction ideality factor (default = unity), also known as the emission coefficient.
  61. re intrinsic emitter resistance, estimated to be 0.3 ohm from low current.
  62. rsat reverse saturation current
  63. \param v Voltage V to compute current I(V).
  64. \param vt Thermal voltage, for example 0.0257025 = 25 mV, computed from boltzmann_k * temp / charge_q;
  65. \param rsat Resistance in series with the diode.
  66. \param re Instrinsic emitter resistance (estimated to be 0.3 ohm from the Rs = 0 data)
  67. \param isat Reverse saturation current (See equation 2).
  68. \param nu Ideality factor (default = unity).
  69. \returns I amp as function of V volt.
  70. */
  71. double iv(double v, double vt, double rsat, double re, double isat, double nu = 1.)
  72. {
  73. // V thermal 0.0257025 = 25 mV
  74. // was double i = (nu * vt/r) * lambert_w((i0 * r) / (nu * vt)); equ 5.
  75. rsat = rsat + re;
  76. double i = nu * vt / rsat;
  77. std::cout << "nu * vt / rsat = " << i << std::endl; // 0.000103223
  78. double x = isat * rsat / (nu * vt);
  79. std::cout << "isat * rsat / (nu * vt) = " << x << std::endl;
  80. double eterm = (v + isat * rsat) / (nu * vt);
  81. std::cout << "(v + isat * rsat) / (nu * vt) = " << eterm << std::endl;
  82. double e = exp(eterm);
  83. std::cout << "exp(eterm) = " << e << std::endl;
  84. double w0 = lambert_w0(x * e);
  85. std::cout << "w0 = " << w0 << std::endl;
  86. return i * w0 - isat;
  87. } // double iv
  88. std::array<double, 5> rss = {0., 2.18, 10., 51., 249}; // series resistance (ohm).
  89. std::array<double, 8> vds = { 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9 }; // Diode voltage.
  90. int main()
  91. {
  92. try
  93. {
  94. std::cout << "Lambert W diode current example." << std::endl;
  95. //[lambert_w_diode_example_1
  96. double x = 0.01;
  97. //std::cout << "Lambert W (" << x << ") = " << lambert_w(x) << std::endl; // 0.00990147
  98. double nu = 1.0; // Assumed ideal.
  99. double vt = v_thermal(25); // v thermal, Shockley equation, expect about 25 mV at room temperature.
  100. double boltzmann_k = 1.38e-23; // joules/kelvin
  101. double temp = 273 + 25;
  102. double charge_q = 1.6e-19; // column
  103. vt = boltzmann_k * temp / charge_q;
  104. std::cout << "V thermal "
  105. << vt << std::endl; // V thermal 0.0257025 = 25 mV
  106. double rsat = 0.;
  107. double isat = 25.e-15; // 25 fA;
  108. std::cout << "Isat = " << isat << std::endl;
  109. double re = 0.3; // Estimated from slope of straight section of graph (equation 6).
  110. double v = 0.9;
  111. double icalc = iv(v, vt, 249., re, isat);
  112. std::cout << "voltage = " << v << ", current = " << icalc << ", " << log(icalc) << std::endl; // voltage = 0.9, current = 0.00108485, -6.82631
  113. //] [/lambert_w_diode_example_1]
  114. }
  115. catch (std::exception& ex)
  116. {
  117. std::cout << ex.what() << std::endl;
  118. }
  119. } // int main()
  120. /*
  121. Output:
  122. //[lambert_w_output_1
  123. Lambert W diode current example.
  124. V thermal 0.0257025
  125. Isat = 2.5e-14
  126. nu * vt / rsat = 0.000103099
  127. isat * rsat / (nu * vt) = 2.42486e-10
  128. (v + isat * rsat) / (nu * vt) = 35.016
  129. exp(eterm) = 1.61167e+15
  130. w0 = 10.5225
  131. voltage = 0.9, current = 0.00108485, -6.82631
  132. //] [/lambert_w_output_1]
  133. */