lmgc2Interface.py 8.27 KB
Newer Older
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from pylmgc90 import chipy
import numpy as np
import os

class LmgcInterface(object):
  """
  A class definition holding data of lmgc90
  in a compatible form to use coupling with
  gmsh
  
  Attributs are :

  - number of disks
  - volume of diskx
  - reference on diskx2rbdy2 map

  The methods are :
  
  - iterate()  : do one time iteration
  - position() : give back position of all diskx
  - velocity() : give back position of all diskx
  - externalForces() : update external forces on particles
  
  - getMeanRadius() : return the mean radius of particles
  - getVolume() : give back the volume of all diskx

  - writeHeader() : write header file for plot
  """

  def __init__(self) :
      """
      Initialize LMGC90
      """
      chipy.checkDirectories()
      ### Set dimension in chipy for dummies ###
      chipy.SetDimension(2)
      
      chipy.Initialize()

      ### definition des parametres du calcul ### 
      
      ### lecture du modele ###
      chipy.utilities_logMes('READ BEHAVIOURS')
      chipy.ReadBehaviours()
      
      chipy.utilities_logMes('READ BODIES')
      chipy.ReadBodies()
      
      chipy.utilities_logMes('LOAD BEHAVIOURS')
      chipy.LoadBehaviours()
      
      chipy.utilities_logMes('READ INI DOF')
      chipy.ReadIniDof()
      
      chipy.utilities_logMes('READ DRIVEN DOF')
      chipy.ReadDrivenDof()
      
      chipy.utilities_logMes('LOAD TACTORS')
      chipy.LoadTactors()
      
      chipy.utilities_logMes('READ INI Vloc Rloc')
      chipy.ReadIniVlocRloc()
      
      ##### ecriture paranoiaque du modele ###
      ##utilities_logMes('WRITE BODIES')
      ##WriteBodies()
      ##utilities_logMes('WRITE BEHAVIOURS')
      ##WriteBehaviours()
      ##utilities_logMes('WRITE DRIVEN DOF')
      ##WriteDrivenDof()
      
      chipy.OpenPostproFiles()
      chipy.OpenDisplayFiles()
      
75
      #chipy.WriteDisplayFiles()
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
76
77
78
79
80

      chipy.ComputeMass()

      self._nbDisk   = chipy.DISKx_GetNbDISKx()
      self._d2bMap   = chipy.DISKx_GetPtrDISKx2RBDY2()
81
82
      self._nbPoly = chipy.POLYG_GetNbPOLYG()
      self._p2bMap = chipy.POLYG_GetPtrPOLYG2RBDY2()
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
      self._position = np.zeros([self._nbDisk,3], 'd')
      self._velocity = np.zeros([self._nbDisk,3], 'd')
      self._externalF= np.zeros([self._nbDisk,3], 'd')
      self._volume   = np.zeros([self._nbDisk,1], 'd')
      self._mass   = np.zeros([self._nbDisk,1], 'd')
      self._r   = np.zeros([self._nbDisk,1], 'd')
      for i in range(self._nbDisk):
        self._r[i] = chipy.DISKx_GetContactorRadius(i+1)
        self._mass[i] = chipy.RBDY2_GetBodyMass(int(self._d2bMap[i,0]))
      self._volume = np.pi * self._r**2 * 2./3
      self._tag2bnd = {}
      self._tag2id = {}
      for i in range(chipy.RBDY2_GetNbRBDY2()):
          for t in range(chipy.RBDY2_GetNbContactor(i+1)):
              tag = chipy.RBDY2_GetContactorColor(i+1,1).rstrip('_')
              self._tag2id.setdefault(tag, []).append(i)

      self._freq_detect   = 1
      self._solver_params = { 'type'  :'Stored_Delassus_Loops         ',
                      'norm'  : 'Quad ',
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
103
                      'conv'  : 1e-4,
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
104
105
                      'relax' : 1.,
                      'gsit1' : 200,
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
106
                      'gsit2' : 10 
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
107
108
109
110
111
                    }

  def setBoundaryVelocity(self, tag, v) :
      self._tag2bnd[tag] = v

Matthieu Constant's avatar
Matthieu Constant committed
112
  def write(self, filemame) :
Frédéric Dubois's avatar
Frédéric Dubois committed
113
    refRadius=np.amin(self._r)*2.
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
114
115
116
    chipy.WriteOutDof(1)
    chipy.WriteOutVlocRloc(1)
    chipy.WritePostproFiles()
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
117
    chipy.WriteDisplayFiles(1, refRadius)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
      
  

  #def iterate(self,freq_detect,freq_write,freq_display,ref_radius,**solver_args):
  def iterate(self, dt, forces):
      """
      Do one step of a LMGC90 computation.
      """

      chipy.TimeEvolution_SetTimeStep(dt)
      chipy.Integrator_InitTheta(1.)

      self._externalF[:,:2] = forces

      chipy.IncrementStep()
      for tag, v in self._tag2bnd.iteritems() :
          for i in self._tag2id[tag] :
              for j, iv in enumerate(v):
                  chipy.RBDY2_SetVlocyDrivenDof(i+1,j+1,iv)
  
      chipy.ComputeFext()
      for i in range(self._nbDisk):
        chipy.RBDY2_PutBodyVector('Fext_', int(self._d2bMap[i,0]), self._externalF[i,:])
      chipy.ComputeBulk()
      chipy.ComputeFreeVelocity()
  
      chipy.SelectProxTactors(self._freq_detect)
  
      chipy.RecupRloc()
      chipy.ExSolver(**self._solver_params)
      chipy.StockRloc()
  
      chipy.ComputeDof()
  
      chipy.UpdateStep()

  def volume(self):
      """ Return the volume of the  particles """
      return self._volume

  def mass(self):
      """ Return the mass of the  particles """
      return self._mass

  def r(self):
      """ Return the radius of the  particles """
      return self._r

  def position(self):
      """ Get current position of contactors and return it """
      for i in range(self._nbDisk):
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
169
        self._position[i,:] = chipy.RBDY2_GetBodyVector('Coor_',int(self._d2bMap[i,0]))
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
      self._position[i,2] = 0.
      return self._position[:,:2]

  def velocity(self):
      """ Get current velocity of body of contactor and return it
      Beware : it should not work very well with clusters !
      """
      for i in range(self._nbDisk):
        self._velocity[i,:] = chipy.RBDY2_GetBodyVector('V____',int(self._d2bMap[i,0]))
      self._velocity[i,2] = 0.
      return self._velocity[:,:2]

  def externalForces(self):
      """ Get an external forces array
      """
      return self._externalF[:,:2]

  def __del__(self):
      """
      """
      chipy.ClosePostproFiles()
      chipy.CloseDisplayFiles()
      chipy.Finalize()

194
def scontactToLmgc90(filename, fric = 0.):
Matthieu Constant's avatar
Matthieu Constant committed
195
    from marblesbag import scontact2
196
    from pylmgc90 import pre
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
197
198
199
200
    datbox_path = 'DATBOX'
    if not os.path.isdir(datbox_path):
      os.mkdir(datbox_path)

Matthieu Constant's avatar
Matthieu Constant committed
201
    sc = scontact2.ParticleProblem(filename)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
202
203
204
205

    x = sc.position()
    v = sc.velocity()
    v[:] = 0.
Matthieu Constant's avatar
Matthieu Constant committed
206
207
    r = sc.r()
    rho_particle = np.mean(sc.mass()/(np.pi * r**2))
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
208

209
210
211
212
213
    avs = pre.avatars()
    mat = pre.materials()
    mod = pre.models()
    svs = pre.see_tables()
    tac = pre.tact_behavs()
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
214

215
216
    mater = pre.material(name='STONE',materialType='RIGID',density=rho_particle)
    model = pre.model(name='rigid', physics='MECAx', element='Rxx2D',dimension=2)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
217
218
219
220
    mat.addMaterial(mater)
    mod.addModel(model)

    for i in range(r.size) :
221
       P = pre.rigidDisk( r=r[i], center=x[i], model=model, material=mater, color='INxxx')
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
222
223
224
225
226
227
228
229
       P.imposeInitValue(component=[1,2],value=list(v[i,:]))
       avs.addAvatar(P)

    seg = sc.segments()
    bndtags = set()
    for i in range(seg.shape[0]) :
        x0 = np.array(seg[i, 0:2])
        x1 = np.array(seg[i, 2:4])
Matthieu Constant's avatar
Matthieu Constant committed
230
231
232
233
234
        tag = 'xxxxx'
#        tag = sc.getTagName(int(sc.segmentTag()[i]))
#        if len(tag) > 5 :
#            print("maximum tag name length in lmgc is 5, '%s' is too long" % tag)
#            exit()
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
235
236
237
        tag += "_" * (5 - len(tag))
        bndtags.add(tag)
        t = (x0 - x1)/np.linalg.norm(x0 - x1)
238
        n = np.array([-t[1], t[0]]) * 1e-4
Frédéric Dubois's avatar
Frédéric Dubois committed
239
        vs = np.array([x0-2*n, x0, x1, x1 -2*n])
240
        av = pre.rigidPolygon(model,mater,np.zeros([2]),color=tag,generation_type="full",vertices=vs)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
241
242
243
        av.imposeDrivenDof(component=[1,2,3],dofty='vlocy')
        avs.addAvatar(av)

244
    clb_fric = pre.tact_behav(name='iqsc0',law='IQS_CLB',fric=fric)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
245
246
    tac     += clb_fric

247
    svs += pre.see_table(CorpsCandidat   ="RBDY2", candidat   ="DISKx", colorCandidat   ='INxxx',
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
248
249
250
251
                             CorpsAntagoniste="RBDY2", antagoniste="DISKx", colorAntagoniste='INxxx',
                             behav=clb_fric, alert=r.min())

    for tag in bndtags :
252
        svs += pre.see_table(CorpsCandidat   ="RBDY2", candidat   ="DISKx", colorCandidat   ='INxxx',
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
253
254
                                 CorpsAntagoniste="RBDY2", antagoniste="POLYG", colorAntagoniste=tag,
                                 behav=clb_fric, alert=r.min())
255
256
    post = pre.postpro_commands()
    solv = pre.postpro_command(name='SOLVER INFORMATIONS', step=1)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
257
258
259
    post.addCommand(solv)

    # file writting
260
261
262
263
264
265
266
267
    pre.writeBodies(avs,chemin=datbox_path)
    pre.writeDrvDof(avs,chemin=datbox_path)
    pre.writeDofIni(avs,chemin=datbox_path)
    pre.writeModels(mod,chemin=datbox_path)
    pre.writeBulkBehav(mat,chemin=datbox_path, gravy=[0.,0.,0.])
    pre.writeTactBehav(tac,svs,chemin=datbox_path)
    pre.writeVlocRlocIni(chemin=datbox_path)
    pre.writePostpro(commands=post, parts=avs, path=datbox_path)