Posted to tcl by jima at Thu Jun 12 18:50:58 GMT 2014view raw

  1. # demonstration of VecTcl computing
  2. # the impedance of serial RCL resonator circuit
  3.  
  4. # this proc should really go into the VecTcl core
  5. vproc logspace {from to N} {
  6. 10.^linspace(from, to, N)
  7. }
  8.  
  9. # compute pi. Should actually be in the core somehow
  10. vexpr { pi=4*atan(1) }
  11.  
  12. # the impedance of a serial circuit
  13. vproc Z {omega R C L} {
  14. R + 1i*omega*L + 1.0/(1i*omega*C)
  15. }
  16.  
  17. # create a toplevel with a plot for amplification and phase
  18. # and 3 sliders to control it
  19.  
  20. toplevel .circ
  21. set mfr [ttk::frame .circ.mfr]
  22. pack $mfr -expand yes -fill both
  23.  
  24.  
  25. ukaz::graph $mfr.gv -width 600 -height 340
  26. ukaz::graph $mfr.gph -width 600 -height 340
  27.  
  28. ttk::label $mfr.r_label -textvariable Rlabel
  29. ttk::scale $mfr.r -variable Rlog -from 0.0 -to 1.0 -command updatePlot
  30.  
  31. ttk::label $mfr.l_label -textvariable Llabel
  32. ttk::scale $mfr.l -variable Llog -from 0.0 -to 1.0 -command updatePlot
  33.  
  34. ttk::label $mfr.c_label -textvariable Clabel
  35. ttk::scale $mfr.c -variable Clog -from 0.0 -to 1.0 -command updatePlot
  36.  
  37. grid $mfr.gv - -sticky nsew
  38. grid $mfr.gph - -sticky nsew
  39. grid $mfr.r_label $mfr.r -sticky nsew
  40. grid $mfr.l_label $mfr.l -sticky nsew
  41. grid $mfr.c_label $mfr.c -sticky nsew
  42.  
  43. grid columnconfigure $mfr 1 -weight 1
  44. grid rowconfigure $mfr 0 -weight 1
  45. grid rowconfigure $mfr 1 -weight 1
  46.  
  47.  
  48. set Rlog 0.5
  49. set Llog 0.5
  50. set Clog 0.5
  51.  
  52. # plot some dummy data
  53. set vid [$mfr.gv plot {1 2 1 2} w l color blue title "Voltage over resistor"]
  54. set phid [$mfr.gph plot {1 2 1 2} w l color red title "Phase"]
  55.  
  56. $mfr.gv set log x
  57. $mfr.gph set log x
  58.  
  59. $mfr.gv set yrange 0.0 1.0
  60. $mfr.gph set yrange {*}[vexpr {list(-pi/2, pi/2)}]
  61.  
  62. proc updatePlot {args} {
  63.  
  64. vexpr {
  65. # base values
  66. R0=5.0; # 5 ohms
  67. L0=7e-3 ;# 7 mH
  68. C0=10e-6 ; # 10µF
  69. R=R0*20^::Rlog
  70. C=C0*20^::Clog
  71. L=L0*20^::Llog
  72. wres = 1.0/sqrt(L*C)
  73. }
  74.  
  75. # create text for description
  76. set ::Llabel [format "L = %g mH" [expr {$L/1e-3}]]
  77. set ::Rlabel [format "R = %g \u03a9" $R]
  78. set ::Clabel [format "C = %g \u03bcF" [expr {$C/1e-6}]]
  79. #puts "Resonance: $wres"
  80.  
  81. vexpr {
  82. # create frequency data
  83. f=logspace(1,3,300)
  84. omega=2*::pi*f
  85. # compute impedance of total resonator
  86. Z=Z(omega, R, C, L)
  87. # compute voltage at the resistor
  88. Vres=R/Z
  89. # split into absolute value and phase
  90. Vabs=abs(Vres)
  91. Vph =arg(Vres)
  92. # create interleaved lists for Vabs and Vph
  93. Vabs=interleave(f, Vabs)
  94. Vph= interleave(f, Vph)
  95. }
  96.  
  97. $::mfr.gv update $::vid data $Vabs
  98. $::mfr.gph update $::phid data $Vph
  99.  
  100. }
  101.  
  102. vproc interleave {x y} {
  103. # from two vectors of equal length
  104. # create an interleaved vector. This is needed
  105. # for e.g. canvas lines or ukaz::graph
  106. N=rows(x)
  107. z=hstack(x,y)
  108. reshape(z,2*N)
  109. }
  110.  
  111. updatePlot