24小时热门版块排行榜    

Znn3bq.jpeg
查看: 277  |  回复: 1
当前主题已经存档。

lovein

金虫 (小有名气)

[交流] 【求助】麻烦帮忙看下一个“随机游走”的程序,为什么运行时老出错!!!

整个程序如下:
(*----------------------------------------------------------------------*)
BeginPackage["RW2D`"]
  RW2D::usage = "RW2D[maxstep, ktry, range, startarea, datadirectory]
    reads the two - dimensional digital image file (text file) and
    exports the mean - square displacement of random walkers as a text
    file, 'msdlist2d.out'.
     'maxstep' is the final value of the time step of the random walker.
     'ktry' is the number of average (i.e., number of walkers).
     'range' is the lower and upper limit of the voxel intensity of
    the pore space.
     'startarea' is the dimension of a square as the percentage to
    the system size.
     The random walker starts to diffuse from inside the square which is
    located at the center of the image.
     'datadirectory' is the directory or folder name where a single
    two-dimensional digital image file (text file) is located. "
  ShowTrajectory::usage = "ShowTrajectory[kk] is for the graphic display
    of the random walk trajectory of the 'kk' - th trial. "
Begin["`Private`"]
(* Reading the two - dimensional data in 'datadirectory' *)
readdata2d[range_List, datadirectory_] :=
    Module[{dataname, datasize, x, m2d, rmin = range[[1]], rmax = \
range[[2]]},
      dataname = First[FileNames["*", {datadirectory}]];
      datasize = Round[Sqrt[Length[ReadList[ToString[dataname], Real]]]];
      m2d =
        Partition[
          Map[If[rmin
(* random walk *)
(* 'xylist' is the trajectory of 'maxstep' - step from 'o2d' *)
tra2d[maxstep_, o2d_List, m2d_List] :=
    Module[{n2d = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}, now = o2d,
        xylist = {o2d}, outoffield = 0, xmax = ymax = Length[m2d],
        step, new, x, y},
      ran2d := Random[Integer, {1, 4}];
      For[i = 1, i
(* random walk of 'ktry' - walkers *)
(* 'tra2dktry' is list of the trajectory of 'ktry' - walkers *)
rw2d[maxstep_, ktry_, startarea_, m2d_List] :=
    Module[{xdatasize = ydatasize = Length[m2d], xylist, outoffield,
        tra2dktry = {}, failure = 0},
      rano2d[lx_, ly_, sa_] :=
        {Random[Integer, {(1 - sa/100)/2 lx, (1 + sa/100)/2lx}],
         Random[Integer, {(1 - sa/100)/2 ly, (1 + sa/100)/2ly}]};
      o2d := (For[i = 1; {x0, y0} = rano2d[xdatasize, ydatasize, startarea],
            m2d[[x0, y0]] != 0, i++,
            {x0, y0} = rano2d[xdatasize, ydatasize, startarea]]; {x0, y0});
      Do[{xylist, outoffield} = tra2d[maxstep, o2d, m2d];
        AppendTo[tra2dktry, xylist]; AddTo[failure, outoffield], {i, 1, \
ktry}];
      Return[{tra2dktry, failure}];
    ];
(* calculation of the mean - square displacement *)
msd2d[data_List] :=
    Module[{ktry = Dimensions[data][[1]], maxstep = Dimensions[data][[2]] - \
1,
         d1, d2, d3, dd, ii},
      d1 = Transpose[data, {2, 1}];
      d2 = Map[(# - First[d1]) &, d1];
      d3 = Transpose[Map[(#^2) &, d2]];
      dd = Apply[Plus, Transpose[Apply[Plus, d3]/ktry]];
      ii = Range[0, maxstep];
      msdlist2d = Transpose[{ii, dd}];
      Return[msdlist2d];
    ];
RW2D[maxstep_, ktry_, range_List, startarea_, datadirectory_] :=
    Module[{failure, msdlist2d},
      (* reading the two - dimensional data in 'datadirectory' *)
      m2d = readdata2d[range, datadirectory];
      (* execution of the random walk *)
      {tra2dktry, failure} = rw2d[maxstep, ktry, startarea, m2d];
      Print["Failure = ", failure, " / Success = ", ktry];
      (* calculation of the mean - square displacement of 'ktry' walkers *)
      msdlist2d = msd2d[tra2dktry];
      (* file output of the mean - square displacement *)
      (* The file name is 'msdlist2d.out' *)
      Export["msdlist2d.out", N[msdlist2d], "Table"];
      (* graphic output of the mean - square displacement *)
      ListPlot[msdlist2d, PlotJoined -> True,
          AxesLabel -> {"time \n step", "mean-square \n displacement"}];
    ];
(* graphic output of the trajectory *)
(* Show the trajectory of a single walker as an example. *)
(* RGBColor[] of 0, 1, 2 are colors of the pore, obstacle, and trajectory,
  respectively. You can change the color as you like. *)
gtra2d[tra2d_List, m2d_List] :=
    Module[{color0 = RGBColor[0.9, 0.9, 0.9],
            color1 = RGBColor[0.5, 0.5, 0.5],
            color2 = RGBColor[0.8, 0.8, 0.8],
            plmin = 1 - 2, plmax = Length[m2d] + 1},
      tra2dshift = Map[(# - {0.5, 0.5}) &, tra2d];
      tra2dm2d = (aa2 = m2d; Map[(aa2[[#[[1]], #[[2]]]] = 2) &, tra2d]; aa2);
      Show[
        Graphics[
          RasterArray[tra2dm2d /. {0 -> color0, 1 -> color1, 2 -> color2}]],
        Graphics[{Line[Map[Reverse, tra2dshift]], AbsolutePointSize[5.0],
            Point[Reverse[First[tra2dshift]]],
            Point[Reverse[Last[tra2dshift]]]}],
        PlotRange -> {{plmin, plmax}, {plmin, plmax}}, AspectRatio -> 1];
    ];
(* Only for the 'kk' - th trial, the trajectory of the random walk is shown
  graphically *)
ShowTrajectory[kk_] := gtra2d[tra2dktry[[kk]], m2d];
End[];
EndPackage[];
(*----------------------------------------------------------------------*)
(* example of usage *)
(*----------------------------------------------------------------------*)
(* As noted below, first of all, you put the command '<< RW2D.m'. *)
(* Then you can put the command set,
  'RW2D[500, 200, {0, 34000}, 10, "DATAsingle"];' and 'ShowTrajectory[2];'. \
*)
(* 'RW2D[maxstep, ktry, range, startarea, datadirectory]' is the command for
  the performance of the random walk and the calculation of mean - square
  displacement. *)
(* The command 'ShowTrajectory[kk]' is for the graphic display of the
  trajectory of the 'kk' - th trial. *)
(* In the example below, a trajectory of the second trial will be shown
  graphically. *)
(* In[1] := << RW2D.m *)
(* In[2] := RW2D[500, 200, {0, 34000}, 10, "DATAsingle"]; ShowTrajectory[2]; \
*)
不知道为啥,运行的时候老是出错,我又看不大懂这程序,请求帮忙

[ Last edited by lovein on 2009-10-26 at 13:35 ]
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

lovein

金虫 (小有名气)

附件是程序中需要调用的图像,即the two - dimensional digital image file (text file)

太长了,只能用附件发。还望帮忙。
灰常灰常感谢!!
2楼2009-10-26 13:39:16
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 lovein 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[基金申请] 今年审到国自然15份,谈谈感受 +8 国自然国社科中 2026-05-17 8/400 2026-05-17 17:18 by zhuzg0628
[基金申请] 这年头没有找到涵评专家,还有中面上的可能吗 +13 dd921ww 2026-05-12 16/800 2026-05-17 12:38 by 于轩
[论文投稿] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +5 xx7gd5zq4e 2026-05-15 7/350 2026-05-17 07:58 by 11n4dfd8yn
[考研] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +4 l7k6xnh0yc 2026-05-14 8/400 2026-05-17 07:26 by 11n4dfd8yn
[公派出国] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +6 l7k6xnh0yc 2026-05-14 6/300 2026-05-17 07:16 by 11n4dfd8yn
[考博] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +6 l7k6xnh0yc 2026-05-14 6/300 2026-05-17 07:11 by 11n4dfd8yn
[硕博家园] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +5 cjf4bx70cj 2026-05-14 7/350 2026-05-17 06:55 by 11n4dfd8yn
[考研] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 k37jurhrau 2026-05-16 3/150 2026-05-17 01:25 by ue3ir18jc3
[论文投稿] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 k37jurhrau 2026-05-16 3/150 2026-05-17 01:25 by ue3ir18jc3
[基金申请] 精华III评审感受-评审感受-评审感受 +16 ferrarichen 2026-05-11 20/1000 2026-05-17 01:10 by 南开小綦
[论文投稿] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +5 v9tggjlwd0 2026-05-15 5/250 2026-05-17 00:32 by xiangfeng
[基金申请] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 x0mp7owy2b 2026-05-15 4/200 2026-05-17 00:30 by ue3ir18jc3
[公派出国] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 v9tggjlwd0 2026-05-15 4/200 2026-05-17 00:15 by ue3ir18jc3
[基金申请] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +4 v9tggjlwd0 2026-05-15 4/200 2026-05-17 00:10 by ue3ir18jc3
[找工作] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +4 l7k6xnh0yc 2026-05-14 4/200 2026-05-16 23:10 by ue3ir18jc3
[硕博家园] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +4 x0mp7owy2b 2026-05-15 4/200 2026-05-16 17:45 by j6b2pdz07o
[有机交流] 如何实现卤原子转化 +3 BT20230424 2026-05-15 5/250 2026-05-16 16:20 by czyzsu
[考研] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +5 cjf4bx70cj 2026-05-14 6/300 2026-05-16 16:17 by 0i5p09z61n
[硕博家园] 售SCI一区T0P文章,我:8.O.5.5.1.O.5.4,科目齐全,可+急 +3 k37jurhrau 2026-05-16 3/150 2026-05-16 13:57 by vcdazktkjx
[考博] 材料类只有一篇综述能申博么 +4 乐逍遥谷 2026-05-13 4/200 2026-05-14 12:05 by zhyzzh
信息提示
请填处理意见