Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# These are directories used by IDEs for storing settings
.idea/
.vscode/
.cache/

# These are common Python virtual enviornment directory names
venv/
Expand Down
3 changes: 2 additions & 1 deletion src/cxx/nux/nux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace nux {

void load_modules(pluginplay::ModuleManager &mm) {
mm.add_module<BOApproximation>("Born-Oppenheimer Approximation");
mm.add_module<XYZToMolecule>("XYZ To Molecule");
}

} // namespace nux
} // namespace nux
3 changes: 2 additions & 1 deletion src/cxx/nux/nux_modules.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
namespace nux {

DECLARE_MODULE(BOApproximation);
DECLARE_MODULE(XYZToMolecule);

}
}
75 changes: 75 additions & 0 deletions src/cxx/nux/xyz_to_mol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2025 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 "nux_modules.hpp"
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

namespace nux {

MODULE_CTOR(XYZToMolecule) {
satisfies_property_type<simde::MoleculeFromString>();
add_submodule<simde::ZFromSymbol>("Z from symbol");
add_submodule<simde::AtomFromZ>("Atom from z");
}

MODULE_RUN(XYZToMolecule) {
const auto& [filename] = simde::MoleculeFromString::unwrap_inputs(inputs);

auto& z_from_sym = submods.at("Z from symbol");
auto& atom_from_z = submods.at("Atom from z");

chemist::Molecule mol;

std::ifstream xyz_file(filename);

if (!xyz_file) {
throw std::runtime_error("File not found: " + filename);
}

std::string line;

std::getline(xyz_file, line);
std::getline(xyz_file, line);

while (std::getline(xyz_file, line)) {
std::istringstream iss(line);
std::vector<std::string> tokens;
std::string token;
while (std::getline(iss, token, ' ')) {
if (token.size()) { tokens.emplace_back(token); }
}
auto Z = z_from_sym.run_as<simde::ZFromSymbol>(tokens.at(0));
auto x = std::stod(tokens.at(1));
auto y = std::stod(tokens.at(2));
auto z = std::stod(tokens.at(3));

auto atm = atom_from_z.run_as<simde::AtomFromZ>(Z);
atm.x() = x;
atm.y() = y;
atm.z() = z;
mol.push_back(atm);
}

xyz_file.close();

auto rv = results();
return simde::MoleculeFromString::wrap_results(rv, mol);
}

} // namespace nux
73 changes: 73 additions & 0 deletions tests/cxx/unit_tests/xyz_to_mol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2024 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_nux.hpp"
#include <nux/nux.hpp>

TEST_CASE("XYZToMolecule") {
pluginplay::ModuleManager mm;
nux::load_modules(mm);

auto make_atoms = [=](auto&& Z) {
double h_mass = 1837.4260218693814;
return simde::type::atom{"H", 1, h_mass, 0.0, 0.0, 0.0};
};

auto atom_mod = pluginplay::make_lambda<simde::AtomFromZ>(make_atoms);

auto z_mod = pluginplay::make_lambda<simde::ZFromSymbol>([=](auto&& symbol) {
return simde::type::atomic_number{1};
});

auto& xyz_mod = mm.at("XYZ To Molecule");
xyz_mod.change_submod("Z from symbol", z_mod);
xyz_mod.change_submod("Atom from z", atom_mod);

SECTION("No XYZ file found") {
std::string file = "file.xyz";
REQUIRE_THROWS_AS(xyz_mod.run_as<simde::MoleculeFromString>(file), std::runtime_error);
}

SECTION("Full XYZ To Molecule run") {
auto atom0{make_atoms(1)};
auto atom1{make_atoms(1)};
atom1.z() = 1;

simde::type::molecule test_mol{atom0, atom1};

std::ofstream xyz_file;
xyz_file.open("h2.xyz");

xyz_file << "2\n";
xyz_file << "This is a comment!\n";
xyz_file << "H 0 0 0\n";
xyz_file << "H 0 0 1\n";
xyz_file.close();

std::string filename = "h2.xyz";

auto mol = xyz_mod.run_as<simde::MoleculeFromString>(filename);

remove("h2.xyz");
std::ifstream del_file("h2.xyz");

std::cout << typeid(mol[0]).name() << std::endl;
std::cout << typeid(test_mol[0]).name() << std::endl;

REQUIRE(mol == test_mol);
REQUIRE(del_file.good() == false);
}
}