Example #1
0
/* This function returns 1 on success, and 0 on failure
*/
int readParams(char *datFileName) {
	int printFlag = 1;
	char title[128], parStr[128], *str;
	int gCount,i,Nkind;  
	char unitCellFile[64];
	atom *tempCell;

	if (!parOpen(datFileName)) {
		printf("Could not open data input file %s\n",datFileName);
		return 0;
	}
	resetParamFile();
	while(readparam("crystal:",parStr,0)) nGrains++;  
	resetParamFile();
	while(readparam("amorph:",parStr,0)) nGrains++;  
	resetParamFile();
	while(readparam("special:",parStr,0)) nGrains++;  
	printf("Found data for %d grain(s) (crystalline frame work and amorphous)\n",nGrains);
	if (nGrains == 0) return 0;

	grains = (grainBox *)malloc(nGrains*sizeof(grainBox));
	/* Now we will loop through all the grains and lok for the necessary 
	* data for each grain 
	*/
	if (readparam("box:",parStr,1)) {
		sscanf(parStr,"%lf %lf %lf",&(superCell.ax),&(superCell.by),&(superCell.cz));
	}
	else {
		printf("Size of super cell box not defined - exit!\n");
		exit(0);
	} 

	/* reset the input file and advance to the next crystal row */
	resetParamFile();
	/*
	readparam("crystal:",parStr,0);
	grains[0].name = (char *)malloc(NAME_BUF_LEN);
	strcpy(grains[0].name,parStr);
	grains[0].nplanes = 0;
	grains[0].planes = NULL;
	*/
	gCount = -1;
	/* We will look for the following tokens:
	* tilt: tiltx,tilty,tiltz;
	* translation: shiftx shifty shiftz
	* plane: vectX vectY vectZ pointX pointY pointZ
	*/
	while (readNextParam(title,parStr)) {
		// printf("%s\n",parStr);
		/* if we found a new crystal ... */
		if (strncmp(title,"crystal:",8) == 0) {
			gCount++;
			grains[gCount].name = (char *)malloc(NAME_BUF_LEN);
			grains[gCount].amorphFlag = 0;
			grains[gCount].density = 0;
			grains[gCount].rmin = 0;
			grains[gCount].rFactor = 1.0;
			grains[gCount].sphereRadius = 0;
			/* first we want to extract crystal name and file name for
			* unit cell data file from this one line
			*/
			strcpy(grains[gCount].name,parStr);
			str = strnext(parStr," \t");
			if (str != NULL) {
				grains[gCount].name[str-parStr-1]='\0';
				// printf("%s, name: %s\n",parStr,grains[gCount].name);
				//str = strnext(str," \t");
				//if (str != NULL) {
				strcpy(unitCellFile,str);
				if ((str = strchr(unitCellFile,' ')) != NULL)
					*str = '\0';
				if ((str = strchr(unitCellFile,'\t')) != NULL)
					*str = '\0';
			}
			else {
				printf("Error: no unit cell data file specified for crystal %s\n",
					grains[gCount].name);
				return 0;       
			}

			// sscanf(parStr,"%s %s",grains[gCount].name,unitCellFile);
			grains[gCount].nplanes = 0;
			grains[gCount].planes = NULL;
			muls->nCellX = 1;
			muls->nCellY = 1;
			muls->nCellZ = 1;
			muls->ctiltx = 0;
			muls->ctilty = 0;
			muls->ctiltz = 0;

			muls->tds = 0;
			tempCell = readUnitCell(&(grains[gCount].natoms), unitCellFile, muls, 0);
			if (tempCell == NULL) {
				printf("Error reading unit cell data - exit!\n");
				exit(0);
			}

			/*****************************************************
			* Test code
			*
			printf("ax: %g, by: %g, cz: %g\n",muls->ax,muls->by,muls->c);
			for (i=0;i<grains[gCount].natoms;i++) {
			printf("%d: %d (%g,%g,%g)\n",i,tempCell[i].Znum,tempCell[i].x,tempCell[i].y,tempCell[i].z);
			}
			*
			*****************************************************/
			grains[gCount].unitCell = (atom *)malloc(grains[gCount].natoms*sizeof(atom));
			memcpy(grains[gCount].unitCell,tempCell,grains[gCount].natoms*sizeof(atom));

			grains[gCount].alpha = muls->cAlpha;
			grains[gCount].beta  = muls->cBeta;
			grains[gCount].gamma = muls->cGamma;
			grains[gCount].ax = muls->ax;
			grains[gCount].by = muls->by;
			grains[gCount].cz = muls->c;
		}
		/***************************************************
		* amorphous stuff
		*/
		else if (strncmp(title,"amorph:",7) == 0) {
			gCount++;
			grains[gCount].name = (char *)malloc(NAME_BUF_LEN);
			grains[gCount].amorphFlag = AMORPHOUS;
			grains[gCount].density = 0;
			grains[gCount].rmin = 1000;      
			grains[gCount].rFactor = 1.2;
			grains[gCount].sphereRadius = 0;
			/* first we want to extract crystal name and file name for
			* unit cell data file from this one line
			*/
			strcpy(grains[gCount].name,parStr);
			str = strnext(parStr," \t");
			if (str != NULL) {
				grains[gCount].name[str-parStr-1]='\0';
				// printf("%s, name: %s\n",parStr,grains[gCount].name);
				//str = strnext(str," \t");
				//if (str != NULL) {
				strcpy(unitCellFile,str);
				if ((str = strchr(unitCellFile,' ')) != NULL)
					*str = '\0';
				if ((str = strchr(unitCellFile,'\t')) != NULL)
					*str = '\0';
			}
			else {
				printf("Error: no unit cell data file specified for crystal %s\n",
					grains[gCount].name);
				return 0;       
			}

			// sscanf(parStr,"%s %s",grains[gCount].name,unitCellFile);
			grains[gCount].nplanes = 0;
			grains[gCount].planes = NULL;
			muls->nCellX = 1;
			muls->nCellY = 1;
			muls->nCellZ = 1;
			muls->ctiltx = 0;
			muls->ctilty = 0;
			tempCell = readUnitCell(&(grains[gCount].natoms), unitCellFile, muls, 0);
			grains[gCount].unitCell = (atom *)malloc(grains[gCount].natoms * sizeof(atom));
			memcpy(grains[gCount].unitCell, tempCell, grains[gCount].natoms * sizeof(atom));
			grains[gCount].alpha = 0;
			grains[gCount].beta  = 0;
			grains[gCount].gamma = 0;
			grains[gCount].ax = 0;
			grains[gCount].by = 0;
			grains[gCount].cz = 0;
		}
		/***************************************************
		* code for specially distributed amorphous stuff
		*/
		else if (strncmp(title,"special:",8) == 0) {
			gCount++;
			grains[gCount].name = (char *)malloc(NAME_BUF_LEN);
			grains[gCount].amorphFlag = SPECIAL_GRAIN;
			grains[gCount].density = 0;
			grains[gCount].rmin = 1000;      
			grains[gCount].rFactor = 1.2;
			grains[gCount].sphereRadius = 0;
			/* first we want to extract crystal name and file name for
			* unit cell data file from this one line
			*/
			strcpy(grains[gCount].name,parStr);  // name of this grain
			// don't need cfg input file
			grains[gCount].natoms = 0;
			grains[gCount].unitCell = NULL;
			// sscanf(parStr,"%s %s",grains[gCount].name,unitCellFile);
			grains[gCount].nplanes = 0;
			grains[gCount].planes = NULL;
			grains[gCount].alpha = 0;
			grains[gCount].beta  = 0;
			grains[gCount].gamma = 0;
			grains[gCount].ax = 0;
			grains[gCount].by = 0;
			grains[gCount].cz = 0;
		}  // end of "special"
		/* if we found tilt data */
		else if (gCount >= 0) { 
			if (strncmp(title,"tilt:",5) == 0) {
				sscanf(parStr,"%lf %lf %lf",&(grains[gCount].tiltx),
					&(grains[gCount].tilty),&(grains[gCount].tiltz));
				if (strstr(parStr,"degree") != NULL) {
					grains[gCount].tiltx *= PI180;
					grains[gCount].tilty *= PI180;
					grains[gCount].tiltz *= PI180;
				}
			}
			/* assign density */
			else if (strncmp(title,"density:",8) == 0) {
				sscanf(parStr,"%lf",&(grains[gCount].density));
				grains[gCount].rmin = pow(sqrt(15.0/144.0)/grains[gCount].density,1.0/3.0);
			}
			/* assign density factor */
			else if (strncmp(title,"rmin:",5) == 0) {
				sscanf(parStr,"%lf",&(grains[gCount].rmin));
				grains[gCount].density = sqrt(15.0/144)*pow(grains[gCount].rmin,3);
			}
			else if (strncmp(title,"r-factor:",9) == 0) {
				sscanf(parStr,"%lf",&(grains[gCount].rFactor));
			}
			/* if we found shift data */
			else if (strncmp(title,"translation:",12) == 0) {
				sscanf(parStr,"%lf %lf %lf",&(grains[gCount].shiftx),
					&(grains[gCount].shifty),&(grains[gCount].shiftz));    
			}
			else if (strncmp(title,"sphere:",7) == 0) {
				sscanf(parStr,"%lf %lf %lf %lf",&(grains[gCount].sphereRadius),
					&(grains[gCount].sphereX),&(grains[gCount].sphereY),&(grains[gCount].sphereZ));    
			}
			/* if we found a new plane for this crystal */
			else if (strncmp(title,"plane:",6) == 0) {
				grains[gCount].nplanes++;
				grains[gCount].planes = (plane *) realloc(grains[gCount].planes,
					grains[gCount].nplanes*sizeof(plane));
				if (grains[gCount].planes == NULL) {
					printf("Sorry, could not allocate memory for new plane\n");
					return 0;
				}
				sscanf(parStr,"%lf %lf %lf %lf %lf %lf %lf %lf %lf",
					&(grains[gCount].planes[grains[gCount].nplanes-1].pointX), 
					&(grains[gCount].planes[grains[gCount].nplanes-1].pointY), 
					&(grains[gCount].planes[grains[gCount].nplanes-1].pointZ),
					&(grains[gCount].planes[grains[gCount].nplanes-1].vect1X), 
					&(grains[gCount].planes[grains[gCount].nplanes-1].vect1Y), 
					&(grains[gCount].planes[grains[gCount].nplanes-1].vect1Z), 
					&(grains[gCount].planes[grains[gCount].nplanes-1].vect2X), 
					&(grains[gCount].planes[grains[gCount].nplanes-1].vect2Y), 
					&(grains[gCount].planes[grains[gCount].nplanes-1].vect2Z));
				crossProduct(&(grains[gCount].planes[grains[gCount].nplanes-1].vect1X),
					&(grains[gCount].planes[grains[gCount].nplanes-1].vect2X),
					&(grains[gCount].planes[grains[gCount].nplanes-1].normX));
			} /* end of if plane ... */
			else if (strncmp(title,"atom:",5) == 0) {
				grains[gCount].natoms++;
				grains[gCount].unitCell = (atom *)realloc(grains[gCount].unitCell,
					grains[gCount].natoms*sizeof(atom));
				// Now read Znum, r (->z), and count (->y)
				sscanf(parStr,"%d %f %d %f",&(grains[gCount].unitCell[grains[gCount].natoms-1].Znum),
					&(grains[gCount].unitCell[grains[gCount].natoms-1].z),&Nkind,
					&(grains[gCount].unitCell[grains[gCount].natoms-1].y));

				// assign number of atoms directly, if specified in input file
				if (Nkind > 0) grains[gCount].unitCell[grains[gCount].natoms-1].y = (float)Nkind;
				for (i=0;i<muls->atomKinds;i++) 	
					if (muls->Znums[i] == grains[gCount].unitCell[grains[gCount].natoms-1].Znum) break;
				if (i == muls->atomKinds) {
					muls->atomKinds++;
					muls->Znums = (int *) realloc(muls->Znums,muls->atomKinds*sizeof(int));
					muls->Znums[i] = grains[gCount].unitCell[grains[gCount].natoms-1].Znum;
				}
			} /* end of if "atom:" */
		} /* end of if gCount >=0 */
	}
	parClose();
	if (printFlag) showData();
	return 1;
}
Example #2
0
void CodeEditor::SyntaxState::ScanSyntax(const wchar *ln, const wchar *e, int tab_size)
{
	Grounding(ln, e);
	if(!linecont) {
		linecomment = false;
		string = false;
	}
	linecont = e > ln && e[-1] == '\\';
	const wchar *p = ln;
	int lindent = 0, pos = 0;
	while(p < e && (*p == '\t' || *p == ' ')) {
		if(*p++ == '\t' || ++pos >= tab_size) {
			pos = 0;
			lindent++;
		}
	}
	int n;
	Color c = GetUvsHighlight(p, n);
	if(n) {
		uvscolor = c;
		p += n;
	}
	if(!comment && *p == '#') {
		while(++p < e && (*p == ' ' || *p == '\t'))
			p++;
		const wchar *id = p;
		while(p < e && iscidl(*p))
			p++;
		int idlen = int(p - id);
		if(id[0] == 'i' && id[1] == 'f'
		&& (idlen == 2 || idlen == 5 && id[2] == 'd' && id[3] == 'e' && id[4] == 'f'
		|| idlen == 6 && id[2] == 'n' && id[3] == 'd' && id[4] == 'e' && id[5] == 'f')) {
			IfState& ifstate = ifstack.Add();
			ifstate.state = IfState::IF;
			ifstate.iftext = sReadLn(ln);
			ifstate.ifline = line + 1;
		}
		switch(idlen)
		{
		case 6:
			if(id[0] == 'd' && id[1] == 'e' && id[2] == 'f' && id[3] == 'i' && id[4] == 'n' && id[5] == 'e')
				macro = SyntaxState::MACRO_CONT;
			break;

		case 4:
			if(id[0] == 'e' && id[1] == 'l')
				if(id[2] == 'i' && id[3] == 'f')
					if(ifstack.GetCount() == 0) {
						IfState& ifstate = ifstack.Add();
						ifstate.ifline = 0;
						ifstate.state = IfState::ELSE_ERROR;
					}
					else {
						IfState& ifstate = ifstack.Top();
						if(ifstate.state == IfState::IF || ifstate.state == IfState::ELIF) {
							ifstate.state = IfState::ELIF;
							ifstate.iftext = WString().Cat() << sReadLn(ln) << ", " << ifstate.iftext;
						}
						else
							ifstate.state = IfState::ELSE_ERROR;
					}
				else
				if(id[2] == 's' && id[3] == 'e')
					if(ifstack.GetCount() == 0) {
						IfState& ifstate = ifstack.Add();
						ifstate.ifline = 0;
						ifstate.state = IfState::ELSE_ERROR;
					}
					else {
						IfState& ifstate = ifstack.Top();
						if(ifstate.state == IfState::IF || ifstate.state == IfState::ELIF) {
							ifstate.state = IfState::ELSE;
							ifstate.iftext = "#else, " + ifstate.iftext;
						}
						else
							ifstate.state = IfState::ELSE_ERROR;
					}
			break;

		case 5:
			if(id[0] == 'e' && id[1] == 'n' && id[2] == 'd' && id[3] == 'i' && id[4] == 'f')
			{
				int itop = ifstack.GetCount() - 1;
				if(itop < 0) {
					IfState& ifstate = ifstack.Add();
					ifstate.ifline = 0;
					ifstate.state = IfState::ENDIF_ERROR;
				}
				else if(ifstack[itop].state != IfState::ENDIF_ERROR)
					ifstack.Trim(itop);
			}
			break;
		}
	}
	if(macro == SyntaxState::MACRO_CONT && !(p < e && e[-1] == '\\'))
		macro = SyntaxState::MACRO_END;
	for(;;) {
		if(comment) {
			p = strnext(p, e, '*');
			if(!p) break;
			if(*++p == '/') {
				comment = false;
				p++;
			}
		}
		else {
			int pc = 0;
			for(;;) {
				if(p >= e) return;
				const wchar *pp;
				if(!iscidl(pc) && (pp = isstmt(p)) != NULL) {
					stmtline = line;
					spar = 0;
					pc = 0;
					p = pp;
				}
				else
				if(!iscidl(pc) && p[0] == 'n' && p[1] == 'a' && p[2] == 'm' && p[3] == 'e' &&
				   p[4] == 's' && p[5] == 'p' && p[6] == 'a' && p[7] == 'c' && p[8] == 'e' &&
				   !iscidl(p[9])) {
					was_namespace = true;
					p += 9;
				}
				else {
					int c = *p++;
					if(c == '/') break;
					if(c == '\'' || c == '\"') {
						p = eatstring(p - 1);
						if(p >= e) {
							string = true;
							return;
						}
					}
					else
					if(c == ';' && spar == 0) {
						seline = stmtline;
						endstmtline = line;
						stmtline = -1;
						was_namespace = false;
					}
					else
					if(c == '{') {
						if(was_namespace) {
							brk.Add(0);
							was_namespace = false;
						}
						else {
							cl++;
							brk.Add('}');
							bid.Add(lindent + 1);
						}
						blk.Add() = line;
						stmtline = -1;
						par.Clear();
					}
					else
					if(c == '}') {
						if(brk.GetCount()) {
							if(brk.Top()) {
								cl--;
								if(bid.GetCount() > 1)
									bid.Drop();
							}
							brk.Drop();
						}
						if(blk.GetCount())
							blk.Drop();
						stmtline = -1;
						par.Clear();
					}
					else
					if(c == '(') {
						pl++;
						brk.Add(')');
						Isx& m = par.Add();
						m.line = line;
						m.pos = int(p - ln);
						spar++;
					}
					else
					if(c == '[') {
						bl++;
						brk.Add(']');
						Isx& m = par.Add();
						m.line = line;
						m.pos = int(p - ln);
						spar++;
					}
					else
					if(c == ')') {
						if(brk.GetCount()) {
							pl--;
							brk.Drop();
						}
						if(par.GetCount())
							par.Drop();
						spar--;
					}
					else
					if(c == ']') {
						if(brk.GetCount()) {
							bl--;
							brk.Drop();
						}
						if(par.GetCount())
							par.Drop();
						spar--;
					}
					pc = c;
				}
			}
			if(*p == '/') {
				linecomment = true;
				return;
			}
			if(*p == '*') {
				comment = true;
				p++;
			}
		}
	}
}