From 20a956df802d4829ab3c5dda5bad49d24f4760d7 Mon Sep 17 00:00:00 2001 From: Peter Munch Date: Mon, 17 Aug 2020 23:38:11 +0200 Subject: [PATCH] DataOut::write_vtu_with_pvtu_record(): prints the path name if file cannot be opened --- source/base/data_out_base.cc | 4 +- ...write_vtu_with_pvtu_record_no_folder_01.cc | 105 ++++++++++++++++++ ...h_pvtu_record_no_folder_01.mpirun=1.output | 4 + ...h_pvtu_record_no_folder_01.mpirun=2.output | 9 ++ 4 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 tests/data_out/write_vtu_with_pvtu_record_no_folder_01.cc create mode 100644 tests/data_out/write_vtu_with_pvtu_record_no_folder_01.mpirun=1.output create mode 100644 tests/data_out/write_vtu_with_pvtu_record_no_folder_01.mpirun=2.output diff --git a/source/base/data_out_base.cc b/source/base/data_out_base.cc index 3d64d59ba6..9ab011e5ae 100644 --- a/source/base/data_out_base.cc +++ b/source/base/data_out_base.cc @@ -7024,6 +7024,7 @@ DataOutInterface::write_vtu_in_parallel( (void)comm; std::ofstream f(filename); + AssertThrow(f, ExcFileNotOpen(filename)); write_vtu(f); #else @@ -7038,7 +7039,7 @@ DataOutInterface::write_vtu_in_parallel( MPI_MODE_CREATE | MPI_MODE_WRONLY, info, &fh); - AssertThrowMPI(ierr); + AssertThrow(ierr == MPI_SUCCESS, ExcFileNotOpen(filename)); ierr = MPI_File_set_size(fh, 0); // delete the file contents AssertThrowMPI(ierr); @@ -7160,6 +7161,7 @@ DataOutInterface::write_vtu_with_pvtu_record( { // every processor writes one file std::ofstream output(filename.c_str()); + AssertThrow(output, ExcFileNotOpen(filename)); this->write_vtu(output); } else if (n_groups == 1) diff --git a/tests/data_out/write_vtu_with_pvtu_record_no_folder_01.cc b/tests/data_out/write_vtu_with_pvtu_record_no_folder_01.cc new file mode 100644 index 0000000000..790f98fc4d --- /dev/null +++ b/tests/data_out/write_vtu_with_pvtu_record_no_folder_01.cc @@ -0,0 +1,105 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2020 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + +// Test that DataOut::write_vtu_with_pvtu_record() prints the path name +// if a file cannot be opened. + +#include + +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +#include + +#include + +#include "../tests.h" + +using namespace dealii; + + +int +main(int argc, char *argv[]) +{ + Utilities::MPI::MPI_InitFinalize mpi_initialization(argc, argv, 1); + MPILogInitAll all; + + const unsigned int dim = 2; + + const MPI_Comm comm = MPI_COMM_WORLD; + + parallel::distributed::Triangulation tria(comm); + + GridGenerator::subdivided_hyper_cube(tria, 2); + + FE_DGQ fe(2); + DoFHandler dof_handler(tria); + dof_handler.distribute_dofs(fe); + + Vector vec(dof_handler.n_dofs()); + + DataOut data_out; + + data_out.attach_dof_handler(dof_handler); + data_out.add_data_vector(vec, "solution"); + + const auto print_line = [](const std::string &str) { + std::stringstream ss(str); + std::string token; + + for (unsigned int i = 0; i < 8; ++i) + std::getline(ss, token, '\n'); + + deallog << token << std::endl; + }; + + try + { + data_out.write_vtu_with_pvtu_record("./folder/", "file", 0, comm, 1, 0); + } + catch (const std::exception &exc) + { + print_line(exc.what()); + } + + try + { + data_out.write_vtu_with_pvtu_record("./folder/", "file", 0, comm, 1, 1); + } + catch (const std::exception &exc) + { + print_line(exc.what()); + } + + try + { + data_out.write_vtu_with_pvtu_record( + "./folder/", "file", 0, comm, 1, Utilities::MPI::n_mpi_processes(comm)); + } + catch (const std::exception &exc) + { + print_line(exc.what()); + } +} diff --git a/tests/data_out/write_vtu_with_pvtu_record_no_folder_01.mpirun=1.output b/tests/data_out/write_vtu_with_pvtu_record_no_folder_01.mpirun=1.output new file mode 100644 index 0000000000..5b4228254e --- /dev/null +++ b/tests/data_out/write_vtu_with_pvtu_record_no_folder_01.mpirun=1.output @@ -0,0 +1,4 @@ + +DEAL:0:: Could not open file ./folder/file_0.0.vtu. +DEAL:0:: Could not open file ./folder/file_0.0.vtu. +DEAL:0:: Could not open file ./folder/file_0.0.vtu. diff --git a/tests/data_out/write_vtu_with_pvtu_record_no_folder_01.mpirun=2.output b/tests/data_out/write_vtu_with_pvtu_record_no_folder_01.mpirun=2.output new file mode 100644 index 0000000000..fd614546c4 --- /dev/null +++ b/tests/data_out/write_vtu_with_pvtu_record_no_folder_01.mpirun=2.output @@ -0,0 +1,9 @@ + +DEAL:0:: Could not open file ./folder/file_0.0.vtu. +DEAL:0:: Could not open file ./folder/file_0.0.vtu. +DEAL:0:: Could not open file ./folder/file_0.0.vtu. + +DEAL:1:: Could not open file ./folder/file_0.1.vtu. +DEAL:1:: Could not open file ./folder/file_0.0.vtu. +DEAL:1:: Could not open file ./folder/file_0.1.vtu. + -- 2.39.5