Skip to content

Commit

Permalink
changed grid
Browse files Browse the repository at this point in the history
removed links
added supports
still needs testing
  • Loading branch information
maekke97 committed Apr 22, 2017
1 parent 8bf93ee commit f432510
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 33 deletions.
14 changes: 12 additions & 2 deletions HierMat/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,18 @@ def get_grid_item(self, item):
:param item: index
:type item: int
"""
# TODO: change this to return the i-th item as in get_item
return self.grid[item]

def get_grid_item_support(self, item):
"""Return supports of item from grid
:param item: index
:type item: int
"""
# TODO: change this to return the support of the i-th item
return self.grid.get_support(item)

def get_index(self, item):
"""Return index at item
Expand Down Expand Up @@ -99,9 +109,9 @@ def diameter(self):
"""
# get all points from grid in indices
points = [self.grid.points[i] for i in self.indices]
# add relevant links
# add relevant supports
for i in self.indices:
points.extend([p for p in self.grid.links[i]])
points.extend([p for p in self.grid.supports[i]])
# compute distance matrix
dist_mat = [numpy.linalg.norm(x - y) for x in points for y in points]
return max(dist_mat)
Expand Down
8 changes: 8 additions & 0 deletions HierMat/cluster_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ def get_grid_item(self, item):
"""
return self.content.get_grid_item(item)

def get_grid_item_support(self, item):
"""Return supports of item from grid
:param item: index
:type item: int
"""
return self.content.get_support(item)

def get_patch_coordinates(self):
"""Return min and max out of indices
Expand Down
29 changes: 17 additions & 12 deletions HierMat/examples/model_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,26 @@
"""
import numpy

import HierMat

import os
import math
import scipy


def model_1d(n=2 ** 5, gauss_points=1):
"""
:param n:
:param gauss_points:
:return:
"""
def model_1d(n=2 ** 5, max_rank=2, gauss_points=1):
""""""
h = float(1)/n
midpoints = [(i + 0.5) * h for i in xrange(n)]
intervals = [[i * h, (i + 1) * h] for i in xrange(n)]
grid = HierMat.Grid(points=midpoints, links=intervals)
supports = {point: lambda x: x == point for point in midpoints}
grid = HierMat.Grid(points=midpoints, supports=supports)
cluster = HierMat.Cluster(grid=grid)
unit_cuboid = HierMat.Cuboid([0], [1])
strategy = HierMat.RegularCuboid(cluster=cluster, cuboid=unit_cuboid)
cluster_tree = HierMat.build_cluster_tree(splitable=strategy, max_leaf_size=1)
cluster_tree = HierMat.build_cluster_tree(splitable=strategy, max_leaf_size=max_rank)
HierMat.export(cluster_tree, form='dot', out_file='galerkin_1d_ct.dot')
os.system('dot -Tpng galerkin_1d_ct.dot > model_1d-ct.png')
os.system('dot -Tsvg galerkin_1d_ct.dot > model_1d-ct.svg')
Expand All @@ -47,15 +45,22 @@ def model_1d(n=2 ** 5, gauss_points=1):


def kernel(x, y):
""""""
return math.log(math.fabs(x - y))


def galerkin_1d_rank_k(block_cluster_tree):
def galerkin_1d_rank_k(block_cluster_tree, max_rank):
"""
:param block_cluster_tree:
:param block_cluster_tree: admissible block cluster tree
:type block_cluster_tree: HierMat.BlockClusterTree
:param max_rank: separation rank
:type max_rank: int
:return:
"""
x_length, y_length = block_cluster_tree.shape()
left_matrix = numpy.matrix(numpy.zeros((x_length, max_rank)))
right_matrix = numpy.matrix(numpy.zeros((y_length, max_rank)))

return HierMat.RMat()

Expand Down
30 changes: 18 additions & 12 deletions HierMat/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@


class Grid(object):
"""Discretized grid characterized by points and links
"""Discretized grid characterized by points and supports
"""
def __init__(self, points, links):
def __init__(self, points, supports):
"""Create a Grid
:param points: list of coordinates
:type points: list[numpy.array or list[float]]
:param links: list of links for every point
:type links: list[list[numpy.array or list[float]]]
:raise ValueError: if points and links have different length
:param supports: list of supports for every point
:type supports: dict{point: function}
:raise ValueError: if points and supports have different length
"""
# check input
if len(points) != len(links):
raise ValueError('points and links must be of same length')
if len(points) != len(supports):
raise ValueError('points and supports must be of same length')
# fill instance
self.points = points
self.links = links
self.supports = supports

def __len__(self):
"""Return length of points
Expand Down Expand Up @@ -52,7 +52,7 @@ def __eq__(self, other):
:rtype: bool
"""
points_eq = numpy.array_equal(self.points, other.points)
links_eq = numpy.array_equal(self.links, other.links)
links_eq = numpy.array_equal(self.supports, other.supports)
return points_eq and links_eq

def __ne__(self, other):
Expand All @@ -74,17 +74,23 @@ def get_point(self, item):
"""
return self.points[item]

def get_link(self, item):
def get_support(self, item):
"""Return link at position item
:param item: index
:type item: int
:return: links
:return: supports
"""
return self.links[item]
return self.supports[item]

def dim(self):
"""Dimension of the Grid
.. note::
this is the dimension of the first point
if you store points of different dimensions, this will be misleading
:return: dimension
:rtype: int
Expand Down
19 changes: 19 additions & 0 deletions HierMat/splitable.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def get_index(self, item):
def get_grid_item(self, item):
raise NotImplementedError()

def get_grid_item_support(self, item):
raise NotImplementedError()

def get_patch_coordinates(self):
raise NotImplementedError()

Expand Down Expand Up @@ -147,6 +150,14 @@ def get_grid_item(self, item):
"""
return self.cluster.get_grid_item(item)

def get_grid_item_support(self, item):
"""Return supports of item from grid
:param item: index
:type item: int
"""
return self.cluster.get_support(item)

def get_patch_coordinates(self):
"""Return min and max out of indices
Expand Down Expand Up @@ -284,6 +295,14 @@ def get_grid_item(self, item):
"""
return self.cluster.get_grid_item(item)

def get_grid_item_support(self, item):
"""Return supports of item from grid
:param item: index
:type item: int
"""
return self.cluster.get_grid_item_support(item)

def get_patch_coordinates(self):
"""Return min and max out of indices
Expand Down
2 changes: 1 addition & 1 deletion HierMat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def grid_plot(obj, filename=None):
length = len(obj.points)
for i in xrange(length):
plt.plot(obj.points[i][0], obj.points[i][1], 'xk')
for link in obj.links[i]:
for link in obj.supports[i]:
plt.plot([obj.points[i][0], link[0]], [obj.points[i][1], link[1]], '-b')
if not filename:
return fig
Expand Down
12 changes: 6 additions & 6 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ def test_get_point(self):
self.assertTrue(numpy.array_equal(self.grid3.get_point(-1), self.points3[-1]))

def test_get_link(self):
self.assertTrue(numpy.array_equal(self.grid1.get_link(0), self.links1[0]))
self.assertTrue(numpy.array_equal(self.grid1.get_link(-1), self.links1[-1]))
self.assertTrue(numpy.array_equal(self.grid2.get_link(0), self.links2[0]))
self.assertTrue(numpy.array_equal(self.grid2.get_link(-1), self.links2[-1]))
self.assertTrue(numpy.array_equal(self.grid3.get_link(0), self.links3[0]))
self.assertTrue(numpy.array_equal(self.grid3.get_link(-1), self.links3[-1]))
self.assertTrue(numpy.array_equal(self.grid1.get_support(0), self.links1[0]))
self.assertTrue(numpy.array_equal(self.grid1.get_support(-1), self.links1[-1]))
self.assertTrue(numpy.array_equal(self.grid2.get_support(0), self.links2[0]))
self.assertTrue(numpy.array_equal(self.grid2.get_support(-1), self.links2[-1]))
self.assertTrue(numpy.array_equal(self.grid3.get_support(0), self.links3[0]))
self.assertTrue(numpy.array_equal(self.grid3.get_support(-1), self.links3[-1]))

def test_iterator(self):
iterator = self.grid1.__iter__()
Expand Down

0 comments on commit f432510

Please sign in to comment.