File: SimpleGouge.py

package info (click to toggle)
esys-particle 2.3.5%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,132 kB
  • sloc: cpp: 81,480; python: 5,872; makefile: 1,259; sh: 313; perl: 225
file content (357 lines) | stat: -rw-r--r-- 12,035 bytes parent folder | download | duplicates (4)
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
88
89
90
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
116
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#############################################################
##                                                         ##
## Copyright (c) 2003-2017 by The University of Queensland ##
## Centre for Geoscience Computing                         ##
## http://earth.uq.edu.au/centre-geoscience-computing      ##
##                                                         ##
## Primary Business: Brisbane, Queensland, Australia       ##
## Licensed under the Open Software License version 3.0    ##
## http://www.apache.org/licenses/LICENSE-2.0              ##
##                                                         ##
#############################################################
from __future__      import division
from esys.finley     import ReadMesh
from esys.linearPDEs import LinearPDE
from numarray        import identity
from esys.escript    import *
from LsmEscriptPy    import *

from esys.lsm.util   import Vec3

import re
import os
import logging

logger = logging.getLogger("esys.lsm.sim.coupled.SimpleGouge")
def getLogger():
    return logger

class NeighbourListPrms:
    def __init__(
        self,
        gridSpacing,
        updateDisplDist
    ):
        self.gridSpacing = gridSpacing
        self.updateDisplDist = updateDisplDist

class LsmGougePrms:
    def __init__(
        self,
        numWorkerProcesses,
        mpiDimList,
        lsmGeomFileName,
        meshFileName,
        timeStepSize,
        neighbourListPrms,
        bondPrms,
        frictionPrms,
        dampingPrms,
        bondedMeshPrms,
        chkptPrms,
        bBox = None
    ):
        self.numWorkerProcesses = numWorkerProcesses
        self.mpiDimList = mpiDimList
        self.lsmGeomFileName = lsmGeomFileName
        self.meshFileName = meshFileName
        self.timeStepSize = timeStepSize
        self.neighbourListPrms = neighbourListPrms
        self.bondPrms = bondPrms
        self.frictionPrms = frictionPrms
        self.dampingPrms = dampingPrms
        self.bondedMeshPrms = bondedMeshPrms
        self.chkptPrms = chkptPrms
        self.dim = None
        self.bBox = bBox

    def getNumWorkerProcesses(self):
        return self.numWorkerProcesses

    def getMpiDimList(self):
        return self.mpiDimList

    def getMeshFileName(self):
        return self.meshFileName

    def getMeshName(self):
        return self.bondedMeshPrms.getMeshName()

    def getDim(self):
        if (self.dim == None):
            regex = re.compile("^Dimension\s+(\d)D")
            f = file(self.lsmGeomFileName, "r")
            line = f.readline()
            match = regex.match(line)
            while (len(line) > 0) and (match == None):
                line = f.readline()
                match = regex.match(line)
            f.close()
            if (match != None):
                self.dim = int(match.group(1))
            else:
                raise Exception(
                    "Could not locate dimension info in file " \
                    + \
                    str(self.lsmGeomFileName)
                )
        return self.dim

    def is2d(self):
        return (self.getDim() == 2)

    def initialise(self, lsm):
        lsm.initVerletModel(
            self.getParticleType(),
            self.neighbourListPrms.gridSpacing,
            self.neighbourListPrms.updateDisplDist,
        )
        lsm.setTimeStepSize(self.timeStepSize)
        if (self.bBox != None):
            lsm.setSpatialDomain(self.bBox.getMinPt(), self.bBox.getMaxPt())
        lsm.readGeometry(self.lsmGeomFileName)
        if (self.is2d()):
            lsm.force2dComputations()
        lsm.createInteractionGroup(self.bondPrms)
        lsm.createInteractionGroup(self.frictionPrms)
        lsm.createExclusion(self.bondPrms.getName(), self.frictionPrms.getName())
        lsm.createDamping(self.dampingPrms)
        if (self.is2d()):
            lsm.readMesh2D(self.meshFileName, self.getMeshName(), 20)
        else:
            lsm.readMesh(self.meshFileName, self.getMeshName())
        lsm.createInteractionGroup(self.bondedMeshPrms)
        lsm.createCheckPointer(self.chkptPrms)

class LsmSimpleGouge(LsmMpiEscript):
    def __init__(self, gougePrms):
        self.gougePrms = gougePrms
        LsmMpiEscript.__init__(
            self,
            self.gougePrms.getNumWorkerProcesses(),
            self.gougePrms.getMpiDimList()
        )
        self.initialise()

    def initialise(self):
        self.gougePrms.initialise(self)

    def is2d(self):
        return self.gougePrms.is2d()

    def getDim(self):
        return self.gougePrms.getDim()

    def getMeshFileName(self):
        return self.gougePrms.getMeshFileName()

    def getMeshName(self):
        return self.gougePrms.getMeshName()

    def visitRefStressPairs(self, visitor):
        if (self.is2d):
            LsmMpiEscript.visitRefStressPairs2d(self, self.getMeshName(), visitor)
        else:
            LsmMpiEscript.visitRefStressPairs(self, self.getMeshName(), visitor)

    def visitNodeRefs(self, visitor):
        if (self.is2d):
            LsmMpiEscript.visitNodeRefs2d(self, self.getMeshName(), visitor)
        else:
            LsmMpiEscript.visitNodeRefs(self, self.getMeshName(), visitor)

    def moveNodeBy(self, nodeRef, displacement):
        self.moveSingleMeshNodeBy(self.getMeshName(), nodeRef, displacement)

class EscriptGougePrms:
    def __init__(
        self,
        lameLambda, # Lame coeff
        lameMu,     # Lame coeff
        density      = 1.0,
        viscosity    = 0.0,
        pressure     = 0.001,
        shearForce   = 0.005,
        maxTime      = 1000,
        timeStepSize = 0.05,
        saveDxIncr   = 10,
        outputDir    = "."
    ):
        self.lameLambda   = lameLambda
        self.lameMu       = lameMu
        self.density      = density
        self.viscosity    = viscosity
        self.pressure     = pressure
        self.shearForce   = shearForce
        self.maxTime      = maxTime
        self.timeStepSize = timeStepSize
        self.outputDir    = outputDir
        self.saveDxIncr   = saveDxIncr

class NodeRefVisitor:
    def __init__(self, data, lsm):
        self.data = data
        self.lsm  = lsm

    def visitNodeRef(self, nodeRef):
        displArray = numarray.array([0.0]*self.lsm.getDim())
        self.data.getRefValue(nodeRef, displArray)
        displVec = Vec3()
        for i in range(0, self.lsm.getDim()):
            displVec[i] = displArray[i]
        getLogger().debug("Moving node " + str(nodeRef) + " by " + str(displVec))
        self.lsm.moveNodeBy(nodeRef, displVec)

class RefStressVisitor:
    def __init__(self, data, dim):
        self.data = data
        self.dim = dim

    def visitRefStressPair(self, ref, stressVec):
        getLogger().debug("Setting stress on boundary element " + str(ref) + " to " + str(stressVec))
        self.data.setRefValue(
            ref,
            numarray.array(stressVec.toList()[0:self.dim])
        )

class EscriptSimpleGouge:
    def __init__(self, lsm, prms=None):
        self.lsm = lsm
        self.prms = prms
        if (self.prms == None):
            self.initialisePrmsFromLsm()

    def initialisePrmsFromLsm(self):
        pass

    def getDim(self):
        return self.lsm.getDim()

    def getMeshFileName(self):
        return self.lsm.getMeshFileName()

    def updateImpactStresses(self, stressData):
        if (self.lsm != None):
            self.lsm.visitRefStressPairs(
                RefStressVisitor(data=stressData, dim=self.lsm.getDim())
            )

    def updateLsmMeshPosn(self, displacementData):
        if (self.lsm != None):
            self.lsm.visitNodeRefs(NodeRefVisitor(displacementData, self.lsm))

    def run(self):
        # material parameter
        lam = self.prms.lameLambda
        mu  = self.prms.lameMu
        rho = self.prms.density
        nu  = self.prms.viscosity
        
        # set up boundary conditions
        pres  = self.prms.pressure
        shear = self.prms.shearForce
        
        getLogger().info("Reading mesh from " + self.getMeshFileName())
        domain=ReadMesh(self.getMeshFileName())
        impact_forces=escript.Vector(0,FunctionOnBoundary(domain))
        impact_forces.expand()
        getLogger().info("Shape = " + str(impact_forces.getShape()))
        x=FunctionOnBoundary(domain).getX()
        getLogger().info("Initialising pressure and shearing boundary conditions...")
        snapDist = 0.001
        external_forces = \
            (abs(x[1]-sup(x[1]))-snapDist).whereNegative()*[-shear,-pres] \
            +                                                          \
            (abs(x[1]-inf(x[1]))-snapDist).whereNegative()*[shear,pres]

        getLogger().info("Setting up PDE...")
        mypde = LinearPDE(domain)
        mypde.setLumpingOn()
        mypde.setValue(D=rho*identity(mypde.getDim()))
        
        getLogger().info("Initialising solution at t=0...")
        u      = Vector(0,ContinuousFunction(domain))
        u_last = Vector(0,ContinuousFunction(domain))
        v      = Vector(0,ContinuousFunction(domain))
        v_last = Vector(0,ContinuousFunction(domain))
        a      = Vector(0,ContinuousFunction(domain))
        a_last = Vector(0,ContinuousFunction(domain))
        
        # initialise iteration prms
        tend = self.prms.maxTime
        dt   = self.prms.timeStepSize
        # dt=1./5.*sqrt(rho/(lam+2*mu))*Lsup(domain.getSize())
        getLogger().info("time step size = " + str(dt))
        n=0
        t=0
        
        getLogger().info("Beginning iteration...")
        while (t < tend):
            getLogger().info("Running LSM time step...");
            self.lsm.runTimeStep()
            
            getLogger().info("Updating impact forces from LSM...");
            self.updateImpactStresses(impact_forces)
            getLogger().info(
              "(inf(impactForces), sup(impact_forces)) = (" + \
              str(inf(impact_forces)) + ", " + str(sup(impact_forces)) + ")"
            )
            
            
            # ... update FEM ...
            getLogger().info("Initialising PDE coefficients...")
            g=grad(u)
            stress=(lam*trace(g))*identity(mypde.getDim())+mu*(g+transpose(g))
            mypde.setValue(
                X = -(1.0/(1.0+(nu*dt/2.0)))*stress,
                Y = - nu*v_last-(nu*dt/2.0)*a_last,
                y=external_forces+impact_forces
            )
            getLogger().info("Solving PDE...")
            a=mypde.getSolution()
            
            getLogger().info("Updating displacements...")
#            u_new=2*u-u_last+dt**2*a
#            u_last=u
#            u=u_new

            v_new = v_last + (dt/2.0)*(a + a_last)
            v_last = v
            v = v_new

            u_new = u_last + dt*v + ((dt**2)/2.0)*a
            u_last = u
            u = u_new
            
            a_last = a

            getLogger().info("Updating LSM mesh node positions...")
            displacement = u - u_last
            self.updateLsmMeshPosn(displacement)
            
            t+=dt
            n+=1
            getLogger().info(str(n) + "-th time step, t=" + str(t))
            getLogger().info("a=" + str(inf(a)) + ", " + str(sup(a)))
            getLogger().info("u=" + str(inf(u)) + ", " + str(sup(u)))
            getLogger().info("inf(u-u_last) = " + str(inf(displacement)))
            getLogger().info("sup(u-u_last) = " + str(sup(displacement)))
            
            # ... save current acceleration and displacement
            if ((self.prms.saveDxIncr > 0) and ((n%self.prms.saveDxIncr) == 0)):
                u.saveDX(os.path.join(self.prms.outputDir, "displ.{0:d}.dx".format(n//self.prms.saveDxIncr)))

class SimpleGouge:
    def __init__(
        self,
        lsmGougePrms,
        escriptPrms
    ):
        self.lsmPrms = lsmGougePrms
        self.escriptPrms = escriptPrms

    def run(self):
        lsm = LsmSimpleGouge(self.lsmPrms)
        escriptSim = EscriptSimpleGouge(lsm, self.escriptPrms)
        escriptSim.run()