Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 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);

}
}
70 changes: 70 additions & 0 deletions src/cxx/nux/xyz_to_mol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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);
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
83 changes: 83 additions & 0 deletions tests/cxx/unit_tests/xyz_to_mol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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>

DECLARE_MODULE(PseudoZFromSymbol);
inline MODULE_CTOR(PseudoZFromSymbol) {
satisfies_property_type<simde::ZFromSymbol>();
}
inline MODULE_RUN(PseudoZFromSymbol){
simde::type::atomic_number Z = 1;
auto rv = results();
return simde::ZFromSymbol::wrap_results(rv, Z);
}

DECLARE_MODULE(PseudoAtomFromZ);
inline MODULE_CTOR(PseudoAtomFromZ) {
satisfies_property_type<simde::AtomFromZ>();
}
inline MODULE_RUN(PseudoAtomFromZ) {
const auto& [Z] = simde::AtomFromZ::unwrap_inputs(inputs);
simde::type::atom atom{"H", Z, 1837.4260218693814, 0.0, 0.0, 0.0};
auto rv = results();
return simde::AtomFromZ::wrap_results(rv, atom);
}

TEST_CASE("XYZToMolecule") {

SECTION("Is this working?") {
pluginplay::ModuleManager mm_test;
nux::load_modules(mm_test);

REQUIRE(mm_test.size() > 0);
}

SECTION("Another One") {
pluginplay::ModuleManager mm;
nux::load_modules(mm);

mm.add_module<PseudoZFromSymbol>("Z from Symbol");
mm.add_module<PseudoAtomFromZ>("Atom from Z");
mm.change_submod("XYZ To Molecule", "Z from symbol", "Z from Symbol");
mm.change_submod("XYZ To Molecule", "Atom from z", "Atom from Z");

chemist::Molecule test_mol;
chemist::Atom atom("H", 1, 1837.42602186938, 0, 0, 0);
chemist::Atom atom2("H", 1, 1837.42602186938, 0, 0, 1);

test_mol.push_back(atom);
test_mol.push_back(atom2);

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 = mm.at("XYZ To Molecule").run_as<simde::MoleculeFromString>(filename);

remove("h2.xyz");

REQUIRE(mol == test_mol);
}
}
Loading