Package solvcon :: Package tests :: Module test_block
[hide private]
[frames] | no frames]

Source Code for Module solvcon.tests.test_block

  1  # -*- coding: UTF-8 -*- 
  2  # Copyright (C) 2008-2010 by Yung-Yu Chen.  See LICENSE.txt for terms of usage. 
  3   
  4  from unittest import TestCase 
  5  from ..testing import get_blk_from_oblique_neu, get_blk_from_sample_neu 
  6   
7 -class TestCreation(TestCase):
8 - def test_import(self):
9 from ..block import Block
10
11 - def test_create_block(self):
12 from ..block import Block 13 blk = Block() 14 self.assertEqual(str(blk), 15 '[Block (0D): 0 nodes, 0 faces (0 BC), 0 cells]')
16
17 - def test_fpdtype(self):
18 from ..conf import env 19 from ..dependency import pointer_of, str_of 20 from ..block import Block 21 blk = Block() 22 self.assertEqual(blk.ndcrd.dtype, env.fpdtype) 23 self.assertEqual(blk.fpdtype, env.fpdtype) 24 self.assertEqual(blk.fpdtypestr, str_of(env.fpdtype)) 25 self.assertEqual(blk.fpptr, pointer_of(env.fpdtype))
26
27 - def test_MAX(self):
28 from .. import block 29 self.assertEqual(block.MAX_FCNND, 4) 30 self.assertEqual(block.MAX_CLNND, 8) 31 self.assertEqual(block.MAX_CLNFC, 6) 32 self.assertEqual(block.Block.FCMND, 4) 33 self.assertEqual(block.Block.CLMND, 8) 34 self.assertEqual(block.Block.CLMFC, 6)
35
36 - def test_metric(self):
37 from ..block import Block 38 # build a simple 2D triangle with 4 subtriangles. 39 blk = Block(ndim=2, nnode=4, nface=6, ncell=3, nbound=3) 40 blk.ndcrd[0,:] = (0,0) 41 blk.ndcrd[1,:] = (-1,-1) 42 blk.ndcrd[2,:] = (1,-1) 43 blk.ndcrd[3,:] = (0,1) 44 blk.cltpn[:] = 3 45 blk.clnds[0,:4] = (3, 0,1,2) 46 blk.clnds[1,:4] = (3, 0,2,3) 47 blk.clnds[2,:4] = (3, 0,3,1) 48 blk.build_interior() 49 # test for volume (actually area in 2D). 50 self.assertEqual(blk.clvol[0], 1) 51 self.assertEqual(blk.clvol[1], .5) 52 self.assertEqual(blk.clvol[2], .5) 53 self.assertEqual(blk.clvol.sum(), 2) 54 # test for ghost information. 55 blk.build_boundary() 56 blk.build_ghost() 57 self.assertEqual(blk.clvol[0], 1) 58 self.assertEqual(blk.clvol[1], .5) 59 self.assertEqual(blk.clvol[2], .5) 60 self.assertEqual(blk.clvol.sum(), 2)
61
62 -class GroupTest(TestCase):
63 __test__ = False 64 testblock = None 65
66 - def test_groupname(self):
67 blk = self.testblock 68 self.assertEqual(len(blk.grpnames), 1) 69 self.assertEqual(blk.grpnames[0], 'fluid')
70
71 - def test_clgrp(self):
72 blk = self.testblock 73 icl = 0 74 while icl < blk.ncell: 75 self.assertEqual(blk.clgrp[icl], 0) 76 icl += 1
77
78 -class TestGroup2D(GroupTest):
79 __test__ = True 80 testblock = get_blk_from_oblique_neu()
81
82 -class TestGroup3D(GroupTest):
83 __test__ = True 84 testblock = get_blk_from_sample_neu()
85
86 -class MetricTest(TestCase):
87 __test__ = False 88 testblock = None 89
90 - def test_fcnml(self):
91 blk = self.testblock 92 ndim = blk.ndim 93 bfcs = blk.bndfcs[:,0] 94 bcls = blk.fccls[bfcs,0] # interior cells next to boundary. 95 igcl = 0 96 while igcl < blk.ngstcell: 97 ibfc = bfcs[igcl] # current boundary face. 98 iicl = bcls[igcl] # current interior cell. 99 dvec = blk.fccnd[ibfc,:] - blk.clcnd[iicl,:] 100 leng = (dvec[:]*blk.fcnml[ibfc,:]).sum() 101 self.assertTrue(leng > 0) 102 # check for nodes not belong to boundary face. 103 clnnd = blk.clnds[iicl,0] 104 for ind in blk.clnds[iicl,1:clnnd+1]: 105 fcnnd = blk.fcnds[ibfc,0] 106 if ind not in blk.fcnds[ibfc,1:fcnnd+1]: 107 dvec = blk.fccnd[ibfc,:] - blk.ndcrd[ind,:] 108 leng = (dvec[:]*blk.fcnml[ibfc,:]).sum() 109 self.assertTrue(leng > 0) 110 igcl += 1
111
112 -class TestMetric2D(MetricTest):
113 __test__ = True 114 testblock = get_blk_from_oblique_neu()
115
116 -class TestMetric3D(MetricTest):
117 __test__ = True 118 testblock = get_blk_from_sample_neu()
119
120 -class GhostTest(TestCase):
121 __test__ = False 122 testblock = None 123 rounding_to = 100 124
125 - def test_cltpn(self):
126 blk = self.testblock 127 bfcs = blk.bndfcs[:,0] 128 bcls = blk.fccls[bfcs,0] # interior cells next to boundary. 129 igcl = 0 130 while igcl < blk.ngstcell: 131 iicl = bcls[igcl] 132 self.assertEqual(blk.gstcltpn[igcl], blk.cltpn[iicl]) 133 igcl += 1
134
135 - def test_clgrp(self):
136 blk = self.testblock 137 bfcs = blk.bndfcs[:,0] 138 bcls = blk.fccls[bfcs,0] # interior cells next to boundary. 139 igcl = 0 140 while igcl < blk.ngstcell: 141 iicl = bcls[igcl] 142 self.assertEqual(blk.gstclgrp[igcl], blk.clgrp[iicl]) 143 igcl += 1
144
145 - def test_fctpn(self):
146 blk = self.testblock 147 bfcs = blk.bndfcs[:,0] 148 bcls = blk.fccls[bfcs,0] # interior cells next to boundary. 149 igcl = 0 150 while igcl < blk.ngstcell: 151 iicl = bcls[igcl] 152 for it in range(1,blk.gstclfcs[igcl,0]+1): 153 igfc = blk.gstclfcs[igcl,it] 154 iifc = blk.clfcs[iicl,it] 155 if igfc >= 0: 156 self.assertEqual(igfc, iifc) 157 else: 158 igfc = -igfc - 1 159 self.assertEqual(blk.gstfctpn[igfc], blk.fctpn[iifc]) 160 igcl += 1
161
162 - def test_ndcrd(self):
163 blk = self.testblock 164 ndim = blk.ndim 165 bfcs = blk.bndfcs[:,0] 166 bcls = blk.fccls[bfcs,0] # interior cells next to boundary. 167 igcl = 0 168 while igcl < blk.ngstcell: 169 ibfc = bfcs[igcl] # current boundary face. 170 iicl = bcls[igcl] # current interior cell. 171 for i in range(1,blk.gstclnds[igcl,0]+1): 172 ignd = blk.gstclnds[igcl,i] # current ghost node. 173 iind = blk.clnds[iicl,i] # current mirrored interior node. 174 if ignd >= 0: # node for ghost cell is real node. 175 self.assertEqual(ignd, iind) 176 else: 177 ignd = -ignd - 1 # flip index for ghost node. 178 v1 = blk.gstndcrd[ignd,:] - blk.fccnd[ibfc,:] 179 v2 = blk.ndcrd[iind,:] - blk.fccnd[ibfc,:] 180 # normal component. 181 v1n = (v1[:]*blk.fcnml[ibfc,:]).sum()*blk.fcnml[ibfc,:] 182 v2n = (v2[:]*blk.fcnml[ibfc,:]).sum()*blk.fcnml[ibfc,:] 183 for idim in range(ndim): 184 # opposite direction. 185 self.assertAlmostEqual(v1n[idim], -v2n[idim], 186 self.rounding_to) 187 # tangent component. 188 v1t = v1-v1n 189 v2t = v2-v2n 190 for idim in range(ndim): 191 # same direction. 192 self.assertAlmostEqual(v1t[idim], v2t[idim], 193 self.rounding_to) 194 igcl += 1
195
196 - def test_fccnd(self):
197 blk = self.testblock 198 ndim = blk.ndim 199 bfcs = blk.bndfcs[:,0] 200 bcls = blk.fccls[bfcs,0] # interior cells next to boundary. 201 igcl = 0 202 while igcl < blk.ngstcell: 203 ibfc = bfcs[igcl] 204 iicl = bcls[igcl] 205 for it in range(1,blk.gstclfcs[igcl,0]+1): 206 igfc = blk.gstclfcs[igcl,it] 207 iifc = blk.clfcs[iicl,it] 208 if igfc >= 0: 209 self.assertEqual(igfc, iifc) 210 else: 211 igfc = -igfc - 1 212 v1 = blk.gstfccnd[igfc,:] - blk.fccnd[ibfc,:] 213 v2 = blk.fccnd[iifc,:] - blk.fccnd[ibfc,:] 214 # normal components. 215 v1n = (v1*blk.fcnml[ibfc,:]).sum()*blk.fcnml[ibfc,:] 216 v2n = (v2*blk.fcnml[ibfc,:]).sum()*blk.fcnml[ibfc,:] 217 for idim in range(ndim): 218 # opposite direction. 219 self.assertAlmostEqual(v1n[idim], -v2n[idim], 220 self.rounding_to) 221 # tangent component. 222 v1t = v1-v1n 223 v2t = v2-v2n 224 for idim in range(ndim): 225 # same direction. 226 self.assertAlmostEqual(v1t[idim], v2t[idim], 227 self.rounding_to) 228 igcl += 1
229
230 - def test_fcnml(self):
231 blk = self.testblock 232 ndim = blk.ndim 233 bfcs = blk.bndfcs[:,0] 234 bcls = blk.fccls[bfcs,0] # interior cells next to boundary. 235 igcl = 0 236 while igcl < blk.ngstcell: 237 ibfc = bfcs[igcl] 238 iicl = bcls[igcl] 239 for it in range(1,blk.gstclfcs[igcl,0]+1): 240 igfc = blk.gstclfcs[igcl,it] 241 iifc = blk.clfcs[iicl,it] 242 if igfc >= 0: 243 self.assertEqual(igfc, iifc) 244 else: 245 igfc = -igfc - 1 246 v1 = blk.gstfcnml[igfc,:] 247 v2 = blk.fcnml[iifc,:] 248 # flip interior face normal vector to have proper direction. 249 if blk.fccls[iifc,0] != iicl: 250 v2 = -v2 251 # normal components. 252 v1n = (v1*blk.fcnml[ibfc,:]).sum()*blk.fcnml[ibfc,:] 253 v2n = (v2*blk.fcnml[ibfc,:]).sum()*blk.fcnml[ibfc,:] 254 for idim in range(ndim): 255 # opposite direction. 256 self.assertAlmostEqual(v1n[idim], -v2n[idim], 257 self.rounding_to) 258 # tangent component. 259 v1t = v1-v1n 260 v2t = v2-v2n 261 for idim in range(ndim): 262 # same direction. 263 self.assertAlmostEqual(v1t[idim], v2t[idim], 264 self.rounding_to) 265 igcl += 1
266
267 - def test_fcara(self):
268 blk = self.testblock 269 bfcs = blk.bndfcs[:,0] 270 bcls = blk.fccls[bfcs,0] # interior cells next to boundary. 271 igcl = 0 272 while igcl < blk.ngstcell: 273 ibfc = bfcs[igcl] 274 iicl = bcls[igcl] 275 for it in range(1,blk.gstclfcs[igcl,0]+1): 276 igfc = blk.gstclfcs[igcl,it] 277 iifc = blk.clfcs[iicl,it] 278 if igfc >= 0: 279 self.assertEqual(igfc, iifc) 280 else: 281 igfc = -igfc - 1 282 self.assertAlmostEqual( 283 blk.gstfcara[igfc], blk.fcara[iifc], self.rounding_to) 284 igcl += 1
285
286 - def test_clvol(self):
287 blk = self.testblock 288 bfcs = blk.bndfcs[:,0] 289 bcls = blk.fccls[bfcs,0] # interior cells next to boundary. 290 igcl = 0 291 while igcl < blk.ngstcell: 292 iicl = bcls[igcl] 293 self.assertAlmostEqual(blk.gstclvol[igcl], blk.clvol[iicl], 294 self.rounding_to) 295 igcl += 1
296
297 -class TestGhostSingle2D(GhostTest):
298 __test__ = True 299 testblock = get_blk_from_oblique_neu(fpdtype='float32') 300 rounding_to = 6
301
302 -class TestGhostDouble2D(GhostTest):
303 __test__ = True 304 testblock = get_blk_from_oblique_neu(fpdtype='float64') 305 rounding_to = 15
306
307 -class TestGhostSingle3D(GhostTest):
308 __test__ = True 309 testblock = get_blk_from_sample_neu(fpdtype='float32') 310 rounding_to = 4
311
312 -class TestGhostDouble3D(GhostTest):
313 __test__ = True 314 testblock = get_blk_from_sample_neu(fpdtype='float64') 315 rounding_to = 13
316
317 -class TestShared(TestCase):
318 oblique = get_blk_from_oblique_neu() 319
320 - def assertSharedGhost(self, nint, ngst, shared, ghost):
321 """ 322 For every ghost entity, assert it located in the reversed order than 323 interior entity, in the shared array. 324 325 @param nint: number of interior entity. 326 @type nint: int 327 @param ngst: number of ghost entity. 328 @type ngst: int 329 @param shared: shared array. 330 @type shared: numpy.ndarray 331 @param ghost: ghost array. 332 @type ghost: numpy.ndarray 333 @return: nothing. 334 """ 335 from random import randint 336 # shape of arrays. 337 self.assertEqual(len(shared.shape), 1) 338 self.assertEqual(len(ghost.shape), 1) 339 self.assertEqual(shared.shape[0], nint+ngst) 340 self.assertEqual(ghost.shape[0], ngst) 341 # test for each ghost entity. 342 for igst in range(ngst): # 0-indexed. 343 oval = ghost[igst] 344 tval = oval 345 while tval == oval: 346 tval = randint(0,10000000) 347 self.assertNotEqual(oval, tval) 348 ghost[igst] = tval 349 self.assertEqual(shared[ngst-1-igst], tval) 350 ghost[igst] = oval 351 self.assertEqual(shared[ngst-1-igst], oval)
352
353 - def test_metrics(self):
354 blk = self.oblique 355 for idim in range(blk.ndim): 356 self.assertSharedGhost(blk.nnode, blk.ngstnode, 357 blk.shndcrd[:,idim], blk.gstndcrd[:,idim]) 358 self.assertSharedGhost(blk.nface, blk.ngstface, 359 blk.shfccnd[:,idim], blk.gstfccnd[:,idim]) 360 self.assertSharedGhost(blk.nface, blk.ngstface, 361 blk.shfcnml[:,idim], blk.gstfcnml[:,idim]) 362 self.assertSharedGhost(blk.ncell, blk.ngstcell, 363 blk.shclcnd[:,idim], blk.gstclcnd[:,idim]) 364 self.assertSharedGhost(blk.nface, blk.ngstface, 365 blk.shfcara, blk.gstfcara) 366 self.assertSharedGhost(blk.ncell, blk.ngstcell, 367 blk.shclvol, blk.gstclvol)
368
369 - def test_type(self):
370 blk = self.oblique 371 self.assertSharedGhost(blk.ncell, blk.ngstcell, 372 blk.shcltpn, blk.gstcltpn) 373 self.assertSharedGhost(blk.ncell, blk.ngstcell, 374 blk.shclgrp, blk.gstclgrp) 375 self.assertSharedGhost(blk.nface, blk.ngstface, 376 blk.shfctpn, blk.gstfctpn)
377
378 - def test_conn(self):
379 blk = self.oblique 380 for it in range(blk.FCMND+1): 381 self.assertSharedGhost(blk.nface, blk.ngstface, 382 blk.shfcnds[:,it], blk.gstfcnds[:,it]) 383 for it in range(4): 384 self.assertSharedGhost(blk.nface, blk.ngstface, 385 blk.shfccls[:,it], blk.gstfccls[:,it]) 386 for it in range(blk.CLMND+1): 387 self.assertSharedGhost(blk.ncell, blk.ngstcell, 388 blk.shclnds[:,it], blk.gstclnds[:,it]) 389 for it in range(blk.CLMFC+1): 390 self.assertSharedGhost(blk.ncell, blk.ngstcell, 391 blk.shclfcs[:,it], blk.gstclfcs[:,it])
392
393 -class TestBlockShape(TestCase):
394 oblique = get_blk_from_oblique_neu() 395
396 - def test_BlockShape(self):
397 shape_text = """type block 398 integer*4 :: ndim = 2 399 integer*4 :: fcmnd = 4 400 integer*4 :: clmnd = 8 401 integer*4 :: clmfc = 6 402 integer*4 :: nnode = 72 403 integer*4 :: nface = 184 404 integer*4 :: ncell = 113 405 integer*4 :: nbound = 29 406 integer*4 :: ngstnode = 29 407 integer*4 :: ngstface = 58 408 integer*4 :: ngstcell = 29 409 end type block""" 410 obft = self.oblique.create_shape() 411 self.assertEqual(str(obft), shape_text)
412
413 -class TestMeshData(TestCase):
414 oblique = get_blk_from_oblique_neu() 415
416 - def test_meshdata(self):
417 msd = self.oblique.create_msd()
418