Skip to content
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

Initial support for constraints #841

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
12 changes: 6 additions & 6 deletions avogadro/qtgui/molecule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ namespace Avogadro::QtGui {
using std::swap;

Molecule::Molecule(QObject* parent_)
: QObject(parent_),
m_undoMolecule(new RWMolecule(*this, this)), Core::Molecule()
: QObject(parent_), Core::Molecule(),
m_undoMolecule(new RWMolecule(*this, this)), constraints()
{
m_undoMolecule->setInteractive(true);
}

Molecule::Molecule(const Molecule& other)
: QObject(), Core::Molecule(other),
m_undoMolecule(new RWMolecule(*this, this))
m_undoMolecule(new RWMolecule(*this, this)), constraints()
{
m_undoMolecule->setInteractive(true);
// Now assign the unique ids
Expand All @@ -33,7 +33,7 @@ Molecule::Molecule(const Molecule& other)
}

Molecule::Molecule(const Core::Molecule& other)
: QObject(), Core::Molecule(other)
: QObject(), Core::Molecule(other), constraints()
{
// Now assign the unique ids
for (Index i = 0; i < atomCount(); i++)
Expand Down Expand Up @@ -204,8 +204,8 @@ void Molecule::swapAtom(Index a, Index b)
Core::Molecule::swapAtom(a, b);
}

Molecule::BondType Molecule::addBond(Index a, Index b,
unsigned char order, Index uniqueId)
Molecule::BondType Molecule::addBond(Index a, Index b, unsigned char order,
Index uniqueId)
{
if (uniqueId >= static_cast<Index>(m_bondUniqueIds.size()) ||
m_bondUniqueIds[uniqueId] != MaxIndex) {
Expand Down
8 changes: 8 additions & 0 deletions avogadro/qtgui/molecule.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
#include <list>

namespace Avogadro {

namespace QtPlugins {
class ConstraintsModel;
}

namespace QtGui {

class Mesh;
Expand All @@ -33,6 +38,9 @@ class AVOGADROQTGUI_EXPORT Molecule : public QObject, public Core::Molecule
Q_OBJECT

public:

QtPlugins::ConstraintsModel* constraints;

/** Typedef for Atom class. */
typedef Core::Molecule::AtomType AtomType;

Expand Down
3 changes: 3 additions & 0 deletions avogadro/qtplugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ if(BUILD_GPL_PLUGINS)
add_subdirectory(qtaim)
endif()

#kantundpeterpan
add_subdirectory(constraints)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${original_library_output_dir}")

# Add all of the static plugins to the initialization file.
Expand Down
23 changes: 23 additions & 0 deletions avogadro/qtplugins/constraints/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
include_directories("${AvogadroLibs_SOURCE_DIR}/thirdparty")

set(constraints
constraintsextension.cpp
constraintsdialog.cpp
constraintsmodel.cpp
constraint.cpp
)

set(constraints_uis
constraintsdialog.ui
)

avogadro_plugin(ConstraintsExtension
"Constraints extension"
ExtensionPlugin
constraintsextension.h
ConstraintsExtension
"${constraints}"
"${constraints_uis}"
)

target_link_libraries(ConstraintsExtension)
176 changes: 176 additions & 0 deletions avogadro/qtplugins/constraints/constraint.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
#include "constraint.h"

namespace Avogadro {
namespace QtPlugins {

Constraint::Constraint(int type,
int a,
int b,
int c,
int d,
double value,
ConstraintsModel* model)
{
c_model = model;
ConstraintType = type;

//adjusting for 0 indexing
a = a-1;
b = b-1;
c = c-1;
d = d-1;

// store unique AtomIds

switch (ConstraintType)
{
case 0:
case 1:
case 2:
case 3:
case 4:
// AtomA
Atoms << c_model->c_molecule->atomUniqueId(a);
break;

case 5:
//AtomA
Atoms << c_model->c_molecule->atomUniqueId(a);
//AtomB
Atoms << c_model->c_molecule->atomUniqueId(b);
break;

case 6:
//AtomA
Atoms << c_model->c_molecule->atomUniqueId(a);
//AtomB
Atoms << c_model->c_molecule->atomUniqueId(b);
//AtomC
Atoms << c_model->c_molecule->atomUniqueId(c);
break;

case 7:
//AtomA
Atoms << c_model->c_molecule->atomUniqueId(a);
//AtomB
Atoms << c_model->c_molecule->atomUniqueId(b);
//AtomC
Atoms << c_model->c_molecule->atomUniqueId(c);
//AtomD
Atoms << c_model->c_molecule->atomUniqueId(d);
}

ConstraintValue = value;

}

Constraint::~Constraint(){}

void Constraint::SetConstraintType(int type)
{
ConstraintType = type;
}

void Constraint::SetValue(double Value)
{
ConstraintValue = Value;
}

int Constraint::GetConstraintType() const
{
return ConstraintType;
}

double Constraint::GetConstraintValue() const
{
return ConstraintValue;
}

const Index Constraint::GetConstraintAtomA() const
{
if (Atoms.size() >= 1) //returned for all constraint types
{
return c_model->c_molecule->atomByUniqueId(Atoms[0]).index()+1;
}
else
{
return 0;
}
}

const Index Constraint::GetConstraintAtomB() const
{
if (Atoms.size() >= 2) //distance, angle and torsion constraints
{
return c_model->c_molecule->atomByUniqueId(Atoms[1]).index()+1;
}
else
{
return 0;
}
}

const Index Constraint::GetConstraintAtomC() const
{
if (Atoms.size() >= 3) //angle and torsion constraints
{
return c_model->c_molecule->atomByUniqueId(Atoms[2]).index()+1;
}
else
{
return 0;
}
}

const Index Constraint::GetConstraintAtomD() const
{
if (Atoms.size() >= 4) //torsion constraints only
{
return c_model->c_molecule->atomByUniqueId(Atoms[3]).index()+1;
}
else
{
return 0;
}
}

QJsonObject Constraint::toJson()
{
QJsonObject ConstraintJ;
ConstraintJ["type"] = GetConstraintType();
ConstraintJ["value"] = GetConstraintValue();

QJsonArray ConstraintAtoms;

switch (GetConstraintType())
{
case 0:
case 1:
case 2:
case 3:
case 4:
ConstraintAtoms << static_cast<int>(GetConstraintAtomA());
break;
case 5:
ConstraintAtoms << static_cast<int>(GetConstraintAtomA())
<< static_cast<int>(GetConstraintAtomB());
break;
case 6:
ConstraintAtoms << static_cast<int>(GetConstraintAtomA())
<< static_cast<int>(GetConstraintAtomB())
<< static_cast<int>(GetConstraintAtomC());
break;
case 7:
ConstraintAtoms << static_cast<int>(GetConstraintAtomA())
<< static_cast<int>(GetConstraintAtomB())
<< static_cast<int>(GetConstraintAtomC())
<< static_cast<int>(GetConstraintAtomD());
break;
}

ConstraintJ.insert("atoms", ConstraintAtoms);

return ConstraintJ;
}
} //namespace QtPlugins
} //namespace Avogadro
70 changes: 70 additions & 0 deletions avogadro/qtplugins/constraints/constraint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <QtCore>
#include <QJsonObject>
#include <QList>

#include <avogadro/qtgui/molecule.h>
#include "constraintsmodel.h"

#ifndef CONSTRAINT_H
#define CONSTRAINT_H

namespace Avogadro {
namespace Core{
class Atom;
}
namespace QtPlugins {
class Constraint
{
public:
/*
Implementation of simple class for the represenation of constraints.

The ConstraintType has the following mapping (reflected in the ConstraintDialog combobox):

0: Ignore Atom
1: Fix Atom
2: Fix Atom X
3: Fix Atom Y
4: Fix Atom Z
5: Distance
6: Angle
7: Torsion

This implementation makes use of the UniqueID that is assigned to each Atom upon creation,
which can be used to unambigously retrieve the current index of the Atom in molecule.

This constraint representation has to be translated into package specific instructions when
passing it to whatever Optimizing/MD/QM code.

*/
explicit Constraint(int ConstraintType,
int AtomIdA,
int AtomIdB,
int AtomIdC,
int AtomIdD,
double ConstraintValue,
ConstraintsModel* model);
~Constraint();

void SetConstraintType(int ConstraintType);
void SetValue(double Value);

int GetConstraintType() const;
double GetConstraintValue() const;

const Index GetConstraintAtomA() const;
const Index GetConstraintAtomB() const;
const Index GetConstraintAtomC() const;
const Index GetConstraintAtomD() const;

QJsonObject toJson();

int ConstraintType;
double ConstraintValue;

QList<Index> Atoms;
ConstraintsModel* c_model = nullptr;
};
}
}
#endif
Loading