Anonymous avatar Anonymous committed ad9505e

graph.generator.*

Comments (0)

Files changed (2)

src/graph/generator.py

         return overridden
 
 
+
+
+class NbhrFinderFactory:
+    """
+    A factory of neighbor finder services.
+    %   This class produces template grid graph neighbor finder instances
+    %   based on input options.
+    """
     
+    @staticmethod
+    def newInstance(options):
+        """
+                    % Returns a new neibhbor findiner instance based on the input
+            % options parsed from VARARGIN.
+        """
+        if (options.bc is "neumann"):
+            # % Neumann B.C.
+            instance = NbhrFinderNeumann(options)
+        elif (options.bc is "periodic"):
+            # % Periodic B.C.
+            instance = NbhrFinderPeriodic(options)
+        else:
+            raise Exception("Unknown grid boundary condition: %s" % options.bc)
+        
+        
+class NbhrFinder(amg.api.HasOptions):
+    """
+    Grid graph neighbor index finder interface.
+    %   Finds node indices of grid graph neighbors.
+    %
+    %   See also: NbhrFinder.
+    """
+    
+    def __init__(self, options):
+        amg.api.HasOptions.__init__(self, options)
+    
+        
+    def findInteriorIndices(self, i):
+        """
+        % Return the indices of the subscript arrays i corresponding to
+        % interior gridpoints in a grid of size n.
+        """
+        raise NotImplementedError("abstract method")
+    
+    def sub2ind(self, i):
+        """
+        % Convert subscripts to indices in this boundary condition
+        % context.
+        """
+        raise NotImplementedError("abstract method")
+        
+        
+        
+class NbhrFinderNeumann(NbhrFinder):
+    """
+    Grid graph neighbor index finder - periodic B.C.
+    %   Finds node indices of grid graph neighbors. Assumes periodic B.C. at all
+    %   grid boundaries.
+    %
+    %   See also: NbhrFinder
+    """
+    
+    def __init__(self, options):
+        NbhrFinder.__init__(self, options)
+    
+    
+    def findInteriorIndices(self, i):
+        """
+            % Return the indices of the subscript arrays i corresponding to
+            % interior gridpoints in a grid of size n.
+        """
+        raise NotImplementedError("TODO:")
+    
+    
+    def sub2ind(self, i):
+        """
+           % Convert subscripts to indices in this boundary condition
+            % context.
+        """
+        raise NotImplementedError("TODO:")
+    
+    
+    
+class NbhrFinderPeriodic(NbhrFinder):
+    """
+        %NbhrFinderNeumann Grid graph neighbor index finder - Neumann B.C.
+    %   Finds node indices of grid graph neighbors. Assumes Neumann B.C. at
+    %   all grid boundaries.
+    %
+    %   See also: NbhrFinder.
+    """
+    
+    def __init__(self, options):
+        NbhrFinder.__init__(self, options)
+    
+    
+    def findInteriorIndices(self, i):
+        """
+            % Return the indices of the subscript arrays i corresponding to
+            % interior gridpoints in a grid of size n.
+        """
+        raise NotImplementedError("TODO:")
+    
+    
+    def sub2ind(self, i):
+        """
+           % Convert subscripts to indices in this boundary condition
+            % context.
+        """
+        raise NotImplementedError("TODO:")
+        
+        
     
 class AbstractGeneratorGrid(amg.api.Builder):
     """
 #        nbhrFinder  % Sets boundary conditions
 #        NBHR_FINDER_FACTORY = graph.generator.NbhrFinderFactory
 
+    NBHR_FINDER_FACTORY = NbhrFinderFactory
+
     
     def __init__(self, options, type):
         """
         
         # % Initializations
         if (lab.size(self.h, 0) > 1):
-            options.h = lab.ctranspose(options.h)
-            self.h = lab.ctranspose(options.h)
+            if (options.h is not None):
+                options.h = lab.ctranspose(options.h)
+            self.h = lab.ctranspose(self.h)
             
         if (lab.size(self.n, 0) > 1):
             options.n = lab.ctranspose(options.n)
         raise NotImplementedError("abstract method")
     
     
+    def build(self):
+        """
+        %Build the grid graph instance G.
+        """
+        (i, j, coord) = self.buildCoord()
+        self.edges = self.buildEdges(i, j)
+        metadata = self.buildMetadata(i)
+        G = graph.api.Graph.newInstanceFromMetadata(metadata, "edge-list", self.edges, coord)
+        #% Back-reference the graph in its metadata so that it can be
+        #% loaded by a BatchReader
+        metadata.attributes.G = G
+        return G
+        
+    
+    
     def buildCoord(self):
         """
                     % Compute subscripts (i(1),...,i(dim)), 1-D indices and
         """
         % Build the Laplacian FD stencil.
         """
-        raise NotImplementedError("TODO:")
-        
+        d = self.dim
+        s = lab.rows(lab.columns(lab.eye(d), lab.rdivide(1, lab.ctranspose(lab.power(self.h, 2)))), \
+                     lab.columns(-lab.eye(d), lab.rdivide(1, lab.ctranspose(lab.power(self.h, 2)))))
+        if (self.normalized):
+            s[:, d] = 1 # CHECK: MATLAB: s[:, d + 1]
+        return s
+        
     # INFO: since python is 0-based, size(x,0) should return the number of rows
     if (type(A) is numpy.ndarray):
         if dim is None:
-            return A.shape
+            size = A.shape
+            if len(size) == 1:
+                # case: [1 2 3].shape == (3,)
+                size = (size[0], 1)
+            return size
         else:
             return A.shape[dim]
     elif (type(A) is PETSc.Mat):
 
 
 
+def eye(m, n=None, classname=None):
+    """
+        eye(..., CLASSNAME) is a matrix with ones of class specified by
+    CLASSNAME on the diagonal and zeros elsewhere.
+ 
+    Note: The size inputs M and N should be nonnegative integers. 
+    Negative integers are treated as 0.
+    """
+    if classname:
+        raise NotImplementedError()
+    return numpy.eye(m, n)
+
+
 
 
 
 
 def columns(*args):
     """
-    columns(x, y, z) constructs the matrix [x y z], where x, y, z
+    columns(x, y, z) constructs the array [x y z], where x, y, z
     are column vectors.
+    
+    MATLAB: [x y z]
     """
     return numpy.column_stack(args)
 
 
+def rows(*args):
+    """
+    rows(x, y, z) constructs an array where x, y, z are row vectors.
+    
+    MATLAB: [x; y; z]
+    """
+    return numpy.vstack(args)
+
+
 
 def flatten(x):
     """
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.