3D EM based on Schur decomposition
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

block.py 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import numpy as np
  2. import pprint
  3. def calcH( oxx, nxx, dxx, pad ):
  4. """Calculates grid spacing along a dimension, if padding cells are requested those are incorporated too"""
  5. px = np.arange(oxx, oxx+(nxx+1)*dxx, dxx)
  6. hx = dxx*np.ones(nxx)
  7. if not pad:
  8. #px = np.arange(oxx-dxx/2, oxx-dxx/2+(nxx+1)*dxx, dxx)
  9. return hx, px
  10. else:
  11. pp = 1.1 # fixed padding expansion factor
  12. px_padhi = np.zeros( pad+1 )
  13. px_padlo = np.zeros( pad+1 )
  14. hx_pad = np.zeros( pad )
  15. hx_pad[0] = dxx*pp
  16. px_padhi[0] = px[-1] # hi end
  17. px_padlo[0] = px[0] # lo end
  18. for ip in range(1,pad):
  19. px_padhi[ip] = px_padhi[ip-1] + hx_pad[ip-1]
  20. px_padlo[ip] = px_padlo[ip-1] - hx_pad[ip-1]
  21. hx_pad[ip] = pp*hx_pad[ip-1]
  22. px_padhi[-1] = px_padhi[-2] + hx_pad[-1]
  23. px_padlo[-1] = px_padlo[-2] - hx_pad[-1]
  24. hx = np.concatenate((hx_pad[::-1],hx, hx_pad))
  25. px = np.concatenate((px_padlo[1::][::-1], px, px_padhi[1::]))
  26. return hx, px
  27. def writeH(fout, h, p):
  28. ix = 0
  29. rx,rxm1 = 0,0
  30. while (ix < len(h)):
  31. fout.write ("%6.2f" %( h[ix]) )
  32. ix += 1
  33. rx += 1
  34. if rx > 4:
  35. fout.write(" //") # [%i:%i]\n" %(rxm1,ix-1))
  36. #fout.write( (p[rxm1:ix-1]) )
  37. for pp in p[rxm1:ix+1]:
  38. fout.write(" %6.2f" %(pp))
  39. fout.write("\n")
  40. rxm1 += rx
  41. rx = 0
  42. else:
  43. fout.write(" ")
  44. def writeGRD(fout, args):
  45. hx,px = calcH( args.ox[0], args.nx[0], args.dx[0], args.pad )
  46. hy,py = calcH( args.oy[0], args.ny[0], args.dy[0], args.pad )
  47. hz,pz = calcH( args.oz[0], args.nz[0], args.dz[0], args.pad )
  48. fout.write( "%i %i %i // nx ny nz\n" %(args.nx[0]+2*args.pad,args.ny[0]+2*args.pad,args.nz[0]+2*args.pad) )
  49. #fout.write( "%f %f %f // ox oy oz (cell centres)\n" %(args.ox[0]+args.dx[0]/2, args.oy[0]+args.dy[0]/2, args.oz[0]+args.dz[0]/2))
  50. fout.write( "%f %f %f // ox oy oz (cell centres)\n" %(px[0]+.5*hx[0], py[0]+.5*hy[0], pz[0]+.5*hz[0]))
  51. fout.write("\n// hx\n")
  52. writeH(fout, hx, px)
  53. fout.write("\n// hy\n")
  54. writeH(fout, hy, py)
  55. fout.write("\n// hz\n")
  56. writeH(fout, hz, pz)
  57. fout.write("\n")
  58. fout.write( "// vim: set filetype=cpp:\n")
  59. return px,py,pz
  60. def writeMOD(fout, args, px, py, pz, pad=0):
  61. sigma = np.zeros( (args.nx[0]+2*pad, args.ny[0]+2*pad, args.nz[0]+2*pad) )
  62. sigma[:,:,pz[1:]>0] = 0.05
  63. xlo = np.where( px[0:-1] >= args.bxlo[0] )[0][0]
  64. xhi = np.where( px[1:] <= args.bxhi[0] )[0][-1] + 1
  65. ylo = np.where( py[0:-1] >= args.bylo[0] )[0][0]
  66. yhi = np.where( py[1:] <= args.byhi[0] )[0][-1] + 1
  67. zlo = np.where( pz[0:-1] >= args.bzlo[0] )[0][0]
  68. zhi = np.where( pz[1:] <= args.bzhi[0] )[0][-1] + 1
  69. sigma[xlo:xhi, ylo:yhi, zlo:zhi] = .1
  70. for val in np.ravel(sigma, order='C'):
  71. fout.write("%f\n" %(val) )
  72. def str2bool(v):
  73. if v.lower() in ('yes', 'true', 't', 'y', '1'):
  74. return True
  75. elif v.lower() in ('no', 'false', 'f', 'n', '0'):
  76. return False
  77. else:
  78. raise argparse.ArgumentTypeError('Boolean value expected.')
  79. import argparse
  80. PAD = 0
  81. parser = argparse.ArgumentParser(description='Set up test EMSchur3D block problem.')
  82. parser.add_argument('nx', metavar='nx', type=int, nargs=1,
  83. help='number of cells in x, does NOT include padding cells')
  84. parser.add_argument('ny', metavar='ny', type=int, nargs=1,
  85. help='number of cells in y, does NOT include padding cells')
  86. parser.add_argument('nz', metavar='nz', type=int, nargs=1,
  87. help='number of cells in z, does NOT include padding cells')
  88. parser.add_argument('ox', metavar='ox', type=float, nargs=1,
  89. help='offset of grid in x (low edge of cell not centre), if pad is true ox should NOT include padding')
  90. parser.add_argument('oy', metavar='oy', type=float, nargs=1,
  91. help='offset of grid in y (low edge of cell not centre), if pad is true oy should NOT include padding')
  92. parser.add_argument('oz', metavar='oz', type=float, nargs=1,
  93. help='offset of grid in z (low edge of cell not centre), if pad is true oz should NOT include padding')
  94. parser.add_argument('dx', metavar='dx', type=int, nargs=1,
  95. help='minimum spacing in x')
  96. parser.add_argument('dy', metavar='dy', type=int, nargs=1,
  97. help='minimum spacing in y')
  98. parser.add_argument('dz', metavar='dz', type=int, nargs=1,
  99. help='minimum spacing in z')
  100. parser.add_argument('bxlo', metavar='bxlo', type=float, nargs=1,
  101. help='low end of block cell location in x')
  102. parser.add_argument('bxhi', metavar='bxhi', type=float, nargs=1,
  103. help='high end of block cell location in x')
  104. parser.add_argument('bylo', metavar='bylo', type=float, nargs=1,
  105. help='low end of block cell location in y')
  106. parser.add_argument('byhi', metavar='byhi', type=float, nargs=1,
  107. help='high end of block cell location in y')
  108. parser.add_argument('bzlo', metavar='bzlo', type=float, nargs=1,
  109. help='low end of block cell location in z')
  110. parser.add_argument('bzhi', metavar='bzhi', type=float, nargs=1,
  111. help='high end of block cell location in z')
  112. parser.add_argument("--pad", type=int, nargs='?',
  113. const=True, default=PAD,
  114. help="Activate this number of padding cells with 10%% growth factor.")
  115. args = parser.parse_args()
  116. fout = open("example.grd", 'w')
  117. px,py,pz = writeGRD(fout, args)
  118. fout.close()
  119. mout = open("example.mod", 'w')
  120. writeMOD(mout, args, px, py, pz, args.pad)
  121. #print(args.nx)
  122. #print(args.accumulate(args.nx))
  123. #print(args.accumulate(args.ny))