Advertisement
jig487

circuitSolver

Sep 22nd, 2021 (edited)
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.13 KB | None | 0 0
  1.     --USAGE: 1:circuit type (p or s)  2:voltage total  3..:resistor values
  2.     --Example: p 12 300 300 39000423 35184
  3. local tArgs = {...}
  4.  
  5.     --Results table colors. Change if you want
  6. local columbC = colors.green
  7. local rowC = colors.green
  8. local tableC = colors.white
  9. local tableSpace = 11
  10.  
  11. term.clear()
  12. term.setCursorPos(1,1)
  13. local rCount = #tArgs-2
  14.  
  15.     --Input error checking
  16. local inputError = "Usage: 1:circuit type (p or s)  2:voltage total  3..:resistor values"
  17. if rCount <= 0 then
  18.     error("rCount less than 0. "..inputError)
  19. elseif tArgs[1] ~= "p" and tArgs[1] ~= "s" then
  20.     error("p/s missing. "..inputError)
  21. elseif not tonumber(tArgs[2]) then
  22.     error("voltage value not a number: "..tArgs[2]..", "..type(tArgs[2])..". "..inputError)
  23. elseif rCount >= 1 then
  24.     for i = 3, #tArgs do
  25.         if not tonumber(tArgs[i]) then
  26.             error("resistor #"..i.." not a number: "..tArgs[i]..", "..type(tArgs[i])..". "..inputError)
  27.         end
  28.     end
  29. end
  30.  
  31.     --E = volts, R = resistors, I = current, P = power
  32.     --t = total
  33. local E = { t = tArgs[2] }
  34. local R = { t = 0 }
  35. local I = { t = 0 }
  36. local P = { t = 0 }
  37.  
  38. local function sci(x)
  39.     return 1*10^(x)
  40. end
  41.  
  42. local function formatE(x)
  43.     if not tonumber(x) then error("Attempted to format non-number: "..x..", "..type(x)) end
  44.     x = tonumber(x)
  45.     local num
  46.     local suffix = ""
  47.    
  48.     if     x >= sci(9) then --giga
  49.         num = x / sci(9)
  50.         suffix = "g"
  51.     elseif x >= sci(6) then --mega
  52.         num = x / sci(6)
  53.         suffix = "M"
  54.     elseif x >= sci(3) then --kilo
  55.         num = x / sci(3)
  56.         suffix = "k"
  57.     elseif x >= 1 and x < 1000 then
  58.         num = x
  59.     elseif x <= 1*10^-9 then --nano
  60.         num = x / sci(-9)
  61.         suffix = "n"
  62.     elseif x <= 1*10^-6 then --micro
  63.         num = x / sci(-6)
  64.         suffix = "u"
  65.     elseif x < 1 then --milli
  66.         num = x / sci(-3)
  67.         suffix = "m"
  68.     else error(x.." wasn't caught in formatting")
  69.     end
  70.  
  71.     local mult = 10^(4)
  72.     num = math.modf(num*mult)/mult
  73.  
  74.     return (num.." "..suffix)
  75. end
  76.  
  77.     --solve a parallel circuit given resistors and voltage total
  78. local function solveP()
  79.         --Fill in ohms and voltage row
  80.     local rBuffer = 0
  81.     for i = 3, #tArgs do
  82.         R[i-2] = tArgs[i]
  83.         E[i-2] = E.t
  84.             --Add up tArgs in buffer
  85.         rBuffer = rBuffer + 1/tArgs[i]
  86.     end
  87.     R.t = 1/rBuffer
  88.  
  89.         --Get remaining totals
  90.     I.t = E.t / R.t
  91.     P.t = E.t * I.t
  92.         --Fill in remaining rows
  93.     for i = 1, rCount do
  94.         I[i] = E[i] / R[i]
  95.         P[i] = E[i] * I[i]
  96.     end
  97. end
  98.  
  99.     --solve a series circuit given resistors and voltage total
  100. local function solveS()
  101.         --Fill in ohms row
  102.     for i = 3, #tArgs do
  103.         R[i-2] = tArgs[i]
  104.             --Add up tArgs to get resistor total
  105.         R.t = R.t + tArgs[i]
  106.     end
  107.         --Get remaining totals
  108.     I.t = E.t / R.t
  109.     P.t = E.t * I.t
  110.         --Fill in remaining rows
  111.     for i = 1, rCount do
  112.         I[i] = I.t
  113.         E[i] = I[i] * R[i]
  114.         P[i] = E[i] * I[i]
  115.     end
  116. end
  117.  
  118. local function solveCircuit() end
  119. solveCircuit = solveS
  120.  
  121.     --Switch circuit type
  122. if tArgs[1] == "p" then
  123.     solveCircuit = solveP
  124. end
  125.  
  126.     --Solve the selected circuit type
  127. solveCircuit()
  128.  
  129.     --Print results table
  130. term.setTextColor(columbC)
  131. term.write("    Totals")
  132. for i = 1, #tArgs-2 do
  133.     term.setCursorPos((i*tableSpace)+4,1)
  134.     term.write("R"..i)
  135. end
  136.  
  137. print()
  138. term.setTextColor(rowC)
  139. term.write("E = ")
  140. term.setTextColor(tableC)
  141. print(formatE(E.t))
  142.  
  143. term.setTextColor(rowC)
  144. term.write("R = ")
  145. term.setTextColor(tableC)
  146. print(formatE(R.t))
  147.  
  148. term.setTextColor(rowC)
  149. term.write("I = ")
  150. term.setTextColor(tableC)
  151. print(formatE(I.t))
  152.  
  153. term.setTextColor(rowC)
  154. term.write("P = ")
  155. term.setTextColor(tableC)
  156. print(formatE(P.t))
  157.  
  158. for i = 1, #tArgs-2 do
  159.     local x = (i*tableSpace)+4
  160.     term.setCursorPos(x,2)
  161.     term.write(formatE(E[i]))
  162.  
  163.     term.setCursorPos(x,3)
  164.     term.write(formatE(R[i]))
  165.  
  166.     term.setCursorPos(x,4)
  167.     term.write(formatE(I[i]))
  168.  
  169.     term.setCursorPos(x,5)
  170.     term.write(formatE(P[i]))
  171. end
  172. print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement