Exemplo n.º 1
0
func main() {
	log.SetFlags(0)
	flag.Parse()

	if *help || flag.NArg() != 1 {
		fmt.Println("Usage: inventory [cyclus-db]")
		fmt.Println("Creates a fast queryable inventory table for a cyclus sqlite output file.\n")
		flag.PrintDefaults()
		return
	}

	fname := flag.Arg(0)

	db, err := sql.Open("sqlite3", fname)
	fatalif(err)
	defer db.Close()

	fatalif(post.Prepare(db))
	defer post.Finish(db)

	simids, err := post.GetSimIds(db)
	fatalif(err)

	for _, simid := range simids {
		ctx := post.NewContext(db, simid)
		if *verbose {
			ctx.Log = log.New(os.Stdout, "", 0)
		}
		err := ctx.WalkAll()
		if err != nil {
			fmt.Println(err)
		}
	}
}
Exemplo n.º 2
0
func main() {
	log.SetFlags(0)
	flag.Parse()

	if *help || flag.NArg() < 1 {
		fmt.Println("Usage: metric -db <cyclus-db> [opts] <command> [args...]")
		fmt.Println("Calculates metrics for cyclus simulation data in a sqlite database.")
		flag.PrintDefaults()
		fmt.Println("\nCommands:")
		for i := range cmds.Names {
			fmt.Printf("    %v: %v\n", cmds.Names[i], cmds.Helps[i])
		}
		return
	} else if *dbname == "" {
		log.Fatal("must specify database with db flag")
	}

	if *custom != "" {
		data, err := ioutil.ReadFile(*custom)
		fatalif(err)
		fatalif(json.Unmarshal(data, &customSql))
	}

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

	if *simidstr == "" {
		ids, err := query.SimIds(db)
		fatalif(err)
		simid = ids[0]
	} else {
		simid, err = hex.DecodeString(*simidstr)
		fatalif(err)
	}

	// post process if necessary
	fatalif(post.Prepare(db))
	simids, err := post.GetSimIds(db)
	fatalif(err)
	for _, simid := range simids {
		ctx := post.NewContext(db, simid)
		err := ctx.WalkAll()
		if !post.IsAlreadyPostErr(err) {
			fatalif(err)
		}
	}
	fatalif(post.Finish(db))

	// run command
	cmds.Execute(flag.Args())
}
Exemplo n.º 3
0
func RunSim(scen *Scenario) (dbfile string, simid []byte, err error) {
	if scen.Handle == "" {
		scen.Handle = "none"
	}

	// generate cyclus input file and run cyclus
	ui := uuid.NewRandom()
	err = os.MkdirAll(tmpDir, 0755)
	if err != nil {
		return "", nil, err
	}

	cycin := filepath.Join(tmpDir, ui.String()+".cyclus.xml")
	cycout := filepath.Join(tmpDir, ui.String()+".sqlite")

	err = GenCyclusInfile(scen, cycin)
	if err != nil {
		return "", nil, err
	}

	cmd := exec.Command(scen.CyclusBin, cycin, "-o", cycout)
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		return "", nil, err
	}

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

	if err := post.Prepare(db); err != nil {
		return "", nil, err
	}
	defer post.Finish(db)

	simids, err := post.GetSimIds(db)
	if err != nil {
		return "", nil, err
	}

	ctx := post.NewContext(db, simids[0])
	if err := ctx.WalkAll(); err != nil {
		return "", nil, err
	}
	return cycout, simids[0], nil
}
Exemplo n.º 4
0
func uploadInner(w http.ResponseWriter, r *http.Request, kill chan bool) {
	// parse database from multi part form data
	if err := r.ParseMultipartForm(MAX_MEMORY); err != nil {
		log.Println(err)
		http.Error(w, err.Error(), http.StatusForbidden)
		return
	}

	gotDb := false
	uid := r.URL.Path[len("/upload/"):]
	fname := uid + ".sqlite"
	for _, fileHeaders := range r.MultipartForm.File {
		for _, fileHeader := range fileHeaders {
			file, _ := fileHeader.Open()
			data, _ := ioutil.ReadAll(file)
			err := ioutil.WriteFile(fname, data, 0644)
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				log.Print(err)
				return
			}
			defer func() { os.Remove(fname) }()
			gotDb = true
			break
		}
		break
	}
	if !gotDb {
		http.Error(w, "No file provided", http.StatusBadRequest)
		log.Print("received request with no file")
		return
	}

	select {
	case <-kill:
		return
	default:
	}

	// post process the database
	db, err := sql.Open("sqlite3", fname)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}
	defer db.Close()

	err = post.Prepare(db)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}
	defer post.Finish(db)

	simids, err := post.GetSimIds(db)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}

	ctx := post.NewContext(db, simids[0])
	if err := ctx.WalkAll(); err != nil {
		log.Println(err)
	}

	// get simid
	ids, err := query.SimIds(db)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}
	simid := ids[0]
	rs := &Results{}

	select {
	case <-kill:
		return
	default:
	}

	// create flow graph
	combineProto := false
	arcs, err := query.FlowGraph(db, simid, 0, -1, combineProto)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}
	dotname := fname + ".dot"
	dotf, err := os.Create(dotname)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}
	defer func() { os.Remove(dotname) }()

	fmt.Fprintln(dotf, "digraph ResourceFlows {")
	fmt.Fprintln(dotf, "    overlap = false;")
	fmt.Fprintln(dotf, "    nodesep=1.0;")
	fmt.Fprintln(dotf, "    edge [fontsize=9];")
	for _, arc := range arcs {
		fmt.Fprintf(dotf, "    \"%v\" -> \"%v\" [label=\"%v\\n(%.3g kg)\"];\n", arc.Src, arc.Dst, arc.Commod, arc.Quantity)
	}
	fmt.Fprintln(dotf, "}")
	dotf.Close()

	var buf bytes.Buffer
	cmd := exec.Command("dot", "-Tsvg", dotname)
	cmd.Stdout = &buf
	err = cmd.Run()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}
	rs.Flowgraph = buf.String()

	select {
	case <-kill:
		return
	default:
	}

	// create agents table
	rs.Agents, err = query.AllAgents(db, simid, "")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}

	// create Material transactions table
	sql := `
		SELECT TransactionId,Time,SenderId,ReceiverId,tr.ResourceId,Commodity,NucId,MassFrac*Quantity
		FROM Transactions AS tr
		INNER JOIN Resources AS res ON tr.ResourceId = res.ResourceId
		INNER JOIN Compositions AS cmp ON res.QualId = cmp.QualId
		WHERE tr.SimId = ? AND cmp.SimId = res.SimId AND res.SimId = tr.SimId
		AND res.Type = 'Material';
		`
	rows, err := db.Query(sql, simid)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}

	for rows.Next() {
		t := &Trans{}
		if err := rows.Scan(&t.Id, &t.Time, &t.Sender, &t.Receiver, &t.ResourceId, &t.Commod, &t.Nuc, &t.Qty); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			log.Print(err)
			return
		}
		rs.TransMats = append(rs.TransMats, t)
	}
	if err := rows.Err(); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}

	// create Material transactions table
	sql = `
		SELECT TransactionId,Time,SenderId,ReceiverId,tr.ResourceId,Commodity,Quality,Quantity
		FROM Transactions AS tr
		INNER JOIN Resources AS res ON tr.ResourceId = res.ResourceId
		INNER JOIN Products AS pd ON res.QualId = pd.QualId
		WHERE tr.SimId = ? AND pd.SimId = res.SimId AND res.SimId = tr.SimId
		AND res.Type = 'Product';
		`
	rows, err = db.Query(sql, simid)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}

	for rows.Next() {
		t := &ProdTrans{}
		if err := rows.Scan(&t.Id, &t.Time, &t.Sender, &t.Receiver, &t.ResourceId, &t.Commod, &t.Quality, &t.Qty); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			log.Print(err)
			return
		}
		rs.TransProds = append(rs.TransProds, t)
	}
	if err := rows.Err(); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}

	select {
	case <-kill:
		return
	default:
	}

	// render all results and save page
	rs.Uid = uid
	resultTmpl.Execute(w, rs)

	f, err := os.Create(uid + ".html")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Print(err)
		return
	}
	defer f.Close()
	resultTmpl.Execute(f, rs)
	close(kill)
}
Exemplo n.º 5
0
func main() {
	flag.Parse()
	log.SetFlags(log.Lshortfile)

	// load problem scen file
	scen := &Scenario{}
	err := scen.Load(*scenfile)
	fatalif(err)

	// build dakota input file from template and return
	if *dakotaGen {
		if *dakotaif == "" {
			*dakotaif = filepath.Base(scen.DakotaTmpl) + ".gen"
		}

		t := template.Must(template.ParseFiles(scen.DakotaTmpl))
		f, err := os.Create(*dakotaif)
		fatalif(err)
		defer f.Close()

		err = t.Execute(f, scen)
		fatalif(err)
		return
	}

	if *sweep != "" {
		db, err := sql.Open("sqlite3", *sweep)
		fatalif(err)
		defer db.Close()
		db.Exec(`
			CREATE TABLE IF NOT EXISTS Sweep (
				f1_t1 INTEGER,
				f1_t2 INTEGER,
				f1_t3 INTEGER,
				f1_t4 INTEGER,
				f1_t5 INTEGER,
				f2_t1 INTEGER,
				f2_t2 INTEGER,
				f2_t3 INTEGER,
				f2_t4 INTEGER,
				f2_t5 INTEGER,
				f3_t1 INTEGER,
				f3_t2 INTEGER,
				f3_t3 INTEGER,
				f3_t4 INTEGER,
				f3_t5 INTEGER,
				f4_t1 INTEGER,
				f4_t2 INTEGER,
				f4_t3 INTEGER,
				f4_t4 INTEGER,
				f4_t5 INTEGER,
				Objective REAL
			);
		`)
		smt, err := db.Prepare("INSERT INTO Sweep VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);")
		fatalif(err)

		tx, err := db.Begin()
		stmt := tx.Stmt(smt)
		fatalif(err)

		for i, p := range buildSweep() {
			if (i+1)%20 == 0 {
				fatalif(tx.Commit())
				tx, err = db.Begin()
				stmt = tx.Stmt(smt)
				fatalif(err)
			}

			scen.InitParams(p)
			cycdb, simid, err := RunSim(scen)
			fatalif(err)
			val, err := CalcObjective(cycdb, simid, scen)
			fatalif(err)

			vals := []interface{}{}
			for _, v := range p {
				vals = append(vals, v)
			}
			vals = append(vals, val)

			_, err = stmt.Exec(vals...)
			fatalif(err)
		}
		fatalif(tx.Commit())
		return
	}

	if *metric != "" {
		// post process cyclus output db
		db, err := sql.Open("sqlite3", *metric)
		fatalif(err)
		defer db.Close()

		fatalif(post.Prepare(db))
		defer post.Finish(db)

		simids, err := post.GetSimIds(db)
		fatalif(err)

		ctx := post.NewContext(db, simids[0])
		if err := ctx.WalkAll(); err != nil {
			fmt.Println(err)
		}

		// calculate and write out objective val
		val, err := CalcObjective(*metric, simids[0], scen)
		fatalif(err)

		fmt.Println(val)
		return
	}

	// Parse dakota params into scenario object
	paramsFile := flag.Arg(0)
	err = ParseParams(scen, paramsFile)
	fatalif(err)

	cycout, simid, err := RunSim(scen)
	fatalif(err)

	// calculate and write out objective val
	resultFile := flag.Arg(1)
	val, err := CalcObjective(cycout, simid, scen)
	fatalif(err)

	err = ioutil.WriteFile(resultFile, []byte(fmt.Sprint(val)), 0755)
	fatalif(err)
}