diff --git a/include/util.hpp b/include/util.hpp index be6734a4..fb64d6b1 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -80,6 +80,11 @@ PetscErrorCode SanityTests(Vec x, PetscReal time); */ int read_vector(const char *filename, double *var, int dim, bool quietmode=false, int skiplines=0, const std::string testheader=""); +/** + * Write data to file that is compatiable for read_vector + */ +int write_vector(const char *filename, double *var, int dim); + /* * Compute eigenvalues of A diff --git a/src/optimproblem.cpp b/src/optimproblem.cpp index 0d15ba45..8000e71b 100644 --- a/src/optimproblem.cpp +++ b/src/optimproblem.cpp @@ -1028,6 +1028,7 @@ void OptimProblem::getStartingPoint(Vec xinit){ mastereq->getOscillator(ioscil)->getParams(xptr + shift); shift += mastereq->getOscillator(ioscil)->getNParams(); } + if (mpirank_world == 0) write_vector("control_initial_guess.dat", xptr, ndesign); VecRestoreArray(xinit, &xptr); } diff --git a/src/util.cpp b/src/util.cpp index 1f82877d..19513c5b 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,4 +1,6 @@ #include "util.hpp" +#include +#include double sigmoid(double width, double x){ @@ -551,6 +553,29 @@ int read_vector(const char *filename, double *var, int dim, bool quietmode, int return success; } +int write_vector(const char *filename, double *var, int dim) { + std::ofstream file(filename, std::ofstream::out); + double tmp; + int success = 0; + + if (file.is_open()) { + printf("Writing file %s.. ", filename); + file << std::setprecision(15); + for (int d = 0; d < dim; d++) + file << var[d] << "\n"; + + file.close(); + printf("Done!\n"); + success = 1; + } + else { + printf("ERROR: Can't open file %s\n", filename); + exit(1); + } + + return success; +} + /* Compute eigenvalues */ int getEigvals(const Mat A, const int neigvals, std::vector& eigvals, std::vector& eigvecs){