43template<
typename Type>
47 Eigen::SparseMatrix<double>
A;
54template <
typename T,
int dim>
59 std::string message =
"The extension \"" + type +
60 "\" was not implemented. Check the list of possible extensions.";
61 M_Assert(type ==
"python" || type ==
"matlab"
62 || type ==
"eigen", message.c_str()
70 OFstream str(folder +
"/" + Name +
"_mat" + est);
71 str << Name <<
"=np.array([";
73 for (
int i = 0;
i < matrix.rows();
i++)
75 for (
int j = 0; j < matrix.cols(); j++)
79 str <<
"[" << setprecision(10) << matrix(
i, j);
83 str <<
"," << setprecision(10) << matrix(
i, j);
87 if (
i != (matrix.rows() - 1))
99 OFstream str(folder +
"/" + Name +
"_mat" + est);
102 for (
int i = 0;
i < matrix.rows();
i++)
104 for (
int j = 0; j < matrix.cols(); j++)
106 str <<
" " << setprecision(10) << matrix(
i, j);
109 if (
i != (matrix.rows() - 1))
120 const static Eigen::IOFormat CSVFormat(6,
false,
", ",
"\n");
123 ofs.open (folder +
"/" + Name +
"_mat.txt");
125 for (
int i = 0;
i < matrix.rows();
i++)
127 for (
int j = 0; j < matrix.cols(); j++)
135 ofs <<
" " << matrix(
i, j);
139 if (
i != (matrix.rows() - 1))
150 -1 > & matrix, word Name, word type,
154 -1 > & matrix, word Name, word type,
158 -1 > & matrix, word Name, word type,
162 1 > & matrix, word Name, word type,
166 1 > & matrix, word Name, word type,
170 1 > & matrix, word Name, word type,
174 word type, word folder)
176 std::string message =
"The extension \"" + type +
177 "\" was not implemented. Check the list of possible extensions.";
178 M_Assert(type ==
"python" || type ==
"matlab"
179 || type ==
"eigen", message.c_str()
185 if (type ==
"python")
188 OFstream str(folder +
"/" + Name +
"_mat" + est);
189 str << Name <<
"=np.zeros([" << matrix.size() <<
"," << matrix[0].rows() <<
190 "," << matrix[0].cols() <<
"])\n";
192 for (
int i = 0;
i < matrix.size();
i++)
194 str << Name <<
"[" <<
i <<
",:,:]=np.array([";
196 for (
int j = 0; j < matrix[0].rows(); j++)
198 for (
int k = 0; k < matrix[0].cols(); k++)
202 str <<
"[" << setprecision(10) << matrix[
i](j, k);
206 str <<
"," << setprecision(10) << matrix[
i](j, k);
210 if (j != (matrix[0].rows() - 1))
216 str <<
"]])\n" << endl;
220 else if (type ==
"matlab")
223 OFstream str(folder +
"/" + Name +
"_mat" + est);
225 for (
int i = 0;
i < matrix.size();
i++)
227 str << Name <<
"(" <<
i + 1 <<
",:,:)=[";
229 for (
int j = 0; j < matrix[0].rows(); j++)
231 for (
int k = 0; k < matrix[0].cols(); k++)
233 str <<
" " << setprecision(10) << matrix[
i](j, k);
236 if (j != (matrix[0].rows() - 1))
245 else if (type ==
"eigen")
247 for (
int i = 0;
i < matrix.size();
i++)
249 word Namei = Name + name(
i);
256 word Name, word type,
259 Eigen::MatrixXd matrix = vector;
265 word type, word folder)
267 std::string message =
"The extension \"" + type +
268 "\" was not implemented. Check the list of possible extensions.";
269 M_Assert(type ==
"python" || type ==
"matlab"
270 || type ==
"eigen", message.c_str()
276 if (type ==
"python")
279 OFstream str(folder +
"/" + Name +
"_mat" + est);
280 str << Name <<
"=np.zeros([" << tensor.dimension(0) <<
"," <<
287 for (
int i = 0;
i < tensor.dimension(0);
i++)
289 str << Name <<
"[" <<
i <<
",:,:]=np.array([";
314 str <<
"]])\n" << endl;
318 else if (type ==
"matlab")
321 OFstream str(folder +
"/" + Name +
"_mat" + est);
323 for (
int i = 0;
i < tensor.dimension(0);
i++)
325 str << Name <<
"(" <<
i + 1 <<
",:,:)=[";
347 else if (type ==
"eigen")
349 for (
int i = 0;
i < tensor.dimension(0);
i++)
352 word Namei = Name + name(
i);
362 word type, word folder);
366 word type, word folder);
370 word type, word folder);
376 struct dirent* entry;
377 dirp = opendir(folder.c_str());
378 List <Eigen::MatrixXd> result;
380 while ((entry = readdir(dirp)) != NULL)
382 if (entry->d_type == DT_REG)
390 for (
int j = 0; j < file_count ; j++)
392 word matname = folder +
"/" + mat_name + name(j) +
"_mat.txt";
402 int cols = 0, rows = 0;
405 std::ifstream infile;
406 infile.open(filename.c_str());
407 std::string message =
"The matrix file \"" + filename +
408 "\" does not exist. Check the existence of the file or the way it is named.";
409 M_Assert(infile.good() != 0, message.c_str()
412 while (! infile.eof())
415 getline(infile, line);
417 std::stringstream stream(line);
419 while (! stream.eof())
421 stream >> buff[cols * rows + temp_cols++];
440 Eigen::MatrixXd result(rows, cols);
442 for (
int i = 0;
i < rows;
i++)
444 for (
int j = 0; j < cols; j++)
446 result(
i, j) = buff[ cols *
i + j ];
453template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
455 PtrList<GeometricField<Type, PatchField, GeoMesh>>& Lfield, word Name,
456 fileName casename,
int first_snap,
int n_snap)
461 if (!Pstream::parRun())
463 Info <<
"######### Reading the Data for " << Name <<
" #########" << endl;
464 fileName rootpath(
".");
466 Foam::Time runTime2(Foam::Time::controlDictName, rootpath, casename);
468 if (first_snap >= runTime2.times().size())
470 Info <<
"Error the index of the first snapshot must be smaller than the number of snapshots"
477 last_s = runTime2.times().size();
481 last_s = min(runTime2.times().size(), n_snap + 2);
484 for (
int i = 2 + first_snap;
i < last_s + first_snap;
i++)
486 GeometricField<Type, PatchField, GeoMesh> tmp_field(
490 casename + runTime2.times()[
i].name(),
496 Lfield.append(tmp_field.clone());
500 std::cout << std::endl;
504 Info <<
"######### Reading the Data for " << Name <<
" #########" <<
506 word timename(
mesh.time().rootPath() +
"/" +
507 mesh.time().caseName() );
508 timename = timename.substr(0, timename.find_last_of(
"\\/"));
509 timename = timename +
"/" + casename +
"processor" + name(Pstream::myProcNo());
511 "processor" + name(Pstream::myProcNo()) +
"/");
513 if (first_snap > last_s)
515 Info <<
"Error the index of the first snapshot must be smaller than the number of snapshots"
525 last_s = min(last_s, n_snap + 2);
528 for (
int i = 1 + first_snap;
i < last_s + first_snap;
i++)
530 GeometricField<Type, PatchField, GeoMesh> tmp_field(
534 timename +
"/" + name(
i),
540 Lfield.append(tmp_field.clone());
548template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
550 PtrList<GeometricField<Type, PatchField, GeoMesh>>& Lfield,
551 GeometricField<Type, PatchField, GeoMesh>& field,
552 fileName casename,
int first_snap,
int n_snap)
554 if (!Pstream::parRun())
556 Info <<
"######### Reading the Data for " << field.name() <<
" #########" <<
558 fileName rootpath(
".");
559 Foam::Time runTime2(Foam::Time::controlDictName, rootpath, casename);
562 if (first_snap >= runTime2.times().size())
564 Info <<
"Error the index of the first snapshot must be smaller than the number of snapshots"
571 last_s = runTime2.times().size();
575 last_s = min(runTime2.times().size(), n_snap + 2);
578 for (
int i = 2 + first_snap;
i < last_s + first_snap;
i++)
580 GeometricField<Type, PatchField, GeoMesh> tmp_field(
584 casename + runTime2.times()[
i].name(),
590 Lfield.append(tmp_field.clone());
594 std::cout << std::endl;
598 Info <<
"######### Reading the Data for " << field.name() <<
" #########" <<
600 word timename(field.mesh().time().rootPath() +
"/" +
601 field.mesh().time().caseName() );
602 timename = timename.substr(0, timename.find_last_of(
"\\/"));
603 timename = timename +
"/" + casename +
"processor" + name(Pstream::myProcNo());
605 "processor" + name(Pstream::myProcNo()) +
"/");
607 if (first_snap > last_s)
609 Info <<
"Error the index of the first snapshot must be smaller than the number of snapshots"
619 last_s = min(last_s, n_snap + 2);
622 for (
int i = 1 + first_snap;
i < last_s + first_snap;
i++)
624 GeometricField<Type, PatchField, GeoMesh> tmp_field(
628 timename +
"/" + name(
i),
634 Lfield.append(tmp_field.clone());
642template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
644 PtrList<GeometricField<Type, PatchField, GeoMesh>>& Lfield,
645 GeometricField<Type, PatchField, GeoMesh>& field, fileName casename)
649 "No parameter dependent solutions stored into Offline folder");
653 read_fields(Lfield, field, casename + name(par) +
"/");
658template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
660 PtrList<GeometricField<Type, PatchField, GeoMesh>>& Lfield,
661 GeometricField<Type, PatchField, GeoMesh>& field,
666 "No parameter dependent solutions stored into Offline folder");
667 std::cout <<
"######### Reading the Data for " << field.name() <<
" #########"
679 GeometricField<Type, PatchField, GeoMesh> tmpField(
683 casename + name(par) +
"/" + name(last - 1),
689 Lfield.append(tmpField.clone());
694template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
696 PtrList<GeometricField<Type, PatchField, GeoMesh>>& Lfield,
697 const GeometricField<Type, PatchField, GeoMesh>& field,
698 const fileName casename)
700 if (!Pstream::parRun())
702 Info <<
"######### Reading the Data for " << field.name() <<
" #########" <<
704 fileName rootpath(
".");
705 Foam::Time runTime2(Foam::Time::controlDictName, rootpath, casename);
706 int last_s (runTime2.times().size());
708 #if defined(OFVER) && (OFVER >= 2212)
714 casename + runTime2.times()[last_s-1].name(),
722 autoPtr<GeometricField<Type, PatchField, GeoMesh>>::New
727 casename + runTime2.times()[last_s-1].name(),
733 Lfield.append(std::move(tfld));
736 std::cout << std::endl;
740 Info <<
"######### Reading the Data for " << field.name() <<
" #########" <<
742 word timename(field.mesh().time().rootPath() +
"/" +
743 field.mesh().time().caseName() );
744 timename = timename.substr(0, timename.find_last_of(
"\\/"));
745 timename = timename +
"/" + casename +
"processor" + name(Pstream::myProcNo());
747 "processor" + name(Pstream::myProcNo()) +
"/");
749 #if defined(OFVER) && (OFVER >= 2212)
755 timename +
"/" + name(last_s-1),
763 autoPtr<GeometricField<Type, PatchField, GeoMesh>>::New
768 timename +
"/" + name(last_s-1),
774 Lfield.append(std::move(tfld));
781template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
783 PtrList<GeometricField<Type, PatchField, GeoMesh>>& Lfield,
784 const GeometricField<Type, PatchField, GeoMesh>& field,
const fileName casename)
788 "No parameter dependent solutions stored into Offline folder");
799 int number_of_files = 0;
801 in.open(folder +
"/" + MatrixName + name(0) + ext,
802 std::ios::in | std::ios::binary);
803 Info << folder +
"/" + MatrixName + name(0) + ext << endl;
809 in.open(folder +
"/" + MatrixName + name(number_of_files) + ext,
810 std::ios::in | std::ios::binary);
814 return number_of_files;
817template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
819 PtrList<GeometricField<Type, PatchField, GeoMesh>>& field,
820 word folder, word fieldname)
823 Info <<
"######### Exporting the Data for " << fieldname <<
" #########" <<
826 for (
int j = 0; j < field.size() ; j++)
832 std::cout << std::endl;
836 PtrList<GeometricField<scalar, fvPatchField, volMesh>>& field,
837 word folder, word fieldname);
839 PtrList<GeometricField<scalar, fvsPatchField, surfaceMesh>>& field,
840 word folder, word fieldname);
842 PtrList<GeometricField<vector, fvPatchField, volMesh>>& field,
843 word folder, word fieldname);
845 PtrList<GeometricField<tensor, fvPatchField, volMesh>>& field,
846 word folder, word fieldname);
848template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
850 fileName subfolder, fileName folder,
853 if (!Pstream::parRun())
855 mkDir(folder +
"/" + subfolder);
857 GeometricField<Type, PatchField, GeoMesh> act(fieldName, s);
858 fileName fieldname = folder +
"/" + subfolder +
"/" + fieldName;
859 OFstream os(fieldname);
865 mkDir(folder +
"/processor" + name(Pstream::myProcNo()) +
"/" + subfolder);
867 GeometricField<Type, PatchField, GeoMesh> act(fieldName, s);
868 fileName fieldname = folder +
"/processor" + name(Pstream::myProcNo()) +
"/" +
869 subfolder +
"/" + fieldName;
870 std::cout << fieldname << std::endl;
871 OFstream os(fieldname);
878 GeometricField<scalar, fvPatchField, volMesh>& s,
879 fileName subfolder, fileName folder,
882 GeometricField<vector, fvPatchField, volMesh>& s,
883 fileName subfolder, fileName folder,
886 GeometricField<tensor, fvPatchField, volMesh>& s,
887 fileName subfolder, fileName folder,
890 GeometricField<scalar, fvsPatchField, surfaceMesh>& s,
891 fileName subfolder, fileName folder,
894template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
896 fileName subfolder, fileName folder)
898 if (!Pstream::parRun())
900 mkDir(folder +
"/" + subfolder);
902 fileName fieldname = folder +
"/" + subfolder +
"/" + s.name();
903 OFstream os(fieldname);
909 mkDir(folder +
"/processor" + name(Pstream::myProcNo()) +
"/" + subfolder);
911 fileName fieldname = folder +
"/processor" + name(Pstream::myProcNo()) +
"/" +
912 subfolder +
"/" + s.name();
913 OFstream os(fieldname);
920 GeometricField<scalar, fvPatchField, volMesh>& s,
921 fileName subfolder, fileName folder);
923 GeometricField<vector, fvPatchField, volMesh>& s,
924 fileName subfolder, fileName folder);
926 GeometricField<tensor, fvPatchField, volMesh>& s,
927 fileName subfolder, fileName folder);
929 GeometricField<scalar, fvsPatchField, surfaceMesh>& s,
930 fileName subfolder, fileName folder);
933 GeometricField<scalar, pointPatchField, pointMesh>& s,
934 fileName subfolder, fileName folder);
936 GeometricField<vector, pointPatchField, pointMesh>& s,
937 fileName subfolder, fileName folder);
939 GeometricField<tensor, pointPatchField, pointMesh>& s,
940 fileName subfolder, fileName folder);
945 if (!Pstream::parRun())
947 mkDir(folder +
"/" + subfolder);
949 fileName fieldname = folder +
"/" + subfolder +
"/" +
"points";
950 OFstream os(fieldname);
951 os <<
"FoamFile \n { \n version 2.0; \n format ascii; \n class vectorField; \n location ""1 / polyMesh""; \n object points; \n }"
953 os << points << endl;
957 mkDir(folder +
"/processor" + name(Pstream::myProcNo()) +
"/" + subfolder);
959 fileName fieldname = folder +
"/processor" + name(Pstream::myProcNo()) +
"/" +
960 subfolder +
"/" +
"points";
961 OFstream os(fieldname);
962 os <<
"FoamFile \n { \n version 2.0; \n format ascii; \n class vectorField; \n location ""1 / polyMesh""; \n object points; \n }"
964 os << points << endl;
970 int val =
static_cast<int>(percentage * 100);
971 int lpad =
static_cast<int> (percentage *
PBWIDTH);
974 if (Pstream::master())
976 printf (
"\r%3d%% [%.*s%*s]", val, lpad,
PBSTR, rpad,
"");
982void save(
const List<Eigen::SparseMatrix<T>>& MatrixList, word folder,
987 for (
int i = 0;
i < MatrixList.size();
i++)
989 word fileName = folder +
"/" + MatrixName + name(
i) +
".npz";
995void load(List<Eigen::SparseMatrix<T>>& MatrixList, word folder,
998 int number_of_files =
numberOfFiles(folder, MatrixName,
".npz");
999 std::cout <<
"Reading the Matrix " + folder +
"/" + MatrixName << std::endl;
1001 "Check if the file you are trying to read exists" );
1002 MatrixList.resize(0);
1003 Eigen::SparseMatrix<T>
A;
1005 for (
int i = 0;
i < number_of_files;
i++)
1007 cnpy::load(
A, folder +
"/" + MatrixName + name(
i) +
".npz");
1008 MatrixList.append(
A);
1014 fileName casename,
int first_snap,
int n_snap);
1017 fileName casename,
int first_snap,
int n_snap);
1020 fileName casename,
int first_snap,
int n_snap);
1023 fileName casename,
int first_snap,
int n_snap);
1026 fileName casename,
int first_snap,
int n_snap);
1028 volScalarField& field, fileName casename,
int first_snap,
int n_snap);
1030 volVectorField& field, fileName casename,
int first_snap,
int n_snap);
1032 volTensorField& field, fileName casename,
int first_snap,
int n_snap);
1034 surfaceScalarField& field, fileName casename,
int first_snap,
int n_snap);
1036 surfaceVectorField& field, fileName casename,
int first_snap,
int n_snap);
1038 volScalarField& field, fileName casename);
1040 volVectorField& field, fileName casename);
1042 volTensorField& field, fileName casename);
1044 Lfield, surfaceScalarField& field, fileName casename);
1046 Lfield, surfaceVectorField& field, fileName casename);
1048 volScalarField& field, fileName casename);
1050 volVectorField& field, fileName casename);
1052 volTensorField& field, fileName casename);
1054 Lfield, surfaceScalarField& field, fileName casename);
1056 Lfield, surfaceVectorField& field, fileName casename);
1059 const volScalarField& field,
const fileName casename);
1061 const volVectorField& field,
const fileName casename);
1063 const volTensorField& field,
const fileName casename);
1065 const surfaceScalarField& field,
const fileName casename);
1067 const surfaceVectorField& field,
const fileName casename);
1069 const volScalarField& field,
const fileName casename);
1071 const volVectorField& field,
const fileName casename);
1073 const volTensorField& field,
const fileName casename);
1075 Lfield,
const surfaceScalarField& field,
const fileName casename);
1077 Lfield,
const surfaceVectorField& field,
const fileName casename);
1083 word fieldname = folder + filename;
1084 OFstream os(fieldname);
1086 for (
int i = 0;
i < list.size();
i++)
1088 os << list[
i] << endl;
1099template void save(
const List<Eigen::SparseMatrix<double>>& MatrixList,
1100 word folder, word MatrixName);
1102template void load(List<Eigen::SparseMatrix<double>>& MatrixList, word folder,