Пример #1
0
// Determine the locality of the supplied atom
AtomAddress Model::locateAtom(Atom* i)
{
	Messenger::enter("Model::locateAtom");
	int patternno, molno, atomno, id;
	Pattern* p;
	AtomAddress result;
	if (!createPatterns())
	{
		Messenger::print("Model::locateAtom : No valid pattern available for model.");
		Messenger::exit("Model::locateAtom");
		return result;
	}
	id = i->id();
	// First, find the pattern the atom is covered by
	patternno = -1;
	p = patterns_.first();
	while (p != NULL)
	{
		if ((id >= p->startAtom()) && (id <= p->endAtom()))
		{
			patternno = p->id();
			break;
		}
		p = p->next;
	}
	if (patternno == -1)
	{
		printf("Fatal error - could not find owner pattern for atom!\n");
		Messenger::exit("Model::locateAtom");
		return result;
	}
	// Next, find its molecule id
	id -= p->startAtom();
	molno = id / p->nAtoms();
	// Finally, get the atom offset
	atomno = id % p->nAtoms();
	// Store values, and return
	result.setPattern(p);
	result.setMolecule(molno);
	result.setOffset(atomno);
	Messenger::exit("Model::locateAtom");
	return result;
}
Пример #2
0
int
main(int argc, char **argv)
{
    struct part *part;
    int opt, n;
    int dump_part = 0;
    int printStructurePotentialEnergy = 0;
    int needVDW = 1;
    char *printPotential = NULL;
    double printPotentialInitial = -1; // pm
    double printPotentialIncrement = -1; // pm
    double printPotentialLimit = -1; // pm
    char *fileNameTemplate = NULL;
    char *outputFilename = NULL;
	
    reinit_globals();

    if (signal(SIGTERM, &SIGTERMhandler) == SIG_ERR) {
        perror("signal(SIGTERM)");
        exit(1);
    }
    
    CommandLine = assembleCommandLine(argc, argv);
    while ((opt = getopt_long(argc, argv,
			    "hnmEi:f:s:t:xXONI:K:rD:o:q:B:",
			    option_vec, NULL)) != -1) {
	switch(opt) {
	case 'h':
	    usage();
	case OPT_DUMP_PART:
	    dump_part = 1;
	    break;
	case OPT_WRITE_GROMACS_TOPOLOGY:
	    GromacsOutputBaseName = optarg;
	    break;
	case OPT_PATH_TO_CPP:
	    PathToCpp = optarg;
	    break;
	case OPT_SYSTEM_PARAMETERS:
	    SystemParametersFileName = optarg;
	    break;
        case OPT_PRINT_POTENTIAL:
            printPotential = optarg;
	    break;
        case OPT_INITIAL:
	    printPotentialInitial = atof(optarg);
	    break;
        case OPT_INCREMENT:
	    printPotentialIncrement = atof(optarg);
	    break;
        case OPT_LIMIT:
	    printPotentialLimit = atof(optarg);
	    break;
        case OPT_DIRECT_EVALUATE:
            DirectEvaluate = 1;
	    break;
        case OPT_INTERPOLATE:
            DirectEvaluate = 0;
	    break;
        case OPT_SIMPLE_MOVIE_FORCE_SCALE:
            SimpleMovieForceScale = atof(optarg);
            break;
        case OPT_MIN_THRESH_CUT_RMS:
            MinimizeThresholdCutoverRMS = atof(optarg);
            break;
        case OPT_MIN_THRESH_CUT_MAX:
            MinimizeThresholdCutoverMax = atof(optarg);
            break;
        case OPT_MIN_THRESH_END_RMS:
            MinimizeThresholdEndRMS = atof(optarg);
            break;
        case OPT_MIN_THRESH_END_MAX:
            MinimizeThresholdEndMax = atof(optarg);
            break;
        case OPT_VDW_CUTOFF_RADIUS:
            VanDerWaalsCutoffRadius = atof(optarg);
            break;
        case OPT_VDW_CUTOFF_FACTOR:
            VanDerWaalsCutoffFactor = atof(optarg);
            break;
        case OPT_ENABLE_ELECTROSTATIC:
            EnableElectrostatic = atoi(optarg);
            break;
        case OPT_TIME_REVERSAL:
            TimeReversal = 1;
            break;
        case OPT_THERMOSTAT_GAMMA:
            ThermostatGamma = atof(optarg);
            break;
        case OPT_PRINT_ENERGIES:
            PrintPotentialEnergy = 1;
            break;
        case OPT_NEIGHBOR_SEARCHING:
            NeighborSearching = atoi(optarg);
            break;
	case 'n':
	    // ignored
	    break;
	case 'm':
	    ToMinimize=1;
	    break;
	case 'E':
	    printStructurePotentialEnergy=1;
	    break;
	case 'i':
	    IterPerFrame = atoi(optarg);
	    break;
	case 'f':
	    NumFrames = atoi(optarg);
	    break;
	case 's':
	    Dt = atof(optarg);
	    break;
	case 't':
	    Temperature = atof(optarg);
	    break;
	case 'x':
	    DumpAsText = 1;
	    break;
	case 'X':
	    DumpIntermediateText = 1;
	    break;
	case 'O':
	    OutputFormat = 1;
	    break;
	case 'N':
	    OutputFormat = 2;
	    break;
	case OPT_OUTPUT_FORMAT_3:
	    OutputFormat = 3;
	    break;
	case 'I':
	    IDKey = optarg;
	    break;
	case 'K':
	    KeyRecordInterval = atoi(optarg);
	    break;
	case 'r':
	    PrintFrameNums = 1;
	    break;
	case 'D':
	    n = atoi(optarg);
	    if (n < 32 && n >= 0) {
		debug_flags |= 1 << n;
	    }
	    break;
	case 'o':
	    outputFilename = optarg;
	    break;
	case 'q':
	    TraceFileName = optarg;
	    break;
	case 'B':
	    BaseFileName = optarg;
	    break;
        case ':':
        case '?':
	default:
	    usage();
	    exit(1);
	}
    }
    if (optind + 1 == argc) {   // (optind < argc) if not paranoid
	fileNameTemplate = argv[optind];
    }

    if (DEBUG(D_PRINT_BEND_STRETCH)) { // -D8
        initializeBondTable();
        printBendStretch();
        exit(0);
    }

    if (DumpAsText) {
        OutputFormat = 0;
    }

    if (!fileNameTemplate) {
        usage();
    }
    InputFileName = replaceExtension(fileNameTemplate, "mmp");

    if (BaseFileName != NULL) {
        int i1;
        int i2;
        struct xyz *basePositions;
        struct xyz *initialPositions;
        
        basePositions = readXYZ(BaseFileName, &i1);
        if (basePositions == NULL) {
            fprintf(stderr, "could not read base positions file from -B<filename>\n");
            exit(1);
        }
        initialPositions = readXYZ(InputFileName, &i2);
        if (initialPositions == NULL) {
            fprintf(stderr, "could not read comparison positions file\n");
            exit(1);
        }
        if (i1 != i2) {
            fprintf(stderr, "structures to compare must have same number of atoms\n");
            exit(1);
        }
        exit(doStructureCompare(i1, basePositions, initialPositions,
                                NumFrames, 1e-8, 1e-4, 1.0+1e-4));
    }

    if (outputFilename) {
        OutputFileName = copy_string(outputFilename);
    } else {
        char *extension;
        
        switch (OutputFormat) {
        case 0:
            extension = "xyz";
            break;
        case 1:
        case 2:
        default:
            extension = "dpb";
            break;
        case 3:
            extension = "gro";
            break;
        }
        
        OutputFileName = replaceExtension(fileNameTemplate, extension);
    }

    if (TraceFileName) {
        TraceFile = fopen(TraceFileName, "w");
        if (TraceFile == NULL) {
            perror(TraceFileName);
            exit(1);
        }
    } else {
        TraceFile = fdopen(1, "w");
        if (TraceFile == NULL) {
            perror("fdopen stdout as TraceFile");
            exit(1);
        }
    }
    traceFileVersion(); // call this before any other writes to trace file.
    // tell where and how the simulator was built. We never build the
    // standalone simulator with distutils.
    fprintf(TraceFile, "%s", tracePrefix);

    initializeBondTable();

    if (IterPerFrame <= 0) IterPerFrame = 1;

    if (printPotential) {
        printPotentialAndGradientFunctions(printPotential,
                                           printPotentialInitial,
                                           printPotentialIncrement,
                                           printPotentialLimit);
        exit(0);
    }
    
    part = readMMP(InputFileName);
    if (EXCEPTION) {
        exit(1);
    }
    if (GromacsOutputBaseName != NULL) {
        needVDW = 0;
    }
    initializePart(part, needVDW);
    createPatterns();
    matchPartToAllPatterns(part);
    
    if (printStructurePotentialEnergy) {
        struct xyz *force = (struct xyz *)allocate(sizeof(struct xyz) * part->num_atoms);
        double potentialEnergy = calculatePotential(part, part->positions);
        calculateGradient(part, part->positions, force);
        printf("%e %e %e %e (Potential energy in aJ, gradient of atom 1)\n", potentialEnergy, force[1].x, force[1].y, force[1].z);
        exit(0);
    }
    
    if (dump_part) {
        //
        // this segment is convenient to run valgrind on to test the
        // part and bond table destructors.  By the time we reach the
        // exit() there should be no malloc'd blocks remaining.
        //
        // valgrind -v --leak-check=full --leak-resolution=high --show-reachable=yes simulator --dump-part part.mmp
        //
        printPart(stdout, part);
        destroyPart(part);
        part = NULL;
        destroyBondTable();
        fclose(TraceFile);
        destroyAccumulator(CommandLine);
        free(InputFileName);
        free(OutputFileName);
        exit(0);
    }

    if (GromacsOutputBaseName != NULL) {
        printGromacsToplogy(GromacsOutputBaseName, part);
        destroyPart(part);
        part = NULL;
        destroyBondTable();
        fclose(TraceFile);
        destroyAccumulator(CommandLine);
        free(InputFileName);
        free(OutputFileName);
        done("");
        exit(0);
    }

    constrainGlobals();
    traceHeader(part);

    if  (ToMinimize) {
	NumFrames = max(NumFrames,(int)sqrt((double)part->num_atoms));
	Temperature = 0.0;
    } else {
        traceJigHeader(part);
    }

    OutputFile = fopen(OutputFileName, DumpAsText ? "w" : "wb");
    if (OutputFile == NULL) {
        perror(OutputFileName);
        exit(1);
    }
    writeOutputHeader(OutputFile, part);

    if  (ToMinimize) {
	minimizeStructure(part);
	exit(0);
    }
    else {
        dynamicsMovie(part);
    }

    done("");
    return 0;
}
int main( int argc, const char** argv)
{
	try
	{
		initRand();

		g_errorBuffer = strus::createErrorBuffer_standard( 0, 1, NULL/*debug trace interface*/);
		if (!g_errorBuffer)
		{
			std::cerr << "construction of error buffer failed" << std::endl;
			return -1;
		}
		else if (argc > 1)
		{
			std::cerr << "too many arguments" << std::endl;
			return 1;
		}
		unsigned int documentSize = 100;

		strus::local_ptr<strus::PatternMatcherInterface> pt( strus::createPatternMatcher_std( g_errorBuffer));
		if (!pt.get()) throw std::runtime_error("failed to create pattern matcher");
		strus::local_ptr<strus::PatternMatcherInstanceInterface> ptinst( pt->createInstance());
		if (!ptinst.get()) throw std::runtime_error("failed to create pattern matcher instance");
		createPatterns( ptinst.get(), testPatterns);
		ptinst->compile();

		if (g_errorBuffer->hasError())
		{
			throw std::runtime_error( "error creating automaton for evaluating rules");
		}
		Document doc = createDocument( 1, documentSize);
		std::cerr << "starting rule evaluation ..." << std::endl;

		// Evaluate results:
		std::vector<strus::analyzer::PatternMatcherResult> 
			results = processDocument( ptinst.get(), doc);

		// Verify results:
		std::vector<strus::analyzer::PatternMatcherResult>::const_iterator
			ri = results.begin(), re = results.end();

		typedef std::pair<std::string,unsigned int> Match;
		std::set<Match> matches;
		for (;ri != re; ++ri)
		{
			matches.insert( Match( ri->name(), ri->ordpos()));
		}
		std::set<Match>::const_iterator li = matches.begin(), le = matches.end();
		for (; li != le; ++li)
		{
			std::cout << "MATCH " << li->first << " -> " << li->second << std::endl;
		}
		unsigned int ti=0;
		for (; testPatterns[ti].name; ++ti)
		{
			unsigned int ei=0;
			for (; testPatterns[ti].results[ei]; ++ei)
			{
				std::cout << "CHECK " << ti << ": " << testPatterns[ti].name << " -> " << testPatterns[ti].results[ei] << std::endl;
				std::set<Match>::iterator
					mi = matches.find( Match( testPatterns[ti].name, testPatterns[ti].results[ei]));
				if (mi == matches.end())
				{
					char numbuf[ 64];
					::snprintf( numbuf, sizeof(numbuf), "%u", testPatterns[ti].results[ei]);
					throw std::runtime_error( std::string("expected match not found '") + testPatterns[ti].name + "' at ordpos " + numbuf);
				}
				else
				{
					matches.erase( mi);
				}
			}
		}
		if (!matches.empty())
		{
			std::set<Match>::const_iterator mi = matches.begin(), me = matches.end();
			for (; mi != me; ++mi)
			{
				std::cerr << "unexpected match of '" << mi->first << "' at ordpos " << mi->second << std::endl;
			}
			throw std::runtime_error( "more matches found than expected");
		}
		if (g_errorBuffer->hasError())
		{
			throw std::runtime_error("error matching rule");
		}
		std::cerr << "OK" << std::endl;
		delete g_errorBuffer;
		return 0;
	}
	catch (const std::runtime_error& err)
	{
		if (g_errorBuffer->hasError())
		{
			std::cerr << "error processing pattern matching: "
					<< g_errorBuffer->fetchError() << " (" << err.what()
					<< ")" << std::endl;
		}
		else
		{
			std::cerr << "error processing pattern matching: "
					<< err.what() << std::endl;
		}
	}
	catch (const std::bad_alloc&)
	{
		std::cerr << "out of memory processing pattern matching" << std::endl;
	}
	delete g_errorBuffer;
	return -1;
}