#include #include #include #include #include "bmad.hpp" void write_bmad_file(const std::vector& metafits, const SimSettings& settings) { // Writes the converter file for the given meta fits std::string bmad_file_name = settings.output_directory + "/converter.bmad"; std::ofstream bmad_file; bmad_file.open(bmad_file_name); if (!bmad_file) { std::cerr << "ERROR: could not open \"" << bmad_file_name << "\"\n"; return; } size_t mf_ix=0; double T = metafits[0].T; bmad_file << "distribution = {\n"; bmad_file << TAB<1>() << "material = " << settings.target_material << ",\n"; bmad_file << TAB<1>() << "species_out = positron" << ",\n"; bmad_file << TAB<1>() << "thickness = " << T; for (const auto& mf : metafits) { if (mf.T != T) { // start a new distribution T = mf.T; bmad_file << "},\ndistribution = {\n"; bmad_file << TAB<1>() << "material = tungsten" << ",\n"; bmad_file << TAB<1>() << "species_out = positron" << ",\n"; bmad_file << TAB<1>() << "thickness = " << mf.T; } bmad_file << ",\n" << TAB<1>() << "sub_distribution = {\n"; bmad_file << TAB<2>() << "pc_in = " << mf.Ein << ",\n"; bmad_file << TAB<2>() << "spin_in = ["; if (settings.polarization_in.size()) { bmad_file << settings.polarization_in[0] << ", "; bmad_file << settings.polarization_in[1] << ", "; bmad_file << settings.polarization_in[2] << "],\n"; } else { bmad_file << "0, 0, 0],\n"; } bmad_file << TAB<2>() << "prob_pc_r = {\n"; bmad_file << TAB<3>() << "r_values = [0.0, "; // fix r=0 // Write r values to table (no comma after last one) auto num_rows = mf.er_table.pc_vals.size(); auto num_cols = mf.er_table.r_vals.size(); for (unsigned i=0; i() << "row = {pc_out = " << mf.er_table.pc_vals[i]; bmad_file << ", prob = [0.0, "; // r=0 -> prob = 0 for (unsigned j=0; j() << "},\n"; // Polarization table bmad_file << TAB<2>() << "spin_z_out = {\n"; bmad_file << TAB<3>() << "r_values = ["; // Write r values to table (no comma after last one) num_rows = mf.polz_table.pc_vals.size(); num_cols = mf.polz_table.r_vals.size(); for (unsigned i=0; i() << "row = {pc_out = " << mf.polz_table.pc_vals[i]; bmad_file << ", spin_z = ["; // r=0 -> prob = 0 for (unsigned j=0; j() << "},\n"; // Output the fit parameters bmad_file << TAB<2>() << "direction_out = {\n"; output_bmad(mf.cx, bmad_file, COMMA_AFTER); output_bmad(mf.ax, bmad_file, COMMA_AFTER); output_bmad(mf.ay, bmad_file, COMMA_AFTER); output_bmad(mf.beta, bmad_file, COMMA_AFTER); output_bmad(mf.dxds_min, bmad_file, COMMA_AFTER); output_bmad(mf.dxds_max, bmad_file, COMMA_AFTER); output_bmad(mf.dyds_max, bmad_file, NO_COMMA); bmad_file << TAB<2>() << "}\n" << TAB<1>() << "}"; } bmad_file << "\n}\n"; return; }