Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spectra file dump feature #379

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion docs/source/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ Default value: 0.25

**TRANSITION_DIPOLES**

Compute the transition dipole moments and oscillator strengths
Compute the transition dipole moments and oscillator strengths, then generate 'spectra.dat' file containing the final results.

Type: bool

Expand Down
13 changes: 13 additions & 0 deletions forte/base_classes/active_space_method.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ void ActiveSpaceMethod::set_print(PrintLevel level) { print_ = level; }

void ActiveSpaceMethod::set_quiet_mode() { set_print(PrintLevel::Quiet); }

std::map<std::string, std::vector<std::string>> ActiveSpaceMethod::get_spectra_results() { return spectra_results_; }

DeterminantHashVec ActiveSpaceMethod::get_PQ_space() { return final_wfn_; }

std::shared_ptr<psi::Matrix> ActiveSpaceMethod::get_PQ_evecs() { return evecs_; }
Expand Down Expand Up @@ -313,6 +315,12 @@ std::vector<double> ActiveSpaceMethod::compute_oscillator_strength_same_orbs(
psi::outfile->Printf("\n %6s %6s", name1.c_str(), name2.c_str());
psi::outfile->Printf("%15.8f%15.8f%15.8f", e_diff, e_diff * pc_hartree2ev, out[i]);

// push to results to spectra_results_ vector
std::vector<std::string> data {name1, name2, std::to_string(energies_[root1]),
std::to_string(energies2[root2]), std::to_string(e_diff),
std::to_string(e_diff * pc_hartree2ev), std::to_string(out[i])};
add_spectra_results(data);

// push to psi4 environment globals
std::string name_env = "OSC. " + multi_label + " " + name1 + " -> " + name2;
push_to_psi4_env_globals(out[i], name_env);
Expand Down Expand Up @@ -439,6 +447,11 @@ std::vector<std::shared_ptr<psi::Vector>> ActiveSpaceMethod::compute_transition_
return trans_dipoles;
}

void ActiveSpaceMethod::add_spectra_results(const std::vector<std::string>& data){
std::string trans_label = data[0] + "->" + data[1];
spectra_results_.emplace(trans_label, data);
}

std::shared_ptr<ActiveSpaceMethod> make_active_space_method(
const std::string& type, StateInfo state, size_t nroot, std::shared_ptr<SCFInfo> scf_info,
std::shared_ptr<MOSpaceInfo> mo_space_info, std::shared_ptr<ActiveSpaceIntegrals> as_ints,
Expand Down
9 changes: 9 additions & 0 deletions forte/base_classes/active_space_method.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ class ActiveSpaceMethod {
const std::vector<std::pair<size_t, size_t>>& root_list,
std::shared_ptr<ActiveSpaceMethod> method2);

/// Store spectra results of calculation
void add_spectra_results(const std::vector<std::string>& data);

/// Dump the wave function to file
/// @param file name
virtual void dump_wave_function(const std::string&) {
Expand Down Expand Up @@ -336,6 +339,9 @@ class ActiveSpaceMethod {
/// Quiet mode (no printing, for use with CASSCF)
void set_quiet_mode();

// Get map of state_i->state_f and the spectroscopic results
std::map<std::string, std::vector<std::string>> get_spectra_results();

/// Get the model space
DeterminantHashVec get_PQ_space();

Expand Down Expand Up @@ -394,6 +400,9 @@ class ActiveSpaceMethod {
/// The average value of S^2 of all the states. If empty this quantity will not be checked
std::vector<double> spin2_;

/// Map of state_i->state_f and the spectroscopic results
std::map<std::string, std::vector<std::string>> spectra_results_;

/// Read wave function from disk as initial guess?
bool read_wfn_guess_ = false;
/// Dump transition density matrix to disk?
Expand Down
33 changes: 33 additions & 0 deletions forte/base_classes/active_space_solver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <algorithm>
#include <numeric>
#include <tuple>
#include <fstream>
#include <filesystem>

#include "ambit/blocked_tensor.h"
#include "ambit/tensor.h"
Expand Down Expand Up @@ -166,6 +168,7 @@ const std::map<StateInfo, std::vector<double>>& ActiveSpaceSolver::compute_energ
compute_quadrupole_moment(as_mp_ints_);
if (options_->get_bool("TRANSITION_DIPOLES")) {
compute_fosc_same_orbs(as_mp_ints_);
dump_spectra_results();
}
}

Expand Down Expand Up @@ -365,6 +368,36 @@ void ActiveSpaceSolver::generalized_sigma(const StateInfo& state, std::shared_pt
state_method_map_[state]->generalized_sigma(x, sigma);
}

void ActiveSpaceSolver::dump_spectra_results() {
std::filesystem::path currentPath = std::filesystem::current_path();
std::string filename = (currentPath / "spectra.dat").string();
std::ofstream outFile(filename);

if (!outFile.is_open()) {
std::cerr << "Error: Unable to open spectra.dat file." << std::endl;
return;
}

outFile << "State1, State2, E1, E2, E2_minus_E1, E2_minus_E1_eV, Osc_au" << std::endl;

for (const auto& m : state_method_map_) {
auto calculation = m.second;
auto spectra = calculation->get_spectra_results();
std::string out;

for (const auto& s : spectra) {
out += s.first + ", ";
out += std::accumulate(s.second.begin(), s.second.end(), std::string{},
[](const std::string& a, const std::string& b) {
return a + (a.empty() ? "" : ", ") + b;
});
out += '\n';
}
outFile << out;
}
outFile.close();
}

void ActiveSpaceSolver::print_options() {
print_h2("Summary of Active Space Solver Input");

Expand Down
3 changes: 3 additions & 0 deletions forte/base_classes/active_space_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ class ActiveSpaceSolver {
/// Return the map of StateInfo to the wave function file name
std::map<StateInfo, std::string> state_filename_map() const { return state_filename_map_; }

/// Save spectra data to spectra.dat file
void dump_spectra_results();

/// Save the wave function to disk
void dump_wave_function();

Expand Down
2 changes: 1 addition & 1 deletion forte/register_forte_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def register_active_space_solver_options(options):

options.add_bool("READ_ACTIVE_WFN_GUESS", False, "Read CI wave function of ActiveSpaceSolver from disk")

options.add_bool("TRANSITION_DIPOLES", False, "Compute the transition dipole moments and oscillator strengths")
options.add_bool("TRANSITION_DIPOLES", False, "Compute the transition dipole moments and oscillator strengths, then generate 'spectra.dat' file containing the final results.")

options.add_bool(
"PRINT_DIFFERENT_GAS_ONLY",
Expand Down