|
| 1 | +/* |
| 2 | +
|
| 3 | +Copyright (C) 2019-2020 Matthew Milton |
| 4 | +
|
| 5 | +This file is part of the LB-LMC Solver C++ Code Generation Library, included in the |
| 6 | +Open Real-Time Simulation (ORTiS) Framework. |
| 7 | +
|
| 8 | +LB-LMC Solver C++ Code Generation Library is free software: you can redistribute it and/or modify |
| 9 | +it under the terms of the GNU General Public License as published by |
| 10 | +the Free Software Foundation, either version 3 of the License, or |
| 11 | +(at your option) any later version. |
| 12 | +
|
| 13 | +LB-LMC Solver C++ Code Generation Library is distributed in the hope that it will be useful, |
| 14 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +GNU General Public License for more details. |
| 17 | +
|
| 18 | +You should have received a copy of the GNU General Public License |
| 19 | +along with LB-LMC Solver C++ Code Generation Library. If not, see <https://www.gnu.org/licenses/>. |
| 20 | +
|
| 21 | +*/ |
| 22 | + |
| 23 | +#include "codegen/System.hpp" |
| 24 | +#include "codegen/components/Component.hpp" |
| 25 | +#include "codegen/SolverEngineGenerator.hpp" |
| 26 | + |
| 27 | +#include <algorithm> |
| 28 | +#include <stdexcept> |
| 29 | +#include <utility> |
| 30 | + |
| 31 | +namespace lblmc |
| 32 | +{ |
| 33 | + |
| 34 | +System::System(std::string model_name) : |
| 35 | + name(model_name), |
| 36 | + num_nodes(0), |
| 37 | + num_ideal_voltage_sources(0), |
| 38 | + components() |
| 39 | +{ |
| 40 | + |
| 41 | + if(model_name == std::string()) |
| 42 | + { |
| 43 | + throw std::invalid_argument("System::constructor -- model_name cannot be null"); |
| 44 | + } |
| 45 | + |
| 46 | +} |
| 47 | + |
| 48 | +System::System(System&& base) : |
| 49 | + name(std::move(base.name)), |
| 50 | + num_nodes(std::move(base.num_nodes)), |
| 51 | + num_ideal_voltage_sources(std::move(base.num_ideal_voltage_sources)), |
| 52 | + components(std::move(base.components)) |
| 53 | +{} |
| 54 | + |
| 55 | +const std::string& System::getName() const |
| 56 | +{ |
| 57 | + return name; |
| 58 | +} |
| 59 | + |
| 60 | +const unsigned int& System::getNumberOfNodes() const |
| 61 | +{ |
| 62 | + return num_nodes; |
| 63 | +} |
| 64 | + |
| 65 | +const unsigned int& System::getNumberOfIdealVoltageSources() const |
| 66 | +{ |
| 67 | + return num_ideal_voltage_sources; |
| 68 | +} |
| 69 | + |
| 70 | +unsigned int System::getNumberOfSolutions() const |
| 71 | +{ |
| 72 | + return num_nodes + num_ideal_voltage_sources; |
| 73 | +} |
| 74 | + |
| 75 | +unsigned int System::getNumberOfComponents() const |
| 76 | +{ |
| 77 | + return components.size(); |
| 78 | +} |
| 79 | + |
| 80 | +Component& System::addComponent(Component* component) |
| 81 | +{ |
| 82 | + if(component == nullptr) |
| 83 | + throw std::invalid_argument("System::addComponent(Component*): component cannot be null or nonexistent"); |
| 84 | + |
| 85 | + components.push_back(std::unique_ptr<Component>{component}); |
| 86 | + |
| 87 | + unsigned int largest_node_index = components.back()->getLargestTerminalConnectionIndex(); |
| 88 | + if(largest_node_index >= num_nodes) |
| 89 | + { |
| 90 | + num_nodes = largest_node_index; |
| 91 | + } |
| 92 | + |
| 93 | + num_ideal_voltage_sources += components.back()->getNumberOfIdealVoltageSources(); |
| 94 | + |
| 95 | + return *(components.back()); |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +Component& System::addComponent(std::unique_ptr<Component>& component) |
| 100 | +{ |
| 101 | + if(component == nullptr) |
| 102 | + throw std::invalid_argument("System::addComponent(std::unique_ptr<Component>&): component cannot be null or nonexistent"); |
| 103 | + |
| 104 | + components.push_back(std::move(component)); |
| 105 | + |
| 106 | + unsigned int largest_node_index = components.back()->getLargestTerminalConnectionIndex(); |
| 107 | + if(largest_node_index >= num_nodes) |
| 108 | + { |
| 109 | + num_nodes = largest_node_index; |
| 110 | + } |
| 111 | + |
| 112 | + num_ideal_voltage_sources += components.back()->getNumberOfIdealVoltageSources(); |
| 113 | + |
| 114 | + return *(components.back()); |
| 115 | +} |
| 116 | + |
| 117 | +Component& System::addComponent(std::unique_ptr<Component>&& component) |
| 118 | +{ |
| 119 | + if(component == nullptr) |
| 120 | + throw std::invalid_argument("System::addComponent(std::unique_ptr<Component>&): component cannot be null or nonexistent"); |
| 121 | + |
| 122 | + components.push_back(std::move(component)); |
| 123 | + |
| 124 | + unsigned int largest_node_index = components.back()->getLargestTerminalConnectionIndex(); |
| 125 | + if(largest_node_index >= num_nodes) |
| 126 | + { |
| 127 | + num_nodes = largest_node_index; |
| 128 | + } |
| 129 | + |
| 130 | + num_ideal_voltage_sources += components.back()->getNumberOfIdealVoltageSources(); |
| 131 | + |
| 132 | + return *(components.back()); |
| 133 | +} |
| 134 | + |
| 135 | +Component* System::getComponent(std::string name) |
| 136 | +{ |
| 137 | + auto iter = components.begin(); |
| 138 | + |
| 139 | + while(iter != components.end()) |
| 140 | + { |
| 141 | + if( (*iter)->getName() == name ) |
| 142 | + return (*iter).get(); |
| 143 | + else |
| 144 | + iter++; |
| 145 | + } |
| 146 | + |
| 147 | + return nullptr; |
| 148 | +} |
| 149 | + |
| 150 | +const Component* System::getComponent(std::string name) const |
| 151 | +{ |
| 152 | + auto iter = components.begin(); |
| 153 | + |
| 154 | + while(iter != components.end()) |
| 155 | + { |
| 156 | + if( (*iter)->getName() == name ) |
| 157 | + return (*iter).get(); |
| 158 | + else |
| 159 | + iter++; |
| 160 | + } |
| 161 | + |
| 162 | + return nullptr; |
| 163 | +} |
| 164 | + |
| 165 | +const std::vector<std::unique_ptr<Component>>& System::getComponents() const |
| 166 | +{ |
| 167 | + return components; |
| 168 | +} |
| 169 | + |
| 170 | +bool System::areComponentNamesUnique() const |
| 171 | +{ |
| 172 | + for(auto i = components.begin(); i != components.end(); i++) |
| 173 | + { |
| 174 | + for(auto j = i+1; j != components.end(); j++) |
| 175 | + { |
| 176 | + if( (*i)->getName() == (*j)->getName() ) |
| 177 | + return false; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + return true; |
| 182 | +} |
| 183 | + |
| 184 | +} //namespace lblmc |
0 commit comments