Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <neigvals> eigenvalues of A
Expand Down
1 change: 1 addition & 0 deletions src/optimproblem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
25 changes: 25 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "util.hpp"
#include <fstream>
#include <iomanip>


double sigmoid(double width, double x){
Expand Down Expand Up @@ -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<double>& eigvals, std::vector<Vec>& eigvecs){
Expand Down