Beispiel #1
0
func initdb() {
	if *showquery {
		// don't need a database for printing queries
		return
	} else if *dbname == "" {
		log.Fatal("must specify database with -db flag")
	}

	var err error
	db, err = sql.Open("sqlite3", *dbname)
	fatalif(err)

	if *simidstr == "" {
		ids, err := query.SimIds(db)
		fatalif(err)
		simid = ids[0]
	} else {
		simid = uuid.Parse(*simidstr)
		if simid == nil {
			log.Fatalf("invalid simid '%s'", *simidstr)
		}
	}

	post.Process(db)
}
Beispiel #2
0
// Local runs scenario scn on the local machine connecting the simulation's
// standard out and error to stdout and stderr respectively.  The file names
// of the generated cyclus input file and database are returned along with the
// objective value.
func Local(scn *scen.Scenario, stdout, stderr io.Writer) (obj float64, err error) {
	execfn := func(s *scen.Scenario) (float64, error) {
		// generate cyclus input file and run cyclus
		ui := uuid.NewRandom()
		infile := ui.String() + ".cyclus.xml"
		dbfile := ui.String() + ".sqlite"

		data, err := s.GenCyclusInfile()
		if err != nil {
			return math.Inf(1), err
		}
		err = ioutil.WriteFile(infile, data, 0644)
		if err != nil {
			return math.Inf(1), err
		}

		cmd := exec.Command("cyclus", infile, "-o", dbfile)
		cmd.Stdout = stdout
		cmd.Stderr = stderr

		if err := cmd.Run(); err != nil {
			return math.Inf(1), err
		}
		defer os.Remove(dbfile)

		// post process cyclus output db
		db, err := sql.Open("sqlite3", dbfile)
		if err != nil {
			return math.Inf(1), err
		}
		defer db.Close()

		simids, err := post.Process(db)
		if err != nil {
			return math.Inf(1), err
		}

		return s.CalcObjective(dbfile, simids[0])
	}
	return scn.CalcTotalObjective(execfn)
}