Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cxx/include/chemcache/chemcache_mm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ namespace chemcache {
*
* Load it into a ModuleManager with `mm.load_plugin<chemcache::ChemCache>()`
* (`mm.load_plugin(chemcache.ChemCache())` from Python). The plugin's name is
* "chemcache". All of the plugin's default submodules are provided by the
* plugin itself, so it does not need other plugins to be loaded.
* "chemcache". The "UQ Initializer" submodule of the atomic density matrix
* modules defaults to the Integrals plugin's "UQ Initializer". If Integrals is
* not loaded, those modules (and the SAD densities using them) need one to be
* supplied.
*/
DECLARE_PLUGIN(ChemCache);

Expand Down
6 changes: 6 additions & 0 deletions cxx/src/chemcache/chemcache_mm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ PLUGIN_CTOR(ChemCache) {
hydrocarbons::set_defaults(modules());
molecule_mods::set_defaults(modules());
change_submod("NWX Molecules", "Atoms", "Atom");

// Submodules ChemCache does not provide come from these plugins, if loaded
set_preferred_providers("integrals");

default_submod_from("integrals", "sto-3g atomic density matrix",
"UQ Initializer", "UQ Initializer");
}

} // namespace chemcache
60 changes: 60 additions & 0 deletions cxx/src/chemcache/density/make_uncertain.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 <simde/simde.hpp>
#include <vector>

namespace chemcache {

/** @brief Converts a tensor of doubles to the kind of values @p factory makes.
*
* The values of @p t are exact, so each becomes an uncertain value with a
* radius of zero. This is needed because WTF does not convert doubles to
* uncertain values in mixed arithmetic.
*
* @param[in] t The tensor to convert. Must hold doubles.
* @param[in] factory The factory making the values of the result.
*
* @return A tensor with the values of @p t, of the kind @p factory makes.
*
* @throw std::bad_alloc if there is a problem allocating the result. Strong
* throw guarantee.
*/
inline simde::type::tensor make_uncertain(const simde::type::tensor& t,
const simde::UQFactory& factory) {
if(factory.kind() == simde::UQKind::none) return t;

using tensorwrapper::buffer::get_raw_data;
using tensorwrapper::buffer::make_contiguous;
const auto& buffer = make_contiguous(t.buffer());
const auto values = get_raw_data<double>(buffer);
auto shape = t.buffer().layout().shape().as_smooth().make_smooth();

return simde::visit_uq_kind(factory.kind(), [&](auto type) {
using float_type = typename decltype(type)::type;
std::vector<float_type> data;
data.reserve(values.size());
for(const auto x : values) {
auto value = factory(x, 0.0);
data.push_back(wtf::fp::float_cast<float_type>(value));
}
tensorwrapper::buffer::Contiguous uq_buffer(std::move(data), shape);
return simde::type::tensor(shape, std::move(uq_buffer));
});
}

} // namespace chemcache
26 changes: 12 additions & 14 deletions cxx/src/chemcache/density/sto_dash_3g/sto_dash_3g.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
*/

#include "../density.hpp"
#include "../make_uncertain.hpp"
#include "sto_dash_3g.hpp"

namespace chemcache {

using atomic_den_pt = simde::AtomicDensityMatrixFromZ;
using udouble = tensorwrapper::types::udouble;

static constexpr auto module_desc = R"(
sto-3g atomic density matrices
Expand All @@ -40,22 +40,20 @@ This module was autogenerated.
MODULE_CTOR(sto_dash_3g_atom_density_matrix) {
description(module_desc);
satisfies_property_type<atomic_den_pt>();
add_input<bool>("With UQ?").set_default(false);
add_submodule<simde::UQInitializer>("UQ Initializer")
.set_description(
"Makes the factory whose kind is the type of the density matrix");
}

MODULE_RUN(sto_dash_3g_atom_density_matrix) {
const auto& [Z] = atomic_den_pt::unwrap_inputs(inputs);
const auto is_uncertain = inputs.at("With UQ?").value<bool>();
auto& rt = this->get_runtime();

simde::type::tensor result;
if(is_uncertain) {
result = sto_dash_3g_atom_density_matrix_<udouble>(Z, rt);
} else {
result = sto_dash_3g_atom_density_matrix_<double>(Z, rt);
}
auto rv = results();
return atomic_den_pt::wrap_results(rv, result);
const auto& [Z] = atomic_den_pt::unwrap_inputs(inputs);
auto& rt = this->get_runtime();
const auto& factory =
submods.at("UQ Initializer").run_as<simde::UQInitializer>();

auto result = sto_dash_3g_atom_density_matrix_<double>(Z, rt);
auto rv = results();
return atomic_den_pt::wrap_results(rv, make_uncertain(result, factory));
}

} // namespace chemcache
4 changes: 2 additions & 2 deletions docs/source/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ properly build ChemCache.
C++ Compiler
------------

ChemCache relies on the C++17 standard and should work with any C++17
compliant compiler (GCC 9.x or newer).
ChemCache relies on the C++20 standard and should work with any C++20
compliant compiler (GCC 10.x or newer).

Optional Dependencies
=====================
Expand Down
19 changes: 18 additions & 1 deletion tests/cxx/unit_tests/density/density.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <simde/chemical_system/atomic_density_matrix.hpp>
#include <simde/density/initial_density.hpp>
#include <simde/types.hpp>
#include <simde/uncertainty/uncertainty.hpp>

using atomic_dm_pt = simde::AtomicDensityMatrixFromZ;
using sad_rho_pt = simde::InitialDensity;
Expand All @@ -35,9 +36,24 @@ using shape_t = tensorwrapper::shape::Smooth;

using Catch::Matchers::Message;

namespace {

// Binds a UQ Initializer making @p kind to the atomic density matrix of @p mm
void bind_uq_initializer(pluginplay::ModuleManager& mm,
simde::UQKind kind = simde::UQKind::none) {
auto uq_initializer = pluginplay::make_lambda<simde::UQInitializer>(
[kind]() { return simde::UQFactory(kind); });
mm.at("sto-3g atomic density matrix")
.change_submod("UQ Initializer", std::make_shared<pluginplay::Module>(
std::move(uq_initializer)));
}

} // namespace

TEST_CASE("sto-3g atomic density matrix") {
pluginplay::ModuleManager mm;
mm.load_plugin<chemcache::ChemCache>();
bind_uq_initializer(mm);
auto& atom_dm_mod = mm.at("sto-3g atomic density matrix");
auto rt = mm.get_runtime();

Expand All @@ -52,7 +68,7 @@ TEST_CASE("sto-3g atomic density matrix") {
REQUIRE(rv == corr);
}
SECTION("Uncertain Values") {
atom_dm_mod.change_input("With UQ?", true);
bind_uq_initializer(mm, simde::UQKind::uncertain);
const auto& rv = atom_dm_mod.run_as<atomic_dm_pt>(Z);
std::vector<udouble_t> data{udouble_t{1.0}};
tensorwrapper::buffer::Contiguous buffer(std::move(data), shape);
Expand All @@ -72,6 +88,7 @@ TEST_CASE("sto-3g atomic density matrix") {
TEST_CASE("SAD Density") {
pluginplay::ModuleManager mm;
mm.load_plugin<chemcache::ChemCache>();
bind_uq_initializer(mm);
auto& sad_mod = mm.at("sto-3g SAD density");
auto rt = mm.get_runtime();

Expand Down
7 changes: 5 additions & 2 deletions tests/cxx/unit_tests/test_chemcache_mm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ TEST_CASE("ChemCache plugin") {

SECTION("Identity") {
REQUIRE(plugin.name() == "chemcache");
// Every default comes from the plugin itself
REQUIRE(plugin.preferred_providers().empty());
// The UQ Initializer comes from Integrals
using provider_list = std::vector<std::string>;
REQUIRE(plugin.preferred_providers() == provider_list{"integrals"});
}

SECTION("Modules from each subcollection") {
Expand All @@ -40,6 +41,8 @@ TEST_CASE("ChemCache plugin") {
REQUIRE(modules.has_submod("sto-3g SAD density", "Atomic Basis"));
REQUIRE(modules.has_submod("sto-3g SAD density", "Atomic Density"));
REQUIRE(modules.has_submod("NWX Molecules", "Atoms"));
REQUIRE_FALSE(
modules.has_submod("sto-3g atomic density matrix", "UQ Initializer"));
}

SECTION("Can be loaded into a ModuleManager") {
Expand Down
4 changes: 3 additions & 1 deletion tests/python/unit_tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ def test_load_plugin(self):
mm.load_plugin(chemcache.ChemCache())
self.assertTrue(mm.has_plugin("chemcache"))
self.assertTrue(mm.count("NWX Molecules"))
self.assertEqual(mm.get_plugin("chemcache").preferred_providers(), [])
self.assertEqual(
mm.get_plugin("chemcache").preferred_providers(), ["integrals"]
)
26 changes: 12 additions & 14 deletions utils/data_management/generate_densities.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ def _write_den_files(

source_template = """
#include "../density.hpp"
#include "../make_uncertain.hpp"
#include "{s_name}.hpp"

namespace chemcache {{

using atomic_den_pt = simde::AtomicDensityMatrixFromZ;
using udouble = tensorwrapper::types::udouble;

static constexpr auto module_desc = R"(
{d_name} atomic density matrices
Expand All @@ -104,22 +104,20 @@ def _write_den_files(
MODULE_CTOR({s_name}_atom_density_matrix) {{
description(module_desc);
satisfies_property_type<atomic_den_pt>();
add_input<bool>("With UQ?").set_default(false);
add_submodule<simde::UQInitializer>("UQ Initializer")
.set_description(
"Makes the factory whose kind is the type of the density matrix");
}}

MODULE_RUN({s_name}_atom_density_matrix) {{
const auto& [Z] = atomic_den_pt::unwrap_inputs(inputs);
const auto is_uncertain = inputs.at("With UQ?").value<bool>();
auto& rt = this->get_runtime();

simde::type::tensor result;
if(is_uncertain) {{
result = {s_name}_atom_density_matrix_<udouble>(Z, rt);
}} else {{
result = {s_name}_atom_density_matrix_<double>(Z, rt);
}}
auto rv = results();
return atomic_den_pt::wrap_results(rv, result);
const auto& [Z] = atomic_den_pt::unwrap_inputs(inputs);
auto& rt = this->get_runtime();
const auto& factory =
submods.at("UQ Initializer").run_as<simde::UQInitializer>();

auto result = {s_name}_atom_density_matrix_<double>(Z, rt);
auto rv = results();
return atomic_den_pt::wrap_results(rv, make_uncertain(result, factory));
}}

}} // namespace chemcache
Expand Down
Loading