pack.SpherePack.save() function saved particles data format

Asked by gaoxuesong on 2020-02-12

Hi. I have a file of spherical particles saved by function of pack.SpherePack().save(). It has the format as follows,

0.000919246 0.000859971 0.000602774 1.20067e-05 -1
0.000959836 0.00055149 0.000975021 2.11065e-05 -1
.....

I want to translate all the particles in x direction and conduct it by the following python code,
=======================================
=======================================
import numpy as np

data = np.loadtxt('initial_spheres.txt', delimiter=' ', skiprows=0)

with open('initial_spheres_translate_x.txt','w') as fd:
    for i in range(len(data)):
        fd.write('%.9f %.9f %.9f %.5e %d\n' % (data[i,0]+0.5e-3, data[i,1], data[i,2], data[i,3], -1))
=======================================
=======================================
 and i got the data as this,

0.001919246 0.000859971 0.000602774 1.20067e-05 -1
0.001959836 0.000551490 0.000975021 2.11065e-05 -1

The format seems the same, however, there is an format error when reading the file by pack.SpherePack().load() function.
By the way, i have tried the delimiter as space and tab and both don't work.

Thanks.

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
gaoxuesong
Solved:
2020-02-18
Last query:
2020-02-18
Last reply:
2020-02-18
Robert Caulk (rcaulk) said : #1

>there is an format error when reading the file

What is the error?

gaoxuesong (260582472-9) said : #2

The error is shown as,

    RuntimeError: bad lexical cast: source type value could not be interpreted as target

Thanks.

Jan Stránský (honzik) said : #3

Please provide a MWE [1] (e.g. a packing with 3 manually inserted spheres), then it will be much easier to help you..

thanks
Jan

[1] https://www.yade-dem.org/wiki/Howtoask

gaoxuesong (260582472-9) said : #4

Sorry for that. Below is my MWE.
1. the initial particles set saved by
    sp = pack.SpherePack()
    sp.save('initial_spheres-before_translation-x.txt')

Find file here,
https://drive.google.com/file/d/1VIYd7qzb5b4iIAyldT49I1Fjko8XxjhI/view?usp=sharing

2. the python script to translate those particles in x direction,
    import numpy as np

    data = np.loadtxt('initial_spheres-before-translation-x.txt', delimiter=' ', skiprows=0)

    with open('initial_spheres_translate_x.txt','w') as fd:
        for i in range(len(data)):
             fd.write('%.9f %.9f %.9f %.5e %d\n' % (data[i,0]+0.5e-3, data[i,1], data[i,2], data[i,3], -1))

Find file here,
https://drive.google.com/file/d/1P2kVPWFX0VoTzyqVD9uwruodb16Zof5s/view?usp=sharing

3. The yade code to load the translated particles,
    from yade import pack,geom,utils

    sp = pack.SpherePack()
    sp.load('initial_spheres_translate_x.txt')
### the initial particles before translation can be loaded ###
    #sp.load('initial_spheres-before-translation-x.txt')
    print('load initial_spheres')
    sp.toSimulation()

Thanks.

Jan Stránský (honzik) said : #5

Hello,

if I used the provided file, I got the same error.
However, if I used the code to create a new translated file, I could load it without any problem.
After diffing the files, it turned out that in the original files there are "^M" (carriage return) symbols at the end of lines.
How/where/on which OS have you produced the translated files?

using terminal command
sed -i 's/\r//' initial_spheres_translate_x.txt
fixes the problem

cheers
Jan

gaoxuesong (260582472-9) said : #6

Hi Jan,

    I use a Spyder editor integrated in Anaconda on Windows 10 to conduct the translation code. By the way, what does the '/\r//' mean in your command?

Thanks.

gaoxuesong (260582472-9) said : #7

Which editor can show the carriage return symbols, such as '^M'?
I have opened the translated file using the Ubuntu default editor but there is no format error.

Jan Stránský (honzik) said : #8

> on Windows 10

^M, carriage return, \n\r... is a "windows newline character" (big quotation marks). Usually it is OK, but sometimes you might meet a problem.

> what does the '/\r//' mean in your command?

's/\r//'
= replace string between first slashes with string between second slashes
= replace \r with nothing = remove \r (carriage return)

> Which editor can show the carriage return symbols, such as '^M'?

e.g. vim. Open a file and run
:e ++ff=unix
I am sure emacs can do it, too.

> but there is no format error.

it is not an error, just the newline symbols are different. Text editors should have no problem.
But, obviously, SpherePack.load does not count with it.

cheers
Jan

gaoxuesong (260582472-9) said : #9

I have tried and removed the '\r' following your method.

Thanks Jan. Your instructions are really helpful.