def find_leaf_nodes(mesh,radius=0.3, ratio_of_nodes = 0.3):
	"""
	marks some areas of the mesh as areas where there is stimulated exitation. 
	radius is the size of the node areas
	ratio of nodes is the amount of nodes you want, divided by the number of nodes 
	found by the purkinje simulation. 
	"""
	marks_vector = np.zeros(fenics_ordered_coordinates.shape[0])
	E, N, left, distance_lv, terminal_lv = call_tree.get_left()
	E, N, right, distance_rv, terminal_rv = call_tree.get_right()

	ratio_of_discarded_nodes = 1-ratio_of_nodes
	coin_rv = np.random.random(len(terminal_rv))
	terminal_rv = terminal_rv*coin_rv
	terminal_rv = (terminal_rv > ratio_of_discarded_nodes)*1.0


	coin_lv = np.random.random(len(terminal_lv))
	terminal_lv = terminal_lv*coin_lv
	terminal_lv = (terminal_lv > ratio_of_discarded_nodes)*1.0
	

	r = radius #some radius
	rr = r**2

	C = fenics_ordered_coordinates # short hand 

	#left ventricle:
	terminal_idx = np.argwhere(terminal_lv)
	for i in range(np.size(terminal_idx)):
		leaf_idx = terminal_idx[i]
		leaf_coor = N[leaf_idx, :]
		dist = C-leaf_coor
		dist = dist**2
		dist = np.sum(dist, axis=1)
		leaf_idx = np.argwhere(dist < rr)
		marks_vector[leaf_idx] = distance_lv[terminal_idx[i]]

	#right ventricle:
	terminal_idx = np.argwhere(terminal_rv)
	for i in range(np.size(terminal_idx)):
		leaf_idx = terminal_idx[i]
		leaf_coor = N[leaf_idx, :]
		dist = C-leaf_coor
		dist = dist**2
		dist = np.sum(dist, axis=1)
		leaf_idx = np.argwhere(dist < rr)
		marks_vector[leaf_idx] = distance_rv[terminal_idx[i]]

	return marks_vector
def find_leaf_nodes(mesh, radius=0.3, ratio_of_nodes=0.3):
    """
	marks some areas of the mesh as areas where there is stimulated exitation. 
	radius is the size of the node areas
	ratio of nodes is the amount of nodes you want, divided by the number of nodes 
	found by the purkinje simulation. 
	"""
    marks_vector = np.zeros(fenics_ordered_coordinates.shape[0])
    E, N, left, distance_lv, terminal_lv = call_tree.get_left()
    E, N, right, distance_rv, terminal_rv = call_tree.get_right()

    ratio_of_discarded_nodes = 1 - ratio_of_nodes
    coin_rv = np.random.random(len(terminal_rv))
    terminal_rv = terminal_rv * coin_rv
    terminal_rv = (terminal_rv > ratio_of_discarded_nodes) * 1.0

    coin_lv = np.random.random(len(terminal_lv))
    terminal_lv = terminal_lv * coin_lv
    terminal_lv = (terminal_lv > ratio_of_discarded_nodes) * 1.0

    r = radius  #some radius
    rr = r**2

    C = fenics_ordered_coordinates  # short hand

    #left ventricle:
    terminal_idx = np.argwhere(terminal_lv)
    for i in range(np.size(terminal_idx)):
        leaf_idx = terminal_idx[i]
        leaf_coor = N[leaf_idx, :]
        dist = C - leaf_coor
        dist = dist**2
        dist = np.sum(dist, axis=1)
        leaf_idx = np.argwhere(dist < rr)
        marks_vector[leaf_idx] = distance_lv[terminal_idx[i]]

    #right ventricle:
    terminal_idx = np.argwhere(terminal_rv)
    for i in range(np.size(terminal_idx)):
        leaf_idx = terminal_idx[i]
        leaf_coor = N[leaf_idx, :]
        dist = C - leaf_coor
        dist = dist**2
        dist = np.sum(dist, axis=1)
        leaf_idx = np.argwhere(dist < rr)
        marks_vector[leaf_idx] = distance_rv[terminal_idx[i]]

    return marks_vector
def find_leaf_nodes(radius=0.3, ratio_of_nodes = 0.3):
	"""
	marks some areas of the mesh as areas where there is stimulated exitation. 
	radius is the size of the node areas
	ratio of nodes is the amount of nodes you want, divided by the number of nodes 
		found by the purkinje simulation. 
	"""
	marks_vector = np.zeros(fenics_ordered_coordinates.shape[0])
	E, N, left, distance_lv, terminal_lv = call_tree.get_left()
	E, N, right, distance_rv, terminal_rv = call_tree.get_right()

	ratio_of_discarded_nodes = 1-ratio_of_nodes
	coin_rv = np.random.random(len(terminal_rv))
	terminal_rv = terminal_rv*coin_rv
	terminal_rv = (terminal_rv > ratio_of_discarded_nodes)*1.0


	coin_lv = np.random.random(len(terminal_lv))
	terminal_lv = terminal_lv*coin_lv
	terminal_lv = (terminal_lv > ratio_of_discarded_nodes)*1.0
	

	r0=0.05
	r1=0.3
	C = fenics_ordered_coordinates # short hand 

	#left ventricle:
	terminals = terminal_lv + terminal_rv

	terminal_idx = np.argwhere(terminals)
	terminal_coor = N[terminal_idx]
	terminal_coor = np.reshape(terminal_coor, (terminal_coor.shape[0], terminal_coor.shape[2]))

	distvec = np.zeros(terminal_coor.shape)
	BZ = np.zeros(C.shape[0])
	distr = np.zeros(terminal_coor.shape[0])
	for i in range(C.shape[0]):
		distvec = C[i,:] - terminal_coor
		distr = np.sum(distvec**2,axis=1)
		dist = np.min(distr)
		r = np.sqrt(dist)
		BZ[i] = ((r1-r)/(r1-r0)).clip(-1,1)

	return BZ
def find_leaf_nodes(radius=0.3, ratio_of_nodes=0.3):
    """
	marks some areas of the mesh as areas where there is stimulated exitation. 
	radius is the size of the node areas
	ratio of nodes is the amount of nodes you want, divided by the number of nodes 
		found by the purkinje simulation. 
	"""
    marks_vector = np.zeros(fenics_ordered_coordinates.shape[0])
    E, N, left, distance_lv, terminal_lv = call_tree.get_left()
    E, N, right, distance_rv, terminal_rv = call_tree.get_right()

    ratio_of_discarded_nodes = 1 - ratio_of_nodes
    coin_rv = np.random.random(len(terminal_rv))
    terminal_rv = terminal_rv * coin_rv
    terminal_rv = (terminal_rv > ratio_of_discarded_nodes) * 1.0

    coin_lv = np.random.random(len(terminal_lv))
    terminal_lv = terminal_lv * coin_lv
    terminal_lv = (terminal_lv > ratio_of_discarded_nodes) * 1.0

    r0 = 0.05
    r1 = 0.3
    C = fenics_ordered_coordinates  # short hand

    #left ventricle:
    terminals = terminal_lv + terminal_rv

    terminal_idx = np.argwhere(terminals)
    terminal_coor = N[terminal_idx]
    terminal_coor = np.reshape(
        terminal_coor, (terminal_coor.shape[0], terminal_coor.shape[2]))

    distvec = np.zeros(terminal_coor.shape)
    BZ = np.zeros(C.shape[0])
    distr = np.zeros(terminal_coor.shape[0])
    for i in range(C.shape[0]):
        distvec = C[i, :] - terminal_coor
        distr = np.sum(distvec**2, axis=1)
        dist = np.min(distr)
        r = np.sqrt(dist)
        BZ[i] = ((r1 - r) / (r1 - r0)).clip(-1, 1)

    return BZ
from dolfin import *
import sys
import os
import numpy as np

parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)
from monodomain_solver import *

sys.path.insert(0, '../purkinje/python/')
import call_tree

E, N, left, distance_lv, terminal_lv = call_tree.get_left()
E, N, right, distance_rv, terminal_rv = call_tree.get_right()

mesh = Mesh('heart.xml')
#plot(mesh, axes=True, grid=True)

print terminal_rv.shape, E.shape, N.shape, mesh.coordinates().shape

mesh = Mesh('meshes/reference.xml')
meshfunc = MeshFunction('double', mesh, 'meshes/rv.attr0.xml')

#plot(meshfunc)
#interactive()

radius = 0.1
V = FunctionSpace(mesh, 'CG', 1)

coordinates = mesh.coordinates()
from dolfin import *
import sys
import os
import numpy as np

parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,parentdir) 
from monodomain_solver import *

sys.path.insert(0, '../purkinje/python/')
import call_tree

E, N, left, distance_lv, terminal_lv = call_tree.get_left()
E, N, right, distance_rv, terminal_rv = call_tree.get_right()


mesh = Mesh('heart.xml')
#plot(mesh, axes=True, grid=True)


print terminal_rv.shape, E.shape, N.shape, mesh.coordinates().shape

mesh = Mesh('meshes/reference.xml')
meshfunc = MeshFunction('double', mesh, 'meshes/rv.attr0.xml')

#plot(meshfunc)
#interactive()

radius = 0.1
V = FunctionSpace(mesh, 'CG', 1)