NAMD
Measure.C
Go to the documentation of this file.
1 
7 #include "InfoStream.h"
8 #include "Measure.h"
9 #include "Node.h"
10 #include "Parameters.h"
11 #include "Molecule.h"
12 
13 #ifdef NAMD_TCL
14 
15 int Measure::wrapCommand(ClientData clientData,
16  Tcl_Interp *interp, int argc, const char *argv[]) {
17 
18 /*
19  double (wraped *)(Vector*, Molecule*, Parameters*);
20  wraped =
21  Node *node = Node::Object();
22  Molecule *molecule = node->molecule;
23  Parameters *parameters = node->parameters;
24  Vector *coordinates = node->coords;
25 
26  double result = *wraped(coordinates,molecule,parameters);
27 
28 */
29  return TCL_OK;
30 }
31 
32 static int Tcl_centerOfNumber(ClientData, Tcl_Interp *interp, int argc, const char *argv[]) {
33 
34  Node *node = Node::Object();
35  Molecule *molecule = node->molecule;
36  // Parameters *parameters = node->parameters;
37  Vector *coordinates = node->coords;
38  int numAtoms = molecule->numAtoms;
39 
40  int number = 0;
41  Vector center = 0;
42  for( int i = 0; i < numAtoms; ++i ) {
43  number += 1;
44  center += coordinates[i];
45  }
46  center /= number;
47 
48  char s[1024];
49  sprintf(s,"%g %g %g", center.x, center.y, center.z);
50  Tcl_SetResult(interp,s,TCL_VOLATILE);
51 
52  return TCL_OK;
53 }
54 
55 static int Tcl_centerOfMass(ClientData, Tcl_Interp *interp, int argc, const char *argv[]) {
56 
57  Node *node = Node::Object();
58  Molecule *molecule = node->molecule;
59  // Parameters *parameters = node->parameters;
60  Vector *coordinates = node->coords;
61  int numAtoms = molecule->numAtoms;
62 
63  Vector center = 0;
64  BigReal totalMass = 0;
65  for( int i = 0; i < numAtoms; ++i ) {
66  BigReal mass = molecule->atommass(i);
67  totalMass += mass;
68  center += mass * coordinates[i];
69  }
70  center /= totalMass;
71 
72  char s[1024];
73  sprintf(s,"%g %g %g", center.x, center.y, center.z);
74  Tcl_SetResult(interp,s,TCL_VOLATILE);
75 
76  return TCL_OK;
77 }
78 
79 static int Tcl_radiusOfGyration(ClientData, Tcl_Interp *interp, int argc, const char *argv[]) {
80 
81  Node *node = Node::Object();
82  Molecule *molecule = node->molecule;
83  // Parameters *parameters = node->parameters;
84  Vector *coordinates = node->coords;
85  int numAtoms = molecule->numAtoms;
86 
87  Vector center = 0;
88  BigReal totalMass = 0;
89  int i;
90  for( i = 0; i < numAtoms; ++i ) {
91  BigReal mass = molecule->atommass(i);
92  totalMass += mass;
93  center += mass * coordinates[i];
94  }
95  center /= totalMass;
96 
97  BigReal moment = 0;
98  for( i = 0; i < numAtoms; ++i ) {
99  BigReal mass = molecule->atommass(i);
100  moment += mass * (coordinates[i] - center).length2();
101  }
102  BigReal radius = sqrt(moment/totalMass);
103 
104  char s[1024];
105  sprintf(s,"%g", radius);
106  Tcl_SetResult(interp,s,TCL_VOLATILE);
107 
108  return TCL_OK;
109 }
110 
111 static int Tcl_loadCoords(ClientData, Tcl_Interp *interp, int objc, Tcl_Obj * const objv[]) {
112 
113  if (objc < 2 || objc > 3) {
114  Tcl_SetResult(interp,(char*)"loadCoords: wrong # args",TCL_VOLATILE);
115  return TCL_ERROR;
116  }
117  Tcl_Obj * const vname = objv[1];
118  Node *node = Node::Object();
119  Molecule *molecule = node->molecule;
120  // Parameters *parameters = node->parameters;
121  const Vector *coords = node->coords;
122  int numAtoms = molecule->numAtoms;
123 
124  if (objc == 2) {
125  // get all the coordinates
126  for (int i=0; i<numAtoms; i++) {
127  Tcl_Obj *newlist = Tcl_NewListObj(0, NULL);
128  Tcl_Obj *arrkey = Tcl_NewIntObj(i+1);
129  Tcl_IncrRefCount(arrkey);
130  Tcl_ListObjAppendElement(interp, newlist,
131  Tcl_NewDoubleObj(coords[i].x));
132  Tcl_ListObjAppendElement(interp, newlist,
133  Tcl_NewDoubleObj(coords[i].y));
134  Tcl_ListObjAppendElement(interp, newlist,
135  Tcl_NewDoubleObj(coords[i].z));
136  if (!Tcl_ObjSetVar2(interp, vname, arrkey, newlist, 0))
137  return TCL_ERROR;
138  Tcl_DecrRefCount(arrkey);
139  }
140  } else {
141  // third argument must be a list of indices
142  int nelems;
143  Tcl_Obj **elems;
144  if (Tcl_ListObjGetElements(interp, objv[2], &nelems, &elems) != TCL_OK) {
145  return TCL_ERROR;
146  }
147  for (int i=0; i<nelems; i++) {
148  int ind;
149  if (Tcl_GetIntFromObj(interp, elems[i], &ind) != TCL_OK)
150  return TCL_ERROR;
151  Tcl_Obj *newlist = Tcl_NewListObj(0, NULL);
152  Tcl_Obj *arrkey = Tcl_NewIntObj(ind);
153  Tcl_IncrRefCount(arrkey);
154  --ind;
155  Tcl_ListObjAppendElement(interp, newlist,
156  Tcl_NewDoubleObj(coords[ind].x));
157  Tcl_ListObjAppendElement(interp, newlist,
158  Tcl_NewDoubleObj(coords[ind].y));
159  Tcl_ListObjAppendElement(interp, newlist,
160  Tcl_NewDoubleObj(coords[ind].z));
161  if (!Tcl_ObjSetVar2(interp, vname, arrkey, newlist, 0))
162  return TCL_ERROR;
163  Tcl_DecrRefCount(arrkey);
164  }
165  }
166  return TCL_OK;
167 }
168 
169 void Measure::createCommands(Tcl_Interp *interp) {
170  Tcl_CreateCommand(interp, "centerOfNumber", Tcl_centerOfNumber,
171  (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
172  Tcl_CreateCommand(interp, "centerOfMass", Tcl_centerOfMass,
173  (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
174  Tcl_CreateCommand(interp, "radiusOfGyration", Tcl_radiusOfGyration,
175  (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
176  Tcl_CreateObjCommand(interp, "loadCoords", Tcl_loadCoords,
177  (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
178 }
179 
180 void Measure::deleteCommands(Tcl_Interp *interp) {
181  Tcl_DeleteCommand(interp, "centerOfNumber");
182  Tcl_DeleteCommand(interp, "centerOfMass");
183  Tcl_DeleteCommand(interp, "radiusOfGyration");
184  Tcl_DeleteCommand(interp, "loadCoords");
185 }
186 
187 #endif
188 
static Node * Object()
Definition: Node.h:86
Definition: Node.h:78
Definition: Vector.h:64
static int Tcl_centerOfNumber(ClientData, Tcl_Interp *interp, int argc, const char *argv[])
Definition: Measure.C:32
BigReal z
Definition: Vector.h:66
static int Tcl_radiusOfGyration(ClientData, Tcl_Interp *interp, int argc, const char *argv[])
Definition: Measure.C:79
gridSize z
BigReal x
Definition: Vector.h:66
int numAtoms
Definition: Molecule.h:556
Vector * coords
Definition: Node.h:185
BigReal y
Definition: Vector.h:66
static void createCommands(Tcl_Interp *)
Definition: Measure.C:169
gridSize y
Real atommass(int anum) const
Definition: Molecule.h:1038
static int Tcl_loadCoords(ClientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
Definition: Measure.C:111
gridSize x
static void deleteCommands(Tcl_Interp *)
Definition: Measure.C:180
Molecule * molecule
Definition: Node.h:176
static float * coords
Definition: ScriptTcl.C:66
double BigReal
Definition: common.h:112
static int Tcl_centerOfMass(ClientData, Tcl_Interp *interp, int argc, const char *argv[])
Definition: Measure.C:55