Changeset 3ce105 for molecuilder/src/builder.cpp
- Timestamp:
- May 21, 2008, 11:15:32 AM (17 years ago)
- Children:
- a6e314
- Parents:
- 927aba
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
molecuilder/src/builder.cpp
r927aba r3ce105 258 258 void CenterAtoms(molecule *mol) 259 259 { 260 vector x ;260 vector x, y; 261 261 char choice; // menu choice char 262 double min[3];263 int j;264 262 265 263 cout << Verbose(0) << "===========CENTER ATOMS=========================" << endl; … … 267 265 cout << Verbose(0) << " b - on center of gravity" << endl; 268 266 cout << Verbose(0) << " c - within box with additional boundary" << endl; 267 cout << Verbose(0) << " d - within given simulation box" << endl; 269 268 cout << Verbose(0) << "all else - go back" << endl; 270 269 cout << Verbose(0) << "===============================================" << endl; … … 288 287 for (int i=0;i<3;i++) { 289 288 cout << Verbose(0) << "Enter axis " << i << " boundary: "; 290 cin >> min[i];289 cin >> y.x[i]; 291 290 } 292 291 mol->CenterEdge((ofstream *)&cout, &x); // make every coordinate positive 293 mol->SetBoxDimension(&x); // update Box of atoms by boundary 294 // translate each coordinate by boundary 295 j=-1; 296 for (int i=0;i<NDIM;i++) { 297 j += i+1; 298 x.x[i] = min[i]; 299 mol->cell_size[j] += x.x[i]*2.; 292 mol->Translate(&y); // translate by boundary 293 mol->SetBoxDimension(&(x+y*2)); // update Box of atoms by boundary 294 break; 295 case 'd': 296 cout << Verbose(1) << "Centering atoms in config file within given simulation box." << endl; 297 for (int i=0;i<3;i++) { 298 cout << Verbose(0) << "Enter axis " << i << " boundary: "; 299 cin >> x.x[i]; 300 300 } 301 mol->Translate(&x); 301 // center 302 mol->CenterInBox((ofstream *)&cout, &x); 303 // update Box of atoms by boundary 304 mol->SetBoxDimension(&x); 302 305 break; 303 306 } … … 696 699 }; 697 700 698 /********************************************** Main routine **************************************/ 699 700 int main(int argc, char **argv) 701 /** Parses the command line options. 702 * \param argc argument count 703 * \param **argv arguments array 704 * \param *mol molecule structure 705 * \param *periode elements structure 706 * \param configuration config file structure 707 * \param *ConfigFileName pointer to config file name in **argv 708 * \param *ElementsFileName pointer to elements db file name in **argv 709 * \return exit code (0 - successful, all else - something's wrong) 710 */ 711 int ParseCommandLineOptions(int argc, char **argv, molecule *&mol, periodentafel *&periode, config& configuration, char *&ConfigFileName, char *&ElementsFileName) 701 712 { 702 periodentafel *periode = new periodentafel; // and a period table of all elements703 molecule *mol = new molecule(periode); // first we need an empty molecule704 config configuration;705 double tmp1;706 double bond, min_bond;707 atom *first, *second;708 713 element *finder; 709 char choice; // menu choice char710 714 vector x,y,z,n; // coordinates for absolute point in cell volume 711 double *factor; // unit factor if desired 712 bool valid; // flag if input was valid or not 715 double *factor; // unit factor if desired 713 716 ifstream test; 714 717 ofstream output; 715 718 string line; 716 char filename[MAXSTRINGSIZE]; 717 char *ConfigFileName = NULL; 718 char *ElementsFileName = NULL; 719 atom *first; 719 720 int ExitFlag = 0; 720 int Z; 721 int j, axis, count, faktor; 722 int *MinimumRingSize = NULL; 721 int j; 723 722 enum ConfigStatus config_present = absent; 724 MoleculeLeafClass *Subgraphs = NULL;725 723 clock_t start,end; 726 element **Elements;727 vector **Vectors;728 724 int argptr; 729 730 // =========================== PARSE COMMAND LINE OPTIONS ==================================== 725 731 726 if (argc > 1) { // config file specified as option 732 727 // 1. : Parse options that just set variables or print help 733 734 735 728 argptr = 1; 729 do { 730 if (argv[argptr][0] == '-') { 736 731 cout << Verbose(0) << "Recognized command line argument: " << argv[argptr][1] << ".\n"; 737 732 argptr++; … … 744 739 cout << "or simply " << argv[0] << " without arguments for interactive session." << endl; 745 740 cout << "\t-a Z x1 x2 x3\tAdd new atom of element Z at coordinates (x1,x2,x3)." << endl; 741 cout << "\t-b x1 x2 x3\tCenter atoms in domain with given edge lengths of (x1,x2,x3)." << endl; 746 742 cout << "\t-c x1 x2 x3\tCenter atoms in domain with a minimum distance to boundary of (x1,x2,x3)." << endl; 747 743 cout << "\t-e <file>\tSets the element database to be parsed from this file (default: elements.db in same dir as " << argv[0] << ")." << endl; … … 756 752 delete(mol); 757 753 delete(periode); 758 return ( 0);754 return (1); 759 755 break; 760 756 case 'v': … … 764 760 delete(mol); 765 761 delete(periode); 766 return ( 0);762 return (1); 767 763 break; 768 764 case 'e': … … 775 771 break; 776 772 } 777 778 773 } else 774 argptr++; 779 775 } while (argptr < argc); 780 781 776 777 // 2. Parse the element database 782 778 if (periode->LoadPeriodentafel(ElementsFileName)) 783 779 cout << Verbose(0) << "Element list loaded successfully." << endl; … … 785 781 cout << Verbose(0) << "Element list loading failed." << endl; 786 782 787 783 // 3. Find config file name and parse if possible 788 784 if (argv[1][0] != '-') { 789 785 cout << Verbose(0) << "Config file given." << endl; … … 827 823 argptr = 1; 828 824 do { 825 cout << "Current Command line argument: " << argv[argptr] << "." << endl; 829 826 if (argv[argptr][0] == '-') { 830 827 argptr++; … … 834 831 ExitFlag = 1; 835 832 cout << Verbose(1) << "Parsing xyz file for new atoms." << endl; 836 if (!mol->AddXYZFile(argv[argptr ++]))833 if (!mol->AddXYZFile(argv[argptr])) 837 834 cout << Verbose(2) << "File not found." << endl; 838 835 else … … 844 841 } 845 842 } 846 if (config_present != empty) { 847 if (config_present == present) { 848 switch(argv[argptr-1][1]) { 849 case 't': 850 ExitFlag = 1; 851 cout << Verbose(1) << "Translating all ions to new origin." << endl; 852 for (int i=0;i<3;i++) 853 x.x[i] = atof(argv[argptr+i]); 854 mol->Translate((const vector *)&x); 855 argptr+=3; 856 break; 857 case 'a': 858 ExitFlag = 1; 859 cout << Verbose(1) << "Adding new atom." << endl; 860 first = new atom; 861 for (int i=0;i<3;i++) 862 first->x.x[i] = atof(argv[argptr+1+i]); 863 finder = periode->start; 864 while (finder != periode->end) { 865 finder = finder->next; 866 if (strncmp(finder->symbol,argv[argptr+1],3) == 0) { 867 first->type = finder; 868 break; 869 } 843 if (config_present == present) { 844 switch(argv[argptr-1][1]) { 845 case 't': 846 ExitFlag = 1; 847 cout << Verbose(1) << "Translating all ions to new origin." << endl; 848 for (int i=0;i<3;i++) 849 x.x[i] = atof(argv[argptr+i]); 850 mol->Translate((const vector *)&x); 851 argptr+=3; 852 break; 853 case 'a': 854 ExitFlag = 1; 855 cout << Verbose(1) << "Adding new atom." << endl; 856 first = new atom; 857 for (int i=0;i<3;i++) 858 first->x.x[i] = atof(argv[argptr+1+i]); 859 finder = periode->start; 860 while (finder != periode->end) { 861 finder = finder->next; 862 if (strncmp(finder->symbol,argv[argptr+1],3) == 0) { 863 first->type = finder; 864 break; 870 865 } 871 mol->AddAtom(first); // add to molecule 872 argptr+=4; 873 break; 874 case 's': 875 ExitFlag = 1; 876 j = -1; 877 cout << Verbose(1) << "Scaling all ion positions by factor." << endl; 878 factor = (double *) Malloc(sizeof(double)*NDIM, "main: *factor"); 879 factor[0] = atof(argv[argptr]); 880 if (argc > argptr+1) 881 argptr++; 882 factor[1] = atof(argv[argptr]); 883 if (argc > argptr+1) 884 argptr++; 885 factor[2] = atof(argv[argptr]); 886 mol->Scale(&factor); 887 for (int i=0;i<3;i++) { 888 j += i+1; 889 x.x[i] = atof(argv[3+i]); 890 mol->cell_size[j]*=factor[i]; 866 } 867 mol->AddAtom(first); // add to molecule 868 argptr+=4; 869 break; 870 case 's': 871 ExitFlag = 1; 872 j = -1; 873 cout << Verbose(1) << "Scaling all ion positions by factor." << endl; 874 factor = (double *) Malloc(sizeof(double)*NDIM, "main: *factor"); 875 factor[0] = atof(argv[argptr]); 876 if (argc > argptr+1) 877 argptr++; 878 factor[1] = atof(argv[argptr]); 879 if (argc > argptr+1) 880 argptr++; 881 factor[2] = atof(argv[argptr]); 882 mol->Scale(&factor); 883 for (int i=0;i<3;i++) { 884 j += i+1; 885 x.x[i] = atof(argv[3+i]); 886 mol->cell_size[j]*=factor[i]; 887 } 888 Free((void **)&factor, "main: *factor"); 889 argptr+=1; 890 break; 891 case 'b': 892 ExitFlag = 1; 893 j = -1; 894 cout << Verbose(1) << "Centering atoms in config file within given simulation box." << endl; 895 j=-1; 896 for (int i=0;i<3;i++) { 897 j += i+1; 898 x.x[i] = atof(argv[argptr++]); 899 mol->cell_size[j] += x.x[i]*2.; 900 } 901 // center 902 mol->CenterInBox((ofstream *)&cout, &x); 903 // update Box of atoms by boundary 904 mol->SetBoxDimension(&x); 905 break; 906 case 'c': 907 ExitFlag = 1; 908 j = -1; 909 cout << Verbose(1) << "Centering atoms in config file within given additional boundary." << endl; 910 // make every coordinate positive 911 mol->CenterEdge((ofstream *)&cout, &x); 912 // update Box of atoms by boundary 913 mol->SetBoxDimension(&x); 914 // translate each coordinate by boundary 915 j=-1; 916 for (int i=0;i<3;i++) { 917 j += i+1; 918 x.x[i] = atof(argv[argptr++]); 919 mol->cell_size[j] += x.x[i]*2.; 920 } 921 mol->Translate((const vector *)&x); 922 break; 923 case 'r': 924 ExitFlag = 1; 925 cout << Verbose(1) << "Converting config file from supposed old to new syntax." << endl; 926 break; 927 case 'f': 928 if (ExitFlag ==0) ExitFlag = 2; // only set if not already by other command line switch 929 cout << "Fragmenting molecule with bond distance " << argv[argptr] << " angstroem, order of " << argv[argptr+1] << "." << endl; 930 if (argc >= argptr+2) { 931 cout << Verbose(0) << "Creating connection matrix..." << endl; 932 start = clock(); 933 mol->CreateAdjacencyList((ofstream *)&cout, atof(argv[argptr++])); 934 cout << Verbose(0) << "Fragmenting molecule with current connection matrix ..." << endl; 935 if (mol->first->next != mol->last) { 936 mol->FragmentMolecule((ofstream *)&cout, atoi(argv[argptr]), &configuration); 891 937 } 892 Free((void **)&factor, "main: *factor"); 938 end = clock(); 939 cout << Verbose(0) << "Clocks for this operation: " << (end-start) << ", time: " << ((double)(end-start)/CLOCKS_PER_SEC) << "s." << endl; 893 940 argptr+=1; 894 break; 895 case 'c': 896 ExitFlag = 1; 897 j = -1; 898 cout << Verbose(1) << "Centering atoms in config file within given additional boundary." << endl; 899 // make every coordinate positive 900 mol->CenterEdge((ofstream *)&cout, &x); 901 // update Box of atoms by boundary 902 mol->SetBoxDimension(&x); 903 // translate each coordinate by boundary 904 j=-1; 905 for (int i=0;i<3;i++) { 906 j += i+1; 907 x.x[i] = atof(argv[argptr++]); 908 mol->cell_size[j] += x.x[i]*2.; 909 } 910 mol->Translate((const vector *)&x); 911 break; 912 case 'r': 913 ExitFlag = 1; 914 cout << Verbose(1) << "Converting config file from supposed old to new syntax." << endl; 915 break; 916 case 'f': 917 if (ExitFlag ==0) ExitFlag = 2; // only set if not already by other command line switch 918 cout << "Fragmenting molecule with bond distance " << argv[argptr] << " angstroem, order of " << argv[argptr+1] << "." << endl; 919 if (argc >= argptr+2) { 920 cout << Verbose(0) << "Creating connection matrix..." << endl; 921 start = clock(); 922 mol->CreateAdjacencyList((ofstream *)&cout, atof(argv[argptr++])); 923 cout << Verbose(0) << "Fragmenting molecule with current connection matrix ..." << endl; 924 if (mol->first->next != mol->last) { 925 mol->FragmentMolecule((ofstream *)&cout, atoi(argv[argptr]), &configuration); 926 } 927 end = clock(); 928 cout << Verbose(0) << "Clocks for this operation: " << (end-start) << ", time: " << ((double)(end-start)/CLOCKS_PER_SEC) << "s." << endl; 929 argptr+=1; 930 } else { 931 cerr << "Not enough arguments for fragmentation: -f <max. bond distance> <bond order>" << endl; 932 } 933 break; 934 default: // no match? Step on 935 argptr++; 936 break; 937 } 941 } else { 942 cerr << "Not enough arguments for fragmentation: -f <max. bond distance> <bond order>" << endl; 943 } 944 break; 945 default: // no match? Step on 946 argptr++; 947 break; 938 948 } 939 949 } 940 941 950 } else argptr++; 951 } while (argptr < argc); 942 952 if (ExitFlag == 1) // 1 means save and exit 943 953 SaveConfig(ConfigFileName, &configuration, periode, mol); … … 954 964 configuration.RetrieveConfigPathAndName("main_pcp_linux"); 955 965 } 956 966 return(0); 967 }; 968 969 /********************************************** Main routine **************************************/ 970 971 int main(int argc, char **argv) 972 { 973 periodentafel *periode = new periodentafel; // and a period table of all elements 974 molecule *mol = new molecule(periode); // first we need an empty molecule 975 config configuration; 976 double tmp1; 977 double bond, min_bond; 978 atom *first, *second; 979 char choice; // menu choice char 980 vector x,y,z,n; // coordinates for absolute point in cell volume 981 double *factor; // unit factor if desired 982 bool valid; // flag if input was valid or not 983 ifstream test; 984 ofstream output; 985 string line; 986 char filename[MAXSTRINGSIZE]; 987 char *ConfigFileName = NULL; 988 char *ElementsFileName = NULL; 989 int Z; 990 int j, axis, count, faktor; 991 int *MinimumRingSize = NULL; 992 MoleculeLeafClass *Subgraphs = NULL; 993 clock_t start,end; 994 element **Elements; 995 vector **Vectors; 996 997 // =========================== PARSE COMMAND LINE OPTIONS ==================================== 998 j = ParseCommandLineOptions(argc, argv, mol, periode, configuration, ConfigFileName, ElementsFileName); 999 if (j) return j; // something went wrong 957 1000 958 1001 // General stuff … … 1003 1046 switch (choice) { 1004 1047 default: 1005 case 'q': // quit1006 break;1007 1008 1048 case 'a': // add atom 1009 1049 AddAtoms(periode, mol); … … 1011 1051 break; 1012 1052 1053 case 'b': // scale a bond 1054 cout << Verbose(0) << "Scaling bond length between two atoms." << endl; 1055 first = mol->AskAtom("Enter first (fixed) atom: "); 1056 second = mol->AskAtom("Enter second (shifting) atom: "); 1057 min_bond = 0.; 1058 for (int i=0;i<3;i++) 1059 min_bond += (first->x.x[i]-second->x.x[i])*(first->x.x[i] - second->x.x[i]); 1060 min_bond = sqrt(min_bond); 1061 cout << Verbose(0) << "Current Bond length between " << first->type->name << " Atom " << first->nr << " and " << second->type->name << " Atom " << second->nr << ": " << min_bond << " a.u." << endl; 1062 cout << Verbose(0) << "Enter new bond length [a.u.]: "; 1063 cin >> bond; 1064 for (int i=0;i<3;i++) { 1065 second->x.x[i] -= (second->x.x[i]-first->x.x[i])/min_bond*(min_bond-bond); 1066 } 1067 //cout << Verbose(0) << "New coordinates of Atom " << second->nr << " are: "; 1068 //second->Output(second->type->No, 1, (ofstream *)&cout); 1069 break; 1070 1071 case 'c': // unit scaling of the metric 1072 cout << Verbose(0) << "Angstroem -> Bohrradius: 1.8897261\t\tBohrradius -> Angstroem: 0.52917721" << endl; 1073 cout << Verbose(0) << "Enter three factors: "; 1074 factor = new double[NDIM]; 1075 cin >> factor[0]; 1076 cin >> factor[1]; 1077 cin >> factor[2]; 1078 valid = true; 1079 mol->Scale(&factor); 1080 delete[](factor); 1081 break; 1082 1013 1083 case 'd': // duplicate the periodic cell along a given axis, given times 1014 1084 cout << Verbose(0) << "State the axis [(+-)123]: "; … … 1020 1090 if (mol->AtomCount != 0) { // if there is more than none 1021 1091 count = mol->AtomCount; // is changed becausing of adding, thus has to be stored away beforehand 1022 Elements = (element **) Malloc(sizeof(element *)*count, "main: duplicateCell - **Elements");1023 Vectors = (vector **) Malloc(sizeof(vector *)*count, "main: duplicateCell - **Vectors");1092 Elements = new element *[count]; 1093 Vectors = new vector *[count]; 1024 1094 j = 0; 1025 1095 first = mol->start; … … 1048 1118 mol->CreateAdjacencyList((ofstream *)&cout, mol->BondDistance); 1049 1119 // free memory 1050 Free((void **)&Elements, "main: duplicateCell - **Elements");1051 Free((void **)&Vectors, "main: duplicateCell - **Vectors");1120 delete[](Elements); 1121 delete[](Vectors); 1052 1122 // correct cell size 1053 1123 if (axis < 0) { // if sign was negative, we have to translate everything … … 1061 1131 break; 1062 1132 1063 case 'g': // center the atoms1064 CenterAtoms(mol);1065 break;1066 1067 case 'b': // scale a bond1068 cout << Verbose(0) << "Scaling bond length between two atoms." << endl;1069 first = mol->AskAtom("Enter first (fixed) atom: ");1070 second = mol->AskAtom("Enter second (shifting) atom: ");1071 min_bond = 0.;1072 for (int i=0;i<3;i++)1073 min_bond += (first->x.x[i]-second->x.x[i])*(first->x.x[i] - second->x.x[i]);1074 min_bond = sqrt(min_bond);1075 cout << Verbose(0) << "Current Bond length between " << first->type->name << " Atom " << first->nr << " and " << second->type->name << " Atom " << second->nr << ": " << min_bond << " a.u." << endl;1076 cout << Verbose(0) << "Enter new bond length [a.u.]: ";1077 cin >> bond;1078 for (int i=0;i<3;i++) {1079 second->x.x[i] -= (second->x.x[i]-first->x.x[i])/min_bond*(min_bond-bond);1080 }1081 //cout << Verbose(0) << "New coordinates of Atom " << second->nr << " are: ";1082 //second->Output(second->type->No, 1, (ofstream *)&cout);1083 break;1084 1085 case 'i': // align all atoms1086 AlignAtoms(periode, mol);1087 break;1088 1089 case 'm': // mirror atoms along a given axis1090 MirrorAtoms(mol);1091 break;1092 1093 case 't': // translate all atoms1094 cout << Verbose(0) << "Enter translation vector." << endl;1095 x.AskPosition(mol->cell_size,0);1096 mol->Translate((const vector *)&x);1097 break;1098 1099 1133 case 'e': // edit each field of the configuration 1100 1134 configuration.Edit(mol); 1101 1135 break; 1102 1103 case 'c': // unit scaling of the metric 1104 cout << Verbose(0) << "Angstroem -> Bohrradius: 1.8897261\t\tBohrradius -> Angstroem: 0.52917721" << endl; 1105 cout << Verbose(0) << "Enter three factors: "; 1106 factor = (double *) Malloc(sizeof(double)*NDIM, "main: *factor"); 1107 cin >> factor[0]; 1108 cin >> factor[1]; 1109 cin >> factor[2]; 1110 valid = true; 1111 mol->Scale(&factor); 1112 Free((void **)&factor, "main: *factor"); 1113 break; 1114 1115 case 'r': // remove atom 1116 RemoveAtoms(mol); 1136 1137 case 'f': 1138 FragmentAtoms(mol, &configuration); 1117 1139 break; 1118 1140 1141 case 'g': // center the atoms 1142 CenterAtoms(mol); 1143 break; 1144 1145 case 'i': // align all atoms 1146 AlignAtoms(periode, mol); 1147 break; 1148 1119 1149 case 'l': // measure distances or angles 1120 1150 MeasureAtoms(periode, mol); 1121 1151 break; 1122 1152 1123 case 'p': // parse and XYZ file 1124 cout << Verbose(0) << "Format should be XYZ with: ShorthandOfElement\tX\tY\tZ" << endl; 1125 do { 1126 cout << Verbose(0) << "Enter file name: "; 1127 cin >> filename; 1128 } while (!mol->AddXYZFile(filename)); 1129 break; 1130 1153 case 'm': // mirror atoms along a given axis 1154 MirrorAtoms(mol); 1155 break; 1156 1131 1157 case 'o': // create the connection matrix 1132 1158 cout << Verbose(0) << "What's the maximum bond distance: "; … … 1147 1173 break; 1148 1174 1149 case 'f': 1150 FragmentAtoms(mol, &configuration); 1151 break; 1175 case 'p': // parse and XYZ file 1176 cout << Verbose(0) << "Format should be XYZ with: ShorthandOfElement\tX\tY\tZ" << endl; 1177 do { 1178 cout << Verbose(0) << "Enter file name: "; 1179 cin >> filename; 1180 } while (!mol->AddXYZFile(filename)); 1181 break; 1182 1183 case 'q': // quit 1184 break; 1152 1185 1186 case 'r': // remove atom 1187 RemoveAtoms(mol); 1188 break; 1189 1190 case 's': // save to config file 1191 SaveConfig(ConfigFileName, &configuration, periode, mol); 1192 break; 1193 1194 case 't': // translate all atoms 1195 cout << Verbose(0) << "Enter translation vector." << endl; 1196 x.AskPosition(mol->cell_size,0); 1197 mol->Translate((const vector *)&x); 1198 break; 1199 1200 case 'T': 1201 testroutine(mol); 1202 break; 1203 1153 1204 case 'u': // change an atom's element 1154 1205 first = NULL; … … 1161 1212 first->type = periode->FindElement(Z); 1162 1213 cout << Verbose(0) << "Atom " << first->nr << "'s element is " << first->type->name << "." << endl; 1163 break;1164 1165 case 'T':1166 testroutine(mol);1167 break;1168 1169 case 's': // save to config file1170 SaveConfig(ConfigFileName, &configuration, periode, mol);1171 1214 break; 1172 1215 };
Note:
See TracChangeset
for help on using the changeset viewer.