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
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()

Frédéric Dubois's avatar
Frédéric Dubois committed
64
      chipy.PRPRx_UseCpCundallDetection(30)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

      ##### 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
        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

Frédéric Dubois's avatar
wip    
Frédéric Dubois committed
112
  def write(self, filename) :
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
113
114
115
    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
      
  

  #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()

Frédéric Dubois's avatar
Frédéric Dubois committed
187
def scontactToLmgc90(filename, fric = 0.):
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
188
    import scontact3
Frédéric Dubois's avatar
Frédéric Dubois committed
189
    from pylmgc90 import pre
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
190
191
192
193
    datbox_path = 'DATBOX'
    if not os.path.isdir(datbox_path):
      os.mkdir(datbox_path)

Frédéric Dubois's avatar
Frédéric Dubois committed
194
    sc = scontact3.ParticleProblem(filename)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
195
196
197
198

    x = sc.position()
    v = sc.velocity()
    v[:] = 0.
Frédéric Dubois's avatar
wip    
Frédéric Dubois committed
199
200

    vol = sc.volume()
Frédéric Dubois's avatar
wip    
Frédéric Dubois committed
201
    r = (3.*vol/(4.*np.pi))**(1./3.)
Frédéric Dubois's avatar
wip    
Frédéric Dubois committed
202

Frédéric Dubois's avatar
wip    
Frédéric Dubois committed
203
    rho_particle = np.mean(sc.mass()/vol)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
204

Frédéric Dubois's avatar
Frédéric Dubois committed
205
206
207
208
209
    avs = pre.avatars()
    mat = pre.materials()
    mod = pre.models()
    svs = pre.see_tables()
    tac = pre.tact_behavs()
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
210

Frédéric Dubois's avatar
wip    
Frédéric Dubois committed
211
212
    mater = pre.material(name='STONE',materialType='RIGID',density=rho_particle)
    model = pre.model(name='rigid', physics='MECAx', element='Rxx3D',dimension=3)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
213
214
215
216
    mat.addMaterial(mater)
    mod.addModel(model)

    for i in range(r.size) :
Frédéric Dubois's avatar
Frédéric Dubois committed
217
       P = pre.rigidSphere( r=r[i], center=x[i], model=model, material=mater, color='INxxx')
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
218
219
220
221
222
223
224
225
226
       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])
Frédéric Dubois's avatar
wip    
Frédéric Dubois committed
227
228
229
230
231
        tag = 'xxxxx'
        # 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()
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
232
233
234
235
236
237
238
239
240
        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
Frédéric Dubois's avatar
Frédéric Dubois committed
241
242
243
244
        av = pre.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)
        
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
245
246
247
        av.imposeDrivenDof(component=[1,2,3],dofty='vlocy')
        avs.addAvatar(av)

Frédéric Dubois's avatar
wip    
Frédéric Dubois committed
248
    clb_fric = pre.tact_behav(name='iqsc0',law='IQS_CLB',fric=fric)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
249
250
    tac     += clb_fric

Frédéric Dubois's avatar
Frédéric Dubois committed
251
    svs += pre.see_table(CorpsCandidat   ="RBDY3", candidat   ="SPHER", colorCandidat   ='INxxx',
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
252
253
254
255
                             CorpsAntagoniste="RBDY3", antagoniste="SPHER", colorAntagoniste='INxxx',
                             behav=clb_fric, alert=r.min())

    for tag in bndtags :
Frédéric Dubois's avatar
Frédéric Dubois committed
256
        svs += pre.see_table(CorpsCandidat   ="RBDY3", candidat   ="SPHER", colorCandidat   ='INxxx',
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
257
258
                                 CorpsAntagoniste="RBDY3", antagoniste="POLYR", colorAntagoniste=tag,
                                 behav=clb_fric, alert=r.min())
Frédéric Dubois's avatar
Frédéric Dubois committed
259
    post = pre.postpro_commands()
Frédéric Dubois's avatar
wip    
Frédéric Dubois committed
260
    solv = pre.postpro_command(name='SOLVER INFORMATIONS', step=1)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
261
262
263
    post.addCommand(solv)

    # file writting
Frédéric Dubois's avatar
Frédéric Dubois committed
264
265
266
267
268
269
270
271
    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)