本文整理汇总了Golang中github.com/cpmech/gosl/io.ReadFile函数的典型用法代码示例。如果您正苦于以下问题:Golang ReadFile函数的具体用法?Golang ReadFile怎么用?Golang ReadFile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ReadFile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ReadMsh
// ReadMsh reads .msh file
func ReadMsh(fnk string) (nurbss []*Nurbs) {
// read file
fn := fnk + ".msh"
buf, err := io.ReadFile(fn)
if err != nil {
chk.Panic("ReadMsh cannot read file = '%s', %v'", fn, err)
}
// decode
var dat Data
err = json.Unmarshal(buf, &dat)
if err != nil {
chk.Panic("ReadMsh cannot unmarshal file = '%s', %v'", fn, err)
}
// list of vertices
verts := make([][]float64, len(dat.Verts))
for i, _ := range dat.Verts {
verts[i] = make([]float64, 4)
for j := 0; j < 4; j++ {
verts[i][j] = dat.Verts[i].C[j]
}
}
// allocate NURBSs
nurbss = make([]*Nurbs, len(dat.Nurbss))
for i, v := range dat.Nurbss {
nurbss[i] = new(Nurbs)
nurbss[i].Init(v.Gnd, v.Ords, v.Knots)
nurbss[i].SetControl(verts, v.Ctrls)
}
return
}
示例2: PyFile
func PyFile(filename string) {
b, err := io.ReadFile(filename)
if err != nil {
chk.Panic("PyFile failed:\n%v", err)
}
io.Ff(&bb, string(b))
}
示例3: Read
// Read reads configuration parameters from JSON file
func (o *Parameters) Read(filenamepath string) {
o.Default()
b, err := io.ReadFile(filenamepath)
if err != nil {
chk.Panic("cannot read parameters file %q", filenamepath)
}
err = json.Unmarshal(b, o)
if err != nil {
chk.Panic("cannot unmarshal parameters file %q", filenamepath)
}
return
}
示例4: NewData
// NewData allocates and initialises a new FEM data structure
func NewData(fnkey string, cpu int) *FemData {
// load data
var o FemData
buf, err := io.ReadFile("od-" + fnkey + ".json")
if err != nil {
chk.Panic("cannot load data:\n%v", err)
}
err = json.Unmarshal(buf, &o)
if err != nil {
chk.Panic("cannot unmarshal data:\n%v", err)
}
// load FEM data
o.Analysis = fem.NewMain(fnkey+".sim", io.Sf("cpu%d", cpu), false, false, false, false, false, cpu)
o.Dom = o.Analysis.Domains[0]
o.Sim = o.Dom.Sim
o.Vars = o.Dom.Sim.AdjRandom
// backup dependent variables
for _, prm := range o.Sim.AdjDependent {
prm.S = prm.V // copy V into S
}
// set stage
err = o.Analysis.SetStage(0)
if err != nil {
chk.Panic("cannot set stage:\n%v", err)
}
// equations to track U
verts := o.Dom.Msh.Verts
if o.VtagU < 0 {
verts = o.Dom.Msh.VertTag2verts[o.VtagU]
if len(verts) < 1 {
chk.Panic("cannot find vertices with tag = %d\n", o.VtagU)
}
}
o.EqsU = make([]int, len(verts))
for i, vert := range verts {
eq := o.Dom.Vid2node[vert.Id].GetEq(o.Ukey)
if eq < 0 {
chk.Panic("cannot find equation corresponding to vertex id=%d and ukey=%q", vert.Id, o.Ukey)
}
o.EqsU[i] = eq
}
return &o
}
示例5: read_pyfem_rod_data
func read_pyfem_rod_data(fn string) []PyfemRodData {
// read file
b, err := io.ReadFile(fn)
if err != nil {
chk.Panic("%v\n", err.Error())
}
// decode
var dat []PyfemRodData
err = json.Unmarshal(b, &dat)
if err != nil {
chk.Panic("%v\n", err.Error())
}
return dat
}
示例6: ReadJson
// ReadJson reads json file
func (o *Path) ReadJson(ndim int, fname string) (err error) {
// read file
b, err := io.ReadFile(fname)
if err != nil {
return chk.Err(_path_err11, fname, err)
}
// decode
err = json.Unmarshal(b, o)
if err != nil {
return chk.Err(_path_err12, fname, err)
}
// set additional information
return o.Init(ndim)
}
示例7: main
func main() {
// filename
filename, fnkey := io.ArgToFilename(0, "sg1121", ".sim", true)
// results
out.Start(filename, 0, 0)
out.Define("A", out.N{30})
out.LoadResults(nil)
// plot FEM results
out.Plot("t", "uy", "A", plt.Fmt{C: "k", Ls: "-", L: "gofem"}, -1)
// old results
b, err := io.ReadFile("cmp/sg1121gofemold.json")
if err != nil {
io.PfRed("cannot read comparison file\n")
return
}
var gofemold struct {
Time, Uy30 []float64
}
err = json.Unmarshal(b, &gofemold)
if err != nil {
io.PfRed("cannot unmarshal comparison file\n")
return
}
// mechsys results
_, res, err := io.ReadTable("cmp/sg1121mechsysN30.cmp")
if err != nil {
io.PfRed("cannot read mechsys comparison file\n")
return
}
// save
plt.SetForPng(0.8, 400, 200)
out.Draw("/tmp", fnkey+".png", false, func(i, j, n int) {
plt.Plot(gofemold.Time, gofemold.Uy30, "'r-', lw=2, label='gofemOld'")
plt.Plot(res["Time"], res["uy"], "'b-', label='mechsys'")
})
}
示例8: ReadMat
// ReadMat reads all materials data from a .mat JSON file
// Note: returns nil on errors
func ReadMat(dir, fn string) *MatDb {
// new mat
var o MatDb
// read file
b, err := io.ReadFile(filepath.Join(dir, fn))
if err != nil {
return nil
}
// decode
err = json.Unmarshal(b, &o)
if err != nil {
return nil
}
// results
return &o
}
示例9: ReadMat
// ReadMat reads all materials data from a .mat JSON file
// Note: returns nil on errors
func ReadMat(dir, fn string) *MatDb {
// new mat
var o MatDb
// read file
b, err := io.ReadFile(filepath.Join(dir, fn))
if LogErr(err, "mat: cannot open materials file "+dir+"/"+fn) {
return nil
}
// decode
if LogErr(json.Unmarshal(b, &o), "mat: cannot unmarshal materials file "+fn) {
return nil
}
// log
log.Printf("mat: fn=%s nfunctions=%d nmaterials=%d\n", fn, len(o.Functions), len(o.Materials))
return &o
}
示例10: Read
// Read reads mesh
func Read(fn string) (o *Mesh, err error) {
// new mesh
o = new(Mesh)
// read file
b, err := io.ReadFile(fn)
if err != nil {
return
}
// decode
err = json.Unmarshal(b, &o)
if err != nil {
return
}
// check
err = o.Check()
return
}
示例11: ReadConfParams
// ReadConfParams reads configuration parameters from JSON file
func ReadConfParams(filenamepath string) *ConfParams {
// new params
var o ConfParams
o.SetDefault()
// read file
b, err := io.ReadFile(filenamepath)
if err != nil {
chk.Panic("cannot read parameters file %q", filenamepath)
}
// decode
err = json.Unmarshal(b, &o)
if err != nil {
chk.Panic("cannot unmarshal parameters file %q", filenamepath)
}
// results
o.CalcDerived()
return &o
}
示例12: Test_step02
func Test_step02(tst *testing.T) {
//verbose()
chk.PrintTitle("step02")
buf, err := io.ReadFile("data/beadpanel.step")
if err != nil {
tst.Errorf("cannot read file:\n%v", err)
return
}
dat := string(buf)
var stp STEP
err = stp.ParseDATA(dat)
if err != nil {
tst.Errorf("Parse filed:\n%v", err)
return
}
for _, c := range stp.BScurves {
io.Pforan("c = %v\n", c)
}
}
示例13: MatfileNew2Old
// MatfileNew2Old converts a new mat file to the old mat file format
// convertsymbols -- convert back symbols with Greek characters to UTF-8
func MatfileNew2Old(dirout string, fnold, fnnew string, convertsymbols bool) {
// read file
b, err := io.ReadFile(fnnew)
if err != nil {
io.PfRed("cannot open file: %v", err.Error())
return
}
// decode
var mats_new MatDb
err = json.Unmarshal(b, &mats_new)
if err != nil {
io.PfRed("cannot unmarshal file: %v", err.Error())
return
}
// oldmatdata implements the old data format
type oldmatdata struct {
Name string
Desc string
Model string
Prms []string
Vals []float64
Units []string
Extra string
}
// convert
var mats_old []oldmatdata
for _, m := range mats_new.Materials {
var oldmat oldmatdata
oldmat.Name = m.Name
oldmat.Desc = m.Desc
oldmat.Model = m.Model
oldmat.Extra = m.Extra
for _, prm := range m.Prms {
name := prm.N
if convertsymbols {
if n, ok := invertedconversiontable[prm.N]; ok {
name = n
}
}
oldmat.Prms = append(oldmat.Prms, name)
oldmat.Vals = append(oldmat.Vals, prm.V)
if len(prm.U) > 0 {
oldmat.Units = append(oldmat.Units, prm.U)
}
}
mats_old = append(mats_old, oldmat)
}
// encode
buf, err := json.MarshalIndent(mats_old, "", " ")
if err != nil {
return
}
// save file
if dirout == "" {
io.WriteFileS(fnold, string(buf))
return
}
io.WriteFileSD(dirout, fnold, string(buf))
}
示例14: MatfileOld2New
// MatfileOld2New converts an old mat file to new mat file format
// convertsymbols -- convert symbols with Greek characters to ANSI
func MatfileOld2New(dirout string, fnnew, fnold string, convertsymbols bool) {
// oldmatdata implements the old data format
type oldmatdata struct {
Name string
Desc string
Model string
Prms []string
Vals []float64
Units []string
Extra string
}
// data holder
var mats_old []oldmatdata
// read file
b, err := io.ReadFile(fnold)
if err != nil {
chk.Panic("cannot open file: %v", err.Error())
}
// decode
err = json.Unmarshal(b, &mats_old)
if err != nil {
chk.Panic("cannot unmarshal file: %v", err.Error())
}
// new data holder
var mats_new MatDb
for _, m := range mats_old {
prms := []*fun.Prm{}
has_units := len(m.Units) == len(m.Prms)
for i, prm := range m.Prms {
name := prm
if convertsymbols {
if n, ok := conversiontable[prm]; ok {
name = n
}
}
if has_units {
prms = append(prms, &fun.Prm{N: name, V: m.Vals[i], U: m.Units[i]})
} else {
prms = append(prms, &fun.Prm{N: name, V: m.Vals[i]})
}
}
mat_new := Material{
Name: m.Name,
Desc: m.Desc,
Model: m.Model,
Extra: m.Extra,
Prms: prms,
}
mats_new.Materials = append(mats_new.Materials, &mat_new)
}
// save file
if dirout == "" {
io.WriteFileS(fnnew, mats_new.String())
return
}
io.WriteFileSD(dirout, fnnew, mats_new.String())
}
示例15: NewData
// NewData allocates and initialises a new FEM data structure
func NewData(filename, fnkey string, cpu int) *FemData {
// load opt data
var o FemData
buf, err := io.ReadFile("od-" + fnkey + ".json")
if err != nil {
chk.Panic("cannot load opt data:\n%v", err)
}
err = json.Unmarshal(buf, &o.Opt)
if err != nil {
chk.Panic("cannot unmarshal opt data:\n%v", err)
}
o.Opt.CalcDerived()
// load FEM data
o.Analysis = fem.NewFEM(filename, io.Sf("cpu%d", cpu), false, false, false, false, false, cpu)
o.Reg = o.Analysis.Sim.Regions[0]
o.Dom = o.Analysis.Domains[0]
// required vertices
for _, vtag := range o.Opt.ReqVtags {
verts := o.Dom.Msh.VertTag2verts[vtag]
for _, vert := range verts {
o.ReqVids = append(o.ReqVids, vert.Id)
}
}
// groups
o.Ncells = len(o.Dom.Msh.Cells)
o.Cid2xid = make([]int, o.Ncells)
if o.Opt.Groups {
o.Nareas = len(o.Dom.Msh.CellTag2cells)
for tag, cells := range o.Dom.Msh.CellTag2cells {
xid := -tag - 1
for _, cell := range cells {
o.Cid2xid[cell.Id] = xid
}
}
} else { // all cells
o.Nareas = o.Ncells
for i, _ := range o.Dom.Msh.Cells {
o.Cid2xid[i] = i
}
}
// vertex to track deflection
o.VidU = -1 // allvertices
if o.Opt.VtagU < 0 {
verts := o.Dom.Msh.VertTag2verts[o.Opt.VtagU]
if len(verts) != 1 {
chk.Panic("cannot set vertex to track deflection: verts = %v", verts)
}
o.VidU = verts[0].Id
}
// compute max weight
o.Analysis.SetStage(0)
for _, elem := range o.Dom.Elems {
ele := elem.(*fem.ElastRod)
o.MaxWeight += ele.Mdl.Rho * ele.Mdl.A * ele.L
}
return &o
}