Skip to content

Commit

Permalink
Created an alpha.json output file for read in
Browse files Browse the repository at this point in the history
  • Loading branch information
ahurta92 committed Jul 1, 2024
1 parent b95778d commit af56bca
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 28 deletions.
7 changes: 1 addition & 6 deletions src/apps/molresponse/FrequencyResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ void FrequencyResponse::iterate(World &world)
int r_vector_size;
all_done = false;
r_vector_size = (compute_y) ? 2 * n : n;
Tensor<double> v_polar(m, m);
Tensor<double> polar;
Tensor<double> res_polar;

vecfuncT rho_omega_old(m);
// initialize DFT XC functional operator
Expand Down Expand Up @@ -124,7 +121,7 @@ void FrequencyResponse::iterate(World &world)
if (world.rank() == 0)
{
function_data_to_json(j_molresponse, iter, chi_norms, x_residual, rho_norms, density_residuals);
frequency_to_json(j_molresponse, iter, polar, res_polar);
frequency_to_json(j_molresponse, iter, polar, polar);
}
if (r_params.print_level() >= 1)
{
Expand Down Expand Up @@ -235,9 +232,7 @@ void FrequencyResponse::iterate(World &world)
iter_function_data["d"] = dnorm;

polar = ((compute_y) ? -2 : -4) * response_context.inner(Chi, PQ);
res_polar = ((compute_y) ? -2 : -4) * response_context.inner(new_res.residual, PQ);
inner_to_json(world, "alpha", polar, iter_function_data);
inner_to_json(world, "r_alpha", res_polar, iter_function_data);
if (r_params.print_level() >= 20)
{
if (world.rank() == 0)
Expand Down
5 changes: 5 additions & 0 deletions src/apps/molresponse/FrequencyResponse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,13 @@ class FrequencyResponse : public ResponseBase

RHS_Generator generator;

std::pair<double,Tensor<double>> get_response_data(){
return {omega, polar};
};

private:
double omega;
Tensor<double> polar;
void iterate(World &world) override;
X_space bsh_update_response(World &world, X_space &theta_X, vector<poperatorT> &bsh_x_ops, vector<poperatorT> &bsh_y_ops, QProjector<double, 3> &projector, double &x_shifts);
static void frequency_to_json(json &j_mol_in, size_t iter, const Tensor<double> &polar_ij, const Tensor<double> &res_polar_ij);
Expand Down
72 changes: 50 additions & 22 deletions src/apps/molresponse/maddft/response_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ void write_molecule_json_to_input_file(const json &molecule_json, std::ostream &
{
output_stream << " " << symbols[i] << " " << geometry[i][0] << " " << geometry[i][1] << " " << geometry[i][2] << std::endl;
}
output_stream << "end" << std::endl << std::endl;
output_stream << "end" << std::endl
<< std::endl;
}

void write_json_to_input_file(const json &input_json, const std::vector<std::string> &keys, std::ostream &output_stream)
Expand All @@ -89,7 +90,8 @@ void write_json_to_input_file(const json &input_json, const std::vector<std::str
{
output_stream << " " << key << " " << value << std::endl;
}
output_stream << "end" << std::endl << std::endl;
output_stream << "end" << std::endl
<< std::endl;
}
}

Expand Down Expand Up @@ -414,7 +416,7 @@ class ResponseCalcManager
path root; // root directory
ParameterManager parameter_manager;

path moldft_path; // base moldft path
path moldft_path; // base moldft path
path moldft_restart; // path to moldft restart file
path moldft_calc_info_path;
path moldft_scf_calc_info_path;
Expand All @@ -426,6 +428,7 @@ class ResponseCalcManager
vector<path> response_restart_paths;
vector<path> response_outfiles_paths;

path alpha_json_path;
path quadratic_json_path; // path to the quadratic json file
// paths to the moldft and response calculations

Expand Down Expand Up @@ -459,14 +462,14 @@ class ResponseCalcManager
//
//


if (parameter_manager.get_molresponse_params().quadratic())
{

vector<double> freqs_copy = freq;
auto num_freqs = freq.size();

auto compare_freqs = [](double x, double y) { return std::abs(x - y) < 1e-3; };
auto compare_freqs = [](double x, double y)
{ return std::abs(x - y) < 1e-3; };

for (int i = 0; i < num_freqs; i++)
{
Expand All @@ -476,7 +479,8 @@ class ResponseCalcManager
auto omega_2 = freq[j];
auto omega_3 = omega_1 + omega_2;

if (std::find_if(freqs_copy.begin(), freqs_copy.end(), [&](double x) { return compare_freqs(x, omega_3); }) != freqs_copy.end())
if (std::find_if(freqs_copy.begin(), freqs_copy.end(), [&](double x)
{ return compare_freqs(x, omega_3); }) != freqs_copy.end())
{
continue;
}
Expand All @@ -486,14 +490,12 @@ class ResponseCalcManager
}
}


freq = freqs_copy;
std::sort(freq.begin(), freq.end());
// only unique frequencies
freq.erase(std::unique(freq.begin(), freq.end()), freq.end());
}


root = std::filesystem::current_path();

json input_json = parameter_manager.get_input_json();
Expand All @@ -504,7 +506,7 @@ class ResponseCalcManager
auto hash = json_hash(input_json);
std::string output_directory = "output"; // + std::to_string(hash);

moldft_path = root / output_directory;
moldft_path = root;
moldft_restart = moldft_path / "moldft.restartdata.00000";
moldft_calc_info_path = moldft_path / "moldft.calc_info.json";
moldft_scf_calc_info_path = moldft_path / "moldft.scf_info.json";
Expand All @@ -522,6 +524,7 @@ class ResponseCalcManager
if (parameter_manager.get_run_response())
{
define_response_paths();
alpha_json_path = moldft_path / "alpha.json";
}

if (parameter_manager.get_molresponse_params().quadratic())
Expand Down Expand Up @@ -557,6 +560,7 @@ class ResponseCalcManager
}

calc_path_json["quadratic_json_path"] = getAbsolutePath(quadratic_json_path, root);
calc_path_json["alpha_json_path"] = getAbsolutePath(alpha_json_path, root);
};

create_calc_path_json();
Expand Down Expand Up @@ -839,7 +843,7 @@ class ResponseCalcManager
* @param restart_path
* @return
*/
auto RunResponse(World &world, const std::string &filename, double frequency, std::filesystem::path restart_path) -> std::pair<std::filesystem::path, bool>
auto RunResponse(World &world, const std::string &filename, double frequency, std::filesystem::path restart_path, nlohmann::ordered_json &alpha_json) -> std::pair<std::filesystem::path, bool>
{
// Set the response parameters
ResponseParameters r_params = parameter_manager.get_molresponse_params();
Expand Down Expand Up @@ -888,6 +892,23 @@ class ResponseCalcManager
calc_params.response_parameters.to_json(calc.j_molresponse);
}
calc.solve(world);
auto [omega, polar_omega] = calc.get_response_data();
// flatten polar_omega
auto alpha = polar_omega.flat();
std::vector<std::string> ij{"XX", "XY", "XZ", "YX", "YY", "YZ", "ZX", "ZY", "ZZ"};

append_to_alpha_json(omega, ij, alpha, alpha_json);
std::ofstream outfile("../alpha.json");
if (outfile.is_open())
{
outfile << alpha_json.dump(4);
outfile.close();
}
else
{
std::cerr << "Failed to open file for writing: " << "../alpha.json" << std::endl;
}

world.gop.fence();
// set protocol to the first
if (world.rank() == 0)
Expand Down Expand Up @@ -918,6 +939,7 @@ class ResponseCalcManager
std::pair<std::filesystem::path, bool> success{moldft_path, false};
bool first = true;

nlohmann::ordered_json alpha_json;

for (const auto &freq : freq)
{
Expand All @@ -943,7 +965,7 @@ class ResponseCalcManager
{
throw Response_Convergence_Error{};
}
success = RunResponse(world, "response.in", freq, restart_path);
success = RunResponse(world, "response.in", freq, restart_path, alpha_json);
// high_prec);
if (world.rank() == 0)
{
Expand Down Expand Up @@ -1085,7 +1107,6 @@ class ResponseCalcManager
::print("Number of frequencies: ", num_freqs);
}


for (int b = 0; b < num_freqs; b++)
{
for (int c = b; c < num_freqs; c++)
Expand Down Expand Up @@ -1147,8 +1168,6 @@ class ResponseCalcManager
}
}
}

nlohmann::ordered_json beta_entry;
append_to_beta_json({-1.0 * omega_a, omega_b, omega_c}, beta_directions, beta, beta_json);
std::ofstream outfile("beta.json");
if (outfile.is_open())
Expand Down Expand Up @@ -1228,16 +1247,30 @@ class ResponseCalcManager
// i need A B C to hold char values and A-freq, B-freq, C-freq to hold
// double values

nlohmann::ordered_json beta_json = {{"A-freq", json::array()}, {"B-freq", json::array()}, {"C-freq", json::array()}, {"A", json::array()},
{"B", json::array()}, {"C", json::array()}, {"Beta", json::array()}};
nlohmann::ordered_json beta_json = {{"A-freq", json::array()}, {"B-freq", json::array()}, {"C-freq", json::array()}, {"A", json::array()}, {"B", json::array()}, {"C", json::array()}, {"Beta", json::array()}};
return beta_json;
}
static nlohmann::ordered_json create_alpha_json()
{
nlohmann::ordered_json alpha_json = {{"omega", json::array()}, {"ij", json::array()}, {"alpha", json::array()}};
return alpha_json;
}

static void append_to_alpha_json(const double &omega, const std::vector<std::string> &ij, const Tensor<double> &alpha, nlohmann::ordered_json &alpha_json)
{
auto num_unique_elements = ij.size();
for (int i = 0; i < num_unique_elements; i++)
{
alpha_json["omega"].push_back(omega);
alpha_json["ij"].push_back(ij[i]);
alpha_json["alpha"].push_back(alpha[i]);
}
}

// for a set of frequencies create a table from the beta values
static void append_to_beta_json(const std::array<double, 3> &freq, const std::vector<std::string> &beta_directions, const Tensor<double> &beta, nlohmann::ordered_json &beta_json)
{


auto num_unique_elements = beta_directions.size();

auto push_back_others = [&](auto other_ijk, auto beta_value)
Expand All @@ -1255,11 +1288,9 @@ class ResponseCalcManager
}
};


if (num_unique_elements == 12)
{


for (int i = 0; i < num_unique_elements; i++)
{
auto ijk = beta_directions[i];
Expand All @@ -1269,7 +1300,6 @@ class ResponseCalcManager
auto B = ijk[1];
auto C = ijk[2];


beta_json["A-freq"].push_back(freq[0]);
beta_json["B-freq"].push_back(freq[1]);
beta_json["C-freq"].push_back(freq[2]);
Expand Down Expand Up @@ -1328,7 +1358,6 @@ class ResponseCalcManager
auto B = ijk[1];
auto C = ijk[2];


beta_json["A-freq"].push_back(freq[0]);
beta_json["B-freq"].push_back(freq[1]);
beta_json["C-freq"].push_back(freq[2]);
Expand Down Expand Up @@ -1396,7 +1425,6 @@ class ResponseCalcManager
auto B = ijk[1];
auto C = ijk[2];


beta_json["A-freq"].push_back(freq[0]);
beta_json["B-freq"].push_back(freq[1]);
beta_json["C-freq"].push_back(freq[2]);
Expand Down

0 comments on commit af56bca

Please sign in to comment.