// GetAndInitPorousModel get porous model from material name // It returns nil on errors, after logging func GetAndInitPorousModel(mdb *inp.MatDb, matname, simfnk string) (mdl *mporous.Model, err error) { // materials cndmat, lrmmat, pormat, err := mdb.GroupGet3(matname, "c", "l", "p") if err != nil { err = chk.Err("materials database failed on getting %q (porous) group:\n%v", matname, err) return } // conductivity models getnew := false cnd := mconduct.GetModel(simfnk, cndmat.Name, cndmat.Model, getnew) if cnd == nil { err = chk.Err("cannot allocate conductivity models with name=%q", cndmat.Model) return } // retention model lrm := mreten.GetModel(simfnk, lrmmat.Name, lrmmat.Model, getnew) if lrm == nil { err = chk.Err("cannot allocate liquid retention model with name=%q", lrmmat.Model) return } // porous model mdl = mporous.GetModel(simfnk, pormat.Name, getnew) if mdl == nil { err = chk.Err("cannot allocate model for porous medium with name=%q", pormat.Name) return } // initialise all models // TODO: initialise just once err = cnd.Init(cndmat.Prms) if err != nil { err = chk.Err("cannot initialise conductivity model:\n%v", err) return } err = lrm.Init(lrmmat.Prms) if err != nil { err = chk.Err("cannot initialise liquid retention model:\n%v", err) return } err = mdl.Init(pormat.Prms, cnd, lrm) if err != nil { err = chk.Err("cannot initialise porous model:\n%v", err) return } return }
func main() { // input data simfn := "elast.sim" matname := "lrm1" pcmax := 30.0 npts := 101 // parse flags flag.Parse() if len(flag.Args()) > 0 { simfn = flag.Arg(0) } if len(flag.Args()) > 1 { matname = flag.Arg(1) } if len(flag.Args()) > 2 { pcmax = io.Atof(flag.Arg(2)) } if len(flag.Args()) > 3 { npts = io.Atoi(flag.Arg(3)) } // check extension if io.FnExt(simfn) == "" { simfn += ".sim" } // print input data io.Pf("\nInput data\n") io.Pf("==========\n") io.Pf(" simfn = %30s // simulation filename\n", simfn) io.Pf(" matname = %30s // material name\n", matname) io.Pf(" pcmax = %30v // max pc\n", pcmax) io.Pf(" npts = %30v // number of points\n", npts) io.Pf("\n") // load simulation sim := inp.ReadSim("", simfn, "lrm_", false) if sim == nil { io.PfRed("cannot load simulation\n") return } // get material data mat := sim.Mdb.Get(matname) if mat == nil { io.PfRed("cannot get material\n") return } io.Pforan("mat = %v\n", mat) // get and initialise model mdl := mreten.GetModel(simfn, matname, mat.Model, false) if mdl == nil { io.PfRed("cannot allocate model\n") return } mdl.Init(mat.Prms) // plot drying path d_Pc := utl.LinSpace(0, pcmax, npts) d_Sl := make([]float64, npts) d_Sl[0] = 1 var err error for i := 1; i < npts; i++ { d_Sl[i], err = mreten.Update(mdl, d_Pc[i-1], d_Sl[i-1], d_Pc[i]-d_Pc[i-1]) if err != nil { io.PfRed("drying: cannot updated model\n%v\n", err) return } } plt.Plot(d_Pc, d_Sl, io.Sf("'b-', label='%s (dry)', clip_on=0", matname)) // plot wetting path w_Pc := utl.LinSpace(pcmax, 0, npts) w_Sl := make([]float64, npts) w_Sl[0] = d_Sl[npts-1] for i := 1; i < npts; i++ { w_Sl[i], err = mreten.Update(mdl, w_Pc[i-1], w_Sl[i-1], w_Pc[i]-w_Pc[i-1]) if err != nil { io.PfRed("wetting: cannot updated model\n%v\n", err) return } } plt.Plot(w_Pc, w_Sl, io.Sf("'c-', label='%s (wet)', clip_on=0", matname)) // save results type Results struct{ Pc, Sl []float64 } res := Results{append(d_Pc, w_Pc...), append(d_Sl, w_Sl...)} var buf bytes.Buffer enc := json.NewEncoder(&buf) err = enc.Encode(&res) if err != nil { io.PfRed("cannot encode results\n") return } fn := path.Join(sim.Data.DirOut, matname+".dat") io.WriteFile(fn, &buf) io.Pf("file <[1;34m%s[0m> written\n", fn) // show figure plt.AxisYrange(0, 1) plt.Cross() plt.Gll("$p_c$", "$s_{\\ell}$", "") plt.Show() }
func Test_derivs01(tst *testing.T) { //verbose() //doplot := true doplot := false chk.PrintTitle("derivs01") // info simfnk := "derivs01" matname := "mat1" getnew := false example := true // conductivity model cnd := mconduct.GetModel(simfnk, matname, "m1", getnew) err := cnd.Init(cnd.GetPrms(example)) if err != nil { tst.Errorf("mconduct.Init failed: %v\n", err) return } // liquid retention model lrm_name := "ref-m1" //lrm_name := "vg" lrm := mreten.GetModel(simfnk, matname, lrm_name, getnew) err = lrm.Init(lrm.GetPrms(example)) if err != nil { tst.Errorf("mreten.Init failed: %v\n", err) return } // porous model mdl := GetModel(simfnk, matname, getnew) err = mdl.Init(mdl.GetPrms(example), cnd, lrm) if err != nil { tst.Errorf("mporous.Init failed: %v\n", err) return } //mdl.Ncns = true //mdl.Ncns2 = true // path pc0 := 0.0 pcf := 20.0 np := 5 //P := []float64{10, 5, 20, 0} P := []float64{5} Pc := GetPathCycle(pc0, P, np) io.Pforan("Pc = %v\n", Pc) // driver var drv Driver err = drv.Init(mdl) if err != nil { tst.Errorf("test failed: %v\n", err) return } err = drv.Run(Pc) if err != nil { tst.Errorf("test failed: %v\n", err) return } // plot if doplot { npts := 41 plt.Reset() mreten.Plot(mdl.Lrm, pc0, 1.0, pcf, npts, "'b.-'", "'r+-'", lrm_name) n := len(drv.Res) Sl := make([]float64, n) for i, s := range drv.Res { Sl[i] = s.A_sl } plt.Plot(Pc, Sl, "'ko--', clip_on=0") mreten.PlotEnd(true) } }
func Test_mdl01(tst *testing.T) { //verbose() //doplot := true doplot := false chk.PrintTitle("mdl01") // info simfnk := "mdl01" matname := "mat1" getnew := false example := true // conductivity model cnd := mconduct.GetModel(simfnk, matname, "m1", getnew) err := cnd.Init(cnd.GetPrms(example)) if err != nil { tst.Errorf("mconduct.Init failed: %v\n", err) return } // liquid retention model lrm_name := "ref-m1" //lrm_name := "vg" lrm := mreten.GetModel(simfnk, matname, lrm_name, getnew) err = lrm.Init(lrm.GetPrms(example)) if err != nil { tst.Errorf("mreten.Init failed: %v\n", err) return } // porous model mdl := GetModel(simfnk, matname, getnew) err = mdl.Init(mdl.GetPrms(example), cnd, lrm) if err != nil { tst.Errorf("mporous.Init failed: %v\n", err) return } //mdl.MEtrial = false mdl.ShowR = true // initial and final values pc0 := -5.0 sl0 := 1.0 pcf := 20.0 // plot lrm if doplot { npts := 41 plt.Reset() mreten.Plot(mdl.Lrm, pc0, sl0, pcf, npts, "'b.-'", "'r+-'", lrm_name) } // state A pcA := 5.0 A, err := mdl.NewState(mdl.RhoL0, mdl.RhoG0, -pcA, 0) if err != nil { tst.Errorf("mporous.NewState failed: %v\n", err) return } // state B pcB := 10.0 B, err := mdl.NewState(mdl.RhoL0, mdl.RhoG0, -pcB, 0) if err != nil { tst.Errorf("mporous.NewState failed: %v\n", err) return } // plot A and B points if doplot { plt.PlotOne(pcA, A.A_sl, "'gs', clip_on=0, label='A', ms=10") plt.PlotOne(pcB, B.A_sl, "'ks', clip_on=0, label='B'") } // incremental update //Δpl := -20.0 // << problems with this one and VG Δpl := -5.0 n := 23 iwet := 10 Pc := make([]float64, n) Sl := make([]float64, n) pl := -pcA Pc[0] = pcA Sl[0] = A.A_sl for i := 1; i < n; i++ { if i > iwet { Δpl = -Δpl iwet = n } pl += Δpl err = mdl.Update(A, Δpl, 0, pl, 0) if err != nil { tst.Errorf("test failed: %v\n", err) return } Pc[i] = -pl Sl[i] = A.A_sl } // show graph if doplot { plt.Plot(Pc, Sl, "'ro-', clip_on=0, label='update'") mreten.PlotEnd(true) } }
func main() { // catch errors defer func() { if err := recover(); err != nil { io.PfRed("ERROR: %v\n", err) } }() // input data simfn, _ := io.ArgToFilename(0, "elast", ".sim", true) matname := io.ArgToString(1, "lrm1") pcmax := io.ArgToFloat(2, 30.0) npts := io.ArgToInt(3, 101) // print input table io.Pf("\n%s\n", io.ArgsTable( "simulation filename", "simfn", simfn, "material name", "matname", matname, "max pc", "pcmax", pcmax, "number of points", "npts", npts, )) // load simulation sim := inp.ReadSim(simfn, "lrm", false, 0) if sim == nil { io.PfRed("cannot load simulation\n") return } // get material data mat := sim.MatParams.Get(matname) if mat == nil { io.PfRed("cannot get material\n") return } io.Pforan("mat = %v\n", mat) // get and initialise model mdl := mreten.GetModel(simfn, matname, mat.Model, false) if mdl == nil { io.PfRed("cannot allocate model\n") return } mdl.Init(mat.Prms) // plot drying path d_Pc := utl.LinSpace(0, pcmax, npts) d_Sl := make([]float64, npts) d_Sl[0] = 1 var err error for i := 1; i < npts; i++ { d_Sl[i], err = mreten.Update(mdl, d_Pc[i-1], d_Sl[i-1], d_Pc[i]-d_Pc[i-1]) if err != nil { io.PfRed("drying: cannot updated model\n%v\n", err) return } } plt.Plot(d_Pc, d_Sl, io.Sf("'b-', label='%s (dry)', clip_on=0", matname)) // plot wetting path w_Pc := utl.LinSpace(pcmax, 0, npts) w_Sl := make([]float64, npts) w_Sl[0] = d_Sl[npts-1] for i := 1; i < npts; i++ { w_Sl[i], err = mreten.Update(mdl, w_Pc[i-1], w_Sl[i-1], w_Pc[i]-w_Pc[i-1]) if err != nil { io.PfRed("wetting: cannot updated model\n%v\n", err) return } } plt.Plot(w_Pc, w_Sl, io.Sf("'c-', label='%s (wet)', clip_on=0", matname)) // save results type Results struct{ Pc, Sl []float64 } res := Results{append(d_Pc, w_Pc...), append(d_Sl, w_Sl...)} var buf bytes.Buffer enc := json.NewEncoder(&buf) err = enc.Encode(&res) if err != nil { io.PfRed("cannot encode results\n") return } fn := path.Join(sim.Data.DirOut, matname+".dat") io.WriteFile(fn, &buf) io.Pf("file <[1;34m%s[0m> written\n", fn) // show figure plt.AxisYrange(0, 1) plt.Cross("") plt.Gll("$p_c$", "$s_{\\ell}$", "") plt.Show() }