PRISMS-PF  v2.1
outputResults.cc
Go to the documentation of this file.
1 //outputResults() method for MatrixFreePDE class
2 
3 #include "../../include/matrixFreePDE.h"
4 #include <deal.II/numerics/data_out.h>
5 
6 //output results
7 template <int dim, int degree>
9  //log time
10  computing_timer.enter_section("matrixFreePDE: output");
11 
12  //create DataOut object
13  DataOut<dim> data_out;
14 
15  //loop over fields
16 
17  for(unsigned int fieldIndex=0; fieldIndex<fields.size(); fieldIndex++){
18  //mark field as scalar/vector
19  std::vector<DataComponentInterpretation::DataComponentInterpretation> dataType \
20  (fields[fieldIndex].numComponents, \
21  (fields[fieldIndex].type==SCALAR ? \
22  DataComponentInterpretation::component_is_scalar: \
23  DataComponentInterpretation::component_is_part_of_vector));
24  //add field to data_out
25  std::vector<std::string> solutionNames (fields[fieldIndex].numComponents, fields[fieldIndex].name.c_str());
26  data_out.add_data_vector(*dofHandlersSet[fieldIndex], *solutionSet[fieldIndex], solutionNames, dataType);
27  }
28 
29  // Test section for outputting postprocessed fields
30  // Currently there are hacks in place, using the matrixFreeObject, invM, constraints, and DoFHandler as the primary variables
31  if (userInputs.postProcessingRequired){
32  std::vector<vectorType*> postProcessedSet;
33  computePostProcessedFields(postProcessedSet);
34 
35  unsigned int invM_size = invM.local_size();
36  for(unsigned int fieldIndex=0; fieldIndex<postProcessedSet.size(); fieldIndex++){
37  for (unsigned int dof=0; dof<postProcessedSet[fieldIndex]->local_size(); ++dof){
38  postProcessedSet[fieldIndex]->local_element(dof)= \
39  invM.local_element(dof%invM_size)*postProcessedSet[fieldIndex]->local_element(dof);
40  }
41  constraintsOtherSet[0]->distribute(*postProcessedSet[fieldIndex]);
42  postProcessedSet[fieldIndex]->update_ghost_values();
43  }
44 
45  // Integrate over selected post-processed fields and output them to the screen and a text file
46  std::ofstream output_file;
47 
48  if (userInputs.num_integrated_fields > 0){
49  if (first_integrated_var_output_complete){
50  output_file.open("integratedFields.txt", std::ios::app);
51  }
52  else {
53  output_file.open("integratedFields.txt", std::ios::out);
54  }
55  output_file.precision(10);
56 
57  if (Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0){
58  output_file << currentTime;
59  }
60 
61  for (unsigned int i=0; i<userInputs.pp_number_of_variables; i++){
62  if (userInputs.pp_calc_integral[i]){
63  double integrated_field;
64  computeIntegral(integrated_field,i,postProcessedSet);
65  pcout << "Integrated value of " << userInputs.pp_var_name[userInputs.integrated_field_indices[i]] << ": " << integrated_field << std::endl;
66  if (Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0){
67  output_file << "\t" << userInputs.pp_var_name[userInputs.integrated_field_indices[i]] << "\t" << integrated_field;
68  }
69  }
70  }
71  if (Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0){
72  output_file << std::endl;
73  }
74  output_file.close();
75  first_integrated_var_output_complete = true;
76  }
77 
78  // Add the postprocessed fields to data_out
79  for(unsigned int fieldIndex=0; fieldIndex<userInputs.pp_number_of_variables; fieldIndex++){
80  //mark field as scalar/vector
81  unsigned int components;
82  if (userInputs.pp_varInfoList[fieldIndex].is_scalar){
83  components = 1;
84  std::vector<DataComponentInterpretation::DataComponentInterpretation> dataType(components,DataComponentInterpretation::component_is_scalar);
85  std::vector<std::string> solutionNames (components, userInputs.pp_var_name[fieldIndex].c_str());
86  //add field to data_out
87  data_out.add_data_vector(*dofHandlersSet[0], *postProcessedSet[fieldIndex], solutionNames, dataType);
88  }
89  else {
90  components = dim;
91  std::vector<DataComponentInterpretation::DataComponentInterpretation> dataType(components,DataComponentInterpretation::component_is_part_of_vector);
92  std::vector<std::string> solutionNames (components, userInputs.pp_var_name[fieldIndex].c_str());
93  //add field to data_out
94  //data_out.add_data_vector(*vector_dofHandler, *postProcessedSet[fieldIndex], solutionNames, dataType);
95  data_out.add_data_vector(*dofHandlersSet[0], *postProcessedSet[fieldIndex], solutionNames, dataType);
96  }
97  }
98  }
99 
100  data_out.build_patches (degree);
101 
102  //write to results file
103  //file name
104  std::ostringstream cycleAsString;
105  cycleAsString << std::setw(std::floor(std::log10(userInputs.totalIncrements))+1) << std::setfill('0') << currentIncrement;
106  char baseFileName[100], vtuFileName[100];
107  sprintf(baseFileName, "%s-%s", userInputs.output_file_name.c_str(), cycleAsString.str().c_str());
108  sprintf(vtuFileName, "%s.%u.%s", baseFileName,Utilities::MPI::this_mpi_process(MPI_COMM_WORLD),userInputs.output_file_type.c_str());
109 
110  // Write to file in either vtu or vtk format
111  if (userInputs.output_file_type == "vtu"){
112 
113  // Set flags to output the time and cycle number as part of the vtu file
114  dealii::DataOutBase::VtkFlags flags;
115  flags.time = currentTime;
116  flags.cycle = currentIncrement;
117  flags.print_date_and_time = true;
118  data_out.set_flags(flags);
119 
120  if (userInputs.output_vtu_per_process){
121  // Write the results to separate files for each process
122  std::ofstream output (vtuFileName);
123  data_out.write_vtu (output);
124 
125  // Create pvtu record that can be used to stitch together the results from all the processes
126  if (Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0){
127  std::vector<std::string> filenames;
128  for (unsigned int i=0;i<Utilities::MPI::n_mpi_processes (MPI_COMM_WORLD); ++i) {
129  char vtuProcFileName[100];
130  sprintf(vtuProcFileName, "%s-%s.%u.%s", userInputs.output_file_name.c_str(),cycleAsString.str().c_str(),i,userInputs.output_file_type.c_str());
131  filenames.push_back (vtuProcFileName);
132  }
133  char pvtuFileName[100];
134  sprintf(pvtuFileName, "%s.p%s", baseFileName, userInputs.output_file_type.c_str());
135  std::ofstream master_output (pvtuFileName);
136 
137  data_out.write_pvtu_record (master_output, filenames);
138  pcout << "Output written to:" << pvtuFileName << "\n\n";
139  }
140 
141  }
142  else {
143  // Write the results to a file shared between all processes
144  char svtuFileName[100];
145  sprintf(svtuFileName, "%s.%s", baseFileName ,userInputs.output_file_type.c_str());
146  data_out.write_vtu_in_parallel(svtuFileName, MPI_COMM_WORLD);
147  pcout << "Output written to:" << svtuFileName << "\n\n";
148  }
149  }
150  else if (userInputs.output_file_type == "vtk"){
151  // Write the results to separate files for each process
152  std::ofstream output (vtuFileName);
153  data_out.write_vtk (output);
154  pcout << "Output written to:" << vtuFileName << "\n\n";
155  }
156  else {
157  std::cerr << "PRISMS-PF Error: The parameter 'outputFileType' must be either \"vtu\" or \"vtk\"" << std::endl;
158  abort();
159  }
160 
161  //log time
162  computing_timer.exit_section("matrixFreePDE: output");
163 }
164 
165 #include "../../include/matrixFreePDE_template_instantiations.h"
void outputResults()
Definition: outputResults.cc:8