Commit 4570189f authored by Jason Swails's avatar Jason Swails
Browse files

Merge branch 'master' into fix-charmm-box-vecs

parents d127d7c2 26790496
// Copyright (C) 2002-2005 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef __IRR_TYPES_H_INCLUDED__
#define __IRR_TYPES_H_INCLUDED__
namespace irr
{
//! 8 bit unsigned variable.
/** This is a typedef for unsigned char, it ensures portability of the engine. */
typedef unsigned char u8;
//! 8 bit signed variable.
/** This is a typedef for signed char, it ensures portability of the engine. */
typedef signed char s8;
//! 8 bit character variable.
/** This is a typedef for char, it ensures portability of the engine. */
typedef char c8;
//! 16 bit unsigned variable.
/** This is a typedef for unsigned short, it ensures portability of the engine. */
typedef unsigned short u16;
//! 16 bit signed variable.
/** This is a typedef for signed short, it ensures portability of the engine. */
typedef signed short s16;
//! 32 bit unsigned variable.
/** This is a typedef for unsigned int, it ensures portability of the engine. */
typedef unsigned int u32;
//! 32 bit signed variable.
/** This is a typedef for signed int, it ensures portability of the engine. */
typedef signed int s32;
// 64 bit signed variable.
// This is a typedef for __int64, it ensures portability of the engine.
// This type is currently not used by the engine and not supported by compilers
// other than Microsoft Compilers, so it is outcommented.
//typedef __int64 s64;
//! 32 bit floating point variable.
/** This is a typedef for float, it ensures portability of the engine. */
typedef float f32;
//! 64 bit floating point variable.
/** This is a typedef for double, it ensures portability of the engine. */
typedef double f64;
} // end namespace
// define the wchar_t type if not already built in.
#ifdef _MSC_VER
#ifndef _WCHAR_T_DEFINED
//! A 16 bit wide character type.
/**
Defines the wchar_t-type.
In VS6, its not possible to tell
the standard compiler to treat wchar_t as a built-in type, and
sometimes we just don't want to include the huge stdlib.h or wchar.h,
so we'll use this.
*/
typedef unsigned short wchar_t;
#define _WCHAR_T_DEFINED
#endif // wchar is not defined
#endif // microsoft compiler
//! define a break macro for debugging only in Win32 mode.
#if defined(WIN32) && defined(_MSC_VER) && defined(_DEBUG)
#define _IRR_DEBUG_BREAK_IF( _CONDITION_ ) if (_CONDITION_) {_asm int 3}
#else
#define _IRR_DEBUG_BREAK_IF( _CONDITION_ )
#endif
//! Defines a small statement to work around a microsoft compiler bug.
/** The microsft compiler 7.0 - 7.1 has a bug:
When you call unmanaged code that returns a bool type value of false from managed code,
the return value may appear as true. See
http://support.microsoft.com/default.aspx?kbid=823071 for details.
Compiler version defines: VC6.0 : 1200, VC7.0 : 1300, VC7.1 : 1310, VC8.0 : 1400*/
#if defined(WIN32) && defined(_MSC_VER) && (_MSC_VER > 1299) && (_MSC_VER < 1400)
#define _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX __asm mov eax,100
#else
#define _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX
#endif // _IRR_MANAGED_MARSHALLING_BUGFIX
#endif // __IRR_TYPES_H_INCLUDED__
This diff is collapsed.
==========================================================================
irrXML 1.2
==========================================================================
Welcome to irrXML
Content of this file:
1. Directory structure overview
2. How to use
3. Requirements
5. License
6. Contact
==========================================================================
1. Directory structure overview
==========================================================================
You will find some directories after decompressing the archive in which
came the SDK. These are:
\doc Documentation of irrXML.
\example A short example showing how to use the parser with solution
and make files.
\src The source code of irrXML.
==========================================================================
2. How to use
==========================================================================
For Linux/Unix users: Simply go into the directory /example and run
'make'. This will create a sample project using irrXML.
Windows users: Just add the source files to your project. That's all.
For more information see the documentation in the \doc directory.
Alternatively, you could compile irrXML as .lib, after this you would
only need to use the irrXML.h header file, nothing more.
==========================================================================
3. Requirements
==========================================================================
You can use one of the following compilers/IDEs to develop applications
with irrXML. However, other compilers/IDEs make work as well,
we simply didn't test them.
* gcc 3.2
* gcc 3.3
* Visual Studio 6.0
* Visual Studio.NET (7.0)
* Visual Studio.NET 2003 (7.1)
* Visual Studio.NET 2005 (8.0)
* DevC++ 4.9 & gcc (project files included)
==========================================================================
5. License
==========================================================================
The license of irrXML is based on the zlib/libpng license.
Even though this license does not require you to mention that you are
using the Irrlicht Engine in your product, an acknowledgement
would be highly appreciated.
The irrXML License
===========================
Copyright (C) 2002-2005 Nikolaus Gebhardt
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
==========================================================================
6. Contact
==========================================================================
If you have problems, questions or suggestions, please visit the
official homepage of irrXML:
http://xml.irrlicht3d.org
You will find forums, patches, documentation, and other stuff
which will help you out.
If want to contact the author of the engine, please send an email to
Nikolaus Gebhardt:
irrlicht@users.sourceforge.net
\ No newline at end of file
// Copyright (C) 2002-2005 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine" and the "irrXML" project.
// For conditions of distribution and use, see copyright notice in irrlicht.h and/or irrXML.h
#include "irrXML.h"
#include "irrString.h"
#include "irrArray.h"
#include "fast_atof.h"
#include "CXMLReaderImpl.h"
namespace irr
{
namespace io
{
//! Implementation of the file read callback for ordinary files
class CFileReadCallBack : public IFileReadCallBack
{
public:
//! construct from filename
CFileReadCallBack(const char* filename)
: File(0), Size(0), Close(true)
{
// open file
File = fopen(filename, "rb");
if (File)
getFileSize();
}
//! construct from FILE pointer
CFileReadCallBack(FILE* file)
: File(file), Size(0), Close(false)
{
if (File)
getFileSize();
}
//! destructor
virtual ~CFileReadCallBack()
{
if (Close && File)
fclose(File);
}
//! Reads an amount of bytes from the file.
virtual int read(void* buffer, int sizeToRead)
{
if (!File)
return 0;
return (int)fread(buffer, 1, sizeToRead, File);
}
//! Returns size of file in bytes
virtual int getSize()
{
return Size;
}
private:
//! retrieves the file size of the open file
void getFileSize()
{
fseek(File, 0, SEEK_END);
Size = ftell(File);
fseek(File, 0, SEEK_SET);
}
FILE* File;
int Size;
bool Close;
}; // end class CFileReadCallBack
// FACTORY FUNCTIONS:
//! Creates an instance of an UFT-8 or ASCII character xml parser.
IrrXMLReader* createIrrXMLReader(const char* filename)
{
return new CXMLReaderImpl<char, IXMLBase>(new CFileReadCallBack(filename));
}
//! Creates an instance of an UFT-8 or ASCII character xml parser.
IrrXMLReader* createIrrXMLReader(FILE* file)
{
return new CXMLReaderImpl<char, IXMLBase>(new CFileReadCallBack(file));
}
//! Creates an instance of an UFT-8 or ASCII character xml parser.
IrrXMLReader* createIrrXMLReader(IFileReadCallBack* callback)
{
return new CXMLReaderImpl<char, IXMLBase>(callback, false);
}
//! Creates an instance of an UTF-16 xml parser.
IrrXMLReaderUTF16* createIrrXMLReaderUTF16(const char* filename)
{
return new CXMLReaderImpl<char16, IXMLBase>(new CFileReadCallBack(filename));
}
//! Creates an instance of an UTF-16 xml parser.
IrrXMLReaderUTF16* createIrrXMLReaderUTF16(FILE* file)
{
return new CXMLReaderImpl<char16, IXMLBase>(new CFileReadCallBack(file));
}
//! Creates an instance of an UTF-16 xml parser.
IrrXMLReaderUTF16* createIrrXMLReaderUTF16(IFileReadCallBack* callback)
{
return new CXMLReaderImpl<char16, IXMLBase>(callback, false);
}
//! Creates an instance of an UTF-32 xml parser.
IrrXMLReaderUTF32* createIrrXMLReaderUTF32(const char* filename)
{
return new CXMLReaderImpl<char32, IXMLBase>(new CFileReadCallBack(filename));
}
//! Creates an instance of an UTF-32 xml parser.
IrrXMLReaderUTF32* createIrrXMLReaderUTF32(FILE* file)
{
return new CXMLReaderImpl<char32, IXMLBase>(new CFileReadCallBack(file));
}
//! Creates an instance of an UTF-32 xml parser.
IrrXMLReaderUTF32* createIrrXMLReaderUTF32(IFileReadCallBack* callback)
{
return new CXMLReaderImpl<char32, IXMLBase>(callback, false);
}
} // end namespace io
} // end namespace irr
......@@ -9,7 +9,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2010-2014 Stanford University and the Authors. *
* Portions copyright (c) 2010-2015 Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
......@@ -38,8 +38,6 @@
#include "openmm/internal/windowsExport.h"
#include <iosfwd>
class TiXmlElement;
namespace OpenMM {
/**
......@@ -77,10 +75,10 @@ public:
return reinterpret_cast<T*>(deserializeStream(stream));
}
private:
class StreamReader;
static void serialize(const SerializationNode& node, std::ostream& stream);
static void* deserializeStream(std::istream& stream);
static void encodeNode(const SerializationNode& node, std::ostream& stream, int depth);
static void decodeNode(SerializationNode& node, const TiXmlElement& element);
};
} // namespace OpenMM
......
......@@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2010-2014 Stanford University and the Authors. *
* Portions copyright (c) 2010-2015 Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
......@@ -30,10 +30,81 @@
* -------------------------------------------------------------------------- */
#include "openmm/serialization/XmlSerializer.h"
#include "tinyxml.h"
#include "irrXML.h"
#include <cstring>
#include <iostream>
#include <map>
using namespace OpenMM;
using namespace std;
using namespace irr;
using namespace io;
/**
* Apply XML encoding to a string. This is adapted from TinyXML (written by Lee Thomason).
*/
static void encodeString(const string& str, string* outString) {
static map<char, string> entities;
static bool hasInitialized = false;
if (!hasInitialized) {
hasInitialized = true;
entities['&'] = "&amp;";
entities['<'] = "&lt;";
entities['>'] = "&gt;";
entities['\"'] = "&quot;";
entities['\''] = "&apos;";
}
int i=0;
while (i<(int)str.length()) {
unsigned char c = (unsigned char) str[i];
if (c == '&'
&& i < ((int)str.length() - 2)
&& str[i+1] == '#'
&& str[i+2] == 'x') {
// Hexadecimal character reference.
// Pass through unchanged.
// &#xA9; -- copyright symbol, for example.
//
// The -1 is a bug fix from Rob Laveaux. It keeps
// an overflow from happening if there is no ';'.
// There are actually 2 ways to exit this loop -
// while fails (error case) and break (semicolon found).
// However, there is no mechanism (currently) for
// this function to return an error.
while (i<(int)str.length()-1) {
outString->append(str.c_str() + i, 1);
++i;
if (str[i] == ';')
break;
}
}
else if (entities.find(c) != entities.end()) {
outString->append(entities[c]);
++i;
}
else if (c < 32) {
// Easy pass at non-alpha/numeric/symbol
// Below 32 is symbolic.
char buf[ 32 ];
snprintf(buf, sizeof(buf), "&#x%02X;", (unsigned) (c & 0xff));
//*ME: warning C4267: convert 'size_t' to 'int'
//*ME: Int-Cast to make compiler happy ...
outString->append(buf, (int)strlen(buf));
++i;
}
else {
//char realc = (char) c;
//outString->append(&realc, 1);
*outString += (char) c; // somewhat more efficient function call.
++i;
}
}
}
void XmlSerializer::serialize(const SerializationNode& node, std::ostream& stream) {
stream << "<?xml version=\"1.0\" ?>\n";
......@@ -47,8 +118,8 @@ void XmlSerializer::encodeNode(const SerializationNode& node, std::ostream& stre
const map<string, string>& properties = node.getProperties();
for (map<string, string>::const_iterator iter = properties.begin(); iter != properties.end(); ++iter) {
string name, value;
TiXmlBase::EncodeString(iter->first, &name);
TiXmlBase::EncodeString(iter->second, &value);
encodeString(iter->first, &name);
encodeString(iter->second, &value);
stream << ' ' << name << "=\"" << value << '\"';
}
const vector<SerializationNode>& children = node.getChildren();
......@@ -64,20 +135,64 @@ void XmlSerializer::encodeNode(const SerializationNode& node, std::ostream& stre
}
}
/**
* Adapter class to let irrXML read a C++ stream.
*/
class XmlSerializer::StreamReader : public IFileReadCallBack {
public:
StreamReader(std::istream& stream) : stream(stream) {
stream.seekg(0, ios_base::end);
size = stream.tellg();
stream.seekg(0);
}
int read(void* buffer, int sizeToRead) {
stream.read((char*) buffer, sizeToRead);
return stream.gcount();
}
int getSize() {
return size;
}
private:
std::istream& stream;
int size;
};
/**
* Process an XML node, storing its content into a SerializationNode.
*/
static void decodeNode(SerializationNode& node, IrrXMLReader& xml) {
for (int i = 0; i < xml.getAttributeCount(); i++)
node.setStringProperty(xml.getAttributeName(i), xml.getAttributeValue(i));
if (xml.isEmptyElement())
return;
while (xml.read()) {
switch (xml.getNodeType()) {
case EXN_ELEMENT:
{
SerializationNode& childNode = node.createChildNode(xml.getNodeName());
decodeNode(childNode, xml);
break;
}
case EXN_ELEMENT_END:
return;
}
}
}
void* XmlSerializer::deserializeStream(std::istream& stream) {
TiXmlDocument doc;
stream >> doc;
SerializationNode root;
decodeNode(root, *doc.FirstChildElement());
StreamReader reader(stream);
IrrXMLReader* xml = createIrrXMLReader(&reader);
// Find the root node in the file.
while (xml->read() && xml->getNodeType() != EXN_ELEMENT)
;
decodeNode(root, *xml);
delete xml;
// Process the SerializationNodes.
const SerializationProxy& proxy = SerializationProxy::getProxy(root.getStringProperty("type"));
return proxy.deserialize(root);
}
void XmlSerializer::decodeNode(SerializationNode& node, const TiXmlElement& element) {
for (const TiXmlAttribute* attribute = element.FirstAttribute(); attribute != NULL; attribute = attribute->Next())
node.setStringProperty(attribute->NameTStr(), attribute->ValueStr());
for (const TiXmlElement* child = element.FirstChildElement(); child != NULL; child = child->NextSiblingElement()) {
SerializationNode& childNode = node.createChildNode(child->ValueTStr());
decodeNode(childNode, *child);
}
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
/*
www.sourceforge.net/projects/tinyxml
Original code (2.0 and earlier )copyright (c) 2000-2006 Lee Thomason (www.grinninglizard.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "tinyxml.h"
// The goal of the seperate error file is to make the first
// step towards localization. tinyxml (currently) only supports
// english error messages, but the could now be translated.
//
// It also cleans up the code a bit.
//
const char* TiXmlBase::errorString[ TIXML_ERROR_STRING_COUNT ] =
{
"No error",
"Error",
"Failed to open file",
"Error parsing Element.",
"Failed to read Element name",
"Error reading Element value.",
"Error reading Attributes.",
"Error: empty tag.",
"Error reading end tag.",
"Error parsing Unknown.",
"Error parsing Comment.",
"Error parsing Declaration.",
"Error document empty.",
"Error null (0) or unexpected EOF found in input stream.",
"Error parsing CDATA.",
"Error when TiXmlDocument added to document, because TiXmlDocument can only be at the root.",
};
This diff is collapsed.
......@@ -54,10 +54,12 @@ foreach(SUBDIR ${SUBDIRS})
"${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/*.sh"
"${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/*.xml"
"${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/*.pdb"
"${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/*.pdbx"
"${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/*.prmtop"
"${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/*.prm"
"${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/*.inpcrd"
"${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/*.crd"
"${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/*.gro"
"${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/*.parm7"
"${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/*.rst7"
"${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/*.ncrst"
......
......@@ -77,14 +77,17 @@ class DCDFile(object):
header += struct.pack('<4i', 164, 4, len(list(topology.atoms())), 4)
file.write(header)
def writeModel(self, positions, unitCellDimensions=None):
def writeModel(self, positions, unitCellDimensions=None, periodicBoxVectors=None):
"""Write out a model to the DCD file.
The periodic box can be specified either by the unit cell dimensions (for a rectangular box), or the full set of box
vectors (for an arbitrary triclinic box). If neither is specified, the box vectors specified in the Topology will be
used. Regardless of the value specified, no dimensions will be written if the Topology does not represent a periodic system.
Parameters:
- positions (list) The list of atomic positions to write
- unitCellDimensions (Vec3=None) The dimensions of the crystallographic unit cell. If None, the dimensions specified in
the Topology will be used. Regardless of the value specified, no dimensions will be written if the Topology does not
represent a periodic system.
- unitCellDimensions (Vec3=None) The dimensions of the crystallographic unit cell.
- periodicBoxVectors (tuple of Vec3=None) The vectors defining the periodic box.
"""
if len(list(self._topology.atoms())) != len(positions):
raise ValueError('The number of positions must match the number of atoms')
......@@ -107,12 +110,22 @@ class DCDFile(object):
# Write the data.
file.seek(0, os.SEEK_END)
boxSize = self._topology.getUnitCellDimensions()
if boxSize is not None:
if unitCellDimensions is not None:
boxSize = unitCellDimensions
size = boxSize.value_in_unit(angstroms)
file.write(struct.pack('<i6di', 48, size[0], 0, size[1], 0, 0, size[2], 48))
boxVectors = self._topology.getPeriodicBoxVectors()
if boxVectors is not None:
if getPeriodicBoxVectors is not None:
boxVectors = getPeriodicBoxVectors
elif unitCellDimensions is not None:
if is_quantity(unitCellDimensions):
unitCellDimensions = unitCellDimensions.value_in_unit(nanometers)
boxVectors = (Vec3(unitCellDimensions[0], 0, 0), Vec3(0, unitCellDimensions[1], 0), Vec3(0, 0, unitCellDimensions[2]))*nanometers
(a_length, b_length, c_length, alpha, beta, gamma) = computeLengthsAndAngles(boxVectors)
a_length = a_length.value_in_unit(angstroms)
b_length = b_length.value_in_unit(angstroms)
c_length = c_length.value_in_unit(angstroms)
angle1 = math.sin(math.pi/2-gamma)
angle2 = math.sin(math.pi/2-beta)
angle3 = math.sin(math.pi/2-alpha)
file.write(struct.pack('<i6di', 48, a_length, angle1, b_length, angle2, angle3, c_length, 48))
length = struct.pack('<i', 4*len(positions))
for i in range(3):
file.write(length)
......
......@@ -6,7 +6,7 @@ Simbios, the NIH National Center for Physics-Based Simulation of
Biological Structures at Stanford, funded under the NIH Roadmap for
Medical Research, grant U54 GM072970. See https://simtk.org.
Portions copyright (c) 2012-2014 Stanford University and the Authors.
Portions copyright (c) 2012-2015 Stanford University and the Authors.
Authors: Peter Eastman, Mark Friedrichs
Contributors:
......@@ -403,9 +403,9 @@ class ForceField(object):
# Set periodic boundary conditions.
boxSize = topology.getUnitCellDimensions()
if boxSize is not None:
sys.setDefaultPeriodicBoxVectors((boxSize[0], 0, 0), (0, boxSize[1], 0), (0, 0, boxSize[2]))
boxVectors = topology.getPeriodicBoxVectors()
if boxVectors is not None:
sys.setDefaultPeriodicBoxVectors(boxVectors[0], boxVectors[1], boxVectors[2])
elif nonbondedMethod is not NoCutoff and nonbondedMethod is not CutoffNonPeriodic:
raise ValueError('Requested periodic boundary conditions for a Topology that does not specify periodic box dimensions')
......
......@@ -6,7 +6,7 @@ Simbios, the NIH National Center for Physics-Based Simulation of
Biological Structures at Stanford, funded under the NIH Roadmap for
Medical Research, grant U54 GM072970. See https://simtk.org.
Portions copyright (c) 2012 Stanford University and the Authors.
Portions copyright (c) 2012-2015 Stanford University and the Authors.
Authors: Lee-Ping Wang, Peter Eastman
Contributors:
......@@ -89,6 +89,18 @@ def _is_gro_box(line):
else:
return 0
def _construct_box_vectors(line):
"""Create the periodic box vectors based on the values stored in the file.
@param[in] line The line containing the description
"""
sline = line.split()
values = [float(i) for i in sline]
if len(sline) == 3:
return (Vec3(values[0], 0, 0), Vec3(0, values[1], 0), Vec3(0, 0, values[2]))*nanometers
return (Vec3(values[0], values[3], values[4]), Vec3(values[5], values[1], values[6]), Vec3(values[7], values[8], values[2]))*nanometers
class GromacsGroFile(object):
"""GromacsGroFile parses a Gromacs .gro file and constructs a set of atom positions from it.
......@@ -140,7 +152,7 @@ class GromacsGroFile(object):
xyz.append(Vec3(pos[0], pos[1], pos[2]))
elif _is_gro_box(line) and ln == na + 2:
sline = line.split()
boxes.append(tuple([float(i) for i in sline])*nanometers)
boxes.append(_construct_box_vectors(line))
xyzs.append(xyz*nanometers)
xyz = []
ln = -1
......@@ -160,7 +172,7 @@ class GromacsGroFile(object):
## A list containing the name of the residue that each atom belongs to
self.residueNames = resname
self._positions = xyzs
self._unitCellDimensions = boxes
self._periodicBoxVectors = boxes
self._numpyPositions = None
def getNumFrames(self):
......@@ -182,10 +194,21 @@ class GromacsGroFile(object):
return self._numpyPositions[frame]
return self._positions[frame]
def getPeriodicBoxVectors(self, frame=0):
"""Get the vectors defining the periodic box.
Parameters:
- frame (int=0) the index of the frame for which to get the box vectors
"""
return self._periodicBoxVectors[frame]
def getUnitCellDimensions(self, frame=0):
"""Get the dimensions of the crystallographic unit cell.
Parameters:
- frame (int=0) the index of the frame for which to get the unit cell dimensions
"""
return self._unitCellDimensions[frame]
xsize = self._periodicBoxVectors[frame][0][0].value_in_unit(nanometers)
ysize = self._periodicBoxVectors[frame][1][1].value_in_unit(nanometers)
zsize = self._periodicBoxVectors[frame][2][2].value_in_unit(nanometers)
return Vec3(xsize, ysize, zsize)*nanometers
......@@ -6,7 +6,7 @@ Simbios, the NIH National Center for Physics-Based Simulation of
Biological Structures at Stanford, funded under the NIH Roadmap for
Medical Research, grant U54 GM072970. See https://simtk.org.
Portions copyright (c) 2012-2014 Stanford University and the Authors.
Portions copyright (c) 2012-2015 Stanford University and the Authors.
Authors: Peter Eastman
Contributors:
......@@ -358,12 +358,14 @@ class GromacsTopFile(object):
raise ValueError('Unsupported function type in [ cmaptypes ] line: '+line);
self._cmapTypes[tuple(fields[:5])] = fields
def __init__(self, file, unitCellDimensions=None, includeDir=None, defines=None):
def __init__(self, file, periodicBoxVectors=None, unitCellDimensions=None, includeDir=None, defines=None):
"""Load a top file.
Parameters:
- file (string) the name of the file to load
- unitCellDimensions (Vec3=None) the dimensions of the crystallographic unit cell
- periodicBoxVectors (tuple of Vec3=None) the vectors defining the periodic box
- unitCellDimensions (Vec3=None) the dimensions of the crystallographic unit cell. For
non-rectangular unit cells, specify periodicBoxVectors instead.
- includeDir (string=None) A directory in which to look for other files
included from the top file. If not specified, we will attempt to locate a gromacs
installation on your system. When gromacs is installed in /usr/local, this will resolve
......@@ -403,7 +405,12 @@ class GromacsTopFile(object):
top = Topology()
## The Topology read from the prmtop file
self.topology = top
top.setUnitCellDimensions(unitCellDimensions)
if periodicBoxVectors is not None:
if unitCellDimensions is not None:
raise ValueError("specify either periodicBoxVectors or unitCellDimensions, but not both")
top.setPeriodicBoxVectors(periodicBoxVectors)
else:
top.setUnitCellDimensions(unitCellDimensions)
PDBFile._loadNameReplacementTables()
for moleculeName, moleculeCount in self._molecules:
if moleculeName not in self._moleculeTypes:
......
......@@ -8,7 +8,7 @@ Simbios, the NIH National Center for Physics-Based Simulation of
Biological Structures at Stanford, funded under the NIH Roadmap for
Medical Research, grant U54 GM072970. See https://simtk.org.
Portions copyright (c) 2012-2013 Stanford University and the Authors.
Portions copyright (c) 2012-2015 Stanford University and the Authors.
Authors: Christopher M. Bruns
Contributors: Peter Eastman
......@@ -37,8 +37,10 @@ __version__ = "1.0"
from simtk.openmm.vec3 import Vec3
import simtk.unit as unit
from .. import element
from unitcell import computePeriodicBoxVectors
import warnings
import sys
import math
class PdbStructure(object):
"""
......@@ -137,7 +139,7 @@ class PdbStructure(object):
self._current_model = None
self.default_model = None
self.models_by_number = {}
self._unit_cell_dimensions = None
self._periodic_box_vectors = None
self.sequences = []
self.modified_residues = []
# read file
......@@ -170,7 +172,13 @@ class PdbStructure(object):
self._current_model._current_chain._add_ter_record()
self._reset_residue_numbers()
elif (pdb_line.find("CRYST1") == 0):
self._unit_cell_dimensions = Vec3(float(pdb_line[6:15]), float(pdb_line[15:24]), float(pdb_line[24:33]))*unit.angstroms
a_length = float(pdb_line[6:15])*0.1
b_length = float(pdb_line[15:24])*0.1
c_length = float(pdb_line[24:33])*0.1
alpha = float(pdb_line[33:40])*math.pi/180.0
beta = float(pdb_line[40:47])*math.pi/180.0
gamma = float(pdb_line[47:54])*math.pi/180.0
self._periodic_box_vectors = computePeriodicBoxVectors(a_length, b_length, c_length, alpha, beta, gamma)
elif (pdb_line.find("CONECT") == 0):
atoms = [int(pdb_line[6:11])]
for pos in (11,16,21,26):
......@@ -187,7 +195,7 @@ class PdbStructure(object):
elif (pdb_line.find("MODRES") == 0):
self.modified_residues.append(ModifiedResidue(pdb_line[16], int(pdb_line[18:22]), pdb_line[12:15].strip(), pdb_line[24:27].strip()))
self._finalize()
def _reset_atom_numbers(self):
self._atom_numbers_are_hex = False
self._next_atom_number = 1
......@@ -283,9 +291,9 @@ class PdbStructure(object):
for model in self.models:
model._finalize()
def get_unit_cell_dimensions(self):
"""Get the dimensions of the crystallographic unit cell (may be None)."""
return self._unit_cell_dimensions
def get_periodic_box_vectors(self):
"""Get the vectors defining the crystallographic unit cell (may be None)."""
return self._periodic_box_vectors
class Sequence(object):
......
"""
unitcell.py: Routines for converting between different representations of the periodic unit cell.
This is part of the OpenMM molecular simulation toolkit originating from
Simbios, the NIH National Center for Physics-Based Simulation of
Biological Structures at Stanford, funded under the NIH Roadmap for
Medical Research, grant U54 GM072970. See https://simtk.org.
Portions copyright (c) 2015 Stanford University and the Authors.
Authors: Peter Eastman
Contributors:
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
__author__ = "Peter Eastman"
__version__ = "1.0"
from simtk.openmm import Vec3
from simtk.unit import nanometers, is_quantity, norm, dot
import math
def computePeriodicBoxVectors(a_length, b_length, c_length, alpha, beta, gamma):
"""Convert lengths and angles to periodic box vectors.
Lengths should be given in nanometers and angles in radians.
"""
# Compute the vectors.
a = [a_length, 0, 0]
b = [b_length*math.cos(gamma), b_length*math.sin(gamma), 0]
cx = c_length*math.cos(beta)
cy = c_length*(math.cos(alpha)-math.cos(beta)*math.cos(gamma))/math.sin(gamma)
cz = math.sqrt(c_length*c_length-cx*cx-cy*cy)
c = [cx, cy, cz]
# If any elements are very close to 0, set them to exactly 0.
for i in range(3):
if abs(a[i]) < 1e-6:
a[i] = 0.0
if abs(b[i]) < 1e-6:
b[i] = 0.0
if abs(c[i]) < 1e-6:
c[i] = 0.0
a = Vec3(*a)
b = Vec3(*b)
c = Vec3(*c)
# Make sure they're in the reduced form required by OpenMM.
c = c - b*round(c[1]/b[1])
c = c - a*round(c[0]/a[0])
b = b - a*round(b[0]/a[0])
return (a, b, c)*nanometers
def computeLengthsAndAngles(periodicBoxVectors):
"""Convert periodic box vectors to lengths and angles.
Lengths are returned in nanometers and angles in radians.
"""
(a, b, c) = vectors.value_in_unit(nanometers)
a_length = norm(a)
b_length = norm(b)
c_length = norm(c)
alpha = math.acos(dot(b, c)/(b_length*c_length))
beta = math.acos(dot(c, a)/(c_length*a_length))
gamma = math.acos(dot(a, b)/(a_length*b_length))
return (a_length, b_length, c_length, alpha, beta, gamma)
......@@ -6,7 +6,7 @@ Simbios, the NIH National Center for Physics-Based Simulation of
Biological Structures at Stanford, funded under the NIH Roadmap for
Medical Research, grant U54 GM072970. See https://simtk.org.
Portions copyright (c) 2012-2013 Stanford University and the Authors.
Portions copyright (c) 2012-2015 Stanford University and the Authors.
Authors: Peter Eastman
Contributors:
......@@ -94,7 +94,7 @@ class Modeller(object):
# Copy over the existing model.
newTopology = Topology()
newTopology.setUnitCellDimensions(deepcopy(self.topology.getUnitCellDimensions()))
newTopology.setPeriodicBoxVectors(self.topology.getPeriodicBoxVectors())
newAtoms = {}
newPositions = []*nanometer
for chain in self.topology.chains():
......@@ -140,7 +140,7 @@ class Modeller(object):
- toDelete (list) a list of Atoms, Residues, Chains, and bonds (specified as tuples of Atoms) to delete
"""
newTopology = Topology()
newTopology.setUnitCellDimensions(deepcopy(self.topology.getUnitCellDimensions()))
newTopology.setPeriodicBoxVectors(self.topology.getPeriodicBoxVectors())
newAtoms = {}
newPositions = []*nanometer
deleteSet = set(toDelete)
......@@ -189,7 +189,7 @@ class Modeller(object):
else:
raise ValueError('Unknown water model: %s' % model)
newTopology = Topology()
newTopology.setUnitCellDimensions(deepcopy(self.topology.getUnitCellDimensions()))
newTopology.setPeriodicBoxVectors(self.topology.getPeriodicBoxVectors())
newAtoms = {}
newPositions = []*nanometer
for chain in self.topology.chains():
......@@ -240,7 +240,7 @@ class Modeller(object):
self.topology = newTopology
self.positions = newPositions
def addSolvent(self, forcefield, model='tip3p', boxSize=None, padding=None, positiveIon='Na+', negativeIon='Cl-', ionicStrength=0*molar):
def addSolvent(self, forcefield, model='tip3p', boxSize=None, boxVectors=None, padding=None, positiveIon='Na+', negativeIon='Cl-', ionicStrength=0*molar):
"""Add solvent (both water and ions) to the model to fill a rectangular box.
The algorithm works as follows:
......@@ -250,15 +250,17 @@ class Modeller(object):
randomly selecting a water molecule and replacing it with the ion.
4. Ion pairs are added to give the requested total ionic strength.
The box size can be specified in three ways. First, you can explicitly give a box size to use. Alternatively, you can
The box size can be specified in four ways. First, you can explicitly give the vectors defining the periodic box to
use. Alternatively, for a rectangular box you can simply give the dimensions of the unit cell. Third, you can
give a padding distance. The largest dimension of the solute (along the x, y, or z axis) is determined, and a cubic
box of size (largest dimension)+2*padding is used. Finally, if neither a box size nor a padding distance is specified,
the existing Topology's unit cell dimensions are used.
box of size (largest dimension)+2*padding is used. Finally, if neither box vectors, box size, nor padding distance is specified,
the existing Topology's box vectors are used.
Parameters:
- forcefield (ForceField) the ForceField to use for determining van der Waals radii and atomic charges
- model (string='tip3p') the water model to use. Supported values are 'tip3p', 'spce', 'tip4pew', and 'tip5p'.
- boxSize (Vec3=None) the size of the box to fill with water
- boxVectors (tuple of Vec3=None) the vectors defining the periodic box to fill with water
- padding (distance=None) the padding distance to use
- positiveIon (string='Na+') the type of positive ion to add. Allowed values are 'Cs+', 'K+', 'Li+', 'Na+', and 'Rb+'
- negativeIon (string='Cl-') the type of negative ion to add. Allowed values are 'Cl-', 'Br-', 'F-', and 'I-'. Be aware
......@@ -268,18 +270,28 @@ class Modeller(object):
"""
# Pick a unit cell size.
if boxSize is not None:
if boxVectors is not None:
if is_quantity(boxVectors[0]):
boxVectors = (boxVectors[0].value_in_unit(nanometer), boxVectors[1].value_in_unit(nanometer), boxVectors[2].value_in_unit(nanometer))
box = Vec3(boxVectors[0][0], boxVectors[1][1], boxVectors[2][2])
vectors = boxVectors
elif boxSize is not None:
if is_quantity(boxSize):
boxSize = boxSize.value_in_unit(nanometer)
box = Vec3(boxSize[0], boxSize[1], boxSize[2])*nanometer
box = Vec3(boxSize[0], boxSize[1], boxSize[2])
vectors = (Vec3(boxSize[0], 0, 0), Vec3(0, boxSize[1], 0), Vec3(0, 0, boxSize[2]))
elif padding is not None:
if is_quantity(padding):
padding = padding.value_in_unit(nanometer)
maxSize = max(max((pos[i] for pos in self.positions))-min((pos[i] for pos in self.positions)) for i in range(3))
maxSize = maxSize.value_in_unit(nanometer)
box = (maxSize+2*padding)*Vec3(1, 1, 1)
vectors = (Vec3(maxSize+2*padding, 0, 0), Vec3(0, maxSize+2*padding, 0), Vec3(0, 0, maxSize+2*padding))
else:
box = self.topology.getUnitCellDimensions()
box = self.topology.getUnitCellDimensions().value_in_unit(nanometer)
vectors = self.topology.getPeriodicBoxVectors().value_in_unit(nanometer)
if box is None:
raise ValueError('Neither the box size nor padding was specified, and the Topology does not define unit cell dimensions')
box = box.value_in_unit(nanometer)
raise ValueError('Neither the box size, box vectors, nor padding was specified, and the Topology does not define unit cell dimensions')
invBox = Vec3(1.0/box[0], 1.0/box[1], 1.0/box[2])
# Identify the ion types.
......@@ -331,7 +343,7 @@ class Modeller(object):
# Copy the solute over.
newTopology = Topology()
newTopology.setUnitCellDimensions(box)
newTopology.setPeriodicBoxVectors(vectors*nanometer)
newAtoms = {}
newPositions = []*nanometer
for chain in self.topology.chains():
......@@ -378,7 +390,9 @@ class Modeller(object):
def periodicDistance(pos1, pos2):
delta = pos1-pos2
delta = [delta[i]-floor(delta[i]*invBox[i]+0.5)*box[i] for i in range(3)]
delta -= vectors[2]*floor(delta[2]*invBox[2]+0.5)
delta -= vectors[1]*floor(delta[1]*invBox[1]+0.5)
delta -= vectors[0]*floor(delta[0]*invBox[0]+0.5)
return norm(delta)
# Find the list of water molecules to add.
......@@ -472,7 +486,6 @@ class Modeller(object):
for atom2 in molAtoms:
if atom2.element == elem.hydrogen:
newTopology.addBond(atom1, atom2)
newTopology.setUnitCellDimensions(deepcopy(box)*nanometer)
self.topology = newTopology
self.positions = newPositions
......@@ -610,7 +623,7 @@ class Modeller(object):
# Loop over residues.
newTopology = Topology()
newTopology.setUnitCellDimensions(deepcopy(self.topology.getUnitCellDimensions()))
newTopology.setPeriodicBoxVectors(self.topology.getPeriodicBoxVectors())
newAtoms = {}
newPositions = []*nanometer
newIndices = []
......@@ -883,7 +896,7 @@ class Modeller(object):
# Create the new Topology.
newTopology = Topology()
newTopology.setUnitCellDimensions(deepcopy(self.topology.getUnitCellDimensions()))
newTopology.setPeriodicBoxVectors(self.topology.getPeriodicBoxVectors())
newAtoms = {}
newPositions = []*nanometer
for chain in self.topology.chains():
......
......@@ -6,7 +6,7 @@ Simbios, the NIH National Center for Physics-Based Simulation of
Biological Structures at Stanford, funded under the NIH Roadmap for
Medical Research, grant U54 GM072970. See https://simtk.org.
Portions copyright (c) 2012 Stanford University and the Authors.
Portions copyright (c) 2012-2015 Stanford University and the Authors.
Authors: Peter Eastman
Contributors:
......@@ -40,7 +40,7 @@ from datetime import date
from simtk.openmm import Vec3, Platform
from simtk.openmm.app.internal.pdbstructure import PdbStructure
from simtk.openmm.app import Topology
from simtk.unit import nanometers, angstroms, is_quantity, norm, Quantity
from simtk.unit import nanometers, angstroms, is_quantity, norm, Quantity, dot
import element as elem
try:
import numpy
......@@ -142,7 +142,7 @@ class PDBFile(object):
self._positions.append(coords*nanometers)
## The atom positions read from the PDB file. If the file contains multiple frames, these are the positions in the first frame.
self.positions = self._positions[0]
self.topology.setUnitCellDimensions(pdb.get_unit_cell_dimensions())
self.topology.setPeriodicBoxVectors(pdb.get_periodic_box_vectors())
self.topology.createStandardBonds()
self.topology.createDisulfideBonds(self.positions)
self._numpyPositions = None
......@@ -250,10 +250,16 @@ class PDBFile(object):
- file (file=stdout) A file to write the file to
"""
print >>file, "REMARK 1 CREATED WITH OPENMM %s, %s" % (Platform.getOpenMMVersion(), str(date.today()))
boxSize = topology.getUnitCellDimensions()
if boxSize is not None:
size = boxSize.value_in_unit(angstroms)
print >>file, "CRYST1%9.3f%9.3f%9.3f 90.00 90.00 90.00 P 1 1 " % size
vectors = topology.getPeriodicBoxVectors()
if vectors is not None:
(a, b, c) = vectors.value_in_unit(angstroms)
a_length = norm(a)
b_length = norm(b)
c_length = norm(c)
alpha = math.acos(dot(b, c)/(b_length*c_length))*180.0/math.pi
beta = math.acos(dot(c, a)/(c_length*a_length))*180.0/math.pi
gamma = math.acos(dot(a, b)/(a_length*b_length))*180.0/math.pi
print >>file, "CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f P 1 1 " % (a_length, b_length, c_length, alpha, beta, gamma)
@staticmethod
def writeModel(topology, positions, file=sys.stdout, modelIndex=None):
......
......@@ -6,7 +6,7 @@ Simbios, the NIH National Center for Physics-Based Simulation of
Biological Structures at Stanford, funded under the NIH Roadmap for
Medical Research, grant U54 GM072970. See https://simtk.org.
Portions copyright (c) 2014 Stanford University and the Authors.
Portions copyright (c) 2014-2015 Stanford University and the Authors.
Authors: Peter Eastman
Contributors:
......@@ -33,8 +33,10 @@ __version__ = "1.0"
import os
import sys
import math
from simtk.openmm import Vec3
from simtk.openmm.app.internal.pdbx.reader.PdbxReader import PdbxReader
from simtk.openmm.app.internal.unitcell import computePeriodicBoxVectors
from simtk.openmm.app import Topology
from simtk.unit import nanometers, angstroms, is_quantity, norm, Quantity
import element as elem
......@@ -145,8 +147,9 @@ class PDBxFile(object):
cell = block.getObj('cell')
if cell is not None and cell.getRowCount() > 0:
row = cell.getRow(0)
cellSize = [float(row[cell.getAttributeIndex(attribute)]) for attribute in ('length_a', 'length_b', 'length_c')]*angstroms
self.topology.setUnitCellDimensions(cellSize)
(a, b, c) = [float(row[cell.getAttributeIndex(attribute)])*0.1 for attribute in ('length_a', 'length_b', 'length_c')]
(alpha, beta, gamma) = [float(row[cell.getAttributeIndex(attribute)])*math.pi/180.0 for attribute in ('angle_alpha', 'angle_beta', 'angle_gamma')]
self.topology.setPeriodicBoxVectors(computePeriodicBoxVectors(a, b, c, alpha, beta, gamma))
# Add bonds based on struct_conn records.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment