| 1 | import numpy as np
|
|---|
| 2 | import copy
|
|---|
| 3 |
|
|---|
| 4 | def AreaAverageOntoPartition(md,vector,layer=None):
|
|---|
| 5 | '''AREAAVERAGEONTOPARTITION
|
|---|
| 6 | compute partition values for a certain vector expressed on the vertices of the mesh.
|
|---|
| 7 | Use area weighted average.
|
|---|
| 8 |
|
|---|
| 9 | Usage:
|
|---|
| 10 | average=AreaAverageOntoPartition(md,vector)
|
|---|
| 11 | average=AreaAverageOntoPartition(md,vector,layer) #if in 3D, chose which layer is partitioned
|
|---|
| 12 | '''
|
|---|
| 13 | #some checks
|
|---|
| 14 | if(md.mesh.dimension()==3):
|
|---|
| 15 | if layer == None:
|
|---|
| 16 | raise RuntimeError('AreaAverageOntoPartition: layer should be provided onto which Area Averaging occurs')
|
|---|
| 17 |
|
|---|
| 18 | #save 3D model
|
|---|
| 19 | md3d = copy.deepcopy(md)
|
|---|
| 20 |
|
|---|
| 21 | md.mesh.elements = md.mesh.elements2d
|
|---|
| 22 | md.mesh.x = md.mesh.x2d
|
|---|
| 23 | md.mesh.y = md.mesh.y2d
|
|---|
| 24 | md.mesh.numberofvertices = md.mesh.numberofvertices2d
|
|---|
| 25 | md.mesh.numberofelements = md.mesh.numberofelements2d
|
|---|
| 26 | md.qmu.vertex_weight = []
|
|---|
| 27 | md.mesh.vertexconnectivity = []
|
|---|
| 28 |
|
|---|
| 29 | #run connectivity routine
|
|---|
| 30 | md = adjacency(md)
|
|---|
| 31 |
|
|---|
| 32 | #finally, project vector:
|
|---|
| 33 | vector = project2d(md3d,vector,layer)
|
|---|
| 34 | md.qmu.partition = project2d(md3d,md3d.qmu.partition,layer)
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 | #ok, first check that part is Matlab indexed
|
|---|
| 38 | part = (md.qmu.partition).copy()
|
|---|
| 39 | part = part.flatten() + 1
|
|---|
| 40 |
|
|---|
| 41 | #some check:
|
|---|
| 42 | if md.qmu.numberofpartitions != max(part):
|
|---|
| 43 | raise RuntimeError('AreaAverageOntoPartition error message: ''npart'' should be equal to max(md.qmu.partition)')
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 | #initialize output
|
|---|
| 47 | partvector = np.zeros((max(part)))
|
|---|
| 48 |
|
|---|
| 49 | #start weight average
|
|---|
| 50 | weightedvector = vector.flatten()*md.qmu.vertex_weight
|
|---|
| 51 | for i in range(max(part)):
|
|---|
| 52 | pos=np.where((part-1)==i)
|
|---|
| 53 | partvector[i]=sum(weightedvector[pos])/sum(md.qmu.vertex_weight[pos])
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 | #in 3D, restore 3D model:
|
|---|
| 57 | if(md.mesh.dimension()==3):
|
|---|
| 58 | md = copy.deepcopy(md3d)
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | return partvector
|
|---|
| 62 |
|
|---|