/******************************************************* * * DESCRIPTION: Electrical circuit and components classes * * AUTHOR: Mariana C. D'Abreu * * EMAIL: mdabreu@dc.uba.ar * ********************************************************/ #ifndef CIRCUIT_H #define CIRCUIT_H #include #include "graph.h" #include "bg.h" #include "excepts.h" //------------------------------- // Valores default //------------------------------- #define DEFAULT_RESISTANCE 1 #define DEFAULT_CAPACITANCE 1 #define DEFAULT_INDUCTANCE 1 #define DEFAULT_TR 1 #define DEFAULT_GC 1 //------------------------------- // Mensajes de error //------------------------------- #define INVALID_PIN_INDEX_MSG "Invalid Pin index" #define INVALID_PIN_ID_MSG "Invalid Pin id" #define INVALID_PORT_INDEX_MSG "Invalid Port index" #define INVALID_PORT_ID_MSG "Invalid Port id" #define INVALID_OPERATION_TYPE_MSG "Invalid operation type" #define CONNECT_NOCOMP_ERROR_MSG "Connect error! Not existing component: " #define CONNECT_SAMECOMP_ERROR_MSG "Connect error! Cannot connect a component to itself: " #define ADD_COMP_ERROR_MSG "Add error! Existing component: " //------------------------------- // Tipos y estructuras //------------------------------- typedef string pId; typedef enum { NegativePin, PositivePin } PinType; typedef struct { pId id; PinType type; } Pin; typedef struct { Pin p; Pin n; } Port; //****************************************************** // // Class EComp // // Description: componente de circuito eléctrico genérico // //****************************************************** class EComp { public: EComp( const Id &pid ) { id = pid; } virtual ~EComp() {}; Id getId() const { return id; } virtual bool addPin( const pId&, const PinType & ) = 0; virtual bool addPin( const Pin & ) = 0; virtual int pins() const = 0; virtual Pin pin( const pId& ) const = 0; virtual bool validPin( const pId& ) const = 0; virtual Pin * getPins() = 0; pId accessPin( const pId & ) const; virtual bool isPortComponent() const { return false; } virtual bool operator==( const EComp &ecomp ) const { return ( id == ecomp.id ); } virtual BGComp *toBGComp() const = 0; virtual string className() const = 0; protected: Id id; }; //****************************************************** // // Class PortComp // // Description: componente eléctrico genérico con pines // //****************************************************** class PortComp : public EComp { public: PortComp( const Id &pid ) :EComp( pid ) {}; virtual ~PortComp() {}; bool addPin( const pId&, const PinType &pType ) { throw PortCompException( INVALID_OPERATION_TYPE_MSG ); } bool addPin( const Pin &pin ) { throw PortCompException( INVALID_OPERATION_TYPE_MSG ); } virtual int pins() const = 0; virtual Pin pin( const pId& ) const = 0; virtual Pin * getPins() = 0; virtual bool validPin( const pId& ) const = 0; virtual bool addPort( const Port &p ) = 0; virtual int ports() const = 0; virtual Port getPort( const int index = 0 ) const = 0; virtual pId accessPortPin( const PinType &, const int index = 0 ) const = 0; bool isPortComponent() const { return true; } virtual BGComp *toBGComp() const = 0; virtual string className() const { return "PortComp"; } }; //****************************************************** // // Class OnePort // // Description: componente eléctrico con dos pines // //****************************************************** class OnePort : public PortComp { public: OnePort( const Id &pid ); ~OnePort() {}; int pins() const { return 2; } Pin pin( const pId& ) const; Pin * getPins(); bool validPin( const pId& ) const; bool addPort( const Port &p ) { throw OnePortException( INVALID_OPERATION_TYPE_MSG ); } int ports() const { return 1; } Port getPort( const int index = 1 ) const; pId accessPortPin( const PinType &, const int index = 1 ) const; virtual BGComp *toBGComp() const = 0; virtual string className() const { return "OnePort"; } private: Port port; }; //****************************************************** // // Class TwoPort // // Description: componente eléctrico con cuatro pines // //****************************************************** class TwoPort : public PortComp { public: TwoPort( const Id &pid ); virtual ~TwoPort() {}; int pins() const { return 4; } Pin pin( const pId& ) const; Pin * getPins(); bool validPin( const pId& ) const; bool addPort( const Port &p ) { throw OnePortException( INVALID_OPERATION_TYPE_MSG ); } int ports() const { return 2; } Port getPort( const int index = 1 ) const; pId accessPortPin( const PinType &, const int index = 1 ) const; virtual BGComp *toBGComp() const = 0; virtual string className() const { return "TwoPort"; } private: Port port1; Port port2; }; //****************************************************** // // Class Ground // // Description: ground de un circuito eléctrico // //****************************************************** class Ground : public EComp { public: Ground( const Id & ); ~Ground() {}; bool addPin( const pId& pid, const PinType &ptype ) { throw ECompException( INVALID_OPERATION_TYPE_MSG ); } bool addPin( const Pin &pin ) { throw ECompException( INVALID_OPERATION_TYPE_MSG ); } int pins() const { return 1; } Pin pin( const pId& ) const; bool validPin( const pId& ) const; Pin * getPins(); BGComp *toBGComp() const { BGGround *bgg = new BGGround( id ); return bgg; } virtual string className() const { return "Ground"; } private: Pin _pin; }; //****************************************************** // // Class Resistance // // Description: resistencia de un circuito eléctrico // //****************************************************** class Resistance : public OnePort { public: Resistance( const Id &id, const RealValue r = DEFAULT_RESISTANCE ) :OnePort( id ) { R = r; } ~Resistance() {}; const RealValue getResistanceConst() const { return R; } BGComp *toBGComp() const { BGResistance *bgr = new BGResistance( id, R ); return bgr; } virtual string className() const { return "Resistance"; } static const RealValue DefaultR; private: RealValue R; }; //****************************************************** // // Class Capacitor // // Description: capacitor de un circuito eléctrico // //****************************************************** class Capacitor : public OnePort { public: Capacitor( const Id &id, const RealValue c = DEFAULT_CAPACITANCE, const RealValue initLoad = 0 ) :OnePort( id ) { C = c; initialLoad = initLoad; } ~Capacitor() {}; const RealValue getCapacitorConst() const { return C; } BGComp *toBGComp() const { BGCapacitor *bgc = new BGCapacitor( id, C, initialLoad ); return bgc; } virtual string className() const { return "Capacitor"; } static const RealValue DefaultC; static const RealValue DefaultInitialLoad; private: RealValue C; RealValue initialLoad; }; //****************************************************** // // Class Inductor // // Description: inductor de un circuito eléctrico // //****************************************************** class Inductor : public OnePort { public: Inductor( const Id &id, const RealValue i = DEFAULT_INDUCTANCE, const RealValue initLoad = 0 ) :OnePort( id ) { I = i; initialLoad = initLoad; } ~Inductor() {}; const RealValue getInductorConst() const { return I; } BGComp *toBGComp() const { BGInductor *bgi = new BGInductor( id, I, initialLoad ); return bgi; } virtual string className() const { return "Inductor"; } static const RealValue DefaultI; static const RealValue DefaultInitialLoad; private: RealValue I; RealValue initialLoad; }; //****************************************************** // // Class VoltageSource // // Description: fuente de voltaje de un circuito eléctrico // //****************************************************** class VoltageSource : public OnePort { public: VoltageSource( const Id &id, Signal *ssignal ) :OnePort( id ) { signal = ssignal; } ~VoltageSource() { if( signal != NULL ) delete signal; } BGComp *toBGComp() const { BGSourceEffort *bgs = new BGSourceEffort( id, signal ); return bgs; } virtual string className() const { return "VoltageSource"; } private: Signal *signal; }; //****************************************************** // // Class CurrentSource // // Description: fuente de corriente de un circuito eléctrico // //****************************************************** class CurrentSource : public OnePort { public: CurrentSource( const Id &id, Signal *ssignal ) :OnePort( id ) { signal = ssignal; } ~CurrentSource() { if( signal != NULL ) delete signal; } BGComp *toBGComp() const { BGSourceFlow *bgs = new BGSourceFlow( id, signal ); return bgs; } virtual string className() const { return "CurrentSource"; } private: Signal *signal; }; //****************************************************** // // Class Transformer // // Description: transformador de un circuito eléctrico // //****************************************************** class Transformer : public TwoPort { public: Transformer( const Id &id, const RealValue t = DEFAULT_TR ) :TwoPort( id ) { T = t; } const RealValue getTransformerConst() const { return T; } BGComp *toBGComp() const { BGTransformer *bgt = new BGTransformer( id, T ); return bgt; } virtual string className() const { return "Transformer"; } static const RealValue DefaultTR; private: RealValue T; }; //****************************************************** // // Class Gyrator // // Description: girador de un circuito eléctrico // //****************************************************** class Gyrator : public TwoPort { public: Gyrator( const Id &id, const RealValue g = DEFAULT_GC ) :TwoPort( id ) { G = g; } const RealValue getGyratorConst() const { return G; } BGComp *toBGComp() const { BGGyrator *bgg = new BGGyrator( id, G ); return bgg; } virtual string className() const { return "Gyrator"; } static const RealValue DefaultGC; private: RealValue G; }; //****************************************************** // // Class ECircuit // // Description: circuito eléctrico // //****************************************************** class ECircuit { public: friend class EC_BG_Mapper; ECircuit( const string &aid = "" ); ~ECircuit(); bool addComponent( EComp * ); bool addComponent( VoltageSource * ); bool addComponent( CurrentSource * ); bool addComponent( Ground * ); bool delComponent( const Id & ); bool connect( const Id &, const pId &, const Id&, const pId & ); bool connect( const EComp &, const pId &, const EComp&, const pId & ); bool connect( const EComp *, const pId &, const EComp*, const pId & ); bool connect( const EComp &, const Pin &, const EComp&, const Pin & ); bool findComponent( const Id &, EComp *& ); string getId() const { return id; } ECircuit &setId( const string aid ) { id = aid; return *this; } void print( ostream &out ) const; private: bool _addComponent( EComp * ); bool removeSource( EComp * ); bool removeGround( EComp * ); typedef Graph CircuitGraph; typedef map > EComps; CircuitGraph *graph; EComps *comps; list sources; list grounds; string id; }; //------------------------------- // inline functions //------------------------------- inline pId EComp::accessPin( const pId& pid ) const { if( !validPin( pid ) ) { throw ECompException( "Invalid Pin id: " + pid ); } return ( id + "." + pid ); } inline bool OnePort::validPin( const pId& pid ) const { return ( port.p.id == pid || port.n.id == pid ); } inline bool TwoPort::validPin( const pId& pid ) const { return ( port1.p.id == pid || port1.n.id == pid || port2.p.id == pid || port2.n.id == pid ); } inline bool Ground::validPin( const pId& pid ) const { return ( _pin.id == pid ); } inline void ECircuit::print( ostream &out ) const { out << "\nCircuit " << id << ":"; graph->print( out, false ); } #endif