static void makeh(void) {int i, j, l, sz, change, N_var; double t1, t2, *tt, **td, *pd, *time, **ldata; haelem **tab, *hp; source_record *sp; time_list *tp; pcons *pp, *pn; /* order the times for each variable in srctab */ sz = srctab->size; tab = srctab->table; N_var = 0; for (i = 0; i < sz; i++) for (hp = tab[i]; hp != NULL; hp = hp->next) {tp = (time_list *) (hp->def); l = tp->length; /* copy the list into arrays */ tt = time = CMAKE_N(double, l); td = ldata = CMAKE_N(double *, l); for (pp = tp->list; pp != NULL; pp = pn) {sp = (source_record *) pp->car; *(tt++) = sp->time; *(td++) = sp->data; pn = (pcons *) pp->cdr; CFREE(sp); CFREE(pp);}; /* sort the arrays according to the times */ change = TRUE; while (change) {change = FALSE; for (j = 1; j < l; j++) {t1 = time[j-1]; t2 = time[j]; if (t2 < t1) {pd = ldata[j-1]; ldata[j-1] = ldata[j]; ldata[j] = pd; time[j] = t1; time[j-1] = t2; change = TRUE;};};}; /* write the variable and source_record array out */ write_var(pdsf, hp->name, time, ldata, l); N_var++; /* release the temporary storage for this variable */ CFREE(time); CFREE(ldata);}; /* finish up */ PD_write(pdsf, "n_variables", "integer", &N_var); PD_close(pdsf); pdsf = NULL; return;}
static void write_test_10_data(PDBfile *strm) {long ind[6]; /* write scalars into the file */ if (PD_write(strm, "qs", "long_double", &qs_w) == 0) error(1, STDOUT, "QS WRITE FAILED - WRITE_TEST_10_DATA\n"); /* write primitive arrays into the file */ ind[0] = 0L; ind[1] = N_FLOAT - 1; ind[2] = 1L; if (PD_write_alt(strm, "qa", "long_double", qa_w, 1, ind) == 0) error(1, STDOUT, "DA WRITE FAILED - WRITE_TEST_10_DATA\n"); return;}
int main(int argc, char **argv) { char *infile; char *outfile; gato_data data; PDBfile *fp; long ind[6]; double **m; int i, j, ip, im; double dl, dr, dz; /* Set input file name */ infile = DEFAULT_INPUT; if(argc > 1) { /* First argument is the input file */ infile = argv[1]; } /* Set output file name */ if(argc > 2) { outfile = argv[2]; }else { /* Default output is input + ".pdb" */ outfile = (char*) malloc(strlen(infile) + 5); sprintf(outfile, "%s.pdb", infile); } /* Read the GATO data file */ if(read_gato(infile, &data)) { return(1); } if(!data.b_present) { /* Calculate Br and Bz from Bpol */ data.Br = dmatrix(data.ntheta, data.npsi); data.Bz = dmatrix(data.ntheta, data.npsi); data.b_present = 1; for(i=0;i<data.ntheta;i++) { ip = (i == (data.ntheta-1)) ? 0 : i+1; im = (i == 0) ? data.ntheta-1 : i-1; for(j=0;j<data.npsi;j++) { dl = sqrt(SQ(data.xnorm[ip][j] - data.xnorm[im][j]) + SQ(data.znorm[ip][j] - data.znorm[im][j])); dr = data.xnorm[ip][j] - data.xnorm[im][j]; dz = data.znorm[ip][j] - data.znorm[im][j]; data.Br[i][j] = dr * data.Bpol[i][j] / dl; data.Bz[i][j] = dz * data.Bpol[i][j] / dl; } } } /* Open PDB file */ if((fp = PD_open(outfile, "w")) == (PDBfile*) NULL) { fprintf(stderr, "Error: Could not open '%s' for writing\n", outfile); return(2); } /* Write to PDB file */ ind[0] = 0L; ind[1] = data.npsi-1; ind[2] = 1L; ind[3] = 0L; ind[4] = data.ntheta-1; ind[5] = 1L; /* Size of the grid */ if(!PD_write(fp, "nx", "integer", &data.npsi)) { fprintf(stderr, "Error: Could not write to file '%s'\n", outfile); return(3); } if(!PD_write(fp, "ny", "integer", &data.ntheta)) { fprintf(stderr, "Error: Could not write to file '%s'\n", outfile); return(3); } /* Write 1D flux surface functions */ WRITE1D("psi", data.psiflux); WRITE1D("f", data.fnorm); WRITE1D("ffprime", data.ffpnorm); WRITE1D("mu0p", data.ponly); WRITE1D("mu0pprime", data.pponly); WRITE1D("qsafe", data.qsf); WRITE1D("Ni", data.d); /* Write 2D variables (Need to reverse indices) */ m = dmatrix(data.npsi, data.ntheta); WRITE2D("Rxy", data.xnorm); WRITE2D("Zxy", data.znorm); WRITE2D("Brxy", data.Br); WRITE2D("Bzxy", data.Bz); WRITE2D("Bpxy", data.Bpol); WRITE2D("Btxy", data.Btor); PD_close(fp); free_gato(&data); /* Free data */ return(0); }
int main(int c, char **v) {int i; float nonew[24]; /* these are all half the size of the */ float middlew[24]; /* space that will be defent for them */ float allw[24]; /* which will be of dim [2][6][4] */ float allwstep[48]; float allrstep[48]; float zerostep[48]; float noner[24]; /* over the course of three cases we */ float middler[24]; /* write and read back non-contiguously, */ float allr[24]; /* partially contiguously, and contiguously */ float zero[24]; PDBfile *file; debug_mode = FALSE; for (i = 1; i < c; i++) {if (v[i][0] == '-') {switch (v[i][1]) {case 'd' : debug_mode = TRUE; SC_gs.mm_debug = TRUE; break; case 'h' : print_help(); return(1); case 'v' : PD_set_fmt_version(SC_stoi(v[++i])); break;};} else break;}; file = PD_open(DATA_FILE, "w+"); if (file == NULL) {printf("Error creating %s\n", DATA_FILE); exit(1);}; /* initialize the write vars to some semi-arbitrary values */ for (i = 0; i < 24; i++) {nonew[i] = (float) i; middlew[i] = (float) i; allw[i] = (float) i; zero[i] = 0.0; noner[i] = 0.0; middler[i] = 0.0; allr[i] = 0.0;}; /* step == 1 CASES */ /* first case: NONE of the dimension descriptor describe contiguity */ if (!PD_defent(file, "none[2,6,4]", "float")) {printf("PD_defent failed\n"); exit(2);}; /* write out one "physics" variable such that there * are no contiguous regions to optimize on */ if (!PD_write(file, "none[0:1, 0:5, 0:1]", "float", nonew)) {printf("PD_write failed\n"); exit(3);}; /* zero out the rest of it */ if (!PD_write(file, "none[0:1, 0:5, 2:3]", "float", zero)) {printf("PD_write failed\n"); exit(3);}; /* read that variable back again such that there * are no contiguous regions to optimize on */ if (!PD_read(file, "none[0:1, 0:5, 0:1]", noner)) {printf("PD_read failed\n"); exit(4);}; /* check it to make sure it is correct */ for (i = 0; i < 24; i++) {if (fabsf(nonew[i] - noner[i]) > FLOAT_TOLERANCE) {printf("Error in check %f != %f ", nonew[i], noner[i]); printf("at position none[%d]\n", i); PD_close(file); exit(-1);};}; /* second case: MIDDLE dimension contains contiguity */ if (!PD_defent(file, "middle[2,6,4]", "float")) {printf("PD_defent failed\n"); exit(5);}; /* write out on "physics" variable such that some * partial contiguous regions exist to optimize upon */ if (!PD_write(file, "middle[0:1, 3:5, 0:3]", "float", middlew)) {printf("PD_write failed\n"); exit(6);}; if (!PD_write(file, "middle[0:1, 0:2, 0:3]", "float", zero)) {printf("PD_write failed\n"); exit(6);}; /* read that variable back again */ if (!PD_read(file, "middle[0:1, 3:5, 0:3]", middler)) {printf("PD_read failed\n"); exit(7);}; /* check it to make sure it is correct */ for (i = 0; i < 24; i++) {if (fabs(middlew[i] - middler[i]) > FLOAT_TOLERANCE) {printf("Error in check %f != %f ", middlew[i], middler[i]); printf("at position middle[%d]\n", i); PD_close(file); exit(-1);};}; /* third case: ALL of the region to write/read is contiguous */ if (!PD_defent(file, "all[2,6,4]", "float")) {printf("PD_defent failed\n"); exit(8);}; /* write out on "physics" variable such that the * entire region is contiguous to check full optimization */ if (!PD_write(file, "all[0:0, 0:5, 0:3]", "float", allw)) {printf("PD_write failed\n"); exit(9);}; if (!PD_write(file, "all[1:1, 0:5, 0:3]", "float", zero)) {printf("PD_write failed\n"); exit(9);}; /* read that variable back again */ if (!PD_read(file, "all[0:0, 0:5, 0:3]", allr)) {printf("PD_read failed\n"); exit(10);}; /* check it to make sure it is correct */ for (i = 0; i < 24; i++) {if (fabs(allw[i] - allr[i]) > FLOAT_TOLERANCE) {printf("Error in check %f != %f ", allw[i], allr[i]); printf("at position all[%d]\n", i); PD_close(file); exit(-1);};}; /* step != 1 CASES */ /* first case: NONE of the dimension descriptor describe contiguity */ if (!PD_defent(file, "nonestep[2,6,4]", "float")) {printf("PD_defent failed\n"); exit(2);}; /* write out one "physics" variable such that there * are no contiguous regions to optimize on */ if (!PD_write(file, "nonestep[0:1, 0:5, 1:3:2]", "float", nonew)) {printf("PD_write failed\n"); exit(3);}; /* zero out the rest of it */ if (!PD_write(file, "nonestep[0:1, 0:5, 0:2:2]", "float", zero)) {printf("PD_write failed\n"); exit(3);}; /* read that variable back again such that there * are no contiguous regions to optimize on */ if (!PD_read(file, "nonestep[0:1, 0:5, 1:3:2]", noner)) {printf("PD_read failed\n"); exit(4);}; /* check it to make sure it is correct */ for (i = 0; i < 24; i++) {if (fabs(nonew[i] - noner[i]) > FLOAT_TOLERANCE) {printf("Error in check %f != %f ", nonew[i], noner[i]); printf("at position nonestep[%d]\n", i); PD_close(file); exit(-1);};}; /* second case: MIDDLE dimension contains step; 3rd dim is contiguity */ if (!PD_defent(file, "middlestep[2,6,4]", "float")) {printf("PD_defent failed\n"); exit(5);}; /* write out on "physics" variable such that some * partial contiguous regions exist to optimize upon */ if (!PD_write(file, "middlestep[0:1, 1:5:2, 0:3]", "float", middlew)) {printf("PD_write failed\n"); exit(6);}; if (!PD_write(file, "middlestep[0:1, 0:4:2, 0:3]", "float", zero)) {printf("PD_write failed\n"); exit(6);}; /* read that variable back again */ if (!PD_read(file, "middlestep[0:1, 1:5:2, 0:3]", middler)) {printf("PD_read failed\n"); exit(7);}; /* check it to make sure it is correct */ for (i = 0; i < 24; i++) {if (fabs(middlew[i] - middler[i]) > FLOAT_TOLERANCE) {printf("Error in check %f != %f ", middlew[i], middler[i]); printf("at position middlestep[%d]\n", i); PD_close(file); exit(-1);};}; /* third case: OUTTER dim steps, hence only 2nd dim in is contiguous * This is the only step != 1 case that causes optimization */ for (i = 0; i < 48; i++) {allwstep[i] = (float) i; allrstep[i] = 0.0; zerostep[i] = 0.0;}; if (!PD_defent(file, "allstep[4,6,4]", "float")) {printf("PD_defent failed\n"); exit(8);}; /* write out on "physics" variable such that the * entire region is contiguous to check full optimization */ if (!PD_write(file, "allstep[0:2:2, 0:5, 0:3]", "float", allwstep)) {printf("PD_write failed\n"); exit(9);}; if (!PD_write(file, "allstep[1:3:2, 0:5, 0:3]", "float", zerostep)) {printf("PD_write failed\n"); exit(9);}; /* read that variable back again */ if (!PD_read(file, "allstep[0:2:2, 0:5, 0:3]", allrstep)) {printf("PD_read failed\n"); exit(10);}; /* check it to make sure it is correct */ for (i = 0; i < 24; i++) {if (fabs(allwstep[i] - allrstep[i]) > FLOAT_TOLERANCE) {printf("Error in check %f != %f ", allwstep[i], allrstep[i]); printf("at position allstep[%d]\n", i); PD_close(file); exit(-1);};}; PD_close(file); SC_remove(DATA_FILE); exit(0);}
/** \b Usage: "dakota_restart_util to_pdb dakota.rst dakota.pdb"\n "dakota_restart_util to_tabular dakota.rst dakota.txt" Unrolls all data associated with a particular tag for all evaluations and then writes this data in a tabular format (e.g., to a PDB database or MATLAB/TECPLOT data file). */ void print_restart_tabular(int argc, char** argv, String print_dest) { if (print_dest != "pdb_file" && print_dest != "text_file") { Cerr << "Error: bad print_dest in print_restart_tabular" << endl; exit(-1); } else if (print_dest == "pdb_file" && argc != 4) { Cerr << "Usage: \"dakota_restart_util to_pdb <restart_file> <pdb_file>\"." << endl; exit(-1); } else if (print_dest == "text_file" && argc != 4) { Cerr << "Usage: \"dakota_restart_util to_tabular <restart_file> " << "<text_file>\"." << endl; exit(-1); } std::ifstream restart_input_fs(argv[2], std::ios::binary); if (!restart_input_fs.good()) { Cerr << "Error: failed to open restart file " << argv[2] << endl; exit(-1); } boost::archive::binary_iarchive restart_input_archive(restart_input_fs); extern PRPCache data_pairs; size_t records_read = 0; // counter for restart records read size_t num_evals = 0; // unique insertions to data_pairs while (restart_input_fs.good() && !restart_input_fs.eof()) { ParamResponsePair current_pair; try { restart_input_archive & current_pair; } catch(const boost::archive::archive_exception& e) { Cerr << "\nError reading restart file (boost::archive exception):\n" << e.what() << std::endl; abort_handler(-1); } catch(const std::string& err_msg) { Cout << "\nWarning reading restart file: " << err_msg << std::endl; break; } ++records_read; std::pair<PRPCacheCIter, bool> insert_result; insert_result = data_pairs.insert(current_pair); if (insert_result.second == false) Cout << "Warning: Restart record " << records_read << " (evaluation id " << current_pair.eval_id() << ") is a duplicate; ignoring." << std::endl; else ++num_evals; // more efficient than evaluating data_pairs.size()? // peek to force EOF if the last restart record was read restart_input_fs.peek(); } // size_t num_evals = data_pairs.size(); size_t i, j; if (print_dest == "pdb_file") { PRPCacheCIter prp_iter = data_pairs.begin(); StringMultiArrayConstView cv_labels = prp_iter->prp_parameters().continuous_variable_labels(); StringMultiArrayConstView div_labels = prp_iter->prp_parameters().discrete_int_variable_labels(); StringMultiArrayConstView drv_labels = prp_iter->prp_parameters().discrete_real_variable_labels(); const StringArray& fn_labels = prp_iter->prp_response().function_labels(); size_t num_cv = cv_labels.size(), num_div = div_labels.size(), num_drv = drv_labels.size(), num_fns = fn_labels.size(); // pack up tabular data matrices (organized such that use of a single index // returns the complete history for an attribute). RealVector uninitialized_rv(num_evals, false); IntVector uninitialized_iv(num_evals, false); RealVectorArray tabular_rdata(num_cv+num_drv+num_fns, uninitialized_rv); std::vector<IntVector> tabular_idata(num_div, uninitialized_iv); for (i=0; i<num_evals; ++i, ++prp_iter) { // Extract variables related data const Variables& local_vars = prp_iter->prp_parameters(); const RealVector& local_c_vars = local_vars.continuous_variables(); const IntVector& local_di_vars = local_vars.discrete_int_variables(); const RealVector& local_dr_vars = local_vars.discrete_real_variables(); for (j=0; j<num_cv; ++j) tabular_rdata[j][i] = local_c_vars[j]; for (j=0; j<num_div; ++j) tabular_idata[j][i] = local_di_vars[j]; for (j=0; j<num_drv; ++j) tabular_rdata[j+num_cv][i] = local_dr_vars[j]; // Extract response related data const Response& local_response = prp_iter->prp_response(); const RealVector& local_fns = local_response.function_values(); //const ShortArray& local_asv =local_response.active_set_request_vector(); //const RealMatrix& local_grads = local_response.function_gradients(); //const RealMatrixArray& local_hessians // = local_response.function_hessians(); // NOTE: if any fns are inactive, they will appear as 0's in tabular_data for (j=0; j<num_fns; ++j) tabular_rdata[j+num_cv+num_drv][i] = local_fns[j]; } #ifdef HAVE_PDB_H // open the PDB file cout << "Writing PDB file:" << argv[3] << '\n'; PDBfile *fileID; if ((fileID = PD_open(argv[3], "w")) == NULL){ cout << "Problem opening PDB file"; return; } // Note: tabular_rdata[j] returns the jth row vector from the matrix and // values() returns the double* pointer to the data of this row // vector. const char* and const double* are passed to PDB_Write. char *tag, cdim[6]; std::sprintf(cdim, "(%d)", num_evals); String tag_name, snum_evals(cdim); // write continuous variables for (j=0; j<num_cv; ++j){ tag_name = cv_labels[j] + snum_evals; tag = const_cast<char *>(tag_name.data()); if(!PD_write(fileID, tag, "double", (void *)tabular_rdata[j].values())) cout << "PD_write error=" << PD_err << '\n'; } // write discrete integer variables for (j=0; j<num_div; ++j){ tag_name = div_labels[j] + snum_evals; tag = const_cast<char *>(tag_name.data()); if(!PD_write(fileID, tag, "integer", (void *)tabular_idata[j].values())) cout << "PD_write error=" << PD_err << '\n'; } // write discrete real variables for (j=0; j<num_drv; ++j){ tag_name = drv_labels[j] + snum_evals; tag = const_cast<char *>(tag_name.data()); if(!PD_write(fileID, tag, "double", (void *)tabular_rdata[j+num_cv].values())) cout << "PD_write error=" << PD_err << '\n'; } // write corresponding function values for (j=0; j<num_fns; ++j){ tag_name = fn_labels[j] + snum_evals; tag = const_cast<char *>(tag_name.data()); if(!PD_write(fileID, tag, "double", (void *)tabular_rdata[j+num_cv+num_drv].values())) cout << "PD_write error=" << PD_err << '\n'; } if(!PD_close(fileID)) cout << "Problem closing PDB file" << argv[3] << '\n'; #else cout << "PDB utilities not available" << endl; exit(0); #endif } else if (print_dest == "text_file") { cout << "Writing tabular text file " << argv[3] << '\n'; std::ofstream tabular_text(argv[3]); String curr_interf; PRPCacheCIter prp_iter = data_pairs.begin(); for (i=0; i<num_evals; ++i, ++prp_iter) { const String& new_interf = prp_iter->interface_id(); if (i == 0 || new_interf != curr_interf) { curr_interf = new_interf; // Header (note: use matlab comment syntax "%"): StringMultiArrayConstView acv_labels = prp_iter->prp_parameters().all_continuous_variable_labels(); StringMultiArrayConstView adiv_labels = prp_iter->prp_parameters().all_discrete_int_variable_labels(); StringMultiArrayConstView adrv_labels = prp_iter->prp_parameters().all_discrete_real_variable_labels(); const StringArray& fn_labels = prp_iter->prp_response().function_labels(); size_t num_acv = acv_labels.size(), num_adiv = adiv_labels.size(), num_adrv = adrv_labels.size(), num_fns = fn_labels.size(); if (!curr_interf.empty()) tabular_text << "%Interface = " << curr_interf << '\n'; tabular_text << "%eval_id "; for (j=0; j<num_acv; ++j) tabular_text << setw(14) << acv_labels[j].data() << ' '; for (j=0; j<num_adiv; ++j) tabular_text << setw(14) << adiv_labels[j].data() << ' '; for (j=0; j<num_adrv; ++j) tabular_text << setw(14) << adrv_labels[j].data() << ' '; for (j=0; j<num_fns; ++j) tabular_text << setw(14) << fn_labels[j].data() << ' '; tabular_text << '\n'; } // Data: (note: not maximum precision) prp_iter->write_tabular(tabular_text); } } cout << "Restart file processing completed: " << num_evals << " evaluations tabulated.\n"; }