Skip to content
Snippets Groups Projects
Commit 00fe58b5 authored by mkirsz's avatar mkirsz
Browse files

Helper methods for comparison

parent dfa76e78
No related branches found
No related tags found
1 merge request!11Helper methods for comparison
Pipeline #48499 failed
...@@ -79,5 +79,11 @@ struct Atom: public Element { ...@@ -79,5 +79,11 @@ struct Atom: public Element {
*/ */
bool operator==(const Atom &) const; 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 #endif
...@@ -251,10 +251,23 @@ struct Structure { ...@@ -251,10 +251,23 @@ struct Structure {
* This operator compares cell, stress tensor, number of atoms, * This operator compares cell, stress tensor, number of atoms,
* eweight, fweight and sweight, all atom positions and forces. * 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; 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 // move iterator forward to the next structure
// return number of atoms in the structure // return number of atoms in the structure
// return 0 if there is no more structures // return 0 if there is no more structures
......
...@@ -5,25 +5,31 @@ ...@@ -5,25 +5,31 @@
Atom::Atom() {} Atom::Atom() {}
Atom::Atom(const Element &element, Atom::Atom(const Element &element,
const double px, const double py, const double pz, const double px, const double py, const double pz,
const double fx, const double fy, const double fz): const double fx, const double fy, const double fz):
Element(element), Element(element),
position(px,py,pz), position(px,py,pz),
force(fx,fy,fz) force(fx,fy,fz)
{} {}
std::ostream& operator<<(std::ostream& os, const Atom& atom) std::ostream& operator<<(std::ostream& os, const Atom& atom)
{ {
os << (Element&)atom; os << (Element&)atom;
os << "Coordinates: " << std::left << atom.position << std::endl; os << "Coordinates: " << std::left << atom.position << std::endl;
os << "Force: " << std::left << atom.force << std::endl; os << "Force: " << std::left << atom.force << std::endl;
return os; return os;
} }
bool Atom::operator==(const Atom &a) const { bool Atom::operator==(const Atom &a) const {
double EPSILON = std::numeric_limits<double>::epsilon(); double EPSILON = std::numeric_limits<double>::epsilon();
return return
position.isApprox(a.position, EPSILON) position.isApprox(a.position, EPSILON)
&& force.isApprox(a.force, EPSILON) && force.isApprox(a.force, EPSILON)
&& Element::operator==(a) && 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 ...@@ -192,6 +192,23 @@ bool Structure::operator==(const Structure& st) const
} }
return result; 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) { int Structure::next_structure(std::ifstream &ifs) {
std::string line; std::string line;
std::getline(ifs,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