switched voronoi seeding to python

This commit is contained in:
Pratheek Shanthraj 2012-03-29 16:41:23 +00:00
parent 4a6cdcc2e2
commit 30d38436c7
3 changed files with 90 additions and 95 deletions

View File

@ -1,93 +0,0 @@
!prec.f90 407 2009-08-31 15:09:15Z MPIE\f.roters
!##############################################################
MODULE prec
!##############################################################
implicit none
! *** Precision of real and integer variables ***
integer, parameter :: pReal = selected_real_kind(15,300) ! 15 significant digits, up to 1e+-300
integer, parameter :: pInt = selected_int_kind(9) ! up to +- 1e9
integer, parameter :: pLongInt = 8 ! should be 64bit
END MODULE prec
program voronoi
use prec, only: pReal, pInt
implicit none
logical, dimension(:), allocatable :: seedmap
character(len=1024) filename
integer(pInt), dimension(3) :: seedcoord
integer(pInt), dimension(:), allocatable :: rndInit
integer(pInt) a, b, c, N_Seeds, seedpoint, i, randomSeed, rndSize
real(pReal), dimension(:,:), allocatable :: grainEuler, seeds
real(pReal), parameter :: pi = 3.14159265358979323846264338327950288419716939937510_pReal
print*, '******************************************************************************'
print*, ' Voronoi description file'
print*, '******************************************************************************'
print*, '$Id$'
print*, ''
print*, 'generates:'
print*, ' * description file "_OUTPUT_.seeds":'
print*, ''
write(*, '(A)', advance = 'NO') 'output seed filename: '
read(*, *), filename
write(*, '(A)', advance = 'NO') 'seed of random number generator: '
read(*, *), randomSeed; randomSeed = max(0_pInt,randomSeed)
write(*, '(A)', advance = 'NO') 'number of grains: '
read(*, *), N_Seeds
write(*, '(A)', advance = 'NO') 'min. Fourier points in x: '
read(*, *), a
write(*, '(A)', advance = 'NO') 'min. Fourier points in y: '
read(*, *), b
write(*, '(A)', advance = 'NO') 'min. Fourier points in z: '
read(*, *), c
allocate (seedmap(a*b*c)); seedmap = .false. ! logical to store information which position is occupied by a voronoi seed
allocate (seeds(N_Seeds,3))
allocate (grainEuler(N_Seeds,3))
call random_seed(size=rndSize)
allocate(rndInit(rndSize))
rndInit = randomSeed
call random_seed(put=rndInit)
call random_seed(get=rndInit)
do i=1, N_Seeds
call random_number(grainEuler(i,1))
call random_number(grainEuler(i,2))
call random_number(grainEuler(i,3))
grainEuler(i,1) = (grainEuler(i,1))*360.0
grainEuler(i,2) = acos(2.0_pReal*(grainEuler(i,2))-1.0_pReal)*180.0/pi
grainEuler(i,3) = grainEuler(i,3)*360.0
enddo
!generate random position of seeds for voronoi tessellation
i = 1
do while (i <= N_Seeds)
call random_number(seeds(i,1)); seedcoord(1) = min(a,int(seeds(i,1)*a)+1_pInt)-1_pInt
call random_number(seeds(i,2)); seedcoord(2) = min(b,int(seeds(i,2)*b)+1_pInt)-1_pInt
call random_number(seeds(i,3)); seedcoord(3) = min(c,int(seeds(i,3)*c)+1_pInt)-1_pInt
seedpoint = seedcoord(1) + seedcoord(2)*a + seedcoord(3)*a*b
if (.not. seedmap(seedpoint+1)) then
seedmap(seedpoint+1) = .true.
i = i + 1
end if
end do
! write description file with orientation and position of each seed
open(21, file = trim(filename)//('.seeds'))
write(21, '(i1,a1,a6)') 4,achar(9),'header'
write(21, '(A, I8, A, I8, A, I8)') 'resolution a ', a, ' b ', b, ' c ', c
write(21, '(A, I8)') 'grains', N_Seeds
write(21, '(A, I8)') 'random seed ',rndInit(1)
write(21,'(6(a,a1))') 'x',achar(9),'y',achar(9),'z',achar(9),'phi1',achar(9),'Phi',achar(9),'phi2',achar(9)
do i = 1, n_Seeds
write(21, '(6(F10.6,a1))'),seeds(i,1), achar(9), seeds(i,2), achar(9), seeds(i,3), achar(9), &
grainEuler(i,1),achar(9), grainEuler(i,2),achar(9), grainEuler(i,3),achar(9)
end do
close(21)
deallocate (rndInit)
end program voronoi

View File

@ -0,0 +1,89 @@
#!/usr/bin/env python
# -*- coding: UTF-8 no BOM -*-
import os,sys,string,re,math,numpy,random
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
# -----------------------------
class extendedOption(Option):
# -----------------------------
# used for definition of new option parser action 'extend', which enables to take multiple option arguments
# taken from online tutorial http://docs.python.org/library/optparse.html
ACTIONS = Option.ACTIONS + ("extend",)
STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
def take_action(self, action, dest, opt, value, values, parser):
if action == "extend":
lvalue = value.split(",")
values.ensure_value(dest, []).extend(lvalue)
else:
Option.take_action(self, action, dest, opt, value, values, parser)
# ----------------------- MAIN -------------------------------
identifiers = {
'resolution': ['a','b','c'],
'dimension': ['x','y','z'],
}
mappings = {
'resolution': lambda x: int(x),
'dimension': lambda x: float(x),
}
parser = OptionParser(option_class=extendedOption, usage='%prog options [file[s]]', description = """
Offset microstructure index for points which see a microstructure different from themselves within a given (cubic) vicinity,
i.e. within the region close to a grain/phase boundary.
""" + string.replace('$Id: spectral_geomCheck 994 2011-09-05 13:38:10Z MPIE\p.eisenlohr $','\n','\\n')
)
parser.add_option('-f', '--file', dest='filename', type="string", \
help='output seed file name')
parser.add_option('-s', '--seed', dest='randomSeed', type='int', \
help='seed of random number generator')
parser.add_option('-n', '--ngrains', dest='N_Seeds', type='int', \
help='seed of random number generator')
parser.add_option('-r','--res', dest='res', type='int', nargs=3, \
help='Min Fourier points in x, y, z')
parser.set_defaults(filename = 'seeds')
parser.set_defaults(randomSeed = 0)
(options, filenames) = parser.parse_args()
if options.N_Seeds > options.res[0]*options.res[1]*options.res[2]:
print 'Warning: Number of grains exceeds min resolution'
options.N_Seeds = options.res[0]*options.res[1]*options.res[2]
seeds = numpy.zeros((3,options.N_Seeds),float)
numpy.random.seed(options.randomSeed)
grainEuler=numpy.random.rand(3,options.N_Seeds)
grainEuler[0,:] = 360*grainEuler[0,:]
grainEuler[1,:] = numpy.arccos(2*grainEuler[1,:]-1)*180/math.pi
grainEuler[2,:] = 360*grainEuler[2,:]
seedpoint = numpy.random.permutation(options.res[0]*options.res[1]*options.res[2])[:options.N_Seeds]
seeds[0,:]=numpy.mod(seedpoint,options.res[0])/options.res[0]+1
seeds[1,:]=numpy.mod((seedpoint-seeds[0,:])/options.res[0],options.res[1])+1
seeds[2,:]=(seedpoint-seeds[0,:]-options.res[1]*seeds[1,:])/(options.res[0]*options.res[1])+1
seeds[0,:]=(seeds[0,:]+numpy.random.rand(1,options.N_Seeds)-0.5)/options.res[0]
seeds[1,:]=(seeds[1,:]+numpy.random.rand(1,options.N_Seeds)-0.5)/options.res[1]
seeds[2,:]=(seeds[2,:]+numpy.random.rand(1,options.N_Seeds)-0.5)/options.res[2]
f = open(options.filename+'.seeds', 'w')
f.write("{0:1d} {1:6s}\n".format(4,'header'))
f.write("{0:s} {1:8d} {2:s} {3:8d} {4:s} {5:8d}\n".format('resolution a',options.res[0],'b',options.res[1],'c',options.res[2]))
f.write("{0:s} {1:8d}\n".format('grains',options.N_Seeds))
f.write("{0:s} {1:8d}\n".format('random seed',options.randomSeed))
f.write("x y z phi1 Phi phi2\n")
f.close()
f=file(options.filename+'.seeds','a')
numpy.savetxt(f,numpy.transpose(numpy.concatenate((seeds,grainEuler),axis=0)),fmt='%10.6f',delimiter=' ')
f.close()

View File

@ -85,7 +85,7 @@ bin_link = { \
'spectral_geomCrop.py',
'spectral_minimalSurface.py',
'spectral_vicinityOffset.py',
'voronoi_randomSeeding.exe',
'voronoi_randomSeeding.py',
'voronoi_tessellation.exe',
],
'post' : [
@ -113,7 +113,6 @@ bin_link = { \
compile = { \
'pre' : [
'voronoi_randomSeeding.f90',
'voronoi_tessellation.f90',
],
'post' : [