Loading [MathJax]/extensions/tex2jax.js
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Recorder_least_squares.cpp
Go to the documentation of this file.
1 // Copyright 2019, University of Maryland and the MANGO development team.
2 //
3 // This file is part of MANGO.
4 //
5 // MANGO is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License as
7 // published by the Free Software Foundation, either version 3 of the
8 // License, or (at your option) any later version.
9 //
10 // MANGO is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with MANGO. If not, see
17 // <https://www.gnu.org/licenses/>.
18 
19 #include <iostream>
20 #include <fstream>
21 #include <iomanip>
22 #include <stdexcept>
23 #include <ctime>
24 #include "Recorder.hpp"
26 
28  solver = solver_in;
29 }
30 
32  if (!solver->mpi_partition->get_proc0_world()) return; // Proceed only on proc0_world.
33 
34  int j;
35 
36  // Open output file
37  output_file.open(solver->output_filename.c_str());
38  if (!output_file.is_open()) {
39  std::cerr << "output file: " << solver->output_filename << std::endl;
40  throw std::runtime_error("Error! Unable to open output file.");
41  }
42  // Write header lines of output file
43  output_file << "Recorder type:" << std::endl << "least_squares" << std::endl << "N_parameters:" << std::endl << solver->N_parameters << std::endl << "function_evaluation,seconds";
44  for (j=0; j<solver->N_parameters; j++) {
45  output_file << ",x(" << j+1 << ")";
46  }
47  output_file << ",objective_function";
48  if (solver->print_residuals_in_output_file) {
49  for (j=0; j<solver->N_terms; j++) {
50  output_file << ",F(" << j+1 << ")";
51  }
52  }
53  output_file << std::endl << std::flush;
54 }
55 
56 
57 void mango::Recorder_least_squares::write_file_line(int function_evaluations, clock_t print_time, const double* x, double f, double* residuals) {
58  // This subroutine writes a line in the output file for non-least-squares problems.
59  /*
60  write_function_evaluation_and_time(print_time);
61  std::string file_string;
62  compose_x_f_string(file_string, x, f);
63  output_file << file_string << std::endl << std::flush;
64  */
65  double elapsed_time = ((float)(print_time - solver->start_time)) / CLOCKS_PER_SEC;
66  output_file << std::setw(6) << std::right << function_evaluations << "," << std::setw(12) << std::setprecision(4) << std::scientific << elapsed_time;
67  for (int j=0; j<solver->N_parameters; j++) {
68  output_file << "," << std::setw(24) << std::setprecision(16) << std::scientific << x[j];
69  }
70  output_file << "," << std::setw(24) << f;
71  if (solver->print_residuals_in_output_file) {
72  for (int j=0; j<solver->N_terms; j++) {
73  output_file << "," << std::setw(24) << std::setprecision(16) << std::scientific << residuals[j];
74  }
75  }
76  output_file << std::endl << std::flush;
77 }
78 
79 
80 void mango::Recorder_least_squares::record_function_evaluation(int function_evaluations, clock_t print_time, const double* x, double f) {
81  if (!solver->mpi_partition->get_proc0_world()) return; // Proceed only on proc0_world.
82 
83  write_file_line(function_evaluations, print_time, x, f, solver->current_residuals);
84 }
85 
86 
88  // Copy the line corresponding to the optimum to the bottom of the output file.
89 
90  if (!solver->mpi_partition->get_proc0_world()) return; // Proceed only on proc0_world.
91 
92  write_file_line(solver->best_function_evaluation, solver->best_time, solver->state_vector, solver->best_objective_function, solver->best_residual_function);
93 
94  output_file.close();
95 }
mango::Least_squares_solver
Definition: Least_squares_solver.hpp:28
mango::Recorder_least_squares::Recorder_least_squares
Recorder_least_squares(Least_squares_solver *)
Definition: Recorder_least_squares.cpp:27
mango::Recorder_least_squares::finalize
void finalize()
Definition: Recorder_least_squares.cpp:87
Recorder_least_squares.hpp
mango::Recorder_least_squares::init
void init()
Definition: Recorder_least_squares.cpp:31
mango::Recorder_least_squares::record_function_evaluation
void record_function_evaluation(int function_evaluations, clock_t print_time, const double *x, double f)
Definition: Recorder_least_squares.cpp:80
Recorder.hpp