wrap a simulation to a class
Hi,
I want to write a class, the __init__() function of which is the set up of a packing, and some other method functions of which include isoConsolidation(), applyStrain(), getStress(), etc. So with this class, I can run a simulation (triaxial, simple shear or any other kind) by directly calling this class and its method functions. Because I want to run several simulations simultaneously within another python code.
First I want to know how to execute a yade file using `python filename` with appropriate libraries imported rather than using `yade filename`? After knowing this, I think I can find out how to write such a wrapped class.
Best regards,
Ning
Question information
- Language:
- English Edit question
- Status:
- Solved
- For:
- Yade Edit question
- Assignee:
- No assignee Edit question
- Solved by:
- Jan Stránský
- Solved:
- Last query:
- Last reply:
Revision history for this message
![]() |
#1 |
On 20 December 2011 12:15, ceguo <email address hidden>wrote:
> New question #182459 on Yade:
> https:/
>
> Hi,
>
> I want to write a class, the __init__() function of which is the set up of
> a packing, and some other method functions of which include
> isoConsolidation(), applyStrain(), getStress(), etc. So with this class, I
> can run a simulation (triaxial, simple shear or any other kind) by directly
> calling this class and its method functions. Because I want to run several
> simulations simultaneously within another python code.
>
> First I want to know how to execute a yade file using `python filename`
> with appropriate libraries imported rather than using `yade filename`?
Maybe you can try to use the following function:
execfile(
> After knowing this, I think I can find out how to write such a wrapped
> class.
>
> Best regards,
> Ning
>
> --
> You received this question notification because you are a member of
> yade-users, which is an answer contact for Yade.
>
> _______
> Mailing list: https:/
> Post to : <email address hidden>
> Unsubscribe : https:/
> More help : https:/
>
Revision history for this message
![]() |
#2 |
Hi Chiara,
Thanks for your quick reply. But my ultimate goal is to wrap a simulation to a class. Something like:
class triaxialTest():
def __init__(self):
self.__O=O
self.
self.
self.
def isoConsolidatio
self.
self.
self.
self.
def applyStrain(self, strain):
self.
self.
self.
self.
def getStress(self):
return self.__stress
......
To achieve this, I need to know how these variables (e.g. O) are loaded and from which library.
Ning
Revision history for this message
![]() |
#3 |
>First I want to know how to execute a yade file using `python filename` with appropriate libraries imported rather than using `yade filename`?
"python yade" works perfectly.
Did you have a look at the yade executable itslef? It is a python script, and I think it will answer most of your questions.
Revision history for this message
![]() |
#4 |
Hi,
I made the yade library callable from python and now I can run a script using `python test.py`. The script for a biaxial test (first isotropic consolidation then undrained shear) is like this:
from yade.wrapper import *
from yade import *
from yade import pack,plot,utils
from miniEigen import Matrix3
O=Omega()
O.materials.
sp = pack.SpherePack()
sp.makeCloud(
sp.toSimulation()
O.cell.hSize = Matrix3(3,0,0, 0,3,0, 0,0,1)
print len(O.bodies)
for p in O.bodies:
p.state.
#plot.plots=
def saveAddData():
plot.addData(
stress_xx = biax.stress[0],
stress_yy = biax.stress[1],
stress_zz = biax.stress[2],
strain_xx = biax.strain[0],
strain_yy = biax.strain[1]
)
# print O.cell.hSize
O.dt = .5*utils.
O.engines = [
ForceResetter(),
InsertionSor
InteractionLoop(
[
[
[
),
Peri3dContro
goal=
stressMask=3,
nSteps=20000,
doneHook=
maxStrain=.5,
label='biax'
),
NewtonIntegr
]
O.run()
O.wait()
#O.materials.
O.engines = [
ForceResetter(),
InsertionSor
InteractionLoop(
[
[
[
),
Peri3dContro
goal=
stressMask=0,
nSteps=50000,
doneHook=
maxStrain=.5,
label='biax'
),
NewtonIntegr
PyRunner(
]
O.run(); O.wait()
plot.saveDataTx
O.exitNoBacktrace()
This works. Now I want to wrap it to a class named simDEM. I wrote like this:
from yade.wrapper import *
from yade import *
from yade import pack,plot,utils
from miniEigen import Matrix3
class simDEM(object):
def __init__(self):
self.
self.
self.
self.
self.
self.
for p in self.__O.bodies:
self.
def countParticle(
return len(self.
def saveAddData(self):
plot.addData(
stress_xx = biax.stress[0],
stress_yy = biax.stress[1],
stress_zz = biax.stress[2],
strain_xx = biax.strain[0],
strain_yy = biax.strain[1]
)
def consolidation(self, stress=-5.e4):
self.
),
),
]
self.
def shear(self, strain=-1.e-2):
self.
),
),
]
self.
def outputResult(self):
plot.
)
def finalization(self):
self.
This doesn't work smoothly within the function consolidation. But it's very close to what I want. I hope anyone who's more familiar with the code can help me to modify it. Below is what I got:
$ python
>>> from multiscaleDEM import simDEM
>>> a = simDEM()
>>> a.countParticle()
1342
>>> a.consolidation() # no response, doneHook not executed!
>>> a.shear() # no response, doneHook not executed! Neither PyRunner()
>>> a.outputResult()
Error! vars=('
plot.py in saveDataTxt for i in range(len(
Revision history for this message
![]() |
#5 |
This question was expired because it remained in the 'Open' state without activity for the last 15 days.
Revision history for this message
![]() |
#6 |
Hi ceguo,
Did you progress on your problem? For me it was not clear if the last error was due to the initial wrapping question or just to a command that would fail even in a standard script (guessing the latest).
Note the typo:
self.__O.eigines=[
Revision history for this message
![]() |
#7 |
Hi Chareyre,
Sorry for the delayed reply. I'm just back from the Chinese New Year Holiday.
After I corrected the typo, there is error when I executed `a.consolidation()` instead of without response:
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'Omega' is not defined
It seems there is problem within the engine (especially in `Peri3dController` because doneHook and PyRunner call other functions). So can I run a compression/shear test without using the engine? e.g. one command by one command...
Revision history for this message
![]() |
#8 |
Hi Ning,
I tried something similar some time ago and somehow I succeeded, but I
dont remember how exactly :-) could you please send me your script(s) so
I can try to find the problem?
In my case it was something like Omega was in some file without
yade.wrapper which resulted in this type of error..
Jan
ceguo píše v Po 30. 01. 2012 v 08:01 +0000:
> Question #182459 on Yade changed:
> https:/
>
> Status: Expired => Open
>
> ceguo is still having a problem:
> Hi Chareyre,
>
> Sorry for the delayed reply. I'm just back from the Chinese New Year Holiday.
> After I corrected the typo, there is error when I executed `a.consolidation()` instead of without response:
>
> Traceback (most recent call last):
> File "<string>", line 1, in <module>
> NameError: name 'Omega' is not defined
>
> It seems there is problem within the engine (especially in
> `Peri3dController` because doneHook and PyRunner call other functions).
> So can I run a compression/shear test without using the engine? e.g. one
> command by one command...
>
Revision history for this message
![]() |
#9 |
Peri3dController has doneHook set to O.pause() by default in r3011, so
you can try the script with none doneHooke specified, let us know if it
hepled.
Jan
ceguo píše v Po 30. 01. 2012 v 08:01 +0000:
> Question #182459 on Yade changed:
> https:/
>
> Status: Expired => Open
>
> ceguo is still having a problem:
> Hi Chareyre,
>
> Sorry for the delayed reply. I'm just back from the Chinese New Year Holiday.
> After I corrected the typo, there is error when I executed `a.consolidation()` instead of without response:
>
> Traceback (most recent call last):
> File "<string>", line 1, in <module>
> NameError: name 'Omega' is not defined
>
> It seems there is problem within the engine (especially in
> `Peri3dController` because doneHook and PyRunner call other functions).
> So can I run a compression/shear test without using the engine? e.g. one
> command by one command...
>
Revision history for this message
![]() |
#10 |
Hi Ning,
try r3015, NameError should not appear..
Jan
ceguo píše v Po 30. 01. 2012 v 08:01 +0000:
> Question #182459 on Yade changed:
> https:/
>
> Status: Expired => Open
>
> ceguo is still having a problem:
> Hi Chareyre,
>
> Sorry for the delayed reply. I'm just back from the Chinese New Year Holiday.
> After I corrected the typo, there is error when I executed `a.consolidation()` instead of without response:
>
> Traceback (most recent call last):
> File "<string>", line 1, in <module>
> NameError: name 'Omega' is not defined
>
> It seems there is problem within the engine (especially in
> `Peri3dController` because doneHook and PyRunner call other functions).
> So can I run a compression/shear test without using the engine? e.g. one
> command by one command...
>
Revision history for this message
![]() |
#11 |
Thanks Jan,
r3015 solves the problem of NameError.
Another thing is how can we export the results stored in `biax` in this case, as I find we cannot use PyRunner to execute saveAddData(). Or how can we get stress, strain, fabric tensors which may be used further in the simulation?
Regards,
Ning
Revision history for this message
![]() |
#12 |
Hi Ning,
The problem is that PyRunner calls functions in global namespace, so
variable self is not defined. I found this workaround - in shear
function do following:
def shear(self, strain=-1.e-2):
import __builtin__
_
self.
...
]
This approach should work. You can also try something with 'global'
statement or globals() dictionary.
Good luck and let us know if it works or not :-)
Jan
ceguo píše v Čt 02. 02. 2012 v 14:20 +0000:
> Question #182459 on Yade changed:
> https:/
>
> Status: Answered => Open
>
> ceguo is still having a problem:
> Thanks Jan,
>
> r3015 solves the problem of NameError.
> Another thing is how can we export the results stored in `biax` in this case, as I find we cannot use PyRunner to execute saveAddData(). Or how can we get stress, strain, fabric tensors which may be used further in the simulation?
>
> Regards,
> Ning
>
Revision history for this message
![]() |
#13 |
Thanks Jan Stránský, that solved my question.
Revision history for this message
![]() |
#14 |
It works, apparently. ;)