Files
control/control_case/models.go
T
2026-07-13 11:17:57 +05:00

133 lines
3.9 KiB
Go

package control_case
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/BurntSushi/toml"
"github.com/otiai10/copy"
"gorm.io/gorm"
)
type ControlCase struct {
ID uint `gorm:"primaryKey" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
Name string `json:"name"`
ParamsID uint `json:"params_id"`
Params Params `json:"params"`
Status string `json:"status"`
}
func (c ControlCase) FooCSVPath() string {
paramsID := c.ParamsID
if paramsID == 0 {
paramsID = c.Params.ID
}
return filepath.Join(BASE_DIR, fmt.Sprintf("%d", paramsID), "foo.csv")
}
func (c ControlCase) StorageH5Path() string {
paramsID := c.ParamsID
if paramsID == 0 {
paramsID = c.Params.ID
}
return filepath.Join(BASE_DIR, fmt.Sprintf("%d", paramsID), "storage.h5")
}
func (c ControlCase) CreateStorageH5FromLastState(outputPath string) error {
return CopyLastStateStorageH5(c.StorageH5Path(), outputPath)
}
type Params struct {
ID uint `gorm:"primaryKey" json:"id" toml:"-" csv:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
Rel float64 `json:"rel" toml:"rel" csv:"rel"`
RelC float64 `json:"rel_c" toml:"rel_c" csv:"rel_c"`
Le float64 `json:"le" toml:"le" csv:"le"`
Sc float64 `json:"sc" toml:"sc" csv:"sc"`
Pe float64 `json:"pe" toml:"pe" csv:"pe"`
Ma float64 `json:"ma" toml:"ma" csv:"ma"`
InitialConditionID *uint `json:"initial_condition_id" toml:"-" csv:"initial_condition_id"`
InitialCondition *InitialCondition `json:"initial_condition" toml:"-" csv:"-"`
Time float64 `json:"time" toml:"time" csv:"time"`
FolderPath string `json:"folder_path" toml:"-" csv:"-"`
}
type InitialCondition struct {
ID uint `gorm:"primaryKey" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
Name string `json:"name"`
FilePath string `json:"file_path"`
}
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 (p *Params) Exist() (bool, error) {
folderPath := filepath.Join(BASE_DIR, fmt.Sprintf("%d", p.ID))
return exists(folderPath)
}
func (p *Params) CreateFolder() error {
BIN_PATH := "./bio-convect"
folderPath := filepath.Join(BASE_DIR, fmt.Sprintf("%d", p.ID))
if err := os.MkdirAll(folderPath, os.ModePerm); err != nil {
return err
}
fmt.Printf("folder: %s\n", folderPath)
execPath := filepath.Join("./", folderPath, BIN_PATH)
err := copy.Copy(BIN_PATH, execPath)
if err != nil {
return err
}
if err := os.Chmod(execPath, 0o755); err != nil {
return err
}
if p.InitialCondition != nil && p.InitialCondition.FilePath != "" {
startPath := filepath.Join("./", folderPath, "storage.h5")
err = copy.Copy(p.InitialCondition.FilePath, startPath)
if err != nil {
return err
}
}
if err := p.ToTOML(filepath.Join(folderPath, "config.toml")); err != nil {
return err
}
p.FolderPath = folderPath
return nil
}
func (p *Params) Run(ctx context.Context) error {
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 {
return err
}
return nil
}