Commit 870915f5 authored by GILLES Sebastien's avatar GILLES Sebastien

#1532 Petsc wrapper: Refactor slightly the possible viewers to add more safety.

parent 5d20fa66
......@@ -54,6 +54,7 @@ namespace MoReFEM
Viewer viewer(mpi,
binary_file,
PETSC_VIEWER_BINARY_MATLAB,
FILE_MODE_READ,
invoking_file, invoking_line);
......@@ -500,9 +501,10 @@ namespace MoReFEM
void Matrix::View(const Mpi& mpi,
const std::string& output_file,
const char* invoking_file, int invoking_line,
PetscViewerFormat format) const
PetscViewerFormat format,
PetscFileMode file_mode) const
{
Viewer viewer(mpi, output_file, format, invoking_file, invoking_line);
Viewer viewer(mpi, output_file, format, file_mode, invoking_file, invoking_line);
int error_code = MatView(Internal(), viewer.GetUnderlyingPetscObject());
if (error_code)
......@@ -514,12 +516,7 @@ namespace MoReFEM
const std::string& output_file,
const char* invoking_file, int invoking_line) const
{
Viewer viewer(mpi, output_file, FILE_MODE_WRITE, invoking_file, invoking_line);
int error_code = MatView(Internal(), viewer.GetUnderlyingPetscObject());
if (error_code)
throw ExceptionNS::Exception(error_code, "MatView", invoking_file, invoking_line);
View(mpi, output_file, invoking_file, invoking_line, PETSC_VIEWER_BINARY_MATLAB, FILE_MODE_WRITE);
}
......
......@@ -521,10 +521,12 @@ namespace MoReFEM
* formats available; relevant ones so far are PETSC_VIEWER_DEFAULT and PETSC_VIEWER_ASCII_MATLAB.
* \param[in] output_file File into which the vector content will be written.
* \copydoc doxygen_hide_invoking_file_and_line
* \copydoc doxygen_hide_petsc_file_mode
*/
void View(const Mpi& mpi, const std::string& output_file,
const char* invoking_file, int invoking_line,
PetscViewerFormat format = PETSC_VIEWER_DEFAULT) const;
PetscViewerFormat format = PETSC_VIEWER_DEFAULT,
PetscFileMode file_mode = FILE_MODE_WRITE) const;
/*!
......
......@@ -424,7 +424,7 @@ namespace MoReFEM
const char* invoking_file, int invoking_line,
PetscViewerFormat format) const
{
Viewer viewer(mpi, output_file, format, invoking_file, invoking_line);
Viewer viewer(mpi, output_file, format, FILE_MODE_WRITE, invoking_file, invoking_line);
int error_code = VecView(Internal(), viewer.GetUnderlyingPetscObject());
if (error_code)
......@@ -436,7 +436,8 @@ namespace MoReFEM
const std::string& output_file,
const char* invoking_file, int invoking_line) const
{
Viewer viewer(mpi, output_file, FILE_MODE_WRITE, invoking_file, invoking_line);
Viewer viewer(mpi, output_file, PETSC_VIEWER_BINARY_MATLAB, FILE_MODE_WRITE,
invoking_file, invoking_line);
int error_code = VecView(Internal(), viewer.GetUnderlyingPetscObject());
if (error_code)
......@@ -449,7 +450,7 @@ namespace MoReFEM
const char* invoking_file, int invoking_line,
PetscViewerFormat format) const
{
Viewer viewer(mpi, input_file, format, invoking_file, invoking_line);
Viewer viewer(mpi, input_file, format, FILE_MODE_READ, invoking_file, invoking_line);
int error_code = VecLoad(Internal(), viewer.GetUnderlyingPetscObject());
if (error_code)
......
......@@ -82,6 +82,7 @@ namespace MoReFEM::Wrappers::Petsc
Viewer viewer(mpi,
binary_file,
PETSC_VIEWER_BINARY_MATLAB,
FILE_MODE_READ,
invoking_file, invoking_line);
......
......@@ -34,19 +34,81 @@ namespace MoReFEM
Viewer::Viewer(const Mpi& mpi,
const std::string& ascii_file,
const std::string& file,
PetscViewerFormat format,
PetscFileMode file_mode,
const char* invoking_file, int invoking_line)
: viewer_(nullptr)
{
switch(format)
{
case PETSC_VIEWER_DEFAULT:
case PETSC_VIEWER_ASCII_MATLAB:
case PETSC_VIEWER_ASCII_MATHEMATICA:
case PETSC_VIEWER_ASCII_IMPL:
case PETSC_VIEWER_ASCII_INFO:
case PETSC_VIEWER_ASCII_INFO_DETAIL:
case PETSC_VIEWER_ASCII_COMMON:
case PETSC_VIEWER_ASCII_SYMMODU:
case PETSC_VIEWER_ASCII_INDEX:
case PETSC_VIEWER_ASCII_DENSE:
case PETSC_VIEWER_ASCII_MATRIXMARKET:
case PETSC_VIEWER_ASCII_VTK:
case PETSC_VIEWER_ASCII_VTK_CELL:
case PETSC_VIEWER_ASCII_VTK_COORDS:
case PETSC_VIEWER_ASCII_PCICE:
case PETSC_VIEWER_ASCII_PYTHON:
case PETSC_VIEWER_ASCII_FACTOR_INFO:
case PETSC_VIEWER_ASCII_LATEX:
case PETSC_VIEWER_ASCII_XML:
case PETSC_VIEWER_ASCII_GLVIS:
case PETSC_VIEWER_ASCII_CSV:
AsciiCase(mpi,
file,
format,
invoking_file, invoking_line);
break;
case PETSC_VIEWER_BINARY_MATLAB:
BinaryCase(mpi,
file,
file_mode,
invoking_file, invoking_line);
break;
case PETSC_VIEWER_HDF5_PETSC:
case PETSC_VIEWER_HDF5_VIZ:
case PETSC_VIEWER_HDF5_XDMF:
case PETSC_VIEWER_HDF5_MAT:
case PETSC_VIEWER_DRAW_BASIC:
case PETSC_VIEWER_DRAW_LG:
case PETSC_VIEWER_DRAW_LG_XRANGE:
case PETSC_VIEWER_DRAW_CONTOUR:
case PETSC_VIEWER_DRAW_PORTS:
case PETSC_VIEWER_VTK_VTS:
case PETSC_VIEWER_VTK_VTR:
case PETSC_VIEWER_VTK_VTU:
case PETSC_VIEWER_NATIVE:
case PETSC_VIEWER_NOFORMAT:
case PETSC_VIEWER_LOAD_BALANCE:
throw Exception("The required Petsc Viewer format is not supported yet; if you need it "
"please contact the maintainers of MoReFEM library.",
__FILE__, __LINE__);
}
}
void Viewer::AsciiCase(const Mpi& mpi,
const std::string& ascii_file,
PetscViewerFormat format,
const char* invoking_file, int invoking_line)
{
int error_code = PetscViewerCreate(mpi.GetCommunicator(), &viewer_);
if (error_code)
throw ExceptionNS::Exception(error_code, "PetscViewerCreate", invoking_file, invoking_line);
if (format == PETSC_VIEWER_ASCII_MATLAB && !Utilities::String::EndsWith(ascii_file, ".m"))
throw ExceptionNS::WrongMatlabExtension(ascii_file, invoking_file, invoking_line);
error_code = PetscViewerASCIIOpen(mpi.GetCommunicator(),
ascii_file.c_str(),
&viewer_);
......@@ -54,27 +116,24 @@ namespace MoReFEM
throw ExceptionNS::Exception(error_code, "PetscViewerASCIIOpen", invoking_file, invoking_line);
error_code = PetscViewerPushFormat(viewer_, format);
if (error_code)
throw ExceptionNS::Exception(error_code, "PetscViewerSetFormat", invoking_file, invoking_line);
throw ExceptionNS::Exception(error_code, "PetscViewerPushFormat", invoking_file, invoking_line);
}
Viewer::Viewer(const Mpi& mpi,
const std::string& binary_file,
PetscFileMode file_mode,
const char* invoking_file, int invoking_line)
: viewer_(nullptr)
{
int error_code = PetscViewerCreate(mpi.GetCommunicator(), &viewer_);
void Viewer::BinaryCase(const Mpi& mpi,
const std::string& file,
PetscFileMode file_mode,
const char* invoking_file, int invoking_line)
{
int error_code = PetscViewerCreate(mpi.GetCommunicator(), &viewer_);
if (error_code)
throw ExceptionNS::Exception(error_code, "PetscViewerCreate", invoking_file, invoking_line);
error_code = PetscViewerBinaryOpen(mpi.GetCommunicator(),
binary_file.c_str(),
file.c_str(),
file_mode,
&viewer_);
......
......@@ -58,41 +58,41 @@ namespace MoReFEM
/// \name Special members.
///@{
/*!
* \brief Constructor for an ascii file.
*
* \copydetails doxygen_hide_mpi_param
* \param[in] ascii_file File to which the Petsc object will be associated.
* \copydoc doxygen_hide_invoking_file_and_line
* \param[in] format Format in which the matrix is written. See Petsc manual pages to get all the
* formats available; relevant ones so far are PETSC_VIEWER_DEFAULT and PETSC_VIEWER_ASCII_MATLAB.
*
*/
Viewer(const Mpi& mpi,
const std::string& ascii_file,
PetscViewerFormat format,
const char* invoking_file, int invoking_line);
/*!
* \class doxygen_hide_petsc_file_mode
*
* \param[in] file_mode Same modes as in Petsc documentation, i.e.:
* \param[in] file_mode Same modes as in
* [Petsc documentation](https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Sys/PetscFileMode.html)
* , i.e.:
* - FILE_MODE_WRITE - create new file for binary output.
* - FILE_MODE_READ - open existing file for binary input.
* - FILE_MODE_APPEND - open existing file for binary output.
* Note: this field is actually not used if the format is an ascii one.
*/
/*!
* \brief Constructor for a binary file.
* \class doxygen_hide_petsc_viewer_format
*
* \param[in] format Format in which the matrix is written. See Petsc manual pages to get all the
* formats available; relevant ones so far are PETSC_VIEWER_DEFAULT and PETSC_VIEWER_ASCII_* ones and
* PETSC_VIEWER_BINARY_MATLAB.
*/
/*!
* \brief Constructor for an ascii file.
*
* \copydetails doxygen_hide_mpi_param
* \param[in] binary_file File to which the Petsc object will be associated.
* \param[in] file Path to the file to which the Petsc object will be associated.
* \copydoc doxygen_hide_invoking_file_and_line
* \copydoc doxygen_hide_petsc_viewer_format
* \copydoc doxygen_hide_petsc_file_mode
*
*/
Viewer(const Mpi& mpi,
const std::string& binary_file,
const std::string& file,
PetscViewerFormat format,
PetscFileMode file_mode,
const char* invoking_file, int invoking_line);
......@@ -119,6 +119,35 @@ namespace MoReFEM
//! Access to the underlying viewer.
PetscViewer& GetUnderlyingPetscObject();
private:
/*!
* \brief Method called by the constructor if the format is an ascii one.
*
* \param[in] file Path to the file to which the Petsc object will be associated.
* \copydetails doxygen_hide_mpi_param
* \copydoc doxygen_hide_petsc_viewer_format
* \copydoc doxygen_hide_invoking_file_and_line
*/
void AsciiCase(const Mpi& mpi,
const std::string& file,
PetscViewerFormat format,
const char* invoking_file, int invoking_line);
/*!
* \brief Method called by the constructor if the format is a binary one.
*
* \param[in] file Path to the file to which the Petsc object will be associated.
* \copydetails doxygen_hide_mpi_param
* \copydoc doxygen_hide_petsc_file_mode
* \copydoc doxygen_hide_invoking_file_and_line
*/
void BinaryCase(const Mpi& mpi,
const std::string& file,
PetscFileMode file_mode,
const char* invoking_file, int invoking_line);
private:
//! Underlying PetscViewer object (it is truly a pointer over an alias);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment