Welcome to ask.woodem.org. You may post when you login through your GitHub account.

problem when using SpherePack to load clumps from a .txt file, and export clumps to a .txt file

0 votes
33 views
asked Apr 18 by ElaineMun (220 points)

I am trying to load random-sizde clumps from a .txt file.

(1) Base on the discretption of this link(https://woodem.org/tutorial/export.html?highlight=spherepack), I prepared a very simple .txt file(name test.txt) which only contains 2 clumps. 5 colums in this file, first colum is the x-coordinate of sphere center, second colum is y-coordinate of sphere center, third colum is z-coordinate of sphere center, forth colum is radius of sphere, fifth colum is clump ID.

(2) Here is a part of my python script: I try to use it for loading 2 clumps from test.txt file when the simulation start, and exporting clumps to AfterStep1.txt before simulation stop.

########prepare runtime and ouput period#######
vtkPeriod = 200
totalRunTime = 90000            
backupCsvTime = 89000
#####################################
    
########Engines#######################################
Scene.run(steps=totalRunTime)
Scene.engines+=[woo.dem.VtkExport(what=VtkExport.all,stepPeriod=vtkPeriod,virtPeriod=-1,out=resultsPath+'/Result_keelgen')]
if Scene.step == 0:
   print("Load clumps from txt file")
   sp=woo.pack.SpherePack()
   sp.loadTxt('test.txt')
   sp.toSimulation(Scene,mat=Mat1)
   
if Scene.step == backupCsvTime:
   print("Now is backup time")
   sp=woo.pack.SpherePack()
   sp.fromDem(Scene,Scene.dem)
   sp.saveTxt('AfterStep1.txt')

if Scene.step == totalRunTime:
   Scene.stop()

(3) I got "RuntimeError: bad lexical cast: source type value could not be interpreted as target"

Did I prepare the .txt file with wrong format? Any idea about this? Thanks a lot!

1 Answer

0 votes
answered Apr 18 by eudoxos (44,930 points)
I give you a short answer, which won't be satisfactory, at least not for me. The file is read in c++, and lines are delimited using "\n" (linefeed character, LF, 0x0D) -- this is the case in Unix. Read some articles about history why WIndows uses CR+LF (two bytes,  0x0A 0x0D). So the 0x0A bytes is at the end of the last number, thus it fails to interpret that string as number. Quick solution for you: save the file with LF endings (Unix convention, it will be called something like that) and be happy.

Better solution: improve the code; I opened an issue https://github.com/woodem/woo/issues/24 to not forget.

Cheers!
commented Apr 19 by eudoxos (44,930 points)

The issue was fixed properly today; newer packages will be able to read the files regardless of line endings used in your file.

commented Apr 19 by ElaineMun (220 points)

Hi eudoxos,

Thanks for your answer. I just updated woo on my machine using sudo apt-get update, and sudo apt-get up grade. Then I tried to run the simulation again. Still not work. I still got "RuntimeError: bad lexical cast: source type value could not be interpreted as target" :(

Best regards,
Lei

commented Apr 19 by eudoxos (44,930 points)

Give it a few hours, before the machinery processes the stuff. But as a quick fix, save with Unix line endings and be happy.

commented Apr 19 by ElaineMun (220 points)

Thanks a lot! yes, I tried with another txt file generated on my Linux machine. It works! 2 clumps are loaded into the simulation from the txt file! But I met a error "failed to obtain meaningful dt" What that means? Thanks! enter image description here

enter image description here

commented Apr 19 by eudoxos (44,930 points)

Set S.dt manually as a quick fix. It probably means you are trying to run simulation before there are any "real" particles inside, thus automatic dt computation fails.

commented Apr 19 by ElaineMun (220 points)

Thanks! It works! I am trying to set-up a simulation which runs 90000 steps, load clumps from test2.txt at Scene.step is 0, and export clumps to AfterStep1.txt at Scene.step is 89000 (before simulation stop). But why the simulation only load clumps at step=0, but do not export clumps at step=89000? Is there something wrong with me script(see below)?

prepare runtime and ouput period

vtkPeriod = 200
totalRunTime = 90000
backupCsvTime = 89000

#
###Engines

Scene.run(steps=totalRunTime)
Scene.engines+=[woo.dem.VtkExport(what=VtkExport.all,stepPeriod=vtkPeriod,virtPeriod=-1,out=resultsPath+'/Result_keelgen')]
if Scene.step == 0:
print("Load clumps from txt file")
sp=woo.pack.SpherePack()
sp.loadTxt('test2.txt')
sp.toSimulation(Scene,mat=Mat1)

if Scene.step == backupCsvTime:
print("Now is backup time")
sp1=woo.pack.SpherePack()
sp1.fromDem(Scene,Scene.dem)
sp1.saveTxt('AfterStep1.txt')

if Scene.step == totalRunTime:
Scene.stop()

commented May 1 by eudoxos (44,930 points)

I somehow missed your question last time. Simulations are usually declarative, i.e. you declare what is supposed to be run in the script, and then run the simulation. So the first if works, as step==0 before the simulation starts. The second condition is never true, because it is evaluated while step is still 0. The same for the third one -- the simulation stops because of S.run(steps=...), not because of that condition.

commented May 2 by ElaineMun (220 points)

Thanks for your reply!

...