Best Python code snippet using sure_python
test_libshape.py
Source:test_libshape.py
...210 # expjaco[2] +=1e-6211 # # breakpoint()212213 msg = f'Consistency for 1d Jacobian determinant fails for {ipoint=} '214 self.compare_iterables(actjdet,expjdet,msg=msg,rtol=closertol,atol=closeatol,desc='integration point')215216 msg = f'Consistency for 1d Jacobian jacobian fails for {ipoint=} '217 self.compare_iterables(actjaco,expjaco,msg=msg,rtol=closertol,atol=closeatol,desc='integration point')218 219 msg = f'Consistency for 1d Jacobian global derivatives fails for {ipoint=} '220 self.compare_iterables(actgder,expgder,msg=msg,rtol=closertol,atol=closeatol,desc='integration point ')221 222 # AssertionError must be raised when length of element is very small ( <1e-12 )223 self.assertRaises(AssertionError,jaco1d,[p1,p1],der)224225 def test_consistency_shift_jaco2d(self):226 for ipoint in range(1,maxinteg):227 # for this test we map an arbitrary rectangle to parent domain and compare global derivatives228 # the sides of the element defining x and y axis must be parallel to the parent x any y axes229 # this is equivalent to saying the lower left point of the global domain should be 1,230 # lower right 2, upper right 3 and upper left 4231 232 # get location of lower left corner233 px = 1024*np.random.rand()234 py = 1024*np.random.rand()235 236 while True:237 # generate random values of length and breadth, both non-zero238 length = abs(1024*np.random.rand())239 breadth = abs(1024*np.random.rand())240 if ( length != 0 and breadth != 0):241 break242243 p1 = np.asarray((px,py,0));244 p2 = np.asarray((px+breadth,py,0));245 p3 = np.asarray((px+breadth,py+length,0));246 p4 = np.asarray((px,py+length,0))247248 plist = [p1,p2,p3,p4]249250 # get global derivatives using jaco2d251 gg = gauss2d(ipoint);252 *ss, = map(shape2d,gg.pts)253 der = [ s.der for s in ss ]254 *jj, = map(jaco2d,itertools.repeat(plist),der)255 actder = [j.gder for j in jj]256257 # interpolate nodal coords to get x,y,z at all integration points258 pp = interp_parent(plist,ss)259260 x1 = p1[0]; y1=p1[1]261 x2 = p2[0]; y2=p2[1]262 x3 = p3[0]; y3=p3[1]263 x4 = p4[0]; y4=p4[1]264265 expder = []266 for i,p in enumerate(pp):267 x = p[0]; y = p[1]; z=p[2]268269 N1x = ( -1.0/(x2-x1) ) * ( (y4-y)/(y4-y1) )270 N1y = ( (x2-x)/(x2-x1) ) * ( -1.0/(y4-y1) )271 N2x = ( -1.0/(x1-x2) ) * ( (y3-y)/(y3-y2) )272 N2y = ( (x1-x)/(x1-x2) ) * ( -1.0/(y3-y2) )273 N3x = ( -1.0/(x4-x3) ) * ( (y2-y)/(y2-y3) )274 N3y = ( (x4-x)/(x4-x3) ) * ( -1.0/(y2-y3) ) 275 N4x = ( -1.0/(x3-x4) ) * ( (y1-y)/(y1-y4) )276 N4y = ( (x3-x)/(x3-x4) ) * ( -1.0/(y1-y4) )277278 # sanity check, must fail279 # if ( ipoint == 3 and i ==2 ):280 # N3x +=1e-6281282 tmp = np.asarray(( (N1x,N1y), (N2x,N2y), (N3x,N3y), (N4x,N4y) ))283 expder.append(tmp)284285 msg = f'Checking global derivatives for jaco2d in test_consistency_shift_jaco2d {ipoint=} '286 self.compare_iterables(actder,expder,msg=msg,rtol=closertol,atol=closeatol,desc='integration point ')287288 def test_consistency_rotation_shift_jaco2d(self):289 # similar to test_consistency_shift_jaco2d but a rotation is added290 # for this test we map an arbitrary rectangle to parent domain and compare global derivatives291 # the sides of the element x and y axis can be inclined to the global/parent x any y axes292293 # we generate a rectangle with sides parallel to global x and y axis. Rotate it by a random angle.294 # compute global derivatives, and jdet using jaco2d295 # compute derivatives analytically in a rotated frame of reference296 # map these derivatives from the rotated frame of reference to the global frame of reference297298 for ipoint in range(1,maxinteg):299 # lower left corner300 px = 16*np.random.rand(); py = 16*np.random.rand()301 302 while True:303 # generate random values of length and breadth, both non-zero304 length = abs(16*np.random.rand())305 breadth = abs(16*np.random.rand())306 if ( length != 0 and breadth != 0):307 break308309 # theta = 45.0*math.pi/180.0310 theta = 90*np.random.rand()*math.pi/180.0311 312 # these are coordinates in the standard global frame of reference313 p1 = np.asarray((px,py));314 p2 = np.asarray((px+breadth,py));315 p3 = np.asarray((px+breadth,py+length));316 p4 = np.asarray((px,py+length))317318 ct = math.cos(theta); st = math.sin(theta)319 Q = np.asarray(( (ct,-st),(st,ct) ))320 Qt = Q.T321322 # rotate the points323 p1r = Q@p1 ; p2r = Q@p2 ; p3r = Q@p3 ; p4r = Q@p4324325 plist = [p1,p2,p3,p4]326 plistrot = [p1r,p2r,p3r,p4r]327328 # compute global derivatives via jaco2d329 gg = gauss2d(ipoint);330 *ss, = map(shape2d,gg.pts)331 der = [ s.der for s in ss ]332 *jj, = map(jaco2d,itertools.repeat(plistrot),der)333 actder = [j.gder for j in jj]334 actjdet = [j.jdet for j in jj]335336 # to create expected output, we are going to work in the rotated frame of reference337 # and then rotate back to global. Note that in the rotated frame of reference,338 # the coordinates are the original unrotated coordinates.339 340 pp = interp_parent(plist,ss)341342 x1 = p1[0]; y1=p1[1]343 x2 = p2[0]; y2=p2[1]344 x3 = p3[0]; y3=p3[1]345 x4 = p4[0]; y4=p4[1]346347 expder = [] ; expjdet = []348 349 for p in pp:350 x = p[0]; y = p[1]; 351352 N1x = ( -1.0/(x2-x1) ) * ( (y4-y)/(y4-y1) )353 N1y = ( (x2-x)/(x2-x1) ) * ( -1.0/(y4-y1) )354 N2x = ( -1.0/(x1-x2) ) * ( (y3-y)/(y3-y2) )355 N2y = ( (x1-x)/(x1-x2) ) * ( -1.0/(y3-y2) )356 N3x = ( -1.0/(x4-x3) ) * ( (y2-y)/(y2-y3) )357 N3y = ( (x4-x)/(x4-x3) ) * ( -1.0/(y2-y3) ) 358 N4x = ( -1.0/(x3-x4) ) * ( (y1-y)/(y1-y4) )359 N4y = ( (x3-x)/(x3-x4) ) * ( -1.0/(y1-y4) )360361 N1x,N1y = Q@(N1x,N1y)362 N2x,N2y = Q@(N2x,N2y)363 N3x,N3y = Q@(N3x,N3y)364 N4x,N4y = Q@(N4x,N4y) 365 366 tmp = np.asarray(( (N1x,N1y), (N2x,N2y), (N3x,N3y), (N4x,N4y) ))367 expder.append(tmp)368 expjdet.append(length*breadth/4.0)369370 msg = f'Checking global derivatives for jaco2d in test_consistency_rotation_shift_jaco2d {ipoint=} '371 self.compare_iterables(actder,expder,msg=msg,rtol=closertol,atol=closeatol,desc='integration point ')372373 # sanity test, must fail374 #if ( ipoint == 4 ):375 # expjdet[2] += 1e-6376 377 msg = f'Checking jacodets for jaco2d in test_consistency_rotation_shift_jaco2d {ipoint=} '378 self.compare_iterables(actjdet,expjdet,msg=msg,rtol=closertol,atol=closeatol,desc='integration point ')379380381 382 def test_jaco1d_general_element(self):383 # consider two points p1 = (1,2,7) and p2 = (5,3,11) and 3 integration points384 # check that jdet,gder,jaco are correct385 p1 = np.asarray((1,2,7)); p2 = np.asarray((5,3,11));386387 # distance between p1 and p2388 dd = 5.744562646538029389390 # hand calculated values391 gder1 = (1.0 - 0.0)/(0 - dd) # derivative of the first shape function392 gder2 = (0.0 - 1.0)/(0 - dd) # derivative of the second shape function393 jdet = dd/2.0 394 jaco = dd/2.0395396 gg = gauss1d(3)397 *ss, = map(shape1d,gg.pts)398 der = [s.der for s in ss]399 *jj, = map(jaco1d,itertools.repeat([p1,p2]),der)400401 actgder = [ j.gder for j in jj]402 actjdet = [ j.jdet for j in jj]403 actjaco = [ j.jaco for j in jj]404405 self.assertAlmostEqual(gder1,actgder[0][0], places=closeplaces,msg='gder1 failure in test_jaco1d')406 self.assertAlmostEqual(gder2,actgder[0][1], places=closeplaces,msg='gder2 failure in test_jaco1d')407 self.assertAlmostEqual(jdet, actjdet[0], places=closeplaces,msg='jdet failure in test_jaco1d')408 self.assertAlmostEqual(jaco, actjaco[0][0][0], places=closeplaces,msg='jdet failure in test_jaco1d')409410411 def test_jaco2d_general_element(self):412 413 # this is the element414 p1 = np.asarray((1,2,0));415 p2 = np.asarray((6,4,0));416 p3 = np.asarray((3,7,0));417 p4 = np.asarray((-3,4,0));418419 gg = gauss2d(3);420 *ss, = map(shape2d,gg.pts)421 der = [s.der for s in ss]422 *jj, = map(jaco2d,itertools.repeat([p1,p2,p3,p4]),der)423424 # check at first integration point: jdet,jaco,gder425 j00 = 2.5563508326896285426 j01 = -1.9436491673103709427 j10 = 1.0563508326896291428 j11 = 1.0563508326896291429430 expjaco = np.asarray(( (j00,j01 ) , (j10,j11 ) ))431 expjdet = np.linalg.det(expjaco)432 tmp = np.asarray(( ( j11, -j01) ,(-j10 , j00) ))433 expjacoinv = (1.0/expjdet)*tmp434 expgder = ss[0].der@expjacoinv435 436 msg = f'Compare general element jaco2d: jdet '437 self.compare_iterables([jj[0].jdet],[expjdet],msg=msg,rtol=closertol,atol=closeatol,desc='integration point ')438439 msg = f'Compare general element jaco2d: jaco '440 self.compare_iterables([jj[0].jaco],[expjaco],msg=msg,rtol=closertol,atol=closeatol,desc='integration point ')441442 msg = f'Compare general element jaco2d: gder '443 self.compare_iterables([jj[0].gder],[expgder],msg=msg,rtol=closertol,atol=closeatol,desc='integration point ')444445446 def test_jaco2d_with_parent_domain(self):447 for ipoint in range(1,maxinteg):448 p1 = np.asarray((-1,-1,0));449 p2 = np.asarray(( 1,-1,0));450 p3 = np.asarray(( 1, 1,0));451 p4 = np.asarray((-1, 1,0));452453 gg = gauss2d(ipoint);454 *ss, = map(shape2d,gg.pts)455 der = [s.der for s in ss]456 # the following is a map. der yields shape function derivatives at the particular integration point457 # der is 'iterable', exhausting der terminates the map458 *jj, = map(jaco2d,itertools.repeat([p1,p2,p3,p4]),der)459460 actjdet = [j.jdet for j in jj]461 actjaco = [j.jaco for j in jj]462 actgder = [j.gder for j in jj]463464 # i'm using deepcopy because I want to perturb values while not modifying other values in the list or linked values465 expjdet = [1]*ipoint*ipoint466 expjaco = np.asarray([ [1.0, 0.0],[0.0, 1.0] ])467 expjaco = [ copy.deepcopy(expjaco) for i in range(ipoint*ipoint)]# deep copy468 expgder = copy.deepcopy(der)469470 #if ( ipoint == 4):471 # breakpoint()472473 msg = f'Consistency of jaco2d for jdet with input parent domain fails for {ipoint=} '474 self.compare_iterables(actjdet,expjdet,msg=msg,rtol=closertol,atol=closeatol,desc='integration point ')475476 msg = f'Consistency of jaco2d for jaco with input parent domain fails for {ipoint=} '477 self.compare_iterables(actjaco,expjaco,msg=msg,rtol=closertol,atol=closeatol,desc='integration point ')478479 msg = f'Consistency of jaco2d for gder with input parent domain fails for {ipoint=} '480 self.compare_iterables(actgder,expgder,msg=msg,rtol=closertol,atol=closeatol,desc='integration point ')481 482 483 484 def test_parent_interp_consistency(self):485 # test 1d interpolation with scalars, vectors and matrices486 # set the nodes to a constant value (scalar/vector/matrix), and see487 # that the constant is reproduced by interpolation at all integration points488 489 # Sanity testing - if the data at each node is a constant,490 # then after interpolation data at integration point should be the same constant491492 for ipoint,idim,ddim in itertools.product(range(1,maxinteg),range(1,3),range(-1,3)):493 494 # ipoint: number of integration points to use495 # idim : dimension we're testing i.e. calling (gauss1d,shape1d) or (gauss2d,shape2d)496 # ddim : dimension dimension of data we're interpolating scalars,vectors or matrices497 # ddim = -1 corresponds to pure python scalar,498 # = 0 corresponds to zero dim np array499 # = 1 to 1 dim np array (vector)500 # = 2 to 2 dim np array (matrix)501502 fgauss = eval(f'gauss{idim}d')503 fshape = eval(f'shape{idim}d')504 gg = fgauss(ipoint)505 *ss, = map(fshape,gg.pts)506507 # get random data at the two nodes of the linear element, or four nodes of a quad508 mult = np.random.randint(1,2**16)509 rowdim = np.random.randint(1,10)510 coldim = np.random.randint(1,16)511 512 if ( ddim == -1 ): dd = mult*np.random.rand() # pure scalar513 if ( ddim == 0 ): dd = np.asarray(mult*np.random.rand()) # zero dim np array514 if ( ddim == 1 ): dd = mult*np.random.rand(rowdim) # 1 dim numpy array515 if ( ddim == 2 ): dd = mult*np.random.rand(rowdim,coldim) # 2 dim numpy array516 517 nodedata = [dd]*(2**idim) 518 519 # output at all integration points520 actout = interp_parent(nodedata,ss)521 # expout = expected output at each integration point522 expout = [dd]*(ipoint**idim)523524 # sanity testing of the test - should fail525 # if ( ( ipoint == 4 ) and ( idim == 2 ) and ( ddim == 2 ) ):526 # actout[2] += 1e-5527528 #if (( ipoint == 2 ) and (idim == 1) and ( ddim == 2)):529 # breakpoint()530531 msg = f'Consistency for {idim}d parent interpolation with constant data fails for {ipoint=} {idim=} {ddim=} '532 self.compare_iterables(actout,expout,msg=msg,rtol=closertol,atol=closeatol,desc='integration point ')533534 def test_parent_interp_1d(self):535 # integration points536 npoint = 3537 gg = gauss1d(npoint)538 *ss, = map(shape1d,gg.pts)539 540 # scalar data541 nodedata = [11.2,-13.7]542 actout = interp_parent(nodedata,ss)543 expout = [8.393728532056468, -1.25,-10.893728532056468]544 msg = 'test_parent_interp_1d fails for scalar data '545 self.compare_iterables(actout,expout,msg=msg,rtol=closertol,atol=closeatol,desc='integration point ')546 547 # vector data548549 d1 = np.asarray([1.23, -0.5]); d2 = np.asarray([7.12, 0.25])550 nodedata = [d1,d2]551 actout = interp_parent(nodedata,ss)552 out1 = np.asarray([ 1.8938128090838315, -0.4154737509655563 ])553 out2 = np.asarray([ 4.175, -0.125 ])554 out3 = np.asarray([ 6.456187190916169 , 0.1654737509655563])555 expout = [out1,out2,out3]556 msg = 'test_parent_interp_1d fails for vector data '557 self.compare_iterables(actout,expout,msg=msg,rtol=closertol,atol=closeatol,desc='integration point ')558559 # matrix data560561 d1 = np.asarray(( (12.0,-6.0), (11.125, 2.5) ))562 d2 = np.asarray(( (1.0, 3.0), (0.175, 1.5) ))563 nodedata = [d1,d2]564 actout = interp_parent(nodedata,ss)565 out1 = np.asarray([[10.76028168082816 , -4.985685011586675 ],[ 9.890916764097122 , 2.3872983346207413]])566 out2 = np.asarray([[ 6.5 , -1.5 ],[ 5.65, 2. ]])567 out3 = np.asarray([[2.2397183191718413, 1.9856850115866753],[1.4090832359028784, 1.6127016653792583]])568 expout = [out1,out2,out3]569 msg = 'test_parent_interp_1d fails for matrix data '570 self.compare_iterables(actout,expout,msg=msg,rtol=closertol,atol=closeatol,desc='integration point ')571572 def test_parent_interp_2d(self):573 npoint = 2574 gg = gauss2d(npoint)575 *ss, = map(shape2d,gg.pts)576 577 # scalar interpolation578 d1 = 0.5; d2 = 1.245; d3 = 11.2; d4 = -5.1579 nodedata = [d1,d2,d3,d4]580 actout = interp_parent(nodedata,ss)581 out1 = 0.16867605983550216; out2=-1.1666437290040874; out3=2.496643729004088;out4=6.346323940164497582 expout = [out1,out2,out3,out4]583 msg = 'test_parent_interp_2d fails for scalar data '584 self.compare_iterables(actout,expout,msg=msg,rtol=closertol,atol=closeatol,desc='integration point ')585 586 # vector interpolation587 d1 = np.asarray((1.23,0.259)); d2 = np.asarray((2.3,0.91));588 d3 = np.asarray((3.1,-0.765)); d4 = np.asarray((-11.2,-1.3));589 nodedata = [d1,d2,d3,d4]590 actout = interp_parent(nodedata,ss)591 592 out1 = np.asarray([-0.5798225016923 , 0.0619366711584217])593 out2 = np.asarray([-6.142114317029974 , -0.8523053807878699])594 out3 = np.asarray([1.652114317029974 , 0.4236387141212032])595 out4 = np.asarray([ 0.4998225016923001, -0.5292700044917551])596 expout = [out1,out2,out3,out4]597 msg = 'test_parent_interp_2d fails for vector data '598 self.compare_iterables(actout,expout,msg=msg,rtol=closertol,atol=closeatol,desc='integration point ')599 600 # matrix interpolation601 d1 = np.asarray(( (121.0,-26.0), (1.15, 0.5) ))602 d2 = np.asarray(( (1.0, 32.0), (0.333, 133.5) ))603 d3 = np.asarray(( (11.0,-61.0), (12.5, 0.15) ))604 d4 = np.asarray(( (199.0, 39.0), (-0.42, 0.5) ))605606 nodedata = [d1,d2,d3,d4]607 actout = interp_parent(nodedata,ss)608 609 out1 = np.asarray([[109.08759813876276 , -7.063036955848214 ],[ 1.2590372223488737, 22.651036297108188 ]])610 out2 = np.asarray([[145.82434331643964 , 11.187392608830354 ],[ 2.0286276236501064, 6.381207098889888 ]])611 out3 = np.asarray([[31.50899001689372 , 7.145940724502975],[ 2.463372376349894, 83.16879290111011 ]])612 out4 = np.asarray([[ 45.57906852790392 , -27.270296377485117],[ 7.811962777651126, 22.448963702891817]])613614 expout = [out1,out2,out3,out4]615 msg = 'test_parent_interp_2d fails for matrix data '616
...
utils.py
Source:utils.py
...123 elif isinstance(number, Decimal):124 return cast_number(str(number))125 elif isinstance(number, (float, int)):126 return number127def compare_iterables(keys, this):128 """129 Compare two iterables.130 Check that all the elements of the (list, tuple or dict) passed as keys exist in131 the (list, tuple or dict) passed as "this"132 Parameters133 ----------134 keys : list, tuple or dict135 this : list, tuple or dict136 Returns137 -------138 bool: True if all the elements of the (list, tuple or dict) passed as keys exist in139 the (list, tuple or dict) passed as "this"140 Examples141 --------142 >>> from core_utils.utils import compare_iterables143 >>> compare_iterables(["a", "b"], ["a", "b", "c"])144 """145 return (146 all([key in this for key in keys])147 if isinstance(keys, (list, dict)) and isinstance(this, (list, dict))148 else False149 )150def dict_strip_nulls(d):151 """152 Remove null values from a dictionary.153 Parameters154 ----------155 d : dict156 Returns157 -------...
conftest.py
Source:conftest.py
...19def dicts_with_null_path():20 return os.path.join('tests', 'data', 'with-null.json')21@pytest.fixture(scope='function')22def compare_iter():23 def compare_iterables(collection1, collection2):24 for i1, i2 in zip(collection1, collection2):25 assert i1 == i2, "%s != %s" % (i1, i2)26 return compare_iterables27def test_compare_iterables(compare_iter):28 compare_iter('abc', 'abc')29 with pytest.raises(AssertionError):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!