|
12 | 12 |
|
13 | 13 | namespace GridKit
|
14 | 14 | {
|
| 15 | + /** |
| 16 | + * Writes a vector to a file in Matrix Market format |
| 17 | + * |
| 18 | + * @param vec The vector to write |
| 19 | + * @param filename The name of the output file |
| 20 | + * @param header Additional header information/comments |
| 21 | + * @return true if the write was successful, false otherwise |
| 22 | + */ |
| 23 | + template <typename T> |
| 24 | + bool writeVectorToMatrixMarket(const std::vector<T>& vec, const std::string& filename, const std::string& header) { |
| 25 | + std::ofstream outFile(filename); |
| 26 | + |
| 27 | + if (!outFile.is_open()) { |
| 28 | + std::cerr << "Error: Could not open file " << filename << " for writing." << std::endl; |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + // Write Matrix Market header |
| 33 | + outFile << "%%MatrixMarket vector array real general" << std::endl; |
| 34 | + |
| 35 | + // Write additional header information as comments |
| 36 | + if (!header.empty()) { |
| 37 | + outFile << "% " << header << std::endl; |
| 38 | + } |
| 39 | + |
| 40 | + // Write the vector size |
| 41 | + outFile << vec.size() << std::endl; |
| 42 | + |
| 43 | + // Write the vector elements |
| 44 | + outFile << std::scientific << std::setprecision(16); |
| 45 | + for (const auto& val : vec) { |
| 46 | + outFile << val << std::endl; |
| 47 | + } |
| 48 | + |
| 49 | + outFile.close(); |
| 50 | + |
| 51 | + std::cout << "Vector successfully written to " << filename << std::endl; |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
15 | 55 |
|
16 | 56 | template <class ScalarT, typename IdxT>
|
17 | 57 | class PowerElectronicsModel : public CircuitComponent<ScalarT, IdxT>
|
@@ -324,6 +364,17 @@ namespace GridKit
|
324 | 364 | alpha_ = a;
|
325 | 365 | }
|
326 | 366 |
|
| 367 | + /** |
| 368 | + * @brief print the system residual in COO format |
| 369 | + * |
| 370 | + * @param[in] filename |
| 371 | + * @param[in] title |
| 372 | + */ |
| 373 | + void printResidualMatrixMarket(std::string filename, std::string title) |
| 374 | + { |
| 375 | + writeVectorToMatrixMarket(f_, filename, title); |
| 376 | + } |
| 377 | + |
327 | 378 | /**
|
328 | 379 | * @brief print the system Jacobian in COO format
|
329 | 380 | *
|
|
0 commit comments