60#include "ITHACAparameters.H"
62#pragma GCC diagnostic push
63#pragma GCC diagnostic ignored "-Wold-style-cast"
64#pragma GCC diagnostic ignored "-Wignored-attributes"
69#include <unsupported/Eigen/CXX11/Tensor>
70#pragma GCC diagnostic pop
72#define MAXBUFSIZE (static_cast<int> (1e6))
73#define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
83#pragma GCC diagnostic push
84#pragma GCC diagnostic ignored "-Wold-style-cast"
86#pragma GCC diagnostic pop
101template<
typename Type>
116template<
typename T,
int dim>
117void exportMatrix(Eigen::Matrix < T, -1, dim > & matrix, word Name,
118 word type =
"python", word folder =
"./Matrices");
131void exportMatrix(List <Eigen::MatrixXd>& matrix, word Name,
132 word type =
"python", word folder =
"./Matrices");
146void exportTensor(Eigen::Tensor<T, 3 > tensor, word Name,
147 word type =
"python", word folder =
"./Matrices");
166template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
167void read_fields(PtrList<GeometricField<Type, PatchField, GeoMesh >>&
169 fileName casename,
int first_snap = 0,
int n_snap = 0);
189template<
class Type,
template<
class>
class PatchField,
class GeoMesh >
190void read_fields(PtrList<GeometricField<Type, PatchField, GeoMesh >>&
192 GeometricField<Type, PatchField, GeoMesh>& field,
193 fileName casename,
int first_snap = 0,
int n_snap = 0);
208template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
211 GeometricField<Type, PatchField, GeoMesh>& field,
227template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
229 PtrList<GeometricField<Type, PatchField, GeoMesh >>& Lfield,
230 GeometricField<Type, PatchField, GeoMesh>& field,
246template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
249 const GeometricField<Type, PatchField, GeoMesh>& field,
250 const fileName casename);
265template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
266void readLastFields(PtrList<GeometricField<Type, PatchField, GeoMesh >>&
268 const GeometricField<Type, PatchField, GeoMesh>& field,
269 const fileName casename);
282template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
283void exportFields(PtrList<GeometricField<Type, PatchField, GeoMesh >>&
285 word folder, word fieldname);
309List <Eigen::MatrixXd>
readMatrix(word folder, word mat_name);
323template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
325 fileName subfolder, fileName folder,
339template<
class Type,
template<
class>
class PatchField,
class GeoMesh>
341 fileName subfolder, fileName folder);
353void exportList(T& list, word folder, word filename);
367template <
typename T,
int Nrows,
typename IND>
369 word folder, word MatrixName);
382template <
typename T,
int Nrows,
typename IND>
384 word folder, word MatrixName);
395template <
typename MatrixType>
407template <
typename MatrixType>
420template <
typename TensorType>
433template <
typename TensorType>
445template <
typename MatrixType>
460template <
typename T,
int Nrows,
typename IND>
462 word folder, word MatrixName);
473template <
typename MatrixType>
487template <
typename MatrixType>
499int numberOfFiles(word folder, word MatrixName, word ext =
"");
508void writePoints(pointField points, fileName folder, fileName subfolder);
518void save(
const List<Eigen::SparseMatrix<T >>& MatrixList, word folder,
522void load(List<Eigen::SparseMatrix<T >>& MatrixList, word folder,
538template<
class Type,
template<
class>
class PatchField,
class GeoMesh >
540 const GeometricField<Type, PatchField, GeoMesh>& field,
546void read_snapshot(T& snapshot,
const Foam::word snap_time,
547 Foam::word path, Foam::word name =
"default_name");
555Ostream& operator<< (Ostream& os,
const Eigen::Matrix < T, -1, -1 > & mat)
557 os << mat.rows() << mat.cols() << UList<T>(
const_cast<Eigen::Matrix<T, -1, -1
>&>
558 (mat).data(), mat.size());
563Istream& operator>> (Istream& is, Eigen::Matrix < T, -1, -1 > & mat)
567 mat.resize(nrow, ncol);
568 UList<T> list(mat.data(), nrow * ncol);
574Ostream& operator<< (Ostream& os,
const Eigen::Matrix < T, -1, 1 > & mat)
576 os << mat.rows() << mat.cols() << UList<T>(
const_cast<Eigen::Matrix<T, -1, 1
>&>
577 (mat).data(), mat.size());
582Istream& operator>> (Istream& is, Eigen::Matrix < T, -1, 1 > & mat)
586 mat.resize(nrow, ncol);
587 UList<T> list(mat.data(), nrow * ncol);
594Ostream& operator<< (Ostream& os,
const Eigen::Transpose<Eigen::Matrix<T, -1, -1>>& mat)
596 Eigen::Matrix<T, -1, -1> evaluated = mat;
597 os << evaluated.rows() << evaluated.cols() << UList<T>(evaluated.data(), evaluated.size());
603Ostream& operator<< (Ostream& os,
const Eigen::Transpose<Eigen::Matrix<T, -1, 1>>& mat)
605 Eigen::Matrix<T, 1, -1> evaluated = mat;
606 os << evaluated.rows() << evaluated.cols() << UList<T>(evaluated.data(), evaluated.size());
612Ostream& operator<< (Ostream& os,
const Eigen::Block<Eigen::Matrix<T, -1, -1>, -1, 1,
true>& mat)
614 Eigen::Matrix<T, -1, 1> evaluated = mat;
620Ostream& operator<< (Ostream& os,
const Eigen::Block<Eigen::Matrix<T, -1, -1>, 1, -1,
false>& mat)
622 Eigen::Matrix<T, 1, -1> evaluated = mat;
628Ostream& operator<< (Ostream& os,
const Eigen::Tensor<T, 3>& tens)
630 os << tens.dimension(0) << tens.dimension(1) << tens.dimension(
631 2) << UList<T>(
const_cast<Eigen::Tensor<T, 3>&
>(tens).data(),
637Istream& operator>> (Istream& is, Eigen::Tensor<T, 3>& tens)
640 is >> d1 >> d2 >> d3;
641 tens.resize(d1, d2, d3);
642 UList<T> list(tens.data(), d1 * d2 * d3);
647inline Ostream& operator<<(Ostream& os,
const Color::Modifier& mod)
650 os <<
"\033[" << int(mod.getCode()) <<
"m";
656template <
typename T,
int whatever,
typename IND>
658 word folder, word MatrixName)
660 typedef Eigen::Triplet<int> Trip;
661 std::vector<Trip> res;
664 std::fstream writeFile;
665 writeFile.open(folder + MatrixName, std::ios::binary | std::ios::out);
667 if (writeFile.is_open())
669 IND rows, cols, nnzs, outS, innS;
672 nnzs = m.nonZeros() ;
673 outS = m.outerSize();
674 innS = m.innerSize();
675 writeFile.write(
reinterpret_cast<const char*
> (& rows),
sizeof(IND));
676 writeFile.write(
reinterpret_cast<const char*
> (& cols),
sizeof(IND));
677 writeFile.write(
reinterpret_cast<const char*
> (& nnzs),
sizeof(IND));
678 writeFile.write(
reinterpret_cast<const char*
> (& outS),
sizeof(IND));
679 writeFile.write(
reinterpret_cast<const char*
> (& innS),
sizeof(IND));
680 writeFile.write(
reinterpret_cast<const char*
>(m.valuePtr()),
681 sizeof(T ) * m.nonZeros());
682 writeFile.write(
reinterpret_cast<const char*
>(m.outerIndexPtr()),
683 sizeof(IND) * m.outerSize());
684 writeFile.write(
reinterpret_cast<const char*
>(m.innerIndexPtr()),
685 sizeof(IND) * m.nonZeros());
690template <
typename T,
int whatever,
typename IND>
692 word folder, word MatrixName)
694 std::fstream readFile;
695 readFile.open(folder + MatrixName, std::ios::binary | std::ios::in);
697 if (!readFile.good())
699 Foam::Info << folder + MatrixName <<
700 " file does not exist, try to rerun the Offline Stage!" << Foam::endl;
703 else if (readFile.is_open())
705 IND rows, cols, nnz, inSz, outSz;
706 readFile.read(
reinterpret_cast<char*
>(& rows ),
sizeof(IND));
707 readFile.read(
reinterpret_cast<char*
>(& cols ),
sizeof(IND));
708 readFile.read(
reinterpret_cast<char*
>(& nnz ),
sizeof(IND));
709 readFile.read(
reinterpret_cast<char*
>(& inSz ),
sizeof(IND));
710 readFile.read(
reinterpret_cast<char*
>(& outSz),
sizeof(IND));
711 m.resize(rows, cols);
713 m.resizeNonZeros(nnz);
714 readFile.read(
reinterpret_cast<char*
>(m.valuePtr()),
sizeof(T ) * nnz );
715 readFile.read(
reinterpret_cast<char*
>(m.outerIndexPtr()),
sizeof(IND) * outSz);
716 readFile.read(
reinterpret_cast<char*
>(m.innerIndexPtr()),
sizeof(IND) * nnz );
722template <
typename MatrixType>
727 std::ofstream out(folder + MatrixName,
728 std::ios::out | std::ios::binary | std::ios::trunc);
729 typename MatrixType::Index rows = Matrix.rows(), cols = Matrix.cols();
730 out.write(
reinterpret_cast<char*
> (& rows),
731 sizeof(
typename MatrixType::Index));
732 out.write(
reinterpret_cast<char*
> (& cols),
733 sizeof(
typename MatrixType::Index));
734 out.write(
reinterpret_cast<char*
> (Matrix.data()),
735 rows * cols *
sizeof(
typename MatrixType::Scalar) );
739template <
typename MatrixType>
744 in.open(folder + MatrixName, std::ios::in | std::ios::binary);
748 Foam::Info << folder + MatrixName <<
749 " file does not exist, try to rerun the Offline Stage!" << Foam::endl;
752 else if (in.is_open())
754 typename MatrixType::Index rows = 0, cols = 0;
755 in.read(
reinterpret_cast<char*
> (& rows),
sizeof(
typename MatrixType::Index));
756 in.read(
reinterpret_cast<char*
> (& cols),
sizeof(
typename MatrixType::Index));
757 Matrix.resize(rows, cols);
758 in.read(
reinterpret_cast<char*
>(Matrix.data()),
759 rows * cols *
sizeof(
typename MatrixType::Scalar) );
765template <
typename TensorType>
769 std::ofstream out(folder + MatrixName,
770 std::ios::out | std::ios::binary | std::ios::trunc);
771 typename TensorType::Dimensions dim = Tensor.dimensions();
774 for (
int k = 0; k < dim.size(); k++)
779 out.write(
reinterpret_cast<char*
> (& dim),
780 sizeof(
typename TensorType::Dimensions));
781 out.write(
reinterpret_cast<char*
> (Tensor.data()),
782 tot *
sizeof(
typename TensorType::Scalar) );
786template <
typename TensorType>
791 in.open(folder + MatrixName, std::ios::in | std::ios::binary);
792 typename TensorType::Dimensions dim;
793 in.read(
reinterpret_cast<char*
> (& dim),
794 sizeof(
typename TensorType::Dimensions));
795 auto dims = Tensor.dimensions();
796 M_Assert(dims.size() == dim.size(),
797 "The rank of the tensor you want to fill does not coincide with the rank of the tensor you are reading");
800 for (
int k = 0; k < dim.size(); k++)
806 in.read(
reinterpret_cast<char*
>(Tensor.data()),
807 tot *
sizeof(
typename TensorType::Scalar) );
811template <
typename MatrixType>
813 word folder, word MatrixName)
815 for (
int i = 0; i < MatrixList.size(); i++)
821template <
typename MatrixType>
823 word folder, word MatrixName)
825 for (
int i = 0; i < MatrixList.size(); i++)
831template <
typename T,
int whatever,
typename IND>
833 List<Eigen::SparseMatrix<T, whatever, IND >>& m, word folder, word MatrixName)
835 int number_of_files = numberOfFiles(folder, MatrixName);
836 Foam::Info <<
"Reading the Matrix " + folder + MatrixName << Foam::endl;
837 M_Assert(number_of_files != 0,
838 "Check if the file you are trying to read exists" );
839 Eigen::SparseMatrix<T, whatever, IND> A;
841 for (
int i = 0; i < number_of_files; i++)
843 ReadSparseMatrix(A, folder, MatrixName + name(i));
849template <
typename MatrixType>
854 Foam::Info <<
"Reading the Matrix " + folder + MatrixName << Foam::endl;
855 M_Assert(number_of_files != 0,
856 "Check if the file you are trying to read exists" );
859 for (
int i = 0; i < number_of_files; i++)
Header file of the EigenFunctions class.
Implementation of the assert function for ITHACA-FV.
Header file of the ITHACAutilities namespace.
Simple header and source file of the Color::Modifier class to change color to the output stream.
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 exportFields(PtrList< GeometricField< Type, PatchField, GeoMesh > > &field, word folder, word fieldname)
Function to export a scalar of vector field.
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.