55 PETSC_COMM_WORLD, filename.c_str(), FILE_MODE_READ, &viewer));
76 MatCopy(temp_mat, internal_mat, DIFFERENT_NONZERO_PATTERN));
87 la::Vector& result,
const std::string& filename) {
93 PETSC_COMM_WORLD, filename.c_str(), FILE_MODE_READ, &viewer));
128 const std::string& filename,
char delimiter =
',') {
129 std::ifstream file(filename);
130 if (!file.is_open()) {
131 throw std::runtime_error(
"Could not open file: " + filename);
135 std::vector<double> data;
138 while (std::getline(file, line)) {
139 std::stringstream ss(line);
141 size_t colsInCurrentRow = 0;
143 while (std::getline(ss, cell, delimiter)) {
145 cell.erase(0, cell.find_first_not_of(
" \t\r\n"));
146 cell.erase(cell.find_last_not_of(
" \t\r\n") + 1);
150 data.push_back(std::stod(cell));
151 }
catch (
const std::exception& e) {
152 throw std::runtime_error(
153 "Failed to convert value to double at row " +
154 std::to_string(numRows + 1) +
", column " +
155 std::to_string(colsInCurrentRow + 1) +
": " + cell);
163 numCols = colsInCurrentRow;
164 }
else if (colsInCurrentRow != numCols) {
166 throw std::runtime_error(
167 "Inconsistent number of columns at row " +
168 std::to_string(numRows + 1) +
": expected " +
169 std::to_string(numCols) +
" but got " +
170 std::to_string(colsInCurrentRow));
176 return Eigen::Map<Eigen::Matrix<double, -1, -1, Eigen::RowMajor>>(
177 data.data(), numRows, numCols);
191 const std::string& filename,
char delimiter =
',') {
192 std::ifstream file(filename);
193 if (!file.is_open()) {
194 throw std::runtime_error(
"Could not open file: " + filename);
198 std::vector<double> data;
200 while (std::getline(file, line)) {
201 std::stringstream ss(line);
203 size_t colInCurrentRow = 0;
205 while (std::getline(ss, cell, delimiter)) {
207 cell.erase(0, cell.find_first_not_of(
" \t\r\n"));
208 cell.erase(cell.find_last_not_of(
" \t\r\n") + 1);
212 data.push_back(std::stod(cell));
213 }
catch (
const std::exception& e) {
214 throw std::runtime_error(
215 "Failed to convert value to double at row " +
216 std::to_string(numRows + 1) +
", column " +
217 std::to_string(colInCurrentRow + 1) +
": " + cell);
228 throw std::runtime_error(
"Empty file: " + filename);
231 return Eigen::VectorXd::Map(
232 data.data(),
static_cast<Eigen::Index
>(data.size()));