first
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package control_case
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/otiai10/copy"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ControlCase struct {
|
||||
gorm.Model
|
||||
Name string `json:"name"`
|
||||
ParamsID uint `json:"params_id"`
|
||||
Params Params `json:"params"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
type Params struct {
|
||||
gorm.Model
|
||||
ID uint `toml:"-" csv:"id"`
|
||||
Rel float64 `toml:"rel" csv:"rel"`
|
||||
RelC float64 `toml:"rel_c" csv:"rel_c"`
|
||||
Le float64 `toml:"le" csv:"le"`
|
||||
Pr float64 `toml:"pr" csv:"pr"`
|
||||
Pe float64 `toml:"pe" csv:"pe"`
|
||||
InitialCondition string `toml:"-" csv:"initial_condition"`
|
||||
Time float64 `toml:"time" csv:"time"`
|
||||
FolderPath string `toml:"-" csv:"-"`
|
||||
}
|
||||
|
||||
func (p *Params) ToTOML(filename string) error {
|
||||
file, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
encoder := toml.NewEncoder(file)
|
||||
return encoder.Encode(p)
|
||||
}
|
||||
|
||||
func (u *Params) AfterCreate(tx *gorm.DB) (err error) {
|
||||
control_case := ControlCase{ParamsID: u.ID, Status: "N"}
|
||||
tx.Create(&control_case)
|
||||
return
|
||||
}
|
||||
func (p *Params) Exist() (bool, error) {
|
||||
folderPath := filepath.Join("./", fmt.Sprintf("%d", p.ID))
|
||||
return exists(folderPath)
|
||||
}
|
||||
func (p *Params) CreateFolder() {
|
||||
BIN_PATH := "./bio-convect"
|
||||
folderPath := filepath.Join("./", fmt.Sprintf("%d", p.ID))
|
||||
os.MkdirAll(folderPath, os.ModePerm)
|
||||
fmt.Printf("folder: %s\n", folderPath)
|
||||
execPath := filepath.Join("./", folderPath, BIN_PATH)
|
||||
err := copy.Copy(BIN_PATH, execPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if p.InitialCondition != "E" {
|
||||
startPath := filepath.Join("./", folderPath, "storag.h5")
|
||||
err = copy.Copy(p.InitialCondition, startPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
p.ToTOML(filepath.Join(folderPath, "config.toml"))
|
||||
p.FolderPath = folderPath
|
||||
|
||||
}
|
||||
func (p *Params) Run(ctx context.Context) {
|
||||
BIN_PATH := "./bio-convect"
|
||||
cmd := exec.CommandContext(ctx, BIN_PATH)
|
||||
cmd.Dir = p.FolderPath
|
||||
//cmd.Stdout = os.Stdout
|
||||
//cmd.Stderr = os.Stderr
|
||||
|
||||
fmt.Printf("before run")
|
||||
err := cmd.Run()
|
||||
fmt.Printf("afrer run")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user