Ignore:
Timestamp:
Nov 21, 2012, 10:03:24 AM (12 years ago)
Author:
Frederik Heber <heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
a0f8d3
Parents:
184943
git-author:
Frederik Heber <heber@…> (08/10/12 08:21:49)
git-committer:
Frederik Heber <heber@…> (11/21/12 10:03:24)
Message:

Added extensive unit tests and some more functions to Fragment.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Fragmentation/SetValues/Fragment.cpp

    r184943 rf3bc5f  
    3838#include "Fragment.hpp"
    3939
     40#include <algorithm>
     41#include <cmath>
    4042#include <iostream>
     43#include <limits>
    4144
    4245#include "CodePatterns/Assert.hpp"
     
    6972Fragment& Fragment::operator=(const Fragment &other)
    7073{
    71   nuclei.clear();
    72   nuclei = other.nuclei;
     74  if (this != &other) {
     75    nuclei.clear();
     76    nuclei = other.nuclei;
     77  }
    7378  return *this;
    7479}
     
    8590{
    8691  for (nuclei_t::const_iterator iter = nuclei.begin();
    87       iter != nuclei.end(); ++iter) {
    88     if (((*iter).first == n.first) && ((*iter).second == n.second))
     92      iter != nuclei.end(); ++iter)
     93    if (Fragment::isPositionEqual(iter->first, n.first))
    8994      return true;
    90   }
    9195  return false;
    9296}
     
    9599{
    96100  for (nuclei_t::iterator iter = nuclei.begin();
    97       iter != nuclei.end(); ++iter) {
    98     if (((*iter).first == n.first) && ((*iter).second == n.second)) {
     101      iter != nuclei.end(); ++iter)
     102    if (Fragment::isPositionEqual(iter->first, n.first)) {
    99103      nuclei.erase(iter);
    100104      break;
    101105    }
    102   }
    103106}
    104107
     
    121124}
    122125
     126Fragment::nucleus_t Fragment::createNucleus(const position_t &position, const double charge)
     127{
     128  return nucleus_t( make_pair( position, charge ) );
     129}
     130
     131bool Fragment::isPositionEqual(const position_t &a, const position_t &b)
     132{
     133  bool status = true;
     134  for (size_t i=0;i<3;++i)
     135    status &= fabs(a[i] - b[i]) < std::numeric_limits<double>::epsilon();
     136  return status;
     137}
     138
     139bool Fragment::operator==(const Fragment& other) const
     140{
     141  bool status = true;
     142  // compare sizes
     143  if (nuclei.size() != other.nuclei.size()) {
     144    return false;
     145  } else {
     146    // if equal, sort, and compare each nuclei
     147    Fragment::nuclei_t thisnuclei(nuclei);
     148    Fragment::nuclei_t othernuclei(other.nuclei);
     149    std::sort(thisnuclei.begin(), thisnuclei.end());
     150    std::sort(othernuclei.begin(), othernuclei.end());
     151    Fragment::nuclei_t::const_iterator iter = thisnuclei.begin();
     152    Fragment::nuclei_t::const_iterator oiter = othernuclei.begin();
     153    for (; iter != thisnuclei.end(); ++iter,++oiter)
     154      status &= (*iter == *oiter);
     155  }
     156  return status;
     157}
     158
    123159std::ostream & operator<<(std::ostream &ost, const Fragment &f)
    124160{
     
    126162  for (Fragment::nuclei_t::const_iterator iter = f.nuclei.begin();
    127163      iter != f.nuclei.end(); ++iter)
    128     ost << NucleiCounter++ << "\t" << iter->first << " with charge " << iter->second << std::endl;
     164    ost << "[" << NucleiCounter++ << "]:" << *iter << ", ";
    129165  return ost;
     166}
     167
     168std::ostream & operator<<(std::ostream &ost, const Fragment::nucleus_t &n)
     169{
     170  ost << n.first << " with charge " << n.second;
     171  return ost;
     172}
     173
     174bool operator==(const Fragment::nucleus_t &a, const Fragment::nucleus_t &b)
     175{
     176  bool status = true;
     177  // check positions
     178  status &= Fragment::isPositionEqual(a.first, b.first);
     179  status &= fabs(a.second - b.second) < std::numeric_limits<double>::epsilon();
     180  return status;
    130181}
    131182
Note: See TracChangeset for help on using the changeset viewer.