-
Notifications
You must be signed in to change notification settings - Fork 19
feat: AvgMol Node #2328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
rprospero
wants to merge
3
commits into
develop2
Choose a base branch
from
AvgMolNode
base: develop2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
feat: AvgMol Node #2328
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,19 @@ | ||||||||||||||||||||||||||||||||||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||||||||||||||||||||||||||||||||||
| // Copyright (c) 2026 Team Dissolve and contributors | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #include "nodes/avgmol/avgmol.h" | ||||||||||||||||||||||||||||||||||
| #include "nodes/constants.h" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| AvgMolNode::AvgMolNode(Graph *parentGraph) : Node(parentGraph) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| addInput("Configuration", "Set target configuration for the module", targetConfiguration_); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| addOption("Site", "Target site about which to calculate average species geometry", targetSite_); | ||||||||||||||||||||||||||||||||||
| addOption("ExportCoordinates", "Whether to save average coordinates to disk", exportFileAndFormat_); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| addPointerOutput<const Species>("Average Species", "The species with the average coordinates", averageSpecies_); | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+9
to
+14
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I believe we are currently structuring this in the following way: |
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| std::string_view AvgMolNode::type() const { return "AvgMol"; } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| std::string_view AvgMolNode::summary() const { return "Calculate Average Molecule"; } | ||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
| // Copyright (c) 2026 Team Dissolve and contributors | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "io/export/species.h" | ||
| #include "math/sampledVector.h" | ||
| #include "nodes/node.h" | ||
| #include "nodes/parameter.h" | ||
|
|
||
| class Configuration; | ||
| class SpeciesSite; | ||
|
|
||
| class AvgMolNode : public Node | ||
| { | ||
| public: | ||
| AvgMolNode(Graph *parentGraph); | ||
| ~AvgMolNode() override = default; | ||
|
|
||
| public: | ||
| std::string_view type() const override; | ||
| std::string_view summary() const override; | ||
|
|
||
| /* | ||
| * Definition | ||
| */ | ||
| private: | ||
| // Target configuration | ||
| Configuration *targetConfiguration_{nullptr}; | ||
| // Target site | ||
| const SpeciesSite *targetSite_{nullptr}; | ||
| // Whether to save average coordinates to disk | ||
| SpeciesExportFileFormat exportFileAndFormat_; | ||
| // Species targeted by module (derived from selected site) | ||
| const Species *targetSpecies_{nullptr}; | ||
| // Local Species representing average of targeted Species | ||
| Species averageSpecies_; | ||
|
|
||
| private: | ||
| SampledVector x_, y_, z_; | ||
| // Ensure arrays are the correct size for the current target Species | ||
| void updateArrays(); | ||
| // Update the local species with the coordinates from the supplied arrays | ||
| void updateSpecies(); | ||
|
|
||
| /* | ||
| * Processing | ||
| */ | ||
| public: | ||
| // Run main processing | ||
| NodeConstants::ProcessResult process() override; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
| // Copyright (c) 2026 Team Dissolve and contributors | ||
|
|
||
| #include "nodes/avgmol/avgmol.h" | ||
| #include "nodes/constants.h" | ||
|
|
||
| void AvgMolNode::updateArrays() | ||
| { | ||
| auto requiredSize = targetSpecies_ ? targetSpecies_->nAtoms() : -1; | ||
|
|
||
| if (requiredSize > 0) | ||
| { | ||
| if (x_.values().size() == requiredSize && y_.values().size() == requiredSize && z_.values().size() == requiredSize) | ||
| Messenger::print("Using existing coordinate arrays for average species.\n"); | ||
| else | ||
| { | ||
| Messenger::print("Initialising arrays for average molecule: size = {}\n", requiredSize); | ||
| x_.initialise(requiredSize); | ||
| y_.initialise(requiredSize); | ||
| z_.initialise(requiredSize); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| x_.clear(); | ||
| y_.clear(); | ||
| z_.clear(); | ||
| } | ||
| } | ||
|
|
||
| void AvgMolNode::updateSpecies() | ||
| { | ||
| for (auto &&[i, rx, ry, rz] : zip(averageSpecies_.atoms(), x_.values(), y_.values(), z_.values())) | ||
| averageSpecies_.setAtomCoordinates(&i, {rx, ry, rz}); | ||
| } | ||
|
|
||
| NodeConstants::ProcessResult AvgMolNode::process() | ||
| { | ||
| // Grab Box pointer | ||
| const auto *box = targetConfiguration_->box(); | ||
|
|
||
| // Get the target site | ||
| if (!targetSite_) | ||
| { | ||
| Messenger::error("No target site defined.\n"); | ||
| return NodeConstants::ProcessResult::Failed; | ||
| } | ||
|
|
||
| // Get site parent species | ||
| targetSpecies_ = targetSite_->parent(); | ||
|
|
||
| Messenger::print("AvgMol: Target site (species) is {} ({}).\n", targetSite_->name(), targetSpecies_->name()); | ||
| if (exportFileAndFormat_.hasFilename()) | ||
| Messenger::print("AvgMol: Coordinates will be exported to '{}' ({}).\n", exportFileAndFormat_.filename(), | ||
| exportFileAndFormat_.formatDescription()); | ||
|
|
||
| Messenger::print("\n"); | ||
|
|
||
| // Update arrays | ||
| updateArrays(); | ||
|
|
||
| // Get the site stack | ||
| const auto *stack = targetConfiguration_->siteStack(targetSite_); | ||
|
|
||
| // Loop over sites | ||
| std::vector<double> rx(targetSpecies_->nAtoms()), ry(targetSpecies_->nAtoms()), rz(targetSpecies_->nAtoms()); | ||
| Vector3 r; | ||
| for (auto n = 0; n < stack->nSites(); ++n) | ||
| { | ||
| const auto &s = stack->site(n); | ||
| assert(s.molecule()->species() == targetSpecies_); | ||
|
|
||
| // Get axes and take inverse | ||
| auto inverseAxes = s.axes(); | ||
| inverseAxes.invert(); | ||
|
|
||
| // Loop over atoms, taking delta position with origin, and rotating into local axes | ||
| for (auto &&[i, x, y, z] : zip(s.molecule()->atoms(), rx, ry, rz)) | ||
| { | ||
| r = inverseAxes * box->minimumVector(s.origin(), i->r()); | ||
| x = r.x; | ||
| y = r.y; | ||
| z = r.z; | ||
| } | ||
|
|
||
| // Accumulate positions | ||
| x_ += rx; | ||
| y_ += ry; | ||
| z_ += rz; | ||
| } | ||
|
|
||
| updateSpecies(); | ||
|
|
||
| // Export data? | ||
| if (exportFileAndFormat_.hasFilename()) | ||
| { | ||
| if (!exportFileAndFormat_.exportData(&averageSpecies_)) | ||
| return NodeConstants::ProcessResult::Failed; | ||
| } | ||
|
|
||
| return NodeConstants::ProcessResult::Success; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
| // Copyright (c) 2026 Team Dissolve and contributors | ||
|
|
||
| #include "nodes/avgmol/avgmol.h" | ||
| #include "classes/speciesSite.h" | ||
| #include "io/export/species.h" | ||
| #include "io/import/trajectory.h" | ||
| #include "nodes/iterableGraph.h" | ||
| #include "tests/graphData.h" | ||
| #include "tests/testData.h" | ||
| #include <gtest/gtest.h> | ||
|
|
||
| namespace UnitTest | ||
| { | ||
| TEST(AvgMolNodeTest, Water) | ||
| { | ||
| GraphTestData data; | ||
| createWaterGraph(&data.graphRoot, 267); | ||
|
|
||
| // Create an iterator | ||
| auto iterator = dynamic_cast<IterableGraph *>(data.graphRoot.createNode("Iterator")); | ||
| ASSERT_TRUE(iterator); | ||
|
|
||
| // Create a dynamic input from the graph's existing Insert node | ||
| EXPECT_TRUE(data.graphRoot.addEdge({"Insert", "Configuration", "Iterator", "Configuration"})); | ||
|
|
||
| // Within the iterator create an ImportTrajectory node | ||
| auto importTrajectory = iterator->createNode("ImportConfigurationTrajectory"); | ||
| ASSERT_TRUE(importTrajectory); | ||
| ASSERT_TRUE(importTrajectory->setOption<std::string>("FilePath", "dlpoly/water267-analysis/water-267-298K.xyz")); | ||
| ASSERT_TRUE(importTrajectory->setOption<TrajectoryImportFileFormat::TrajectoryImportFormat>( | ||
| "FileFormat", TrajectoryImportFileFormat::TrajectoryImportFormat::XYZ)); | ||
| ASSERT_TRUE(iterator->addEdge({"Inputs", "Configuration", "ImportConfigurationTrajectory", "Configuration"})); | ||
|
|
||
| // Add average molecule module to the iterator | ||
| auto avg = dynamic_cast<AvgMolNode *>(iterator->createNode("AvgMol")); | ||
| ASSERT_TRUE(avg); | ||
|
|
||
| auto *water = data.graphRoot.findNode("Water")->getOutputValue<const Species *>("Species"); | ||
| ASSERT_TRUE(water); | ||
| auto *configuration = data.graphRoot.findNode("Bulk")->getOutputValue<Configuration *>("Configuration"); | ||
| ASSERT_TRUE(configuration); | ||
| SpeciesExportFileFormat exporter("test.AVERAGE"); | ||
| ASSERT_TRUE(avg->setOption("ExportCoordinates", exporter)); | ||
| auto site = water->findSite("Origin"); | ||
| ASSERT_TRUE(site); | ||
| ASSERT_TRUE(avg->setOption("Site", water->findSite("Origin"))); | ||
|
|
||
| ASSERT_TRUE(iterator->addEdge({"ImportConfigurationTrajectory", "Configuration", "AvgMol", "Configuration"})); | ||
|
|
||
| // Run from the iterator node explicitly | ||
| ASSERT_TRUE(iterator->setOption<Number>("N", 95)); | ||
| ASSERT_EQ(iterator->run(), NodeConstants::ProcessResult::Success); | ||
|
|
||
| // Data1DExportFileFormat exporter("test.ANGLE"); | ||
| // exporter.exportData(angle->rdfBC()); | ||
| // EXPECT_TRUE(DissolveSystemTest::checkData1D( | ||
| // angle->rdfBC(), "B-C RDF", | ||
| // {"dlpoly/water267-analysis/water-267-298K.aardf_21_23_inter_sum", Data1DImportFileFormat::Data1DImportFormat::XY}, | ||
| // 2.0e-2)); | ||
| // EXPECT_TRUE(DissolveSystemTest::checkData1D(angle->angleABC(), "A-B-C angle", | ||
| // {"dlpoly/water267-analysis/water-267-298K.dahist1_02_1_01_02.angle.norm", | ||
| // Data1DImportFileFormat::Data1DImportFormat::XY}, | ||
| // 6.0e-5)); | ||
| } | ||
| } // namespace UnitTest |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.