3# program that builds water boxes
5# author = "Chris Fennell
6# copyright = "Copyright (c) 2004-present The University of Notre Dame. All Rights Reserved."
10use List::Util qw[min max];
14$mass = 2.99151E-23; # mass of H2O in grams
15$cm3ToAng3 = 1E24; # convert cm^3 to angstroms^3
16$densityConvert = $mass*$cm3ToAng3;
28getopts('hmrvd:l:c:o:w:x:y:z:');
31$opt_h = "true" if $#ARGV >= 0;
35 print "waterBoxer: builds water boxes\n\n";
36 print "usage: waterBoxer [-hmrv] [-d density] [-l lattice] [-n # waters]\n";
37 print "\t[-o file name] [-w water name] \n\n";
38 print " -h : show this message\n";
39 print " -m : print out a water.inc file (file with all water models)\n";
40 print " -r : randomize orientations\n";
41 print " -v : verbose output\n\n";
42 print " -d real : density in g/cm^3\n";
43 print " (default: 1)\n";
44 print " -l integer : 0 - face centered cubic, 1 - simple cubic\n";
45 print " (default: 0)\n";
48 print " -c real : default overlap cutoff in angstroms\n";
49 print " (default: 3.3)\n";
50 print " -o char : output file name\n";
51 print " (default: freshWater.omd)\n";
52 print " -w char : name of the water stunt double\n";
53 print " (default: SPCE)\n";
54 print " -x real : dimension of the box along the x direction\n";
55 print " -y real : dimension of the box along the y direction\n";
56 print " -z real : dimension of the box along the z direction\n\n";
57 print "Note: you can only use values of x, y, or z that are smaller\n";
58 print " than the derived box length for a given density and\n";
59 print " number of molecules.\n\n";
61 die " waterBoxer -d 0.997 -w SSD_RF -o ssdrfWater.omd\n";
64# set some variables to be used in the code
69 $fileName = 'freshWater.omd';
72 die "Error: $fileName cannot be \"water.inc\"\n Please choose a different name\n" if $fileName eq 'water.inc';
73 $waterFileHandle = 'WATERMD';
76 $waterFileHandle = 'OUTFILE';
79 $doRandomize = $opt_r;
85 if ($opt_d =~ /^[0-9]/) {
88 die "Error: the value for '-d' ($opt_d) is not a valid number\n Please choose a positive real # value\n";
93 if ($opt_c =~ /^[0-9]/) {
96 die "Error: the value for '-c' ($opt_c) is not a valid number\n Please choose a positive real # value\n";
102 $nothingSelected = 0;
107if ($invalidWater == 1){
108 print "Warning: \'$waterName\' is not a recognized water model name.\n";
109 print " Use the \'-m\' option to generate a \'water.inc\' with the\n";
110 print " recognized water model geometries.\n\n";
112if ($waterName eq 'DPD') {
113 # DPD waters are stand-ins for 4 water molecules
114 $density = $density * 0.25;
116if ($waterName eq 'CG2') {
117 # CG2 waters are stand-ins for 2 water molecules
118 $density = $density * 0.5;
122 $nothingSelected = 0;
123 if ($opt_l =~ /^[0-9]/) {
125 if ($lattice != 0 && $lattice != 1){
126 die "Error: the '-l' value ($opt_l) is not a valid number\n Please choose 0 or 1\n";
129 die "Error: the '-l' value ($opt_l) is not a valid number\n Please choose 0 or 1\n";
133 $nothingSelected = 0;
134 if ($opt_x =~ /^[0-9]/) {
137 die "Error: the value for '-x' ($opt_x) is not a valid number\n Please choose a positive real # value\n";
140 die "Error: the size of the box '-x' must be set\n";
143 $nothingSelected = 0;
144 if ($opt_y =~ /^[0-9]/) {
147 die "Error: the value for '-y' ($opt_y) is not a valid number\n Please choose a positive real # value\n";
150 die "Error: the size of the box '-y' must be set\n";
153 $nothingSelected = 0;
154 if ($opt_z =~ /^[0-9]/) {
157 die "Error: the value for '-z' ($opt_z) is not a valid number\n Please choose a positive real # value\n";
160 die "Error: the size of the box '-z' must be set\n";
163if ($nothingSelected == 1) {
164 die "(For help, use the \'-h\' option.)\n";
167# open the file writer
168open(OUTFILE, ">./$fileName") || die "Error: can't open file $fileName\n";
170# check to set magic lattice numbers
172 $a = (4*18.01/(0.602*$density))**(1.0/3.0);
173 $acut = $rcut * sqrt(2.0);
176 $newDensity = 4.0*18.01/(0.602*$a**3);
177 print "using density of $newDensity to match cutoff value ($rcut)\n";
179} elsif ($lattice == 1){
180 $a = (18.01/(0.602*$density))**(1.0/3.0);
184 $newDensity = 18.01/(0.602*$a**3);
185 print "using density of $newDensity to match cutoff value ($rcut)\n";
189$nx = int($nxFloat + $nxFloat/abs($nxFloat*2));
191$ny = int($nyFloat + $nyFloat/abs($nyFloat*2));
193$nz = int($nzFloat + $nzFloat/abs($nzFloat*2));
195$anew = min($boxx/$nx, $boxy/$ny, $boxz/$nz);
201$nxFloat = $boxx/$anew;
202$nx = int($nxFloat + $nxFloat/abs($nxFloat*2));
203$nyFloat = $boxy/$anew;
204$ny = int($nyFloat + $nyFloat/abs($nyFloat*2));
205$nzFloat = $boxz/$anew;
206$nz = int($nzFloat + $nzFloat/abs($nzFloat*2));
209 $nMol = 4 * $nx * $ny * $nz;
211 $nMol = $nx * $ny * $nz;
214$newDensity = $nMol * $densityConvert / ($boxx*$boxy*$boxz);
216if (abs($newDensity-$density) > $tolerance) {
217 print "Resetting density to $newDensity to make chosen box sides work out\n";
219$cellLengthX = $boxx/$nx;
220$cellLengthY = $boxy/$ny;
221$cellLengthZ = $boxz/$nz;
223$cell2X = $cellLengthX*0.5;
224$cell2Y = $cellLengthY*0.5;
225$cell2Z = $cellLengthZ*0.5;
245# assemble the lattice
247 for ($z = 0; $z < $nz; $z++) {
248 for ($y = 0; $y < $ny; $y++) {
249 for ($x = 0; $x < $nx; $x++) {
250 for ($uc = 0; $uc < 4; $uc++) {
251 $xCorr[$uc+$counter] = $xCorr[$uc] + $cellLengthX*$x;
252 $yCorr[$uc+$counter] = $yCorr[$uc] + $cellLengthY*$y;
253 $zCorr[$uc+$counter] = $zCorr[$uc] + $cellLengthZ*$z;
255 $counter = $counter + 4;
260} elsif ($lattice == 1) {
261 # build the unit cell
268 for ($z = 0; $z < $nz; $z++) {
269 for ($y = 0; $y < $ny; $y++) {
270 for ($x = 0; $x < $nx; $x++) {
271 $xCorr[$counter] = $xCorr[0] + $cellLengthX*$x;
272 $yCorr[$counter] = $yCorr[0] + $cellLengthY*$y;
273 $zCorr[$counter] = $zCorr[0] + $cellLengthZ*$z;
281print "The water box \"$fileName\" was generated.\n";
285 print "The file \"water.inc\" was generated for inclusion in \"$fileName\"\n";
290# this marks the end of the main program, below is subroutines
294 my $ret = atan2(sqrt(1 - $rad*$rad), $rad);
299 # write out the header
300 print OUTFILE "<OpenMD version=1>\n";
304 print OUTFILE " <StuntDoubles>\n";
306 # shift the box center to the origin and write out the coordinates
307 for ($i = 0; $i < $nMol; $i++) {
308 $xCorr[$i] -= 0.5*$boxx;
309 $yCorr[$i] -= 0.5*$boxy;
310 $zCorr[$i] -= 0.5*$boxz;
317 if ($doRandomize == 1){
318 $cosTheta = 2.0*rand() - 1.0;
319 $theta = acos($cosTheta);
320 $phi = 2.0*3.14159265359*rand();
321 $psi = 2.0*3.14159265359*rand();
323 $q0 = cos(0.5*$theta)*cos(0.5*($phi + $psi));
324 $q1 = sin(0.5*$theta)*cos(0.5*($phi - $psi));
325 $q2 = sin(0.5*$theta)*sin(0.5*($phi - $psi));
326 $q3 = cos(0.5*$theta)*sin(0.5*($phi + $psi));
329 print OUTFILE "$i\tpq\t$xCorr[$i] $yCorr[$i] $zCorr[$i] ";
330 print OUTFILE "$q0 $q1 $q2 $q3\n";
333 print OUTFILE " </StuntDoubles>\n </Snapshot>\n</OpenMD>\n";
337 print OUTFILE " <MetaData>\n";
339# print the water model or includes
341 print OUTFILE "#include \"water.inc\"";
345 printFakeWater() if $invalidWater == 1;
347# now back to the metaData output
348 print OUTFILE "\n\ncomponent{
349 type = \"$waterName\";
354forceField = \"Water\";
355electrostaticSummationMethod = \"shifted_force\";
356electrostaticScreeningMethod = \"damped\";
357cutoffRadius = $cutoff;
358switchingRadius = $cutoff;
359dampingAlpha = $alpha;
386 $boxLength2 = 0.5*$bm;
387 if ($boxLength2 > $cutoff){
388 # the default is good
390 $cutoff = int($boxLength2);
399 Hmat: {{ $boxx, 0, 0 }, { 0, $boxy, 0 }, { 0, 0, $boxz }}
404 open(WATERMD, ">./water.inc") || die "Error: can't open file water.inc\n";
405 $waterFileHandle = 'WATERMD';
407 print WATERMD "#ifndef _WATER_INC_\n#define _WATER_INC_\n";
434 print WATERMD "\n\n#endif";
438 print $waterFileHandle "\n\nmolecule{
443 position(0.0, 0.0, 0.0);
449 print $waterFileHandle "\n\nmolecule{
454 position(0.0, 0.0, 0.0);
460 print $waterFileHandle "\n\nmolecule{
465 position( 0.0, 0.0, 0.0 );
466 orientation( 0.0, 0.0, 0.0 );
472 print $waterFileHandle "\n\nmolecule{
477 position( 0.0, 0.0, 0.0 );
478 orientation( 0.0, 0.0, 0.0 );
484 print $waterFileHandle "\n\nmolecule{
489 position( 0.0, 0.0, 0.0 );
490 orientation( 0.0, 0.0, 0.0 );
496 print $waterFileHandle "\n\nmolecule{
501 position( 0.0, 0.0, 0.0 );
502 orientation( 0.0, 0.0, 0.0 );
508 print $waterFileHandle "\n\nmolecule{
513 position( 0.0, 0.0, 0.0 );
514 orientation( 0.0, 0.0, 0.0 );
520 print $waterFileHandle "\n\nmolecule{
525 position( 0.0, 0.0, 0.0 );
526 orientation( 0.0, 0.0, 0.0 );
532 print $waterFileHandle "\n\nmolecule{
537 position( 0.0, 0.0, 0.0 );
538 orientation( 0.0, 0.0, 0.0 );
542 position( 0.0, 0.0, 0.5 );
553 print $waterFileHandle "\n\nmolecule{
558 position( 0.0, 0.0, -0.06556 );
562 position( 0.0, 0.75695, 0.52032 );
566 position( 0.0, -0.75695, 0.52032 );
577 print $waterFileHandle "\n\nmolecule{
582 position( 0.0, 0.0, -0.06556 );
586 position( 0.0, 0.75695, 0.52032 );
590 position( 0.0, -0.75695, 0.52032 );
594 position( 0.0, 0.0, 0.08444 );
605 print $waterFileHandle "\n\nmolecule{
606 name = \"TIP4P-Ice\";
609 type = \"O_TIP4P-Ice\";
610 position( 0.0, 0.0, -0.06556 );
613 type = \"H_TIP4P-Ice\";
614 position( 0.0, 0.75695, 0.52032 );
617 type = \"H_TIP4P-Ice\";
618 position( 0.0, -0.75695, 0.52032 );
621 type = \"EP_TIP4P-Ice\";
622 position( 0.0, 0.0, 0.09214 );
633 print $waterFileHandle "\n\nmolecule{
637 type = \"O_TIP4P-Ew\";
638 position( 0.0, 0.0, -0.06556 );
641 type = \"H_TIP4P-Ew\";
642 position( 0.0, 0.75695, 0.52032 );
645 type = \"H_TIP4P-Ew\";
646 position( 0.0, -0.75695, 0.52032 );
649 type = \"EP_TIP4P-Ew\";
650 position( 0.0, 0.0, 0.05944 );
661 print $waterFileHandle "\n\nmolecule{
662 name = \"TIP4P-2005\";
665 type = \"O_TIP4P-2005\";
666 position( 0.0, 0.0, -0.06556 );
669 type = \"H_TIP4P-2005\";
670 position( 0.0, 0.75695, 0.52032 );
673 type = \"H_TIP4P-2005\";
674 position( 0.0, -0.75695, 0.52032 );
677 type = \"EP_TIP4P-2005\";
678 position( 0.0, 0.0, 0.08904 );
689 print $waterFileHandle "\n\nmolecule{
694 position( 0.0, 0.0, -0.06556 );
698 position( 0.0, 0.75695, 0.52032 );
702 position( 0.0, -0.75695, 0.52032 );
706 position( 0.57154, 0.0, -0.46971 );
710 position( -0.57154, 0.0, -0.46971 );
714 members(0, 1, 2, 3, 4);
721 print $waterFileHandle "\n\nmolecule{
725 type = \"O_TIP5P-E\";
726 position( 0.0, 0.0, -0.06556 );
730 position( 0.0, 0.75695, 0.52032 );
734 position( 0.0, -0.75695, 0.52032 );
738 position( 0.57154, 0.0, -0.46971 );
742 position( -0.57154, 0.0, -0.46971 );
746 members(0, 1, 2, 3, 4);
753 print $waterFileHandle "\n\nmolecule{
758 position( 0.0, 0.0, 0.0 );
762 position( 0.0, 0.576029, 0.79283665 );
766 position( 0.0, -0.576029, 0.79283665 );
770 position( 0.0, 0.23, 0.0 );
774 position( 0.732813007, -0.50364843, 0.0 );
778 position( -0.732813007, -0.50364843, 0.0 );
782 members(0, 1, 2, 3, 4, 5);
788print $waterFileHandle "\n\nmolecule{
793 position( 0.0, 0.0, -0.06461 );
797 position( 0.0, 0.81649, 0.51275 );
801 position( 0.0, -0.81649, 0.51275 );
812print $waterFileHandle "\n\nmolecule{
817 position( 0.0, 0.0, -0.06461 );
821 position( 0.0, 0.81649, 0.51275 );
825 position( 0.0, -0.81649, 0.51275 );
836print $waterFileHandle "\n\nmolecule{
841 position( 0.0, 0.0, -0.0603651 );
845 position( 0.0, 0.685582, 0.479134 );
849 position( 0.0, -0.685582, 0.479134 );
853 position( 0.0, 0.0, 0.0990349 );
863print $waterFileHandle "\n\nmolecule{
868 position( 0.0, 0.0, -0.0632382 );
872 position( 0.0, 0.799262, 0.501939 );
876 position( 0.0, -0.799262, 0.501939 );
886print $waterFileHandle "\n\nmolecule{
890 type = \"O_TIP3P-FB\";
891 position( 0.0, 0.0, -0.066424 );
894 type = \"H_TIP3P-FB\";
895 position( 0.0, 0.819341, 0.527225 );
898 type = \"H_TIP3P-FB\";
899 position( 0.0, -0.819341, 0.527225 );
909print $waterFileHandle "\n\nmolecule{
913 type = \"O_TIP4P-FB\";
914 position( 0.0, 0.0, -0.0655549 );
917 type = \"H_TIP4P-FB\";
918 position( 0.0, 0.75695, 0.520327 );
921 type = \"H_TIP4P-FB\";
922 position( 0.0, -0.75695, 0.520327 );
925 type = \"EP_TIP4P-FB\";
926 position( 0.0, 0.0, 0.0397151 );
936print $waterFileHandle "\n\nmolecule{
941 position( 0.0, 0.0, -0.06461 );
945 position( 0.0, 0.81649, 0.51275 );
949 position( 0.0, -0.81649, 0.51275 );
960 print $waterFileHandle "\n\nmolecule{
965 position(0.0, 0.0, 0.0);
971 print $waterFileHandle "\n\nmolecule{
976 position(0.0, 0.0, 0.0);
982 print $waterFileHandle "\n\nmolecule{
983 name = \"$waterName\";
986 type = \"$waterName\";
987 position(0.0, 0.0, 0.0);
994 if ($waterName eq 'Cl-') { $waterCase = 0; }
995 elsif ($waterName eq 'Na+') { $waterCase = 1; }
996 elsif ($waterName eq 'SSD_E') { $waterCase = 2; }
997 elsif ($waterName eq 'SSD_RF') { $waterCase = 3; }
998 elsif ($waterName eq 'SSD') { $waterCase = 4; }
999 elsif ($waterName eq 'SSD1') { $waterCase = 5; }
1000 elsif ($waterName eq 'TIP3P') { $waterCase = 6; }
1001 elsif ($waterName eq 'TIP4P') { $waterCase = 7; }
1002 elsif ($waterName eq 'TIP4P-Ew') { $waterCase = 8; }
1003 elsif ($waterName eq 'TIP5P') { $waterCase = 9; }
1004 elsif ($waterName eq 'TIP5P-E') { $waterCase = 10; }
1005 elsif ($waterName eq 'SPCE') { $waterCase = 11; }
1006 elsif ($waterName eq 'SPC') { $waterCase = 12; }
1007 elsif ($waterName eq 'DPD') { $waterCase = 13; }
1008 elsif ($waterName eq 'CG2') { $waterCase = 14; }
1009 elsif ($waterName eq 'SSDQ') { $waterCase = 15; }
1010 elsif ($waterName eq 'SSDQO') { $waterCase = 16; }
1011 elsif ($waterName eq 'TIP4P-Ice'){ $waterCase = 17; }
1012 elsif ($waterName eq 'TIP4P-2005'){ $switchCase = 18; }
1013 elsif ($waterName eq 'SPC-HW') { $waterCase == 19; }
1014 elsif ($waterName eq 'NE6') { $waterCase == 20; }
1015 elsif ($waterName eq 'TIP3P-FB') { $waterCase == 21; }
1016 elsif ($waterName eq 'TIP4P-FB') { $waterCase == 22; }
1017 elsif ($waterName eq 'OPC') { $waterCase == 23; }
1018 elsif ($waterName eq 'OPC3') { $waterCase == 24; }
1019 else { $invalidWater = 1; }
1022sub printWaterModel {
1023 if ($waterCase == 0) { printCl(); }
1024 elsif ($waterCase == 1) { printNa(); }
1025 elsif ($waterCase == 2) { printSSD_E(); }
1026 elsif ($waterCase == 3) { printSSD_RF(); }
1027 elsif ($waterCase == 4) { printSSD(); }
1028 elsif ($waterCase == 5) { printSSD1(); }
1029 elsif ($waterCase == 6) { printTIP3P(); }
1030 elsif ($waterCase == 7) { printTIP4P(); }
1031 elsif ($waterCase == 8) { printTIP4PEw(); }
1032 elsif ($waterCase == 9) { printTIP5P(); }
1033 elsif ($waterCase == 10) { printTIP5PE(); }
1034 elsif ($waterCase == 11) { printSPCE(); }
1035 elsif ($waterCase == 12) { printSPC(); }
1036 elsif ($waterCase == 13) { printDPD(); }
1037 elsif ($waterCase == 14) { printCG2(); }
1038 elsif ($waterCase == 15) { printSSDQ(); }
1039 elsif ($waterCase == 16) { printSSDQO(); }
1040 elsif ($waterCase == 17) { printTIP4PIce();}
1041 elsif ($waterCase == 18) { printTIP4P2005();}
1042 elsif ($waterCase == 19) { printSPCHW(); }
1043 elsif ($waterCase == 20) { printNE6(); }
1044 elsif ($waterCase == 21) { printTIP3PFB(); }
1045 elsif ($waterCase == 22) { printTIP4PFB(); }
1046 elsif ($waterCase == 23) { printOPC(); }
1047 elsif ($waterCase == 24) { printOPC3(); }