Ticket #1480: mdff.cpp

File mdff.cpp, 2.4 KB (added by tic20@…, 7 years ago)

Added by email2trac

Line 
1/**
2 * @Author: Tristan Croll <tic20>
3 * @Date: 18-Apr-2018
4 * @Email: tic20@cam.ac.uk
5 * @Last modified by: tic20
6 * @Last modified time: 26-Apr-2018
7 * @License: Free for non-commercial use (see license.pdf)
8 * @Copyright: 2017-2018 Tristan Croll
9 */
10
11
12
13#define PYINSTANCE_EXPORT
14
15#include "mdff.h"
16#include <pyinstance/PythonInstance.instantiate.h>
17
18template class pyinstance::PythonInstance<isolde::MDFF_Atom>;
19template class pyinstance::PythonInstance<isolde::MDFF_Mgr>;
20
21namespace isolde
22{
23
24MDFF_Atom::MDFF_Atom(Atom* atom, MDFF_Mgr *mgr) : _atom(atom), _mgr(mgr)
25{
26 _coupling_constant = _atom->element().mass();
27}
28
29Change_Tracker* MDFF_Atom::change_tracker() const
30{
31 return mgr()->change_tracker();
32}
33
34void MDFF_Atom::set_coupling_constant(double k)
35{
36 _coupling_constant = k<0 ? 0.0 : k;
37 mgr()->track_change(this, change_tracker()->REASON_SPRING_CONSTANT_CHANGED);
38}
39
40void MDFF_Atom::set_enabled(bool flag)
41{
42 if (_enabled != flag)
43 {
44 _enabled = flag;
45 mgr()->track_change(this, change_tracker()->REASON_ENABLED_CHANGED);
46 }
47}
48
49MDFF_Atom* MDFF_Mgr::_new_mdff_atom(Atom *atom)
50{
51 if (atom->structure() != _atomic_model) {
52 throw std::logic_error(error_different_mol());
53 }
54 MDFF_Atom* mdffa = new MDFF_Atom(atom, this);
55 _atom_to_mdff[atom] = mdffa;
56 track_created(mdffa);
57 return mdffa;
58}
59
60MDFF_Atom* MDFF_Mgr::get_mdff_atom(Atom *atom, bool create)
61{
62 auto it = _atom_to_mdff.find(atom);
63 if (it != _atom_to_mdff.end())
64 return it->second;
65 if (create)
66 return _new_mdff_atom(atom);
67 return nullptr;
68}
69
70void MDFF_Mgr::delete_mdff_atoms(const std::set<MDFF_Atom *>& to_delete)
71{
72 auto db = DestructionBatcher(this);
73 _delete_mdff_atoms(to_delete);
74}
75
76void MDFF_Mgr::_delete_mdff_atoms(const std::set<MDFF_Atom *>& to_delete)
77{
78 for (auto a: to_delete) {
79 _atom_to_mdff.erase(a->atom());
80 delete a;
81 }
82}
83
84void MDFF_Mgr::destructors_done(const std::set<void *>& destroyed)
85{
86 auto db = DestructionBatcher(this);
87 std::set<MDFF_Atom *> to_delete;
88 for (const auto &it: _atom_to_mdff) {
89 auto a = it.second;
90 if (destroyed.find(static_cast<void *>(a->atom())) != destroyed.end()) {
91 to_delete.insert(a);
92 }
93 }
94 _delete_mdff_atoms(to_delete);
95}
96
97MDFF_Mgr::~MDFF_Mgr()
98{
99 auto du = DestructionUser(this);
100 for (auto &it: _atom_to_mdff) {
101 delete it.second;
102 }
103}
104
105} //namespace isolde