lmgc3Interface.py 8.42 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
75
76
77
78
79
80
81
82
83
84
85
86
87
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 spheres 
  - volume of spheres
  - reference on spher2rbdy3 map

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

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

  def __init__(self) :
      """
      Initialize LMGC90
      """
      chipy.checkDirectories()
      ### Set dimension in chipy for dummies ###
      chipy.SetDimension(3)
      
      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()

      chipy.PRPRx_UseCpCundallDetection(10)

      ##### ecriture paranoiaque du modele ###
      ##utilities_logMes('WRITE BODIES')
      ##WriteBodies()
      ##utilities_logMes('WRITE BEHAVIOURS')
      ##WriteBehaviours()
      ##utilities_logMes('WRITE DRIVEN DOF')
      ##WriteDrivenDof()
      
      chipy.OpenPostproFiles()
      chipy.OpenDisplayFiles()
      
      chipy.WriteDisplayFiles()

      chipy.ComputeMass()

      self._nbSpher   = chipy.SPHER_GetNbSPHER()
      self._d2bMap   = chipy.SPHER_GetPtrSPHER2BDYTY()
      self._position = np.zeros([self._nbSpher,3], 'd')
      self._velocity = np.zeros([self._nbSpher,6], 'd')
      self._externalF= np.zeros([self._nbSpher,6], 'd')
      self._volume   = np.zeros([self._nbSpher,1], 'd')
      self._mass   = np.zeros([self._nbSpher,1], 'd')
88
      self._rmin=1e20
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
89
      for i in range(self._nbSpher):
90
        self._rmin = min(self._rmin,chipy.SPHER_GetContactorRadius(i+1))
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        self._volume[i] = np.pi * chipy.SPHER_GetContactorRadius(i+1)**3 * 4./3
        self._mass[i] = chipy.RBDY3_GetMass(int(self._d2bMap[i,0]))
      self._tag2bnd = {}
      self._tag2id = {}
      for i in range(chipy.RBDY3_GetNbRBDY3()):
          for t in range(chipy.RBDY3_GetNbContactor(i+1)):
              tag = chipy.RBDY3_GetContactorColor(i+1,1).rstrip('_')
              self._tag2id.setdefault(tag, []).append(i)

      self._freq_detect   = 1
      self._solver_params = { 'type'  :'Stored_Delassus_Loops         ',
                      'norm'  : 'Quad ',
                      'conv'  : 1e-5,
                      'relax' : 1.,
                      'gsit1' : 200,
                      'gsit2' : 200
                    }

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

  def write(self, odir, i, t) :
    chipy.WriteOutDof(1)
    chipy.WriteOutVlocRloc(1)
    chipy.WritePostproFiles()
116
    chipy.WriteDisplayFiles(1, self._rmin*2.)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
117
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
      
  

  #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[:,:3] = forces

      chipy.IncrementStep()
      for tag, v in self._tag2bnd.iteritems() :
          for i in self._tag2id[tag] :
              for j, iv in enumerate(v):
                  chipy.RBDY3_SetVlocyDrivenDof(i+1,j+1,iv)
  
      chipy.ComputeFext()
      for i in range(self._nbSpher):
        chipy.RBDY3_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 position(self):
      """ Get current position of contactors and return it """
      for i in range(self._nbSpher):
        self._position[i,:] = chipy.SPHER_GetContactorCoor(i+1)[:3]
      return self._position

  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._nbSpher):
        self._velocity[i,:] = chipy.RBDY3_GetBodyVector('V____',int(self._d2bMap[i,0]))
      return self._velocity[:,:3]

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

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

def scontactToLmgc90(filename):
    import scontact3
    from pylmgc90 import pre_lmgc
    datbox_path = 'DATBOX'
    if not os.path.isdir(datbox_path):
      os.mkdir(datbox_path)

    sc = scontact3.ParticleProblem()
    sc.load(filename)

    fric = 0

    x = sc.position()
    v = sc.velocity()
    v[:] = 0.
    r = sc.particles()[:, 0]
    rho_particle = np.mean(sc.particles()[:, 1]/(np.pi * r**3 * 4./3))

    avs = pre_lmgc.avatars()
    mat = pre_lmgc.materials()
    mod = pre_lmgc.models()
    svs = pre_lmgc.see_tables()
    tac = pre_lmgc.tact_behavs()

    mater = pre_lmgc.material(name='STONE',type='RIGID',density=rho_particle)
    model = pre_lmgc.model(name='rigid', type='MECAx', element='Rxx3D',dimension=3)
    mat.addMaterial(mater)
    mod.addModel(model)

    for i in range(r.size) :
       P = pre_lmgc.rigidSphere( r=r[i], center=x[i], model=model, material=mater, color='INxxx')
       P.imposeInitValue(component=[1,2,3],value=list(v[i,:]))
       avs.addAvatar(P)

    tri = sc.triangles()
    bndtags = set()
    for i in range(tri.shape[0]) :
        x0 = np.array(tri[i, 0:3])
        x1 = np.array(tri[i, 3:6])
        x2 = np.array(tri[i, 6:9])
        tag = sc.getTagName(int(sc.triangleTag()[i]))
        if len(tag) > 5 :
            print("maximum tag name length in lmgc is 5, '%s' is too long" % tag)
            exit()
        tag += "_" * (5 - len(tag))
        bndtags.add(tag)
        t0 = (x0 - x1)/np.linalg.norm(x0 - x1)
        t1 = (x0 - x2)/np.linalg.norm(x0 - x2)
        n = np.cross(t0, t1)
        n *= 1e-12 / np.linalg.norm(n)

        vs = np.array([x0-n, x1 - n, x2 - n, x0 + n, x1 + n, x2 + n])
        fs = np.array([[0, 2, 1], [3, 4, 5], [0, 1, 3], [3, 1, 4], [1, 5, 4], [1, 2, 5], [0, 3, 2], [3, 5, 2]]) + 1
        av = pre_lmgc.rigidPolyhedron(model,mater,np.zeros([3]),color=tag,generation_type="full",vertices=vs, faces=fs)
        av.imposeDrivenDof(component=[1,2,3],dofty='vlocy')
        avs.addAvatar(av)

    clb_fric = pre_lmgc.tact_behav(name='iqsc0',type='IQS_CLB',fric=fric)
    tac     += clb_fric

    svs += pre_lmgc.see_table(CorpsCandidat   ="RBDY3", candidat   ="SPHER", colorCandidat   ='INxxx',
                             CorpsAntagoniste="RBDY3", antagoniste="SPHER", colorAntagoniste='INxxx',
                             behav=clb_fric, alert=r.min())

    for tag in bndtags :
        svs += pre_lmgc.see_table(CorpsCandidat   ="RBDY3", candidat   ="SPHER", colorCandidat   ='INxxx',
                                 CorpsAntagoniste="RBDY3", antagoniste="POLYR", colorAntagoniste=tag,
                                 behav=clb_fric, alert=r.min())
    post = pre_lmgc.postpro_commands()
    solv = pre_lmgc.postpro_command(type='SOLVER INFORMATIONS', step=1)
    post.addCommand(solv)

    # file writting
    pre_lmgc.writeBodies(avs,chemin=datbox_path)
    pre_lmgc.writeDrvDof(avs,chemin=datbox_path)
    pre_lmgc.writeDofIni(avs,chemin=datbox_path)
    pre_lmgc.writeModels(mod,chemin=datbox_path)
    pre_lmgc.writeBulkBehav(mat,chemin=datbox_path, gravy=[0.,0.,0.])
    pre_lmgc.writeTactBehav(tac,svs,chemin=datbox_path)
    pre_lmgc.writeVlocRlocIni(chemin=datbox_path)
    pre_lmgc.writePostpro(commands=post, parts=avs, path=datbox_path)