diff --git a/cxx/include/scf/property_types/convergence_uncertainty.hpp b/cxx/include/scf/property_types/convergence_uncertainty.hpp new file mode 100644 index 0000000..e2844aa --- /dev/null +++ b/cxx/include/scf/property_types/convergence_uncertainty.hpp @@ -0,0 +1,49 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once +#include +#include + +namespace scf::property_types { + +/** @brief Attaches the uncertainty from incomplete convergence to an energy. + * + * Called by an iterative method once it has converged. The result is the + * energy with uncertainty attached, *e.g.*, from the change in the energy + * in the final iteration. + */ +DECLARE_PROPERTY_TYPE(ConvergenceUncertainty); + +PROPERTY_TYPE_INPUTS(ConvergenceUncertainty) { + using tensor_type = const simde::type::tensor&; + auto rv = pluginplay::declare_input() + .add_field("Energy") + .template add_field("Energy change"); + rv["Energy"].set_description("The converged energy"); + rv["Energy change"].set_description( + "The change in the energy in the final iteration"); + return rv; +} + +PROPERTY_TYPE_RESULTS(ConvergenceUncertainty) { + auto rv = + pluginplay::declare_result().add_field("Energy"); + rv["Energy"].set_description("The energy with uncertainty attached"); + return rv; +} + +} // namespace scf::property_types diff --git a/cxx/include/scf/property_types.hpp b/cxx/include/scf/property_types/mo_uncertainty.hpp similarity index 66% rename from cxx/include/scf/property_types.hpp rename to cxx/include/scf/property_types/mo_uncertainty.hpp index 40206cf..2b7f535 100644 --- a/cxx/include/scf/property_types.hpp +++ b/cxx/include/scf/property_types/mo_uncertainty.hpp @@ -14,19 +14,10 @@ * limitations under the License. */ -/** @file property_types.hpp - * - * The property types defined by SCF. They are candidates for SimDE once - * their APIs have settled. - */ #pragma once #include #include -/** @namespace scf::property_types - * - * @brief The namespace for property types defined by SCF - */ namespace scf::property_types { /** @brief Attaches uncertainty to converged molecular orbitals. @@ -66,30 +57,4 @@ PROPERTY_TYPE_RESULTS(MOUncertainty) { return rv; } -/** @brief Attaches the uncertainty from incomplete convergence to an energy. - * - * Called by an iterative method once it has converged. The result is the - * energy with uncertainty attached, *e.g.*, from the change in the energy - * in the final iteration. - */ -DECLARE_PROPERTY_TYPE(ConvergenceUncertainty); - -PROPERTY_TYPE_INPUTS(ConvergenceUncertainty) { - using tensor_type = const simde::type::tensor&; - auto rv = pluginplay::declare_input() - .add_field("Energy") - .template add_field("Energy change"); - rv["Energy"].set_description("The converged energy"); - rv["Energy change"].set_description( - "The change in the energy in the final iteration"); - return rv; -} - -PROPERTY_TYPE_RESULTS(ConvergenceUncertainty) { - auto rv = - pluginplay::declare_result().add_field("Energy"); - rv["Energy"].set_description("The energy with uncertainty attached"); - return rv; -} - } // namespace scf::property_types diff --git a/cxx/include/scf/property_types/property_types.hpp b/cxx/include/scf/property_types/property_types.hpp new file mode 100644 index 0000000..71cb068 --- /dev/null +++ b/cxx/include/scf/property_types/property_types.hpp @@ -0,0 +1,31 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** @file property_types.hpp + * + * Convenience header that includes all of the property types defined by SCF. + * They are candidates for SimDE once their APIs have settled. + */ +#pragma once +#include +#include +#include +#include + +/** @namespace scf::property_types + * + * @brief The namespace for property types defined by SCF + */ diff --git a/cxx/include/scf/property_types/quadrature.hpp b/cxx/include/scf/property_types/quadrature.hpp new file mode 100644 index 0000000..2f8a663 --- /dev/null +++ b/cxx/include/scf/property_types/quadrature.hpp @@ -0,0 +1,63 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once +#include +#include +#include +#include + +namespace scf::property_types { + +/** @brief Evaluates the quantity of another property type by quadrature. + * + * Modules satisfying this property type evaluate the quantity computed by + * @p BasePT by quadrature on a grid they are given, rather than on a grid + * they build themselves. This lets the caller choose the grid, *e.g.*, to + * evaluate the same quadrature on a finer grid to estimate the error of the + * grid. The inputs are those of @p BasePT plus the grid, and the results are + * those of @p BasePT. + * + * @tparam BasePT The property type whose quantity is evaluated by + * quadrature, *e.g.*, simde::aos_xc_e_aos for the XC + * potential. + */ +template +DECLARE_TEMPLATED_PROPERTY_TYPE(Quadrature, BasePT); + +template +TEMPLATED_PROPERTY_TYPE_INPUTS(Quadrature, BasePT) { + using grid_type = const chemist::Grid&; + auto rv = BasePT::inputs(); + auto rv0 = rv.template add_field("Grid"); + rv0["Grid"].set_description("The grid the quadrature is evaluated on"); + return rv0; +} + +template +TEMPLATED_PROPERTY_TYPE_RESULTS(Quadrature, BasePT) { + return BasePT::results(); +} + +/// Property type for evaluating the XC energy by quadrature +using xc_energy_quadrature = + Quadrature>; + +/// Property type for evaluating the AO matrix of the XC potential by quadrature +using xc_potential_quadrature = Quadrature; + +} // namespace scf::property_types diff --git a/cxx/include/scf/property_types/xc_uncertain.hpp b/cxx/include/scf/property_types/xc_uncertain.hpp new file mode 100644 index 0000000..152186b --- /dev/null +++ b/cxx/include/scf/property_types/xc_uncertain.hpp @@ -0,0 +1,63 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once +#include +#include + +namespace scf::property_types { + +/** @brief Attaches uncertainty to the result of another property type. + * + * Modules satisfying this property type are error models for the XC energy + * and potential, *e.g.*, for the error from the incompleteness of the grid. + * They are given the inputs of @p BasePT and the results of calling a module + * satisfying @p BasePT with those inputs. The results are those of + * @p BasePT, but with uncertainty attached: each value is a UQ type whose + * nominal value is the corresponding value of the given results. The module + * satisfying this property type gets the UQ type from its own + * ``UQ Initializer`` submodule. + * + * This property type is a stop gap. It should eventually replace + * SimDE's ``Uncertainty`` property type. + * + * @tparam BasePT The property type of the module whose result is having + * uncertainty attached, *e.g.*, xc_potential_quadrature. + */ +template +DECLARE_TEMPLATED_PROPERTY_TYPE(XCUncertain, BasePT); + +template +TEMPLATED_PROPERTY_TYPE_INPUTS(XCUncertain, BasePT) { + // Appends each result of BasePT, with the same key and type, as an input + auto rv = BasePT::inputs() + BasePT::results(); + for(const auto& [key, value] : BasePT::results()) + rv[key].set_description("The value to attach uncertainty to"); + return rv; +} + +template +TEMPLATED_PROPERTY_TYPE_RESULTS(XCUncertain, BasePT) { + return BasePT::results(); +} + +/// Property type for attaching uncertainty to the XC energy from quadrature +using xc_energy_uncertainty = XCUncertain; + +/// Property type for attaching uncertainty to the XC potential from quadrature +using xc_potential_uncertainty = XCUncertain; + +} // namespace scf::property_types diff --git a/cxx/include/scf/scf.hpp b/cxx/include/scf/scf.hpp index 6414391..0b1b469 100644 --- a/cxx/include/scf/scf.hpp +++ b/cxx/include/scf/scf.hpp @@ -16,7 +16,7 @@ #pragma once #include -#include +#include #include #include #ifdef BUILD_TAMM_SCF diff --git a/cxx/src/scf/driver/driver.hpp b/cxx/src/scf/driver/driver.hpp index 73a2d65..4f79931 100644 --- a/cxx/src/scf/driver/driver.hpp +++ b/cxx/src/scf/driver/driver.hpp @@ -15,7 +15,7 @@ */ #pragma once -#include +#include #include namespace scf::driver { diff --git a/cxx/src/scf/export_scf.cpp b/cxx/src/scf/export_scf.cpp index 4880bcf..d25f31a 100644 --- a/cxx/src/scf/export_scf.cpp +++ b/cxx/src/scf/export_scf.cpp @@ -46,6 +46,10 @@ EXPORT_PLUGIN(scf, SCF, m) { using namespace scf::property_types; EXPORT_PROPERTY_TYPE(MOUncertainty, m); EXPORT_PROPERTY_TYPE(ConvergenceUncertainty, m); + EXPORT_PROPERTY_TYPE(xc_energy_quadrature, m); + EXPORT_PROPERTY_TYPE(xc_potential_quadrature, m); + EXPORT_PROPERTY_TYPE(xc_energy_uncertainty, m); + EXPORT_PROPERTY_TYPE(xc_potential_uncertainty, m); } } // namespace scf diff --git a/cxx/src/scf/scf_mm.cpp b/cxx/src/scf/scf_mm.cpp index 531ae29..f419378 100644 --- a/cxx/src/scf/scf_mm.cpp +++ b/cxx/src/scf/scf_mm.cpp @@ -76,6 +76,11 @@ PLUGIN_CTOR(SCF) { "Analytic Error"); default_submod_from("integrals", "QCUP SCF", "Benchmark ERIs", "Analytic Benchmark ERI4"); + + default_submod_from("integrals", "Reference Grid XC Energy Error", + "UQ Initializer", "UQ Initializer"); + default_submod_from("integrals", "Reference Grid XC Potential Error", + "UQ Initializer", "UQ Initializer"); } } // namespace scf diff --git a/cxx/src/scf/uncertainty/uncertainty.hpp b/cxx/src/scf/uncertainty/uncertainty.hpp index dd1907d..3a8d20a 100644 --- a/cxx/src/scf/uncertainty/uncertainty.hpp +++ b/cxx/src/scf/uncertainty/uncertainty.hpp @@ -15,7 +15,7 @@ */ #pragma once -#include +#include #include namespace scf::uncertainty { diff --git a/cxx/src/scf/xc/libxc/libxc.cpp b/cxx/src/scf/xc/libxc/libxc.cpp index 590a06f..b1c9c6f 100644 --- a/cxx/src/scf/xc/libxc/libxc.cpp +++ b/cxx/src/scf/xc/libxc/libxc.cpp @@ -14,6 +14,7 @@ * limitations under the License. */ +#include "../xc.hpp" #include "libxc.hpp" #ifdef BUILD_LIBXC #include "xc.h" @@ -41,19 +42,25 @@ const auto* get_const_pointer(const tensorwrapper::buffer::BufferBase& buffer) { } // namespace void load_modules(pluginplay::ModuleMap& modules) { - modules.add_module("LibXC Energy"); - modules.add_module("LibXC Potential"); + modules.add_module("LibXC Energy"); + modules.add_module("LibXC Potential"); + modules.add_module("LibXC Energy Quadrature"); + modules.add_module("LibXC Potential Quadrature"); } void set_defaults(pluginplay::ModuleMap& modules) { - modules.change_submod("LibXC Energy", "Integration grid", - "Grid From IntegratorXX"); - modules.change_submod("LibXC Potential", "Integration grid", - "Grid From IntegratorXX"); - modules.change_submod("LibXC Energy", "Density on a grid", "Density2Grid"); - modules.change_submod("LibXC Potential", "Density on a grid", - "Density2Grid"); - modules.change_submod("LibXC Potential", "AOs on a grid", "AOs on a Grid"); + const auto e_key = "LibXC Energy"; + const auto v_key = "LibXC Potential"; + const auto e_q_key = "LibXC Energy Quadrature"; + const auto v_q_key = "LibXC Potential Quadrature"; + const auto grid_key = "Grid From IntegratorXX"; + modules.change_submod(e_key, "Integration grid", grid_key); + modules.change_submod(v_key, "Integration grid", grid_key); + modules.change_submod(e_key, "Quadrature", e_q_key); + modules.change_submod(v_key, "Quadrature", v_q_key); + modules.change_submod(e_q_key, "Density on a grid", "Density2Grid"); + modules.change_submod(v_q_key, "Density on a grid", "Density2Grid"); + modules.change_submod(v_q_key, "AOs on a grid", "AOs on a grid"); } std::pair to_libxc_codes(chemist::qm_operator::xc_functional func) { diff --git a/cxx/src/scf/xc/libxc/libxc.hpp b/cxx/src/scf/xc/libxc/libxc.hpp index f827980..e862afe 100644 --- a/cxx/src/scf/xc/libxc/libxc.hpp +++ b/cxx/src/scf/xc/libxc/libxc.hpp @@ -20,8 +20,8 @@ namespace scf::xc::libxc { -DECLARE_MODULE(LibXCEnergy); -DECLARE_MODULE(LibXCPotential); +DECLARE_MODULE(LibXCEnergyQuadrature); +DECLARE_MODULE(LibXCPotentialQuadrature); void set_defaults(pluginplay::ModuleMap& modules); void load_modules(pluginplay::ModuleMap& modules); diff --git a/cxx/src/scf/xc/libxc/libxc_energy.cpp b/cxx/src/scf/xc/libxc/libxc_energy.cpp index 2b3ddb5..30c4746 100644 --- a/cxx/src/scf/xc/libxc/libxc_energy.cpp +++ b/cxx/src/scf/xc/libxc/libxc_energy.cpp @@ -15,34 +15,34 @@ */ #include "libxc.hpp" +#include #include namespace scf::xc::libxc { namespace { -const auto desc = R"()"; +const auto desc = R"( +Exchange-Correlation Energy Quadrature via LibXC +================================================ -} // namespace - -using XC_e_t = simde::type::XC_e_type; +Evaluates the exchange-correlation energy of a restricted wavefunction by +quadrature on the provided grid, using LibXC to evaluate the LDA functional. +Only double-precision densities are supported. +)"; -template -using pt = simde::eval_braket; +} // namespace -using grid_pt = simde::MolecularGrid; +using pt = scf::property_types::xc_energy_quadrature; using rho2grid_pt = simde::EDensityCollocationMatrix; -MODULE_CTOR(LibXCEnergy) { - using wf_type = simde::type::rscf_wf; - satisfies_property_type>(); +MODULE_CTOR(LibXCEnergyQuadrature) { + satisfies_property_type(); description(desc); - add_submodule("Integration grid"); add_submodule("Density on a grid"); } -MODULE_RUN(LibXCEnergy) { - using wf_type = simde::type::rscf_wf; - const auto& [braket] = pt::unwrap_inputs(inputs); +MODULE_RUN(LibXCEnergyQuadrature) { + const auto& [braket, grid] = pt::unwrap_inputs(inputs); const auto& bra_wf = braket.bra(); const auto& xc_op = braket.op(); @@ -53,14 +53,6 @@ MODULE_RUN(LibXCEnergy) { const auto func_name = xc_op.get_functional_name(); const auto& P = xc_op.get_rhs_particle(); - const auto& aos = P.basis_set().ao_basis_set(); - - // Molecule from AOs - const auto mol = libxc::aos2molecule(aos); - - // Get grid - auto& grid_mod = submods.at("Integration grid"); - const auto& grid = grid_mod.run_as(mol); // Get density on grid auto rho_mod = submods.at("Density on a grid"); @@ -75,7 +67,7 @@ MODULE_RUN(LibXCEnergy) { exc_xc("") = weight("i") * x_xc("i"); auto rv = results(); - return pt::wrap_results(rv, exc_xc); + return pt::wrap_results(rv, exc_xc); } } // namespace scf::xc::libxc diff --git a/cxx/src/scf/xc/libxc/libxc_potential.cpp b/cxx/src/scf/xc/libxc/libxc_potential.cpp index 7ab43f6..f13569c 100644 --- a/cxx/src/scf/xc/libxc/libxc_potential.cpp +++ b/cxx/src/scf/xc/libxc/libxc_potential.cpp @@ -15,31 +15,34 @@ */ #include "libxc.hpp" +#include #include namespace scf::xc::libxc { namespace { const auto desc = R"( -Exchange-Correlation Potential via LibXC -======================================== +Exchange-Correlation Potential Quadrature via LibXC +=================================================== + +Evaluates the AO matrix of the exchange-correlation potential by quadrature on +the provided grid, using LibXC to evaluate the LDA functional. Only +double-precision densities are supported. )"; } -using pt = simde::aos_xc_e_aos; -using grid_pt = simde::MolecularGrid; +using pt = scf::property_types::xc_potential_quadrature; using ao_on_grid_pt = simde::AOCollocationMatrix; using rho2grid_pt = simde::EDensityCollocationMatrix; -MODULE_CTOR(LibXCPotential) { +MODULE_CTOR(LibXCPotentialQuadrature) { satisfies_property_type(); description(desc); - add_submodule("Integration grid"); add_submodule("AOs on a grid"); add_submodule("Density on a grid"); } -MODULE_RUN(LibXCPotential) { - const auto& [braket] = pt::unwrap_inputs(inputs); +MODULE_RUN(LibXCPotentialQuadrature) { + const auto& [braket, grid] = pt::unwrap_inputs(inputs); const auto& bra_aos = braket.bra(); const auto& xc_op = braket.op(); @@ -52,10 +55,6 @@ MODULE_RUN(LibXCPotential) { const auto& P = xc_op.get_rhs_particle(); const auto& aos = bra_aos.ao_basis_set(); - // Get grid - auto& grid_mod = submods.at("Integration grid"); - const auto& grid = grid_mod.run_as(libxc::aos2molecule(aos)); - // Get AOs on the grid auto& aos_on_grid_mod = submods.at("AOs on a grid"); const auto& aos_on_grid = aos_on_grid_mod.run_as(grid, aos); diff --git a/cxx/src/scf/xc/native/native.cpp b/cxx/src/scf/xc/native/native.cpp index 9e9f4d7..e5df826 100644 --- a/cxx/src/scf/xc/native/native.cpp +++ b/cxx/src/scf/xc/native/native.cpp @@ -14,6 +14,7 @@ * limitations under the License. */ +#include "../xc.hpp" #include "native.hpp" #include "svwn3.hpp" #include @@ -89,19 +90,25 @@ simde::type::tensor visit(KernelType&& k, const simde::type::tensor& t) { } // namespace void load_modules(pluginplay::ModuleMap& modules) { - modules.add_module("SVWN3 Energy"); - modules.add_module("SVWN3 Potential"); + modules.add_module("SVWN3 Energy"); + modules.add_module("SVWN3 Potential"); + modules.add_module("SVWN3 Energy Quadrature"); + modules.add_module("SVWN3 Potential Quadrature"); } void set_defaults(pluginplay::ModuleMap& modules) { - modules.change_submod("SVWN3 Energy", "Integration grid", - "Grid From IntegratorXX"); - modules.change_submod("SVWN3 Potential", "Integration grid", - "Grid From IntegratorXX"); - modules.change_submod("SVWN3 Energy", "Density on a grid", "Density2Grid"); - modules.change_submod("SVWN3 Potential", "Density on a grid", - "Density2Grid"); - modules.change_submod("SVWN3 Potential", "AOs on a grid", "AOs on a grid"); + const auto e_key = "SVWN3 Energy"; + const auto v_key = "SVWN3 Potential"; + const auto e_q_key = "SVWN3 Energy Quadrature"; + const auto v_q_key = "SVWN3 Potential Quadrature"; + const auto grid_key = "Grid From IntegratorXX"; + modules.change_submod(e_key, "Integration grid", grid_key); + modules.change_submod(v_key, "Integration grid", grid_key); + modules.change_submod(e_key, "Quadrature", e_q_key); + modules.change_submod(v_key, "Quadrature", v_q_key); + modules.change_submod(e_q_key, "Density on a grid", "Density2Grid"); + modules.change_submod(v_q_key, "Density on a grid", "Density2Grid"); + modules.change_submod(v_q_key, "AOs on a grid", "AOs on a grid"); } simde::type::tensor svwn3_energy_density( diff --git a/cxx/src/scf/xc/native/native.hpp b/cxx/src/scf/xc/native/native.hpp index 7d9f63a..e17d442 100644 --- a/cxx/src/scf/xc/native/native.hpp +++ b/cxx/src/scf/xc/native/native.hpp @@ -26,8 +26,8 @@ namespace scf::xc::native { -DECLARE_MODULE(SVWN3Energy); -DECLARE_MODULE(SVWN3Potential); +DECLARE_MODULE(SVWN3EnergyQuadrature); +DECLARE_MODULE(SVWN3PotentialQuadrature); void set_defaults(pluginplay::ModuleMap& modules); void load_modules(pluginplay::ModuleMap& modules); diff --git a/cxx/src/scf/xc/native/svwn3_energy.cpp b/cxx/src/scf/xc/native/svwn3_energy.cpp index 707cd8b..b6cd4ca 100644 --- a/cxx/src/scf/xc/native/svwn3_energy.cpp +++ b/cxx/src/scf/xc/native/svwn3_energy.cpp @@ -14,21 +14,21 @@ * limitations under the License. */ -#include "../libxc/libxc.hpp" #include "native.hpp" +#include #include namespace scf::xc::native { namespace { const auto desc = R"( -SVWN3 Exchange-Correlation Energy -================================= +SVWN3 Exchange-Correlation Energy Quadrature +============================================ -Computes the SVWN3 (Slater exchange plus VWN3 correlation) exchange-correlation -energy of a restricted wavefunction by numerical integration. The functional is -implemented natively, so this module works with any of the floating-point types -supported by the grid, AOs-on-a-grid, and density-on-a-grid submodules +Evaluates the SVWN3 (Slater exchange plus VWN3 correlation) exchange-correlation +energy of a restricted wavefunction by quadrature on the provided grid. The +functional is implemented natively, so this module works with any of the +floating-point types supported by the grid and the density-on-a-grid submodule (including the UQ types). For a spin-unpolarized density SVWN3 is identical to LibXC's XC_LDA_X plus XC_LDA_C_VWN_3. Unlike LibXC, no density threshold is applied. @@ -36,25 +36,17 @@ applied. } // namespace -using XC_e_t = simde::type::XC_e_type; - -template -using pt = simde::eval_braket; - -using grid_pt = simde::MolecularGrid; +using pt = scf::property_types::xc_energy_quadrature; using rho2grid_pt = simde::EDensityCollocationMatrix; -MODULE_CTOR(SVWN3Energy) { - using wf_type = simde::type::rscf_wf; - satisfies_property_type>(); +MODULE_CTOR(SVWN3EnergyQuadrature) { + satisfies_property_type(); description(desc); - add_submodule("Integration grid"); add_submodule("Density on a grid"); } -MODULE_RUN(SVWN3Energy) { - using wf_type = simde::type::rscf_wf; - const auto& [braket] = pt::unwrap_inputs(inputs); +MODULE_RUN(SVWN3EnergyQuadrature) { + const auto& [braket, grid] = pt::unwrap_inputs(inputs); const auto& bra_wf = braket.bra(); const auto& xc_op = braket.op(); @@ -67,12 +59,7 @@ MODULE_RUN(SVWN3Energy) { chemist::qm_operator::xc_functional::SVWN3) throw std::runtime_error("SVWN3 Energy only supports SVWN3"); - const auto& P = xc_op.get_rhs_particle(); - const auto& aos = P.basis_set().ao_basis_set(); - - // Get grid - auto& grid_mod = submods.at("Integration grid"); - const auto& grid = grid_mod.run_as(libxc::aos2molecule(aos)); + const auto& P = xc_op.get_rhs_particle(); // Get density on grid auto& rho_mod = submods.at("Density on a grid"); @@ -85,7 +72,7 @@ MODULE_RUN(SVWN3Energy) { exc("") = weights("i") * e_xc("i"); auto rv = results(); - return pt::wrap_results(rv, exc); + return pt::wrap_results(rv, exc); } } // namespace scf::xc::native diff --git a/cxx/src/scf/xc/native/svwn3_potential.cpp b/cxx/src/scf/xc/native/svwn3_potential.cpp index ebc7c6f..f1353a6 100644 --- a/cxx/src/scf/xc/native/svwn3_potential.cpp +++ b/cxx/src/scf/xc/native/svwn3_potential.cpp @@ -16,41 +16,40 @@ #include "../libxc/libxc.hpp" #include "native.hpp" +#include #include namespace scf::xc::native { namespace { const auto desc = R"( -SVWN3 Exchange-Correlation Potential -==================================== - -Computes the AO matrix of the SVWN3 (Slater exchange plus VWN3 correlation) -exchange-correlation potential by numerical integration. The functional is -implemented natively, so this module works with any of the floating-point types -supported by the grid, AOs-on-a-grid, and density-on-a-grid submodules -(including the UQ types). For a spin-unpolarized density SVWN3 is identical to -LibXC's XC_LDA_X plus XC_LDA_C_VWN_3. Unlike LibXC, no density threshold is -applied. +SVWN3 Exchange-Correlation Potential Quadrature +=============================================== + +Evaluates the AO matrix of the SVWN3 (Slater exchange plus VWN3 correlation) +exchange-correlation potential by quadrature on the provided grid. The +functional is implemented natively, so this module works with any of the +floating-point types supported by the grid, AOs-on-a-grid, and +density-on-a-grid submodules (including the UQ types). For a spin-unpolarized +density SVWN3 is identical to LibXC's XC_LDA_X plus XC_LDA_C_VWN_3. Unlike +LibXC, no density threshold is applied. )"; } // namespace -using pt = simde::aos_xc_e_aos; -using grid_pt = simde::MolecularGrid; +using pt = scf::property_types::xc_potential_quadrature; using ao_on_grid_pt = simde::AOCollocationMatrix; using rho2grid_pt = simde::EDensityCollocationMatrix; -MODULE_CTOR(SVWN3Potential) { +MODULE_CTOR(SVWN3PotentialQuadrature) { satisfies_property_type(); description(desc); - add_submodule("Integration grid"); add_submodule("AOs on a grid"); add_submodule("Density on a grid"); } -MODULE_RUN(SVWN3Potential) { - const auto& [braket] = pt::unwrap_inputs(inputs); +MODULE_RUN(SVWN3PotentialQuadrature) { + const auto& [braket, grid] = pt::unwrap_inputs(inputs); const auto& bra_aos = braket.bra(); const auto& xc_op = braket.op(); @@ -66,10 +65,6 @@ MODULE_RUN(SVWN3Potential) { const auto& P = xc_op.get_rhs_particle(); const auto& aos = bra_aos.ao_basis_set(); - // Get grid - auto& grid_mod = submods.at("Integration grid"); - const auto& grid = grid_mod.run_as(libxc::aos2molecule(aos)); - // Get AOs on the grid auto& aos_on_grid_mod = submods.at("AOs on a grid"); const auto& aos_on_grid = aos_on_grid_mod.run_as(grid, aos); diff --git a/cxx/src/scf/xc/reference_grid_error.cpp b/cxx/src/scf/xc/reference_grid_error.cpp new file mode 100644 index 0000000..09057ce --- /dev/null +++ b/cxx/src/scf/xc/reference_grid_error.cpp @@ -0,0 +1,175 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "libxc/libxc.hpp" +#include "xc.hpp" +#include +#include +#include +#include +#include +#include + +namespace scf::xc { +namespace { + +const auto desc = R"( +Reference Grid Error +==================== + +Estimates the error of an exchange-correlation (XC) quadrature from the +incompleteness of its grid, and attaches it to the result of the quadrature. + +The quadrature is evaluated a second time, by the "Reference XC Module" +submodule, on the much finer grid built by the "Reference grid" submodule. Let +d be the reference value minus the given value. The sign of d is taken as the +direction of the error, so the given value becomes an uncertain value whose +nominal value is the given value, whose deviation towards the reference value +is |d|, and whose deviation away from it is "Opposite Side Fraction" times +|d|. This is done element-wise, e.g., for each element of the XC potential. + +The kind of uncertain value comes from the "UQ Initializer" submodule. Only the +triplex kind stores both deviations exactly; the other kinds store the +tightest value they can which contains them. + +This is an estimate, not a bound: refinement need not converge monotonically, +so the reference value can lie on the wrong side of the exact integral, and a +one-sided deviation then excludes it. Increasing "Opposite Side Fraction" +guards against this at the cost of a wider interval. + +The "Reference XC Module" must evaluate the same functional as the module +whose result is being given. Each call costs one quadrature on the reference +grid, including the density and AOs on that grid. + +The given value must hold doubles. +)"; + +/// Returns the AO basis set of an XC energy or XC potential braket +template +const auto& get_aos(const BraKetType& braket) { + using bra_type = std::decay_t; + if constexpr(std::is_same_v) { + return braket.bra().ao_basis_set(); + } else { + return braket.op().get_rhs_particle().basis_set().ao_basis_set(); + } +} + +/// Returns the elements of @p t, which must hold doubles, in row-major order +std::vector to_doubles(const simde::type::tensor& t, + const std::string& what) { + using tensorwrapper::buffer::make_contiguous; + using tensorwrapper::buffer::visit_contiguous_buffer; + auto visitor = [&](auto span) -> std::vector { + using value_type = typename decltype(span)::value_type; + if constexpr(std::is_same_v, double>) { + return std::vector(span.begin(), span.end()); + } else { + throw std::runtime_error("Reference Grid Error: the " + what + + " must hold doubles"); + } + }; + return visit_contiguous_buffer(visitor, make_contiguous(t.buffer())); +} + +/// Returns the shape of @p t +tensorwrapper::shape::Smooth get_shape(const simde::type::tensor& t) { + using tensorwrapper::buffer::make_contiguous; + const auto shape = make_contiguous(t.buffer()).shape(); + std::vector extents; + for(std::size_t i = 0; i < shape.rank(); ++i) + extents.push_back(shape.extent(i)); + return tensorwrapper::shape::Smooth(extents.begin(), extents.end()); +} + +} // namespace + +template +using error_pt = scf::property_types::XCUncertain; + +using grid_pt = simde::MolecularGrid; +using uq_pt = simde::UQInitializer; + +template +TEMPLATED_MODULE_CTOR(ReferenceGridError, QuadPT) { + satisfies_property_type>(); + description(desc); + + add_submodule("Reference grid") + .set_description("Builds the finer grid the reference value is " + "evaluated on"); + add_submodule("Reference XC Module") + .set_description("Evaluates the reference value. Must use the same " + "functional as the given value"); + add_submodule("UQ Initializer") + .set_description("Determines the kind of uncertain value returned"); + + add_input("Opposite Side Fraction") + .set_default(0.0) + .set_description("The deviation away from the reference value, as a " + "fraction of |reference - value|. Must be " + "non-negative"); +} + +template +TEMPLATED_MODULE_RUN(ReferenceGridError, QuadPT) { + const auto& [braket, grid, value] = error_pt::unwrap_inputs(inputs); + const auto f = inputs.at("Opposite Side Fraction").value(); + if(f < 0.0) + throw std::invalid_argument( + "Reference Grid Error: Opposite Side Fraction must be non-negative"); + + const auto& aos = get_aos(braket); + auto& grid_mod = submods.at("Reference grid"); + const auto& ref_grid = grid_mod.run_as(libxc::aos2molecule(aos)); + + auto& ref_mod = submods.at("Reference XC Module"); + const auto ref = ref_mod.run_as(braket, ref_grid); + + const auto v = to_doubles(value, "given value"); + const auto r = to_doubles(ref, "reference value"); + if(v.size() != r.size()) + throw std::runtime_error( + "Reference Grid Error: the given and reference values differ in " + "size"); + + const auto& factory = submods.at("UQ Initializer").run_as(); + + auto shape = get_shape(value); + auto rv_t = simde::visit_uq_kind(factory.kind(), [&](auto type) { + using float_type = typename decltype(type)::type; + std::vector elements; + elements.reserve(v.size()); + for(std::size_t i = 0; i < v.size(); ++i) { + const auto d = r[i] - v[i]; + const auto plus = d >= 0.0 ? std::fabs(d) : f * std::fabs(d); + const auto minus = d >= 0.0 ? f * std::fabs(d) : std::fabs(d); + auto element = factory(v[i], minus, plus); + elements.push_back(wtf::fp::float_cast(element)); + } + tensorwrapper::buffer::Contiguous buffer(std::move(elements), shape); + return simde::type::tensor(shape, std::move(buffer)); + }); + + auto rv = results(); + return error_pt::wrap_results(rv, std::move(rv_t)); +} + +template struct ReferenceGridError; +template struct ReferenceGridError< + scf::property_types::xc_potential_quadrature>; + +} // namespace scf::xc diff --git a/cxx/src/scf/xc/uq_xc_energy.cpp b/cxx/src/scf/xc/uq_xc_energy.cpp new file mode 100644 index 0000000..8dc7bd0 --- /dev/null +++ b/cxx/src/scf/xc/uq_xc_energy.cpp @@ -0,0 +1,77 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "libxc/libxc.hpp" +#include "xc.hpp" +#include +#include + +namespace scf::xc { +namespace { + +const auto desc = R"( +Exchange-Correlation Energy with Uncertainty +============================================ + +Computes the exchange-correlation energy of a restricted wavefunction by +quadrature and attaches the uncertainty from the error of the grid. This module +builds the integration grid for the molecule the AO basis set is centered on, +and passes the grid, along with its inputs, to the "XC Energy" submodule, which +evaluates the quadrature on that grid. The inputs, the grid, and the result of +the quadrature are then passed to the "Incompleteness Error Model" submodule, +which attaches the uncertainty from the incompleteness of the grid. Since the +quadrature and the error model are given the same grid, they see the same grid. + +Uncertainty in the inputs, *e.g.*, the density, is not attached here. If the +inputs are uncertain, it propagates through the quadrature. +)"; + +} // namespace + +using wf_type = simde::type::rscf_wf; +using pt = simde::eval_braket; +using grid_pt = simde::MolecularGrid; +using quad_pt = scf::property_types::xc_energy_quadrature; +using uq_pt = scf::property_types::xc_energy_uncertainty; + +MODULE_CTOR(UQXCEnergy) { + satisfies_property_type(); + description(desc); + add_submodule("Integration grid"); + add_submodule("XC Energy"); + add_submodule("Incompleteness Error Model"); +} + +MODULE_RUN(UQXCEnergy) { + const auto& [braket] = pt::unwrap_inputs(inputs); + + const auto& P = braket.op().get_rhs_particle(); + const auto& aos = P.basis_set().ao_basis_set(); + + auto& grid_mod = submods.at("Integration grid"); + const auto& grid = grid_mod.run_as(libxc::aos2molecule(aos)); + + auto& quad_mod = submods.at("XC Energy"); + const auto exc = quad_mod.run_as(braket, grid); + + auto& error_mod = submods.at("Incompleteness Error Model"); + const auto uq_exc = error_mod.run_as(braket, grid, exc); + + auto rv = results(); + return pt::wrap_results(rv, uq_exc); +} + +} // namespace scf::xc diff --git a/cxx/src/scf/xc/uq_xc_potential.cpp b/cxx/src/scf/xc/uq_xc_potential.cpp new file mode 100644 index 0000000..0e16780 --- /dev/null +++ b/cxx/src/scf/xc/uq_xc_potential.cpp @@ -0,0 +1,75 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "libxc/libxc.hpp" +#include "xc.hpp" +#include +#include + +namespace scf::xc { +namespace { + +const auto desc = R"( +Exchange-Correlation Potential with Uncertainty +=============================================== + +Computes the AO matrix of the exchange-correlation potential by quadrature and +attaches the uncertainty from the error of the grid. This module builds the +integration grid for the molecule the AO basis set is centered on, and passes +the grid, along with its inputs, to the "XC Potential" submodule, which +evaluates the quadrature on that grid. The inputs, the grid, and the result of +the quadrature are then passed to the "Incompleteness Error Model" submodule, +which attaches the uncertainty from the incompleteness of the grid. Since the +quadrature and the error model are given the same grid, they see the same grid. + +Uncertainty in the inputs, *e.g.*, the density, is not attached here. If the +inputs are uncertain, it propagates through the quadrature. +)"; + +} // namespace + +using pt = simde::aos_xc_e_aos; +using grid_pt = simde::MolecularGrid; +using quad_pt = scf::property_types::xc_potential_quadrature; +using uq_pt = scf::property_types::xc_potential_uncertainty; + +MODULE_CTOR(UQXCPotential) { + satisfies_property_type(); + description(desc); + add_submodule("Integration grid"); + add_submodule("XC Potential"); + add_submodule("Incompleteness Error Model"); +} + +MODULE_RUN(UQXCPotential) { + const auto& [braket] = pt::unwrap_inputs(inputs); + + const auto& aos = braket.bra().ao_basis_set(); + + auto& grid_mod = submods.at("Integration grid"); + const auto& grid = grid_mod.run_as(libxc::aos2molecule(aos)); + + auto& quad_mod = submods.at("XC Potential"); + const auto vxc = quad_mod.run_as(braket, grid); + + auto& error_mod = submods.at("Incompleteness Error Model"); + const auto uq_vxc = error_mod.run_as(braket, grid, vxc); + + auto rv = results(); + return pt::wrap_results(rv, uq_vxc); +} + +} // namespace scf::xc diff --git a/cxx/src/scf/xc/xc.cpp b/cxx/src/scf/xc/xc.cpp index 5e780b1..1da9275 100644 --- a/cxx/src/scf/xc/xc.cpp +++ b/cxx/src/scf/xc/xc.cpp @@ -22,6 +22,19 @@ #include "xc.hpp" namespace scf::xc { +namespace { + +/// The angular size of "Reference IntegratorXX Grid": the largest +/// Lebedev-Laikov grid IntegratorXX has (algebraic order 131) +constexpr std::size_t reference_angular_size = 5810; + +/// The radial size of "Reference IntegratorXX Grid". For SVWN3 water/STO-3G +/// with 5810 angular points, E_xc and V_xc change by less than 1.1E-11 from +/// 150 to 250 radial points (see xc_uq.rst) +constexpr std::size_t reference_radial_size = 150; + +} // namespace + void load_modules(pluginplay::ModuleMap& modules) { #ifdef BUILD_GAUXC gauxc::load_modules(modules); @@ -33,6 +46,23 @@ void load_modules(pluginplay::ModuleMap& modules) { modules.add_module("Grid From File"); modules.add_module("Grid From IntegratorXX"); modules.add_module("Density2Grid"); + modules.add_module("UQ XC Energy"); + modules.add_module("UQ XC Potential"); + + using scf::property_types::xc_energy_quadrature; + using scf::property_types::xc_potential_quadrature; + modules.add_module>( + "Reference Grid XC Energy Error"); + modules.add_module>( + "Reference Grid XC Potential Error"); + + // The reference grid of the reference grid error models + const auto ref_grid_key = "Reference IntegratorXX Grid"; + modules.copy_module("Grid From IntegratorXX", ref_grid_key); + modules.change_input(ref_grid_key, "Angular Size", + std::size_t{reference_angular_size}); + modules.change_input(ref_grid_key, "Radial Size", + std::size_t{reference_radial_size}); } void set_defaults(pluginplay::ModuleMap& modules) { @@ -42,6 +72,28 @@ void set_defaults(pluginplay::ModuleMap& modules) { libxc::set_defaults(modules); native::set_defaults(modules); modules.change_submod("Density2Grid", "AOs on a grid", "AOs on a grid"); + + // The UQ XC modules default to the native SVWN3 quadratures, since they + // support the UQ types, and to the reference grid error models. The + // "UQ Initializer" of the error models comes from Integrals (see + // scf_mm.cpp). + const auto grid_key = "Grid From IntegratorXX"; + const auto e_quad = "SVWN3 Energy Quadrature"; + const auto v_quad = "SVWN3 Potential Quadrature"; + const auto e_error = "Reference Grid XC Energy Error"; + const auto v_error = "Reference Grid XC Potential Error"; + const auto ref_grid = "Reference IntegratorXX Grid"; + const auto error_key = "Incompleteness Error Model"; + modules.change_submod("UQ XC Energy", "Integration grid", grid_key); + modules.change_submod("UQ XC Potential", "Integration grid", grid_key); + modules.change_submod("UQ XC Energy", "XC Energy", e_quad); + modules.change_submod("UQ XC Potential", "XC Potential", v_quad); + modules.change_submod("UQ XC Energy", error_key, e_error); + modules.change_submod("UQ XC Potential", error_key, v_error); + modules.change_submod(e_error, "Reference grid", ref_grid); + modules.change_submod(v_error, "Reference grid", ref_grid); + modules.change_submod(e_error, "Reference XC Module", e_quad); + modules.change_submod(v_error, "Reference XC Module", v_quad); } } // namespace scf::xc diff --git a/cxx/src/scf/xc/xc.hpp b/cxx/src/scf/xc/xc.hpp index 70e2ce2..2d9ca33 100644 --- a/cxx/src/scf/xc/xc.hpp +++ b/cxx/src/scf/xc/xc.hpp @@ -16,6 +16,7 @@ #pragma once #include +#include namespace scf::xc { @@ -24,6 +25,23 @@ DECLARE_MODULE(Density2Grid); DECLARE_MODULE(Gau2Grid); DECLARE_MODULE(GridFromFile); DECLARE_MODULE(GridFromIntegratorXX); +DECLARE_MODULE(UQXCEnergy); +DECLARE_MODULE(UQXCPotential); +DECLARE_MODULE(XCEnergyViaQuadrature); +DECLARE_MODULE(XCPotentialViaQuadrature); + +/** @brief Estimates the error of a quadrature from a finer grid. + * + * @tparam QuadPT The property type of the quadrature, e.g., + * scf::property_types::xc_potential_quadrature. + */ +template +DECLARE_MODULE(ReferenceGridError); + +extern template struct ReferenceGridError< + scf::property_types::xc_energy_quadrature>; +extern template struct ReferenceGridError< + scf::property_types::xc_potential_quadrature>; void set_defaults(pluginplay::ModuleMap& modules); void load_modules(pluginplay::ModuleMap& modules); diff --git a/cxx/src/scf/xc/xc_energy_via_quadrature.cpp b/cxx/src/scf/xc/xc_energy_via_quadrature.cpp new file mode 100644 index 0000000..80aadaa --- /dev/null +++ b/cxx/src/scf/xc/xc_energy_via_quadrature.cpp @@ -0,0 +1,67 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "libxc/libxc.hpp" +#include "xc.hpp" +#include +#include + +namespace scf::xc { +namespace { + +const auto desc = R"( +Exchange-Correlation Energy via Quadrature +========================================== + +Computes the exchange-correlation energy of a restricted wavefunction by +quadrature. This module builds the integration grid for the molecule the AO +basis set is centered on, and then passes the grid, along with its inputs, to +the "Quadrature" submodule, which evaluates the quadrature on that grid. The +choice of functional, and the checks on the inputs, are made by the +"Quadrature" submodule. +)"; + +} // namespace + +using wf_type = simde::type::rscf_wf; +using pt = simde::eval_braket; +using grid_pt = simde::MolecularGrid; +using quad_pt = scf::property_types::xc_energy_quadrature; + +MODULE_CTOR(XCEnergyViaQuadrature) { + satisfies_property_type(); + description(desc); + add_submodule("Integration grid"); + add_submodule("Quadrature"); +} + +MODULE_RUN(XCEnergyViaQuadrature) { + const auto& [braket] = pt::unwrap_inputs(inputs); + + const auto& P = braket.op().get_rhs_particle(); + const auto& aos = P.basis_set().ao_basis_set(); + + auto& grid_mod = submods.at("Integration grid"); + const auto& grid = grid_mod.run_as(libxc::aos2molecule(aos)); + + auto& quad_mod = submods.at("Quadrature"); + const auto exc = quad_mod.run_as(braket, grid); + + auto rv = results(); + return pt::wrap_results(rv, exc); +} + +} // namespace scf::xc diff --git a/cxx/src/scf/xc/xc_potential_via_quadrature.cpp b/cxx/src/scf/xc/xc_potential_via_quadrature.cpp new file mode 100644 index 0000000..bf00175 --- /dev/null +++ b/cxx/src/scf/xc/xc_potential_via_quadrature.cpp @@ -0,0 +1,65 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "libxc/libxc.hpp" +#include "xc.hpp" +#include +#include + +namespace scf::xc { +namespace { + +const auto desc = R"( +Exchange-Correlation Potential via Quadrature +============================================= + +Computes the AO matrix of the exchange-correlation potential by quadrature. +This module builds the integration grid for the molecule the AO basis set is +centered on, and then passes the grid, along with its inputs, to the +"Quadrature" submodule, which evaluates the quadrature on that grid. The choice +of functional, and the checks on the inputs, are made by the "Quadrature" +submodule. +)"; + +} // namespace + +using pt = simde::aos_xc_e_aos; +using grid_pt = simde::MolecularGrid; +using quad_pt = scf::property_types::xc_potential_quadrature; + +MODULE_CTOR(XCPotentialViaQuadrature) { + satisfies_property_type(); + description(desc); + add_submodule("Integration grid"); + add_submodule("Quadrature"); +} + +MODULE_RUN(XCPotentialViaQuadrature) { + const auto& [braket] = pt::unwrap_inputs(inputs); + + const auto& aos = braket.bra().ao_basis_set(); + + auto& grid_mod = submods.at("Integration grid"); + const auto& grid = grid_mod.run_as(libxc::aos2molecule(aos)); + + auto& quad_mod = submods.at("Quadrature"); + const auto vxc = quad_mod.run_as(braket, grid); + + auto rv = results(); + return pt::wrap_results(rv, vxc); +} + +} // namespace scf::xc diff --git a/docs/source/design/qcup_scf.rst b/docs/source/design/qcup_scf.rst index faa81c9..7d51d07 100644 --- a/docs/source/design/qcup_scf.rst +++ b/docs/source/design/qcup_scf.rst @@ -202,7 +202,7 @@ the Fock matrix holds an uncertain type: (``inflate_uncertainty``). These become two new property types. They are declared in SCF, in -``scf/property_types.hpp``, until their APIs have settled, and may move to +``scf/property_types/``, until their APIs have settled, and may move to SimDE later: .. list-table:: diff --git a/docs/source/design/xc_uq.rst b/docs/source/design/xc_uq.rst index 9b6b52d..7463309 100644 --- a/docs/source/design/xc_uq.rst +++ b/docs/source/design/xc_uq.rst @@ -51,8 +51,10 @@ symmetric interval stores only the bounds: pinning whose midpoint, :math:`(\rho_i + r_i)/2`, is larger than :math:`\rho_i`, so the best estimate of the density would be biased upward. The error of a quadrature also tends to have a known direction, which an asymmetric interval can -represent. ``sigma::Interval`` stores only the bounds, so this type does not -exist yet. +represent. ``sigma::Interval`` stores only the bounds. ``sigma::Triplex`` +stores the bounds and, independently, a nominal value, i.e., it is an +asymmetric interval. It is available in TensorWrapper as ``tpdouble`` and from a +``UQ Initializer`` as the ``triplex`` kind. ************************************* Errors in the XC Energy and Potential @@ -293,12 +295,14 @@ To summarize, the submodules of the ``UQ XC Potential`` module are: - ``SVWN3 Potential Quadrature`` - The module that evaluates the quadrature. Satisfies ``Quadrature``. * - ``Incompleteness Error Model`` - - ``Reference Grid Error`` + - ``Reference Grid XC Potential Error`` - The error model that estimates the uncertainty from the incompleteness of the grid. -The ``Reference Grid Error`` module refers to the "Reference Grid" error model -introduced in the next section. +``Reference Grid XC Potential Error`` is the "Reference Grid" error model +introduced in the next section. ``UQ XC Energy`` is the same, with +``XC Energy``, ``SVWN3 Energy Quadrature``, and +``Reference Grid XC Energy Error``. *************************** Incompleteness Error Models @@ -326,18 +330,56 @@ integral, and a one-sided deviation then excludes it. Making both deviations not a bound, and costs one evaluation on the finer grid, including the density and AOs on that grid. -The strategy is implemented as an error model, ``Reference Grid Error`` (see -`XC UQ Implementation`_) that satisfies the ``XCUncertain>`` -property type. It has a submodule, ``Reference XC Module``, which satisfies -``Quadrature``. The module gets the original grid as an input and thus -should be able to determine a finer grid from it. This requires that the grid -has identifiers for its radial and angular quadratures. The module then passes -the inputs and the finer grid to the ``Reference XC Module`` submodule and -uses the difference to construct the deviations. Finally, it converts the -original result to the UQ type with its ``UQ Initializer`` submodule, keeping -the original result as the mean, attaches the deviations, and returns that -uncertain result, not the difference. This requires that the -``Reference Grid Error`` module also have a submodule for generating grids. +The strategy is implemented as the error models +``Reference Grid XC Energy Error`` and ``Reference Grid XC Potential Error`` +(see `XC UQ Implementation`_), which satisfy ``XCUncertain>``. +Their submodules are: + +.. list-table:: + :header-rows: 1 + :widths: 30 30 40 + + * - Hook name + - Default key + - Purpose + * - ``Reference grid`` + - ``Reference IntegratorXX Grid`` + - Builds the finer grid from the molecule. + * - ``Reference XC Module`` + - ``SVWN3 Energy Quadrature`` or ``SVWN3 Potential Quadrature`` + - Evaluates the quadrature on the finer grid. Must use the same + functional as the quadrature being corrected. + * - ``UQ Initializer`` + - Integrals' ``UQ Initializer`` + - The kind of uncertain value returned. + +The module builds the finer grid, passes the inputs and the finer grid to the +``Reference XC Module`` submodule, and uses the element-wise difference to +construct the deviations: :math:`|d|` towards the reference value and +``Opposite Side Fraction`` (an input, default 0) times :math:`|d|` away from +it. It then converts the original result to the UQ type with its +``UQ Initializer`` submodule, keeping the original result as the nominal value, +and returns that uncertain result, not the difference. Only the ``triplex`` +kind stores both deviations exactly. The other kinds store the tightest value +they can which contains them: the interval-like kinds store the bounds, and +``Uncertain`` uses the larger deviation. For now, the given result must hold +doubles. + +The finer grid is not derived from the given grid. Instead, +``Reference IntegratorXX Grid`` is a copy of ``Grid From IntegratorXX`` with +150 radial (Mura-Knowles) and 5810 angular (Lebedev-Laikov, the largest +available) points per atom, unpruned, with Becke partitioning. The sizes come +from a convergence study of SVWN3 water/STO-3G at a fixed (core-guess) density. +With 5810 angular points, increasing the radial size from 150 to 250 changes +:math:`E^{XC}` by :math:`1.1\times 10^{-11}` and :math:`V^{XC}` (largest +element) by :math:`6.4\times 10^{-12}`. From 200 to 250 the changes are about +:math:`2\times 10^{-12}`, which is at the level of the rounding error of the +sums. With 150 radial points, 3074 angular points agree with 5810 to +:math:`5\times 10^{-12}`, whereas 2030 do not (:math:`6\times 10^{-10}`). + +The reference grid is expensive: about :math:`2.6\times 10^{6}` points for +water. Each call evaluates the quadrature, including the density and AOs, on +it. Grid Extrapolation ------------------ diff --git a/tests/cxx/test_scf.hpp b/tests/cxx/test_scf.hpp index 035173a..f0f1529 100644 --- a/tests/cxx/test_scf.hpp +++ b/tests/cxx/test_scf.hpp @@ -287,6 +287,28 @@ inline auto he_xc(chemist::qm_operator::xc_functional func) { return simde::type::XC_e_type(func, es, rho); } +/** @brief A 4-point grid for testing XC quadratures on H2. + * + * Only the weights of the grid matter for the tests that use it; the density + * on the grid is hard-coded (see h2_rho_on_xc_grid). These are the points and + * weights of the first batch of a modified Psi4numpy example with 6 angular + * and 2 radial points per atom. + */ +inline auto h2_xc_grid() { + using grid_point = chemist::GridPoint; + std::vector grid_points; + grid_points.push_back(grid_point(15.80337241, 0.0, 0.0, 0.0)); + grid_points.push_back(grid_point(0.03961399, 0.0, 0.0, 0.0)); + grid_points.push_back(grid_point(15.80337241, 0.0, 0.0, -1.3984)); + grid_points.push_back(grid_point(0.03961399, 0.0, 0.0, -1.3984)); + return chemist::Grid(grid_points.begin(), grid_points.end()); +} + +/// The density of h2_density on the points of h2_xc_grid +inline auto h2_rho_on_xc_grid() { + return simde::type::tensor({0.01313525, 0.32141338, 0.01313525, 0.3214133}); +} + inline std::filesystem::path get_test_directory_path() { auto path = std::filesystem::current_path().parent_path(); // Spack testing hack diff --git a/tests/cxx/unit_tests/property_types/convergence_uncertainty.cpp b/tests/cxx/unit_tests/property_types/convergence_uncertainty.cpp new file mode 100644 index 0000000..f25eade --- /dev/null +++ b/tests/cxx/unit_tests/property_types/convergence_uncertainty.cpp @@ -0,0 +1,25 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "test_property_types.hpp" +#include + +using namespace scf::property_types; + +TEST_CASE("ConvergenceUncertainty") { + test_scf::test_property_type( + {"Energy", "Energy change"}, {"Energy"}); +} diff --git a/tests/cxx/unit_tests/property_types/mo_uncertainty.cpp b/tests/cxx/unit_tests/property_types/mo_uncertainty.cpp new file mode 100644 index 0000000..27de33f --- /dev/null +++ b/tests/cxx/unit_tests/property_types/mo_uncertainty.cpp @@ -0,0 +1,27 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "test_property_types.hpp" +#include + +using namespace scf::property_types; + +TEST_CASE("MOUncertainty") { + test_scf::test_property_type( + {"Fock matrix", "Overlap matrix", "Orbital energies", + "Orbital coefficients", "Density change"}, + {"Orbital coefficients"}); +} diff --git a/tests/cxx/unit_tests/property_types/quadrature.cpp b/tests/cxx/unit_tests/property_types/quadrature.cpp new file mode 100644 index 0000000..a09e27d --- /dev/null +++ b/tests/cxx/unit_tests/property_types/quadrature.cpp @@ -0,0 +1,32 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "test_property_types.hpp" +#include + +using namespace scf::property_types; + +TEST_CASE("Quadrature") { + SECTION("xc_energy_quadrature") { + test_scf::test_property_type( + {"BraKet", "Grid"}, {"tensor representation"}); + } + + SECTION("xc_potential_quadrature") { + test_scf::test_property_type( + {"BraKet", "Grid"}, {"tensor representation"}); + } +} diff --git a/tests/cxx/unit_tests/property_types.cpp b/tests/cxx/unit_tests/property_types/test_property_types.hpp similarity index 56% rename from tests/cxx/unit_tests/property_types.cpp rename to tests/cxx/unit_tests/property_types/test_property_types.hpp index 6247e41..c9bf7ed 100644 --- a/tests/cxx/unit_tests/property_types.cpp +++ b/tests/cxx/unit_tests/property_types/test_property_types.hpp @@ -14,18 +14,17 @@ * limitations under the License. */ -#include "../test_scf.hpp" -#include +#pragma once +#include "../../test_scf.hpp" +#include +#include -using namespace scf::property_types; +namespace test_scf { -namespace { - -using string_list = std::initializer_list; - -// Checks that the property type T has exactly the given inputs and results +/// Checks that the property type @p T has exactly the given inputs and results template -void test_property_type(string_list input_fields, string_list result_fields) { +void test_property_type(std::initializer_list input_fields, + std::initializer_list result_fields) { auto inputs = T::inputs(); REQUIRE(inputs.size() == input_fields.size()); for(const auto& field : input_fields) REQUIRE(inputs.count(field) == 1); @@ -35,16 +34,4 @@ void test_property_type(string_list input_fields, string_list result_fields) { for(const auto& field : result_fields) REQUIRE(results.count(field) == 1); } -} // namespace - -TEST_CASE("MOUncertainty") { - test_property_type( - {"Fock matrix", "Overlap matrix", "Orbital energies", - "Orbital coefficients", "Density change"}, - {"Orbital coefficients"}); -} - -TEST_CASE("ConvergenceUncertainty") { - test_property_type({"Energy", "Energy change"}, - {"Energy"}); -} +} // namespace test_scf diff --git a/tests/cxx/unit_tests/property_types/xc_uncertain.cpp b/tests/cxx/unit_tests/property_types/xc_uncertain.cpp new file mode 100644 index 0000000..7785ec1 --- /dev/null +++ b/tests/cxx/unit_tests/property_types/xc_uncertain.cpp @@ -0,0 +1,34 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "test_property_types.hpp" +#include + +using namespace scf::property_types; + +TEST_CASE("XCUncertain") { + SECTION("xc_energy_uncertainty") { + test_scf::test_property_type( + {"BraKet", "Grid", "tensor representation"}, + {"tensor representation"}); + } + + SECTION("xc_potential_uncertainty") { + test_scf::test_property_type( + {"BraKet", "Grid", "tensor representation"}, + {"tensor representation"}); + } +} diff --git a/tests/cxx/unit_tests/scf_mm.cpp b/tests/cxx/unit_tests/scf_mm.cpp index 8c7c88e..a344b96 100644 --- a/tests/cxx/unit_tests/scf_mm.cpp +++ b/tests/cxx/unit_tests/scf_mm.cpp @@ -59,6 +59,7 @@ class FakeIntegrals : public pluginplay::Plugin { add_module>>( "Analytic Error"); add_module>("Analytic Benchmark ERI4"); + add_module>("UQ Initializer"); } }; @@ -81,7 +82,11 @@ const std::vector> wiring{ {"SAD guess", "SAD Density", "chemcache", "sto-3g SAD density"}, {"QCUP SCF", "ERIs", "integrals", "UQ Atom Symm Blocked Driver"}, {"QCUP SCF", "ERI Error Model", "integrals", "Analytic Error"}, - {"QCUP SCF", "Benchmark ERIs", "integrals", "Analytic Benchmark ERI4"}}; + {"QCUP SCF", "Benchmark ERIs", "integrals", "Analytic Benchmark ERI4"}, + {"Reference Grid XC Energy Error", "UQ Initializer", "integrals", + "UQ Initializer"}, + {"Reference Grid XC Potential Error", "UQ Initializer", "integrals", + "UQ Initializer"}}; } // namespace @@ -100,6 +105,30 @@ TEST_CASE("SCF plugin") { REQUIRE(modules.has_submod("SCF Driver", "Optimizer")); REQUIRE(modules.has_submod("Generalized eigensolve", "Eigen Solve")); REQUIRE(modules.has_submod("Density2Grid", "AOs on a grid")); + for(std::string backend : {"SVWN3", "LibXC"}) { + const auto e_key = backend + " Energy"; + const auto v_key = backend + " Potential"; + REQUIRE(modules.has_submod(e_key, "Integration grid")); + REQUIRE(modules.has_submod(e_key, "Quadrature")); + REQUIRE(modules.has_submod(v_key, "Integration grid")); + REQUIRE(modules.has_submod(v_key, "Quadrature")); + REQUIRE( + modules.has_submod(e_key + " Quadrature", "Density on a grid")); + REQUIRE( + modules.has_submod(v_key + " Quadrature", "Density on a grid")); + REQUIRE(modules.has_submod(v_key + " Quadrature", "AOs on a grid")); + } + REQUIRE(modules.has_submod("UQ XC Energy", "Integration grid")); + REQUIRE(modules.has_submod("UQ XC Energy", "XC Energy")); + REQUIRE(modules.has_submod("UQ XC Potential", "Integration grid")); + REQUIRE(modules.has_submod("UQ XC Potential", "XC Potential")); + for(std::string q : {"Energy", "Potential"}) { + const auto error_key = "Reference Grid XC " + q + " Error"; + REQUIRE( + modules.has_submod("UQ XC " + q, "Incompleteness Error Model")); + REQUIRE(modules.has_submod(error_key, "Reference grid")); + REQUIRE(modules.has_submod(error_key, "Reference XC Module")); + } REQUIRE(modules.has_submod("Loop", "Charge-charge")); } @@ -111,6 +140,13 @@ TEST_CASE("SCF plugin") { *modules.at("No convergence uncertainty")); } + SECTION("Reference IntegratorXX Grid") { + const auto& inputs = + modules.at("Reference IntegratorXX Grid")->inputs(); + REQUIRE(inputs.at("Angular Size").value() == 5810); + REQUIRE(inputs.at("Radial Size").value() == 150); + } + SECTION("QCUP SCF is a driver") { REQUIRE(modules.at("QCUP SCF")->is_driver()); } diff --git a/tests/cxx/unit_tests/xc/libxc/libxc_energy.cpp b/tests/cxx/unit_tests/xc/libxc/libxc_energy.cpp index 48f68d6..18500d1 100644 --- a/tests/cxx/unit_tests/xc/libxc/libxc_energy.cpp +++ b/tests/cxx/unit_tests/xc/libxc/libxc_energy.cpp @@ -16,21 +16,18 @@ #include "../../../test_scf.hpp" -using grid_pt = simde::MolecularGrid; -using rho_pt = simde::EDensityCollocationMatrix; -using wf_type = simde::type::rscf_wf; -using pt = simde::eval_braket; +using rho_pt = simde::EDensityCollocationMatrix; +using pt = scf::property_types::xc_energy_quadrature; using tensorwrapper::operations::approximately_equal; -TEST_CASE("LibXCEnergy") { +TEST_CASE("LibXCEnergyQuadrature") { using float_type = double; pluginplay::ModuleManager mm; mm.load_plugin(); - auto& mod = mm.at("LibXC Energy"); + auto& mod = mm.at("LibXC Energy Quadrature"); auto rho = test_scf::h2_density(); - auto aos = test_scf::h2_aos(); auto psi = test_scf::h2_wave_function(); auto func = chemist::qm_operator::xc_functional::SVWN3; auto xc_hat = test_scf::h2_xc(func); @@ -43,37 +40,22 @@ TEST_CASE("LibXCEnergy") { // (which differ from Psi4 by about 1e-3) reproduces the correct value here // to 1e-6, i.e., this test is consistent with the libxc_lda_energy_density // test. - - // For this test only the weights matter (we hardcode the density array) - using grid_point = chemist::GridPoint; - std::vector grid_points; - grid_points.push_back(grid_point(15.80337241, 0.0, 0.0, 0.0)); - grid_points.push_back(grid_point(0.03961399, 0.0, 0.0, 0.0)); - grid_points.push_back(grid_point(15.80337241, 0.0, 0.0, -1.3984)); - grid_points.push_back(grid_point(0.03961399, 0.0, 0.0, -1.3984)); - chemist::Grid grid(grid_points.begin(), grid_points.end()); - - auto grid_mod = pluginplay::make_lambda([&](auto&& mol) { - REQUIRE(mol.size() == 2); - return grid; - }); + auto grid = test_scf::h2_xc_grid(); auto rho_mod = pluginplay::make_lambda([&](auto&& grid_in, auto&& P) { REQUIRE(grid_in == grid); REQUIRE(P == rho); - return simde::type::tensor( - {0.01313525, 0.32141338, 0.01313525, 0.3214133}); + return test_scf::h2_rho_on_xc_grid(); }); - mod.change_submod("Integration grid", grid_mod); mod.change_submod("Density on a grid", rho_mod); #ifdef BUILD_LIBXC - auto exc = mod.run_as(braket); + auto exc = mod.run_as(braket, grid); simde::type::tensor corr(-0.1031596741234082); REQUIRE(approximately_equal(exc, corr, 1e-6)); #else - REQUIRE_THROWS_AS(mod.run_as(braket), std::runtime_error); + REQUIRE_THROWS_AS(mod.run_as(braket, grid), std::runtime_error); #endif } diff --git a/tests/cxx/unit_tests/xc/libxc/libxc_potential.cpp b/tests/cxx/unit_tests/xc/libxc/libxc_potential.cpp index 93e41a6..be77d61 100644 --- a/tests/cxx/unit_tests/xc/libxc/libxc_potential.cpp +++ b/tests/cxx/unit_tests/xc/libxc/libxc_potential.cpp @@ -16,57 +16,41 @@ #include "../../../test_scf.hpp" -using grid_pt = simde::MolecularGrid; -using ao_pt = simde::AOCollocationMatrix; -using rho_pt = simde::EDensityCollocationMatrix; -using pt = simde::aos_xc_e_aos; +using rho_pt = simde::EDensityCollocationMatrix; +using pt = scf::property_types::xc_potential_quadrature; -TEST_CASE("LibXCPotential") { +TEST_CASE("LibXCPotentialQuadrature") { using float_type = double; using tensorwrapper::operations::approximately_equal; pluginplay::ModuleManager mm; mm.load_plugin(); - mm.change_submod("LibXC Potential", "Integration grid", "Grid From File"); - auto& mod = mm.at("LibXC Potential"); + auto& mod = mm.at("LibXC Potential Quadrature"); auto rho = test_scf::h2_density(); auto aos = test_scf::h2_aos(); auto func = chemist::qm_operator::xc_functional::SVWN3; // For this test only the weights matter (we hardcode the density array) - using grid_point = chemist::GridPoint; - std::vector grid_points; - grid_points.push_back(grid_point(15.80337241, 0.0, 0.0, 0.0)); - grid_points.push_back(grid_point(0.03961399, 0.0, 0.0, 0.0)); - grid_points.push_back(grid_point(15.80337241, 0.0, 0.0, -1.3984)); - grid_points.push_back(grid_point(0.03961399, 0.0, 0.0, -1.3984)); - chemist::Grid grid(grid_points.begin(), grid_points.end()); - - auto grid_mod = pluginplay::make_lambda([&](auto&& mol) { - REQUIRE(mol.size() == 2); - return grid; - }); + auto grid = test_scf::h2_xc_grid(); auto rho_mod = pluginplay::make_lambda([&](auto&& grid_in, auto&& P) { REQUIRE(grid_in == grid); REQUIRE(P == rho); - return simde::type::tensor( - {0.01313525, 0.32141338, 0.01313525, 0.3214133}); + return test_scf::h2_rho_on_xc_grid(); }); - mod.change_submod("Integration grid", grid_mod); mod.change_submod("Density on a grid", rho_mod); simde::type::xc_e_type xc_op(func, simde::type::electron{}, rho); chemist::braket::BraKet braket(aos, xc_op, aos); #ifdef BUILD_LIBXC - auto vxc = mod.run_as(braket); + auto vxc = mod.run_as(braket, grid); simde::type::tensor corr{{-1.83222, -0.402489}, {-0.402489, -0.0886007}}; REQUIRE(approximately_equal(vxc, corr, 1e-5)); #else - REQUIRE_THROWS_AS(mod.run_as(braket), std::runtime_error); + REQUIRE_THROWS_AS(mod.run_as(braket, grid), std::runtime_error); #endif } diff --git a/tests/cxx/unit_tests/xc/native/svwn3_energy.cpp b/tests/cxx/unit_tests/xc/native/svwn3_energy.cpp new file mode 100644 index 0000000..2065747 --- /dev/null +++ b/tests/cxx/unit_tests/xc/native/svwn3_energy.cpp @@ -0,0 +1,62 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "../../../test_scf.hpp" + +using rho_pt = simde::EDensityCollocationMatrix; +using pt = scf::property_types::xc_energy_quadrature; +using tensorwrapper::operations::approximately_equal; + +// Uses the same grid, density, and reference value as the LibXC test (see +// libxc/libxc_energy.cpp), since for a spin-unpolarized density the native +// SVWN3 is identical to LibXC's. +TEST_CASE("SVWN3EnergyQuadrature") { + using float_type = double; + + pluginplay::ModuleManager mm; + mm.load_plugin(); + auto& mod = mm.at("SVWN3 Energy Quadrature"); + + auto rho = test_scf::h2_density(); + auto psi = test_scf::h2_wave_function(); + auto grid = test_scf::h2_xc_grid(); + + auto rho_mod = + pluginplay::make_lambda([&](auto&& grid_in, auto&& P) { + REQUIRE(grid_in == grid); + REQUIRE(P == rho); + return test_scf::h2_rho_on_xc_grid(); + }); + + mod.change_submod("Density on a grid", rho_mod); + + SECTION("SVWN3") { + auto func = chemist::qm_operator::xc_functional::SVWN3; + auto xc_hat = test_scf::h2_xc(func); + chemist::braket::BraKet braket(psi, xc_hat, psi); + + auto exc = mod.run_as(braket, grid); + simde::type::tensor corr(-0.1031596741234082); + REQUIRE(approximately_equal(exc, corr, 1e-6)); + } + + SECTION("Throws for other functionals") { + auto func = chemist::qm_operator::xc_functional::SVWN5; + auto xc_hat = test_scf::h2_xc(func); + chemist::braket::BraKet braket(psi, xc_hat, psi); + REQUIRE_THROWS_AS(mod.run_as(braket, grid), std::runtime_error); + } +} diff --git a/tests/cxx/unit_tests/xc/native/svwn3_potential.cpp b/tests/cxx/unit_tests/xc/native/svwn3_potential.cpp new file mode 100644 index 0000000..e9bb269 --- /dev/null +++ b/tests/cxx/unit_tests/xc/native/svwn3_potential.cpp @@ -0,0 +1,63 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "../../../test_scf.hpp" + +using rho_pt = simde::EDensityCollocationMatrix; +using pt = scf::property_types::xc_potential_quadrature; + +// Uses the same grid, density, and reference value as the LibXC test (see +// libxc/libxc_potential.cpp), since for a spin-unpolarized density the native +// SVWN3 is identical to LibXC's. +TEST_CASE("SVWN3PotentialQuadrature") { + using float_type = double; + using tensorwrapper::operations::approximately_equal; + + pluginplay::ModuleManager mm; + mm.load_plugin(); + auto& mod = mm.at("SVWN3 Potential Quadrature"); + + auto rho = test_scf::h2_density(); + auto aos = test_scf::h2_aos(); + auto grid = test_scf::h2_xc_grid(); + + auto rho_mod = + pluginplay::make_lambda([&](auto&& grid_in, auto&& P) { + REQUIRE(grid_in == grid); + REQUIRE(P == rho); + return test_scf::h2_rho_on_xc_grid(); + }); + + mod.change_submod("Density on a grid", rho_mod); + + SECTION("SVWN3") { + auto func = chemist::qm_operator::xc_functional::SVWN3; + simde::type::xc_e_type xc_op(func, simde::type::electron{}, rho); + chemist::braket::BraKet braket(aos, xc_op, aos); + + auto vxc = mod.run_as(braket, grid); + simde::type::tensor corr{{-1.83222, -0.402489}, + {-0.402489, -0.0886007}}; + REQUIRE(approximately_equal(vxc, corr, 1e-5)); + } + + SECTION("Throws for other functionals") { + auto func = chemist::qm_operator::xc_functional::SVWN5; + simde::type::xc_e_type xc_op(func, simde::type::electron{}, rho); + chemist::braket::BraKet braket(aos, xc_op, aos); + REQUIRE_THROWS_AS(mod.run_as(braket, grid), std::runtime_error); + } +} diff --git a/tests/cxx/unit_tests/xc/reference_grid_error.cpp b/tests/cxx/unit_tests/xc/reference_grid_error.cpp new file mode 100644 index 0000000..8a3014b --- /dev/null +++ b/tests/cxx/unit_tests/xc/reference_grid_error.cpp @@ -0,0 +1,213 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "../../test_scf.hpp" +#include +#include +#include +#include + +using grid_pt = simde::MolecularGrid; +using uq_pt = simde::UQInitializer; +using e_quad_pt = scf::property_types::xc_energy_quadrature; +using v_quad_pt = scf::property_types::xc_potential_quadrature; +using simde::UQKind; +using simde::type::tensor; + +namespace { + +/// Runs a fresh copy of the error model @p key, with all submodules mocked +template +tensor run_model(pluginplay::ModuleManager& mm, const std::string& key, + const BraKetType& braket, const tensor& value, + const tensor& ref, UQKind kind, double fraction = 0.0) { + using error_pt = scf::property_types::XCUncertain; + + // Modules lock once they run, so each call uses its own copy + static std::size_t counter = 0; + const auto copy_key = key + " " + std::to_string(counter++); + mm.copy_module(key, copy_key); + auto& mod = mm.at(copy_key); + + // The grid the given value was evaluated on, and the reference grid + auto grid = test_scf::h2_xc_grid(); + std::vector ref_points{ + chemist::GridPoint(1.0, 0.0, 0.0, 0.0)}; + chemist::Grid ref_grid(ref_points.begin(), ref_points.end()); + + mod.change_submod("Reference grid", + pluginplay::make_lambda([&](auto&& mol) { + REQUIRE(mol.size() == 2); + return ref_grid; + })); + mod.change_submod( + "Reference XC Module", + pluginplay::make_lambda([&](auto&& braket_in, auto&& grid_in) { + REQUIRE(braket_in == braket); + REQUIRE(grid_in == ref_grid); + return ref; + })); + mod.change_submod("UQ Initializer", pluginplay::make_lambda([=]() { + return simde::UQFactory(kind); + })); + mod.change_input("Opposite Side Fraction", fraction); + return mod.template run_as(braket, grid, value); +} + +/// The elements of @p t (a scalar or a matrix) as T, in row-major order +template +std::vector elements(const tensor& t) { + using tensorwrapper::buffer::make_contiguous; + const auto& buffer = make_contiguous(t.buffer()); + const auto shape = buffer.shape(); + std::vector rv; + if(shape.rank() == 0) { + rv.push_back(wtf::fp::float_cast(buffer.get_elem({}))); + } else { + for(std::size_t i = 0; i < shape.extent(0); ++i) + for(std::size_t j = 0; j < shape.extent(1); ++j) + rv.push_back(wtf::fp::float_cast(buffer.get_elem({i, j}))); + } + return rv; +} + +/// The elements of @p t, which holds doubles +std::vector doubles(const tensor& t) { return elements(t); } + +#ifdef ENABLE_SIGMA +/// The (lower, nominal, upper) of each element of @p t, which holds UQType +template +std::vector> bounds(const tensor& t) { + using namespace tensorwrapper::types; + std::vector> rv; + for(const auto& x : elements(t)) + rv.push_back({uq_lower(x), uq_center(x), uq_upper(x)}); + return rv; +} +#endif + +} // namespace + +TEST_CASE("ReferenceGridError (potential)") { + pluginplay::ModuleManager mm; + mm.load_plugin(); + const auto key = "Reference Grid XC Potential Error"; + + auto rho = test_scf::h2_density(); + auto aos = test_scf::h2_aos(); + auto func = chemist::qm_operator::xc_functional::SVWN3; + simde::type::xc_e_type xc_op(func, simde::type::electron{}, rho); + chemist::braket::BraKet braket(aos, xc_op, aos); + + // d = ref - value = {0.5, 0, -1, 0.25}: above, equal, below, above + tensor value{{1.0, 2.0}, {3.0, 4.0}}; + tensor ref{{1.5, 2.0}, {2.0, 4.25}}; + + SECTION("none returns the given value") { + auto rv = + run_model(mm, key, braket, value, ref, UQKind::none); + REQUIRE(doubles(rv) == doubles(value)); + } + + SECTION("throws if the value does not hold doubles") { + tensorwrapper::shape::Smooth shape{2, 2}; + tensorwrapper::buffer::Contiguous buffer( + std::vector{1.0f, 2.0f, 3.0f, 4.0f}, shape); + tensor float_value(shape, std::move(buffer)); + REQUIRE_THROWS_AS( + run_model(mm, key, braket, float_value, ref, UQKind::none), + std::runtime_error); + } + + SECTION("throws for a negative Opposite Side Fraction") { + REQUIRE_THROWS_AS( + run_model(mm, key, braket, value, ref, UQKind::none, -0.5), + std::invalid_argument); + } + +#ifdef ENABLE_SIGMA + using tensorwrapper::types::idouble; + using tensorwrapper::types::tpdouble; + using triple = std::array; + + SECTION("triplex, one-sided") { + auto rv = + run_model(mm, key, braket, value, ref, UQKind::triplex); + std::vector corr{ + {1.0, 1.0, 1.5}, {2.0, 2.0, 2.0}, {2.0, 3.0, 3.0}, {4.0, 4.0, 4.25}}; + REQUIRE(bounds(rv) == corr); + } + + SECTION("triplex, opposite side fraction 0.5") { + auto rv = run_model(mm, key, braket, value, ref, + UQKind::triplex, 0.5); + std::vector corr{{0.75, 1.0, 1.5}, + {2.0, 2.0, 2.0}, + {2.0, 3.0, 3.5}, + {3.875, 4.0, 4.25}}; + REQUIRE(bounds(rv) == corr); + } + + SECTION("interval keeps the bounds") { + auto rv = + run_model(mm, key, braket, value, ref, UQKind::interval); + auto b = bounds(rv); + std::vector> corr{ + {1.0, 1.5}, {2.0, 2.0}, {2.0, 3.0}, {4.0, 4.25}}; + for(std::size_t i = 0; i < corr.size(); ++i) { + REQUIRE(b[i][0] == corr[i][0]); + REQUIRE(b[i][2] == corr[i][1]); + } + } +#endif +} + +TEST_CASE("ReferenceGridError (energy)") { + pluginplay::ModuleManager mm; + mm.load_plugin(); + const auto key = "Reference Grid XC Energy Error"; + + auto psi = test_scf::h2_wave_function(); + auto func = chemist::qm_operator::xc_functional::SVWN3; + auto xc_hat = test_scf::h2_xc(func); + chemist::braket::BraKet braket(psi, xc_hat, psi); + tensor value(-1.0); + + SECTION("none returns the given value") { + auto rv = run_model(mm, key, braket, value, tensor(-0.5), + UQKind::none); + REQUIRE(doubles(rv) == doubles(value)); + } + +#ifdef ENABLE_SIGMA + using tensorwrapper::types::tpdouble; + using triple = std::array; + + SECTION("triplex, reference above") { + auto rv = run_model(mm, key, braket, value, tensor(-0.5), + UQKind::triplex); + REQUIRE(bounds(rv) == + std::vector{{-1.0, -1.0, -0.5}}); + } + + SECTION("triplex, reference below") { + auto rv = run_model(mm, key, braket, value, tensor(-1.5), + UQKind::triplex); + REQUIRE(bounds(rv) == + std::vector{{-1.5, -1.0, -1.0}}); + } +#endif +} diff --git a/tests/cxx/unit_tests/xc/uq_xc_energy.cpp b/tests/cxx/unit_tests/xc/uq_xc_energy.cpp new file mode 100644 index 0000000..c72641c --- /dev/null +++ b/tests/cxx/unit_tests/xc/uq_xc_energy.cpp @@ -0,0 +1,87 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "../../test_scf.hpp" + +using grid_pt = simde::MolecularGrid; +using rho_pt = simde::EDensityCollocationMatrix; +using wf_type = simde::type::rscf_wf; +using pt = simde::eval_braket; +using quad_pt = scf::property_types::xc_energy_quadrature; +using error_pt = scf::property_types::xc_energy_uncertainty; +using tensorwrapper::operations::approximately_equal; + +TEST_CASE("UQXCEnergy") { + pluginplay::ModuleManager mm; + mm.load_plugin(); + auto& mod = mm.at("UQ XC Energy"); + + auto psi = test_scf::h2_wave_function(); + auto func = chemist::qm_operator::xc_functional::SVWN3; + auto xc_hat = test_scf::h2_xc(func); + chemist::braket::BraKet braket(psi, xc_hat, psi); + auto grid = test_scf::h2_xc_grid(); + + auto grid_mod = pluginplay::make_lambda([&](auto&& mol) { + REQUIRE(mol.size() == 2); + return grid; + }); + mod.change_submod("Integration grid", grid_mod); + + // Stands in for the uncertain result the error model returns + simde::type::tensor uq_corr(4.56); + + SECTION("Forwards the grid and the quadrature to the error model") { + simde::type::tensor exc(1.23); + auto quad_mod = pluginplay::make_lambda( + [&](auto&& braket_in, auto&& grid_in) { + REQUIRE(braket_in == braket); + REQUIRE(grid_in == grid); + return exc; + }); + + auto error_mod = pluginplay::make_lambda( + [&](auto&& braket_in, auto&& grid_in, auto&& exc_in) { + REQUIRE(braket_in == braket); + REQUIRE(grid_in == grid); + REQUIRE(exc_in == exc); + return uq_corr; + }); + + mod.change_submod("XC Energy", quad_mod); + mod.change_submod("Incompleteness Error Model", error_mod); + REQUIRE(mod.run_as(braket) == uq_corr); + } + + SECTION("Defaults to the native SVWN3 quadrature") { + // Hardcode the density on the grid (see the SVWN3EnergyQuadrature + // test, whose reference value this is) + auto rho_mod = pluginplay::make_lambda( + [&](auto&&, auto&&) { return test_scf::h2_rho_on_xc_grid(); }); + mm.at("SVWN3 Energy Quadrature") + .change_submod("Density on a grid", rho_mod); + + simde::type::tensor corr(-0.1031596741234082); + auto error_mod = + pluginplay::make_lambda([&](auto&&, auto&&, auto&& exc_in) { + REQUIRE(approximately_equal(exc_in, corr, 1e-6)); + return uq_corr; + }); + + mod.change_submod("Incompleteness Error Model", error_mod); + REQUIRE(mod.run_as(braket) == uq_corr); + } +} diff --git a/tests/cxx/unit_tests/xc/uq_xc_potential.cpp b/tests/cxx/unit_tests/xc/uq_xc_potential.cpp new file mode 100644 index 0000000..76691bc --- /dev/null +++ b/tests/cxx/unit_tests/xc/uq_xc_potential.cpp @@ -0,0 +1,154 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "../../test_scf.hpp" +#include + +using grid_pt = simde::MolecularGrid; +using rho_pt = simde::EDensityCollocationMatrix; +using pt = simde::aos_xc_e_aos; +using quad_pt = scf::property_types::xc_potential_quadrature; +using error_pt = scf::property_types::xc_potential_uncertainty; +using tensorwrapper::operations::approximately_equal; + +TEST_CASE("UQXCPotential") { + pluginplay::ModuleManager mm; + mm.load_plugin(); + auto& mod = mm.at("UQ XC Potential"); + + auto rho = test_scf::h2_density(); + auto aos = test_scf::h2_aos(); + auto func = chemist::qm_operator::xc_functional::SVWN3; + simde::type::xc_e_type xc_op(func, simde::type::electron{}, rho); + chemist::braket::BraKet braket(aos, xc_op, aos); + auto grid = test_scf::h2_xc_grid(); + + auto grid_mod = pluginplay::make_lambda([&](auto&& mol) { + REQUIRE(mol.size() == 2); + return grid; + }); + mod.change_submod("Integration grid", grid_mod); + + // Stands in for the uncertain result the error model returns + simde::type::tensor uq_corr{{5.0, 6.0}, {7.0, 8.0}}; + + SECTION("Forwards the grid and the quadrature to the error model") { + simde::type::tensor vxc{{1.0, 2.0}, {3.0, 4.0}}; + auto quad_mod = pluginplay::make_lambda( + [&](auto&& braket_in, auto&& grid_in) { + REQUIRE(braket_in == braket); + REQUIRE(grid_in == grid); + return vxc; + }); + + auto error_mod = pluginplay::make_lambda( + [&](auto&& braket_in, auto&& grid_in, auto&& vxc_in) { + REQUIRE(braket_in == braket); + REQUIRE(grid_in == grid); + REQUIRE(vxc_in == vxc); + return uq_corr; + }); + + mod.change_submod("XC Potential", quad_mod); + mod.change_submod("Incompleteness Error Model", error_mod); + REQUIRE(mod.run_as(braket) == uq_corr); + } + + SECTION("Defaults to the native SVWN3 quadrature") { + // Hardcode the density on the grid (see the SVWN3PotentialQuadrature + // test, whose reference values these are) + auto rho_mod = pluginplay::make_lambda( + [&](auto&&, auto&&) { return test_scf::h2_rho_on_xc_grid(); }); + mm.at("SVWN3 Potential Quadrature") + .change_submod("Density on a grid", rho_mod); + + simde::type::tensor corr{{-1.83222, -0.402489}, + {-0.402489, -0.0886007}}; + auto error_mod = + pluginplay::make_lambda([&](auto&&, auto&&, auto&& vxc_in) { + REQUIRE(approximately_equal(vxc_in, corr, 1e-5)); + return uq_corr; + }); + + mod.change_submod("Incompleteness Error Model", error_mod); + REQUIRE(mod.run_as(braket) == uq_corr); + } +} + +// Runs the default wiring end to end: the default grid and SVWN3 quadrature, +// then the reference grid error model with the real reference grid. Only the +// "UQ Initializer" (which comes from Integrals) is mocked, and the reference +// grid is made smaller (but still different) to keep the test fast. +TEST_CASE("UQXCPotential with the reference grid error model") { + pluginplay::ModuleManager mm; + mm.load_plugin(); + + auto rho = test_scf::he_density(); + auto aos = test_scf::he_aos(); + auto func = chemist::qm_operator::xc_functional::SVWN3; + simde::type::xc_e_type xc_op(func, simde::type::electron{}, rho); + chemist::braket::BraKet braket(aos, xc_op, aos); + + const auto ref_grid_key = "Reference IntegratorXX Grid"; + mm.change_input(ref_grid_key, "Radial Size", std::size_t{100}); + mm.change_input(ref_grid_key, "Angular Size", std::size_t{590}); + + // The value and the reference value the error model should use + auto vxc = mm.at("SVWN3 Potential").run_as(braket); + auto molecule = scf::xc::libxc::aos2molecule(aos.ao_basis_set()); + auto ref_grid = mm.at(ref_grid_key).run_as(molecule); + auto ref_vxc = + mm.at("SVWN3 Potential Quadrature").run_as(braket, ref_grid); + + auto use_kind = [&](simde::UQKind kind) { + mm.at("Reference Grid XC Potential Error") + .change_submod("UQ Initializer", + pluginplay::make_lambda( + [=]() { return simde::UQFactory(kind); })); + }; + + using tensorwrapper::buffer::make_contiguous; + auto element = [](const auto& t) { + return make_contiguous(t.buffer()).get_elem({0, 0}); + }; + const auto v = wtf::fp::float_cast(element(vxc)); + const auto ref = wtf::fp::float_cast(element(ref_vxc)); + REQUIRE(v != ref); // Otherwise there is no error to attach + + SECTION("none") { + use_kind(simde::UQKind::none); + auto rv = mm.at("UQ XC Potential").run_as(braket); + REQUIRE(wtf::fp::float_cast(element(rv)) == v); + } + +#ifdef ENABLE_SIGMA + SECTION("triplex") { + use_kind(simde::UQKind::triplex); + auto rv = mm.at("UQ XC Potential").run_as(braket); + auto x = + wtf::fp::float_cast(element(rv)); + REQUIRE(x.nominal() == v); + // One-sided: the deviation is entirely towards the reference value + if(ref > v) { + REQUIRE(x.lower() == v); + REQUIRE(x.upper() == Catch::Approx(ref).epsilon(1e-14)); + } else { + REQUIRE(x.upper() == v); + REQUIRE(x.lower() == Catch::Approx(ref).epsilon(1e-14)); + } + } +#endif +} diff --git a/tests/cxx/unit_tests/xc/xc_energy_via_quadrature.cpp b/tests/cxx/unit_tests/xc/xc_energy_via_quadrature.cpp new file mode 100644 index 0000000..0fdf9d0 --- /dev/null +++ b/tests/cxx/unit_tests/xc/xc_energy_via_quadrature.cpp @@ -0,0 +1,55 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "../../test_scf.hpp" + +using grid_pt = simde::MolecularGrid; +using wf_type = simde::type::rscf_wf; +using pt = simde::eval_braket; +using quad_pt = scf::property_types::xc_energy_quadrature; + +// The wrapper should build the grid and forward it, with its inputs, to the +// quadrature submodule, returning the quadrature's result unchanged. +TEST_CASE("XCEnergyViaQuadrature") { + pluginplay::ModuleManager mm; + mm.load_plugin(); + + auto psi = test_scf::h2_wave_function(); + auto func = chemist::qm_operator::xc_functional::SVWN3; + auto xc_hat = test_scf::h2_xc(func); + chemist::braket::BraKet braket(psi, xc_hat, psi); + auto grid = test_scf::h2_xc_grid(); + simde::type::tensor corr(1.23); + + auto grid_mod = pluginplay::make_lambda([&](auto&& mol) { + REQUIRE(mol.size() == 2); + return grid; + }); + + auto quad_mod = + pluginplay::make_lambda([&](auto&& braket_in, auto&& grid_in) { + REQUIRE(braket_in == braket); + REQUIRE(grid_in == grid); + return corr; + }); + + for(std::string key : {"SVWN3 Energy", "LibXC Energy"}) { + auto& mod = mm.at(key); + mod.change_submod("Integration grid", grid_mod); + mod.change_submod("Quadrature", quad_mod); + REQUIRE(mod.run_as(braket) == corr); + } +} diff --git a/tests/cxx/unit_tests/xc/xc_potential_via_quadrature.cpp b/tests/cxx/unit_tests/xc/xc_potential_via_quadrature.cpp new file mode 100644 index 0000000..f78c607 --- /dev/null +++ b/tests/cxx/unit_tests/xc/xc_potential_via_quadrature.cpp @@ -0,0 +1,55 @@ +/* + * Copyright 2026 NWChemEx-Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "../../test_scf.hpp" + +using grid_pt = simde::MolecularGrid; +using pt = simde::aos_xc_e_aos; +using quad_pt = scf::property_types::xc_potential_quadrature; + +// The wrapper should build the grid and forward it, with its inputs, to the +// quadrature submodule, returning the quadrature's result unchanged. +TEST_CASE("XCPotentialViaQuadrature") { + pluginplay::ModuleManager mm; + mm.load_plugin(); + + auto rho = test_scf::h2_density(); + auto aos = test_scf::h2_aos(); + auto func = chemist::qm_operator::xc_functional::SVWN3; + simde::type::xc_e_type xc_op(func, simde::type::electron{}, rho); + chemist::braket::BraKet braket(aos, xc_op, aos); + auto grid = test_scf::h2_xc_grid(); + simde::type::tensor corr{{1.0, 2.0}, {3.0, 4.0}}; + + auto grid_mod = pluginplay::make_lambda([&](auto&& mol) { + REQUIRE(mol.size() == 2); + return grid; + }); + + auto quad_mod = + pluginplay::make_lambda([&](auto&& braket_in, auto&& grid_in) { + REQUIRE(braket_in == braket); + REQUIRE(grid_in == grid); + return corr; + }); + + for(std::string key : {"SVWN3 Potential", "LibXC Potential"}) { + auto& mod = mm.at(key); + mod.change_submod("Integration grid", grid_mod); + mod.change_submod("Quadrature", quad_mod); + REQUIRE(mod.run_as(braket) == corr); + } +} diff --git a/tests/python/unit_tests/property_types/__init__.py b/tests/python/unit_tests/property_types/__init__.py new file mode 100644 index 0000000..1584d82 --- /dev/null +++ b/tests/python/unit_tests/property_types/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 NWChemEx-Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/tests/python/unit_tests/property_types/test_convergence_uncertainty.py b/tests/python/unit_tests/property_types/test_convergence_uncertainty.py new file mode 100644 index 0000000..cf87979 --- /dev/null +++ b/tests/python/unit_tests/property_types/test_convergence_uncertainty.py @@ -0,0 +1,27 @@ +# Copyright 2026 NWChemEx-Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +import scf + + +class TestConvergenceUncertainty(unittest.TestCase): + def test_fields(self): + pt = scf.ConvergenceUncertainty() + self.assertEqual(len(pt.inputs()), 2) + self.assertIn("Energy", pt.inputs()) + self.assertIn("Energy change", pt.inputs()) + self.assertEqual(len(pt.results()), 1) + self.assertIn("Energy", pt.results()) diff --git a/tests/python/unit_tests/test_property_types.py b/tests/python/unit_tests/property_types/test_mo_uncertainty.py similarity index 75% rename from tests/python/unit_tests/test_property_types.py rename to tests/python/unit_tests/property_types/test_mo_uncertainty.py index 9b6015a..4d1e273 100644 --- a/tests/python/unit_tests/test_property_types.py +++ b/tests/python/unit_tests/property_types/test_mo_uncertainty.py @@ -32,13 +32,3 @@ def test_fields(self): self.assertIn(label, pt.inputs()) self.assertEqual(len(pt.results()), 1) self.assertIn("Orbital coefficients", pt.results()) - - -class TestConvergenceUncertainty(unittest.TestCase): - def test_fields(self): - pt = scf.ConvergenceUncertainty() - self.assertEqual(len(pt.inputs()), 2) - self.assertIn("Energy", pt.inputs()) - self.assertIn("Energy change", pt.inputs()) - self.assertEqual(len(pt.results()), 1) - self.assertIn("Energy", pt.results()) diff --git a/tests/python/unit_tests/property_types/test_quadrature.py b/tests/python/unit_tests/property_types/test_quadrature.py new file mode 100644 index 0000000..d0673f1 --- /dev/null +++ b/tests/python/unit_tests/property_types/test_quadrature.py @@ -0,0 +1,32 @@ +# Copyright 2026 NWChemEx-Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +import scf + + +class TestQuadrature(unittest.TestCase): + def check_fields(self, pt): + self.assertEqual(len(pt.inputs()), 2) + self.assertIn("BraKet", pt.inputs()) + self.assertIn("Grid", pt.inputs()) + self.assertEqual(len(pt.results()), 1) + self.assertIn("tensor representation", pt.results()) + + def test_xc_energy_quadrature(self): + self.check_fields(scf.xc_energy_quadrature()) + + def test_xc_potential_quadrature(self): + self.check_fields(scf.xc_potential_quadrature()) diff --git a/tests/python/unit_tests/property_types/test_xc_uncertain.py b/tests/python/unit_tests/property_types/test_xc_uncertain.py new file mode 100644 index 0000000..14465b3 --- /dev/null +++ b/tests/python/unit_tests/property_types/test_xc_uncertain.py @@ -0,0 +1,33 @@ +# Copyright 2026 NWChemEx-Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +import scf + + +class TestXCUncertain(unittest.TestCase): + def check_fields(self, pt): + inputs = ["BraKet", "Grid", "tensor representation"] + self.assertEqual(len(pt.inputs()), len(inputs)) + for label in inputs: + self.assertIn(label, pt.inputs()) + self.assertEqual(len(pt.results()), 1) + self.assertIn("tensor representation", pt.results()) + + def test_xc_energy_uncertainty(self): + self.check_fields(scf.xc_energy_uncertainty()) + + def test_xc_potential_uncertainty(self): + self.check_fields(scf.xc_potential_uncertainty())