Skip to content
Snippets Groups Projects
Commit 8c2bc616 authored by Marcin Kirsz's avatar Marcin Kirsz
Browse files

Merge branch 'develop' into 'main'

Helper methods for comparison

See merge request !11
parents 5ad667b2 00fe58b5
No related branches found
No related tags found
1 merge request!11Helper methods for comparison
Pipeline #48529 passed
......@@ -79,5 +79,11 @@ struct Atom: public Element {
*/
bool operator==(const Atom &) const;
/** Return true if both atoms have the same position and type.
*
* This method compares chemical type and position only
*/
bool is_the_same(const Atom &, double thr=1e-6) const;
};
#endif
......@@ -251,10 +251,23 @@ struct Structure {
* This operator compares cell, stress tensor, number of atoms,
* eweight, fweight and sweight, all atom positions and forces.
*
* It does NOT compare labels/
* Assumes atoms have the same order.
*
* It does NOT compare labels.
*/
bool operator==(const Structure& st) const;
/** Return true if both structures are the same.
*
* This method compares cell vectors, number of atoms,
* species and atomic coordinates
*
* Order ot atoms does not matter.
*
* It does NOT compare energies, stresses, forces and labels.
*/
bool is_the_same(const Structure& st, double thr=1e-6) const;
// move iterator forward to the next structure
// return number of atoms in the structure
// return 0 if there is no more structures
......
......@@ -5,25 +5,31 @@
Atom::Atom() {}
Atom::Atom(const Element &element,
const double px, const double py, const double pz,
const double fx, const double fy, const double fz):
Element(element),
position(px,py,pz),
force(fx,fy,fz)
const double px, const double py, const double pz,
const double fx, const double fy, const double fz):
Element(element),
position(px,py,pz),
force(fx,fy,fz)
{}
std::ostream& operator<<(std::ostream& os, const Atom& atom)
{
os << (Element&)atom;
os << "Coordinates: " << std::left << atom.position << std::endl;
os << "Force: " << std::left << atom.force << std::endl;
return os;
os << (Element&)atom;
os << "Coordinates: " << std::left << atom.position << std::endl;
os << "Force: " << std::left << atom.force << std::endl;
return os;
}
bool Atom::operator==(const Atom &a) const {
double EPSILON = std::numeric_limits<double>::epsilon();
return
position.isApprox(a.position, EPSILON)
&& force.isApprox(a.force, EPSILON)
&& Element::operator==(a)
;
double EPSILON = std::numeric_limits<double>::epsilon();
return
position.isApprox(a.position, EPSILON)
&& force.isApprox(a.force, EPSILON)
&& Element::operator==(a)
;
}
bool Atom::is_the_same(const Atom &a, double thr) const {
return
position.isApprox(a.position, thr)
&& Element::operator==(a)
;
}
......@@ -192,6 +192,23 @@ bool Structure::operator==(const Structure& st) const
}
return result;
}
bool Structure::is_the_same(const Structure& st, double thr) const
{
bool result =
cell.isApprox(st.cell, thr)
&& natoms()==st.natoms();
if (!result) return result;
size_t count=0;;
for (size_t i=0;i<natoms();++i) {
for (size_t j=i;j<natoms();++j) {
result = atoms[i].is_the_same(st.atoms[j], thr);
if (result) count++;
}
}
return count==natoms() ? true : false;
}
int Structure::next_structure(std::ifstream &ifs) {
std::string line;
std::getline(ifs,line);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment