Sharing of my labs carried out during the TDDE18 course at Linköping University
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cc 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <string>
  5. #include "components.h"
  6. /* Lab 4 - TDDE18 - Simulator lab - Main file
  7. Usage:
  8. $ ./a.out [ITERATIONS] [NB_OUTPUT] [TIME_STEP] [BATTERY_VOLTAGE]
  9. If you are using our build script (available on our private Github), you can do:
  10. $ ./build_and_run.sh -v [ITERATIONS] [NB_OUTPUT] [TIME_STEP] [BATTERY_VOLTAGE]
  11. to run the binary in valgrind.
  12. Note: Compilation warning dues to unused parameter is normal (seen with lab assistant).
  13. New circuits can be created in the /// Simulations setup /// section (line 90) by
  14. following existing functions patterns. Add also a line at the end of the program
  15. to execute simulation.
  16. */
  17. using namespace std;
  18. /////////////////////////
  19. /// Utility functions ///
  20. /////////////////////////
  21. /// Function that print output's header
  22. void print_header(vector <Component*> circuit) {
  23. for (Component* element : circuit) {
  24. cout << setw(15) << element->get_name();
  25. }
  26. cout << endl;
  27. for (long unsigned int i = 0; i < circuit.size(); ++i) {
  28. cout << setw(8) << "Volt" << setw(7) << "Curr";
  29. }
  30. cout << endl;
  31. }
  32. /// Function that print circuit status at a given step
  33. void print_status(vector <Component*> circuit) {
  34. for (Component* element : circuit)
  35. {
  36. cout << fixed << setprecision(2) << setw(8) << element->voltage() << setw(7) << element->current();
  37. }
  38. cout << endl;
  39. }
  40. /// Simulates the circuit's charges motion
  41. void simulate(vector <Component*> circuit, int const iterations, int const nb_output, double const dt) {
  42. int output_step = iterations / nb_output;
  43. print_header(circuit);
  44. for (int step = 1; step <= iterations; ++step){
  45. // Simulate each component
  46. for (Component* element : circuit)
  47. {
  48. element->simulate(dt);
  49. }
  50. // Print info every output_step
  51. if (step % output_step == 0){
  52. print_status(circuit);
  53. }
  54. }
  55. cout << endl;
  56. }
  57. /// Delete components to avoid memory leaks
  58. void clean(vector <Component*> circuit) {
  59. for (Component* element : circuit)
  60. {
  61. delete element;
  62. }
  63. }
  64. void invalid_arguments() {
  65. cout << "Invalid arguments, usage: " << endl;
  66. cout << "./bin [ITERATIONS] [NB_OUTPUT] [TIME_STEP] [BATTERY_VOLTAGE]" << endl;
  67. cout << "Example: ./bin 200000 10 0.01 24" << endl;
  68. }
  69. /////////////////////////
  70. /// Simulations setup ///
  71. /////////////////////////
  72. // A very basic simulation, to test resistance simulation.
  73. void simulation0(int iterations, int nb_output, double dt, double battery_voltage) {
  74. Link P, N, r12;
  75. vector<Component*> circuit;
  76. circuit.push_back(new Battery("Bat", P, N, battery_voltage));
  77. circuit.push_back(new Resistor("R1", P, r12, 6.0));
  78. circuit.push_back(new Resistor("R2", r12, N, 8.0));
  79. simulate(circuit, iterations, nb_output, dt);
  80. clean(circuit);
  81. }
  82. // Corresponds to the first (top) circuit in the lab PDF (figure 1).
  83. void simulation1(int iterations, int nb_output, double dt, double battery_voltage) {
  84. Link P, N, r124, r23;
  85. vector<Component*> circuit;
  86. circuit.push_back(new Battery("Bat", P, N, battery_voltage));
  87. circuit.push_back(new Resistor("R1", P, r124, 6.0));
  88. circuit.push_back(new Resistor("R2", r124, r23, 4.0));
  89. circuit.push_back(new Resistor("R3", r23, N, 8.0));
  90. circuit.push_back(new Resistor("R4", r124, N, 12.0));
  91. simulate(circuit, iterations, nb_output, dt);
  92. clean(circuit);
  93. }
  94. // Corresponds to the second (middle) circuit in the lab PDF (figure 1).
  95. void simulation2(int iterations, int nb_output, double dt, double battery_voltage) {
  96. Link P, N, L, R;
  97. vector<Component*> circuit;
  98. circuit.push_back(new Battery("Bat", P, N, battery_voltage));
  99. circuit.push_back(new Resistor("R1", P, L, 150.0));
  100. circuit.push_back(new Resistor("R2", P, R, 50.0));
  101. circuit.push_back(new Resistor("R3", R, L, 100.0));
  102. circuit.push_back(new Resistor("R4", L, N, 300.0));
  103. circuit.push_back(new Resistor("R5", R, N, 250.0));
  104. simulate(circuit, iterations, nb_output, dt);
  105. clean(circuit);
  106. }
  107. // Corresponds to the third (bottom) circuit in the lab PDF (figure 1).
  108. void simulation3(int iterations, int nb_output, double dt, double battery_voltage) {
  109. Link P, N, L, R;
  110. vector<Component*> circuit;
  111. circuit.push_back(new Battery("Bat", P, N, battery_voltage));
  112. circuit.push_back(new Resistor("R1", P, L, 150.0));
  113. circuit.push_back(new Resistor("R2", P, R, 50.0));
  114. circuit.push_back(new Capacitor("C3", R, L, 1.0));
  115. circuit.push_back(new Resistor("R4", L, N, 300.0));
  116. circuit.push_back(new Capacitor("C5", R, N, 0.75));
  117. simulate(circuit, iterations, nb_output, dt);
  118. clean(circuit);
  119. }
  120. /////////////////////////////////////
  121. /// Main function & args. parsing ///
  122. /////////////////////////////////////
  123. int main(int argc, char* argv[]){
  124. int iterations{};
  125. int nb_output{};
  126. double dt{};
  127. double battery_voltage{};
  128. // Arguments parsing
  129. if (argc == 5)
  130. {
  131. try
  132. {
  133. vector<string> args {argv, argv + argc};
  134. iterations = std::stoi(args[1]);
  135. nb_output = std::stoi(args[2]);
  136. dt = std::stod(args[3]);
  137. battery_voltage = std::stod(args[4]);
  138. if (iterations <= 0) {
  139. throw std::logic_error{"Negative iterations"};
  140. }
  141. else if (nb_output > iterations) {
  142. throw std::logic_error{"Too many output for given iterations"};
  143. }
  144. else if (dt <= 0) {
  145. throw std::logic_error{"Time step must be strictly positive"};
  146. }
  147. }
  148. catch (std::exception& e)
  149. {
  150. cerr << "ERROR (" << e.what() << ")." << endl;
  151. invalid_arguments();
  152. return 1;
  153. }
  154. }
  155. else
  156. {
  157. cerr << "ERROR (no enough arguments)." << endl;
  158. invalid_arguments();
  159. return 1;
  160. }
  161. // Running simulations
  162. simulation0(iterations, nb_output, dt, battery_voltage);
  163. simulation1(iterations, nb_output, dt, battery_voltage);
  164. simulation2(iterations, nb_output, dt, battery_voltage);
  165. simulation3(iterations, nb_output, dt, battery_voltage);
  166. return 0;
  167. }