62#pragma GCC diagnostic push
63#pragma GCC diagnostic ignored "-Wold-style-cast"
64#pragma GCC diagnostic ignored "-Wignored-attributes"
68#include <unsupported/Eigen/CXX11/Tensor>
69#pragma GCC diagnostic pop
71#define MAXBUFSIZE (static_cast<int> (1e6))
72#define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
82#pragma GCC diagnostic push
83#pragma GCC diagnostic ignored "-Wold-style-cast"
85#pragma GCC diagnostic pop
100template<
typename Type>
115template<
typename T,
int dim>
116void exportMatrix(Eigen::Matrix < T, -1, dim > & matrix, word Name,
117 word type =
"python", word folder =
"./Matrices");
130void exportMatrix(List <Eigen::MatrixXd>& matrix, word Name,
131 word type =
"python", word folder =
"./Matrices");
145void exportTensor(Eigen::Tensor<T, 3 > tensor, word Name,
146 word type =
"python", word folder =
"./Matrices");
165template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
166void read_fields(PtrList<GeometricField<Type, PatchField, GeoMesh >> &
168 fileName casename,
int first_snap = 0,
int n_snap = 0);
188template<
class Type,
template<
class>
class PatchField,
class GeoMesh >
189void read_fields(PtrList<GeometricField<Type, PatchField, GeoMesh >> &
191 GeometricField<Type, PatchField, GeoMesh>& field,
192 fileName casename,
int first_snap = 0,
int n_snap = 0);
207template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
210 GeometricField<Type, PatchField, GeoMesh>& field,
226template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
228 PtrList<GeometricField<Type, PatchField, GeoMesh >> & Lfield,
229 GeometricField<Type, PatchField, GeoMesh>& field,
245template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
248 const GeometricField<Type, PatchField, GeoMesh>& field,
249 const fileName casename);
264template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
265void readLastFields(PtrList<GeometricField<Type, PatchField, GeoMesh >> &
267 const GeometricField<Type, PatchField, GeoMesh>& field,
268 const fileName casename);
281template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
282void exportFields(PtrList<GeometricField<Type, PatchField, GeoMesh >> &
284 word folder, word fieldname);
308List <Eigen::MatrixXd>
readMatrix(word folder, word mat_name);
322template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
324 fileName subfolder, fileName folder,
338template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
340 fileName subfolder, fileName folder);
352void exportList(
T& list, word folder, word filename);
366template <
typename T,
int Nrows,
typename IND>
368 word folder, word MatrixName);
381template <
typename T,
int Nrows,
typename IND>
383 word folder, word MatrixName);
394template <
typename MatrixType>
406template <
typename MatrixType>
419template <
typename TensorType>
432template <
typename TensorType>
444template <
typename MatrixType>
459template <
typename T,
int Nrows,
typename IND>
461 word folder, word MatrixName);
472template <
typename MatrixType>
486template <
typename MatrixType>
498int numberOfFiles(word folder, word MatrixName, word ext =
"");
507void writePoints(pointField points, fileName folder, fileName subfolder);
517void save(
const List<Eigen::SparseMatrix<T >> & MatrixList, word folder,
521void load(List<Eigen::SparseMatrix<T >> & MatrixList, word folder,
537template<
class Type,
template<
class>
class PatchField,
class GeoMesh >
539 const GeometricField<Type, PatchField, GeoMesh>& field,
549Ostream&
operator<< (Ostream& os,
const Eigen::Matrix < T, -1, -1 > & mat)
551 os << mat.rows() << mat.cols() << UList<T>(
const_cast<Eigen::MatrixXd&
>
552 (mat).data(), mat.size());
557Istream&
operator>> (Istream& is, Eigen::Matrix < T, -1, -1 > & mat)
561 mat.resize(nrow, ncol);
562 UList<T> list(mat.data(), nrow * ncol);
568Ostream&
operator<< (Ostream& os,
const Eigen::Matrix < T, -1, 1 > & mat)
570 os << mat.rows() << mat.cols() << UList<T>(
const_cast<Eigen::VectorXd&
>
571 (mat).data(), mat.size());
576Istream&
operator>> (Istream& is, Eigen::Matrix < T, -1, 1 > & mat)
580 mat.resize(nrow, ncol);
581 UList<T> list(mat.data(), nrow * ncol);
587Ostream&
operator<< (Ostream& os,
const Eigen::Tensor<T, 3>& tens)
589 os << tens.dimension(0) << tens.dimension(1) << tens.dimension(
590 2) << UList<T>(
const_cast<Eigen::Tensor<T, 3>&
>(tens).data(),
599 is >> d1 >> d2 >> d3;
600 tens.resize(d1, d2, d3);
601 UList<T> list(tens.data(), d1 * d2 * d3);
608template <
typename T,
int whatever,
typename IND>
610 word folder, word MatrixName)
612 typedef Eigen::Triplet<int> Trip;
613 std::vector<Trip> res;
616 std::fstream writeFile;
617 writeFile.open(folder + MatrixName, std::ios::binary | std::ios::out);
619 if (writeFile.is_open())
621 IND rows, cols, nnzs, outS, innS;
624 nnzs = m.nonZeros() ;
625 outS = m.outerSize();
626 innS = m.innerSize();
627 writeFile.write(
reinterpret_cast<const char*
> (& rows),
sizeof(IND));
628 writeFile.write(
reinterpret_cast<const char*
> (& cols),
sizeof(IND));
629 writeFile.write(
reinterpret_cast<const char*
> (& nnzs),
sizeof(IND));
630 writeFile.write(
reinterpret_cast<const char*
> (& outS),
sizeof(IND));
631 writeFile.write(
reinterpret_cast<const char*
> (& innS),
sizeof(IND));
632 writeFile.write(
reinterpret_cast<const char*
>(m.valuePtr()),
633 sizeof(
T ) * m.nonZeros());
634 writeFile.write(
reinterpret_cast<const char*
>(m.outerIndexPtr()),
635 sizeof(IND) * m.outerSize());
636 writeFile.write(
reinterpret_cast<const char*
>(m.innerIndexPtr()),
637 sizeof(IND) * m.nonZeros());
642template <
typename T,
int whatever,
typename IND>
644 word folder, word MatrixName)
646 std::fstream readFile;
647 readFile.open(folder + MatrixName, std::ios::binary | std::ios::in);
649 if (!readFile.good())
651 std::cout << folder + MatrixName <<
652 " file does not exist, try to rerun the Offline Stage!" << std::endl;
655 else if (readFile.is_open())
657 IND rows, cols, nnz, inSz, outSz;
658 readFile.read(
reinterpret_cast<char*
>(& rows ),
sizeof(IND));
659 readFile.read(
reinterpret_cast<char*
>(& cols ),
sizeof(IND));
660 readFile.read(
reinterpret_cast<char*
>(& nnz ),
sizeof(IND));
661 readFile.read(
reinterpret_cast<char*
>(& inSz ),
sizeof(IND));
662 readFile.read(
reinterpret_cast<char*
>(& outSz),
sizeof(IND));
663 m.resize(rows, cols);
665 m.resizeNonZeros(nnz);
666 readFile.read(
reinterpret_cast<char*
>(m.valuePtr()),
sizeof(
T ) * nnz );
667 readFile.read(
reinterpret_cast<char*
>(m.outerIndexPtr()),
sizeof(IND) * outSz);
668 readFile.read(
reinterpret_cast<char*
>(m.innerIndexPtr()),
sizeof(IND) * nnz );
674template <
typename MatrixType>
679 std::ofstream out(folder + MatrixName,
680 std::ios::out | std::ios::binary | std::ios::trunc);
681 typename MatrixType::Index rows = Matrix.rows(), cols = Matrix.cols();
682 out.write(
reinterpret_cast<char*
> (& rows),
683 sizeof(
typename MatrixType::Index));
684 out.write(
reinterpret_cast<char*
> (& cols),
685 sizeof(
typename MatrixType::Index));
686 out.write(
reinterpret_cast<char*
> (Matrix.data()),
687 rows * cols *
sizeof(
typename MatrixType::Scalar) );
691template <
typename MatrixType>
696 in.open(folder + MatrixName, std::ios::in | std::ios::binary);
700 std::cout << folder + MatrixName <<
701 " file does not exist, try to rerun the Offline Stage!" << std::endl;
704 else if (in.is_open())
706 typename MatrixType::Index rows = 0, cols = 0;
707 in.read(
reinterpret_cast<char*
> (& rows),
sizeof(
typename MatrixType::Index));
708 in.read(
reinterpret_cast<char*
> (& cols),
sizeof(
typename MatrixType::Index));
709 Matrix.resize(rows, cols);
710 in.read(
reinterpret_cast<char*
>(Matrix.data()),
711 rows * cols *
sizeof(
typename MatrixType::Scalar) );
717template <
typename TensorType>
721 std::ofstream out(folder + MatrixName,
722 std::ios::out | std::ios::binary | std::ios::trunc);
723 typename TensorType::Dimensions dim = Tensor.dimensions();
726 for (
int k = 0; k < dim.size(); k++)
731 out.write(
reinterpret_cast<char*
> (& dim),
732 sizeof(
typename TensorType::Dimensions));
733 out.write(
reinterpret_cast<char*
> (Tensor.data()),
734 tot *
sizeof(
typename TensorType::Scalar) );
738template <
typename TensorType>
743 in.open(folder + MatrixName, std::ios::in | std::ios::binary);
744 typename TensorType::Dimensions dim;
745 in.read(
reinterpret_cast<char*
> (& dim),
746 sizeof(
typename TensorType::Dimensions));
747 auto dims = Tensor.dimensions();
749 "The rank of the tensor you want to fill does not coincide with the rank of the tensor you are reading");
752 for (
int k = 0; k < dim.size(); k++)
758 in.read(
reinterpret_cast<char*
>(Tensor.data()),
759 tot *
sizeof(
typename TensorType::Scalar) );
763template <
typename MatrixType>
765 word folder, word MatrixName)
767 for (
int i = 0;
i < MatrixList.size();
i++)
773template <
typename MatrixType>
775 word folder, word MatrixName)
777 for (
int i = 0;
i < MatrixList.size();
i++)
783template <
typename T,
int whatever,
typename IND>
785 List<Eigen::SparseMatrix<T, whatever, IND >> & m, word folder, word MatrixName)
787 int number_of_files = numberOfFiles(folder, MatrixName);
788 std::cout <<
"Reading the Matrix " + folder + MatrixName << std::endl;
790 "Check if the file you are trying to read exists" );
791 Eigen::SparseMatrix<T, whatever, IND>
A;
793 for (
int i = 0;
i < number_of_files;
i++)
795 ReadSparseMatrix(
A, folder, MatrixName + name(
i));
801template <
typename MatrixType>
806 std::cout <<
"Reading the Matrix " + folder + MatrixName << std::endl;
808 "Check if the file you are trying to read exists" );
811 for (
int i = 0;
i < number_of_files;
i++)
Header file of the EigenFunctions class.
Implementation of the assert function for ITHACA-FV.
#define M_Assert(Expr, Msg)
Header file of the ITHACAutilities namespace.
Ostream & operator<<(Ostream &os, const Eigen::Matrix< T, -1, -1 > &mat)
Istream & operator>>(Istream &is, Eigen::Matrix< T, -1, -1 > &mat)
Namespace for input-output manipulation.
void writePoints(pointField points, fileName folder, fileName subfolder)
Write points of a mesh to a file.
void ReadDenseMatrixList(List< MatrixType > &m, word folder, word MatrixName)
Read a sparse matrix list to bynary files.
void SaveSparseMatrix(Eigen::SparseMatrix< T, Nrows, IND > &m, word folder, word MatrixName)
Export an Eigen sparse matrix into bynary format file.
GeometricField< Type, PatchField, GeoMesh > readFieldByIndex(const GeometricField< Type, PatchField, GeoMesh > &field, fileName casename, label index)
Function to read a single field by index from a folder.
void ReadDenseMatrix(MatrixType &Matrix, word folder, word MatrixName)
Read a dense matrix from a binary format file.
int numberOfFiles(word folder, word MatrixName, word ext)
Count the number of files with a certain name prefix, used by reading functions of list of matrices.
void readConvergedFields(PtrList< GeometricField< Type, PatchField, GeoMesh > > &Lfield, GeometricField< Type, PatchField, GeoMesh > &field, fileName casename)
Function to read a list of volVectorField from name of the field including only converged snapshots.
void save(const List< Eigen::SparseMatrix< T > > &MatrixList, word folder, word MatrixName)
void exportFields(PtrList< GeometricField< Type, PatchField, GeoMesh > > &field, word folder, word fieldname)
Function to export a scalar of vector field.
void load(List< Eigen::SparseMatrix< T > > &MatrixList, word folder, word MatrixName)
void exportSolution(GeometricField< Type, PatchField, GeoMesh > &s, fileName subfolder, fileName folder, word fieldName)
Export a field to file in a certain folder and subfolder.
void read_last_fields(PtrList< GeometricField< Type, PatchField, GeoMesh > > &Lfield, const GeometricField< Type, PatchField, GeoMesh > &field, const fileName casename)
Function to read a list of fields from the name of the field including only the last snapshot.
void readMiddleFields(PtrList< GeometricField< Type, PatchField, GeoMesh > > &Lfield, GeometricField< Type, PatchField, GeoMesh > &field, fileName casename)
Funtion to read a list of volVectorField from name of the field including all the intermediate snapsh...
void exportMatrix(Eigen::Matrix< T, -1, dim > &matrix, word Name, word type, word folder)
Export the reduced matrices in numpy (type=python), matlab (type=matlab) and txt (type=eigen) format ...
void exportList(T &list, word folder, word filename)
Export a list to file.
void ReadSparseMatrixList(List< Eigen::SparseMatrix< T, Nrows, IND > > &m, word folder, word MatrixName)
Read a sparse matrix list to bynary files.
void SaveDenseMatrix(MatrixType &Matrix, word folder, word MatrixName)
Save a dense matrix to a binary format file.
void ReadDenseTensor(TensorType &Tensor, word folder, word MatrixName)
Read a dense tensor from file.
void exportFvMatrix(fvMatrix< Type > &Matrix, word folder, word MatrixName)
Export an Fv Matrix to folder together with its source term.
void SaveDenseMatrixList(List< MatrixType > &MatrixList, word folder, word MatrixName)
Save a dense matrix list to bynary files.
List< Eigen::MatrixXd > readMatrix(word folder, word mat_name)
Read a three dimensional matrix from a txt file in Eigen format.
void SaveSparseMatrixList(List< MatrixType > &MatrixList, word folder, word MatrixName)
Save a sparse matrix list to bynary files.
void read_fields(PtrList< GeometricField< Type, PatchField, GeoMesh > > &Lfield, word Name, fileName casename, int first_snap, int n_snap)
Function to read a list of fields from the name of the field and casename.
void readLastFields(PtrList< GeometricField< Type, PatchField, GeoMesh > > &Lfield, const GeometricField< Type, PatchField, GeoMesh > &field, const fileName casename)
Funtion to read a list of volVectorField from name of the field including only the last snapshots.
void exportTensor(Eigen::Tensor< T, 3 > tensor, word Name, word type, word folder)
Export the reduced tensor in numpy (tipo=python), matlab (tipo=matlab) and txt (tipo=eigen) format.
void ReadSparseMatrix(Eigen::SparseMatrix< T, Nrows, IND > &m, word folder, word MatrixName)
Read an Eigen sparse matrix from a bynary format file.
void SaveDenseTensor(TensorType &Tensor, word folder, word MatrixName)
Save a dense tensor to file.
void printProgress(double percentage)
Print progress bar given the percentage.