diff --git a/src/sim/Simulation.cpp b/src/sim/Simulation.cpp index 397dac4fb..4f7974edb 100644 --- a/src/sim/Simulation.cpp +++ b/src/sim/Simulation.cpp @@ -44,51 +44,6 @@ void CSimulation::drawSim() { for (int i = 0; i < wires.size(); i++) { wires[i]->drawWire(); } - // sim - for (int i = 0; i < junctions.size(); i++) { - CJunction *ju = junctions[i]; - if (ju->isCurrentSource() == false) { - ju->setVoltage(-1); - } - ju->setVisitCount(0); - } - for (int i = 0; i < junctions.size(); i++) { - CJunction *ju = junctions[i]; - if (ju->hasName("VDD")) { - floodJunctions(ju,3.3f); - } - else if (ju->hasName("GND")) { - floodJunctions(ju, 0); - } - else if (ju->isCurrentSource()) { - floodJunctions(ju, ju->getVoltage()); - } - } -} -// Idea: count steps to VDD/GND and use it to support multiple objects on line? -void CSimulation::floodJunctions(CJunction *ju, float voltage) { - if (ju == 0) - return; - if (ju->getVisitCount() != 0) - return; - ju->setVisitCount(1); - ju->setVoltage(voltage); - for (int i = 0; i < ju->getLinksCount(); i++) { - CJunction *other = ju->getLink(i); - floodJunctions(other, voltage); - } - for (int i = 0; i < ju->getEdgesCount(); i++) { - CEdge *e = ju->getEdge(i); - CJunction *o = e->getOther(ju); - floodJunctions(o, voltage); - } - CControllerBase *cntr = ju->findOwnerController_r(); - if (cntr != 0) { - CJunction *other = cntr->findOtherJunctionIfPassable(ju); - if (other) { - floodJunctions(other, voltage); - } - } } class CShape *CSimulation::findShapeByBoundsPoint(const class Coord &p) { for (int i = 0; i < objects.size(); i++) { diff --git a/src/sim/Simulation.h b/src/sim/Simulation.h index e8c1ba81c..2d46557f6 100644 --- a/src/sim/Simulation.h +++ b/src/sim/Simulation.h @@ -17,6 +17,13 @@ class CSimulation { TArray junctions; public: + int getJunctionsCount() const { + return junctions.size(); + } + class CJunction *getJunction(int i) { + return junctions[i]; + } + void recalcBounds(); class CObject *generateBulb(); class CObject *generateWB3S(); diff --git a/src/sim/Simulator.cpp b/src/sim/Simulator.cpp index 054c21a65..cc26067c6 100644 --- a/src/sim/Simulator.cpp +++ b/src/sim/Simulator.cpp @@ -13,6 +13,7 @@ #include "Tool_Info.h" #include "Simulation.h" #include "CursorManager.h" +#include "Solver.h" CSimulator::CSimulator() { @@ -28,6 +29,7 @@ CSimulator::CSimulator() { setTool(new Tool_Move()); sim = new CSimulation(); sim->createDemo(); + solver = new CSolver(); } void CSimulator::setTool(Tool_Base *tb) { @@ -113,6 +115,9 @@ void CSimulator::drawWindow() { //glEnd(); sim->drawSim(); + // sim + solver->setSimulation(sim); + solver->solveVoltages(); if (activeTool) { activeTool->drawTool(); diff --git a/src/sim/Simulator.h b/src/sim/Simulator.h index ff1bf1efd..1f96adfec 100644 --- a/src/sim/Simulator.h +++ b/src/sim/Simulator.h @@ -14,6 +14,7 @@ class CSimulator { class Tool_Base *activeTool; class CSimulation *sim; class CursorManager *cur; + class CSolver *solver; void onKeyDown(int keyCode); void setTool(Tool_Base *tb); diff --git a/src/sim/Solver.cpp b/src/sim/Solver.cpp new file mode 100644 index 000000000..3e89aa097 --- /dev/null +++ b/src/sim/Solver.cpp @@ -0,0 +1,54 @@ +#ifdef WINDOWS +#include "Solver.h" +#include "Junction.h" +#include "Simulation.h" +#include "Controller_Base.h" + +void CSolver::solveVoltages() { + for (int i = 0; i < sim->getJunctionsCount(); i++) { + CJunction *ju = sim->getJunction(i); + if (ju->isCurrentSource() == false) { + ju->setVoltage(-1); + } + ju->setVisitCount(0); + } + for (int i = 0; i < sim->getJunctionsCount(); i++) { + CJunction *ju = sim->getJunction(i); + if (ju->hasName("VDD")) { + floodJunctions(ju, 3.3f); + } + else if (ju->hasName("GND")) { + floodJunctions(ju, 0); + } + else if (ju->isCurrentSource()) { + floodJunctions(ju, ju->getVoltage()); + } + } +} +// Idea: count steps to VDD/GND and use it to support multiple objects on line? +void CSolver::floodJunctions(CJunction *ju, float voltage) { + if (ju == 0) + return; + if (ju->getVisitCount() != 0) + return; + ju->setVisitCount(1); + ju->setVoltage(voltage); + for (int i = 0; i < ju->getLinksCount(); i++) { + CJunction *other = ju->getLink(i); + floodJunctions(other, voltage); + } + for (int i = 0; i < ju->getEdgesCount(); i++) { + CEdge *e = ju->getEdge(i); + CJunction *o = e->getOther(ju); + floodJunctions(o, voltage); + } + CControllerBase *cntr = ju->findOwnerController_r(); + if (cntr != 0) { + CJunction *other = cntr->findOtherJunctionIfPassable(ju); + if (other) { + floodJunctions(other, voltage); + } + } +} + +#endif diff --git a/src/sim/Solver.h b/src/sim/Solver.h new file mode 100644 index 000000000..397a9aa4d --- /dev/null +++ b/src/sim/Solver.h @@ -0,0 +1,17 @@ +#ifndef __SOLVER_H__ +#define __SOLVER_H__ + +#include "sim_local.h" + +class CSolver { + class CSimulation *sim; + + void floodJunctions(class CJunction *ju, float voltage); +public: + void setSimulation(class CSimulation *p) { + sim = p; + } + void solveVoltages(); +}; + +#endif \ No newline at end of file