function getEigs(fileBaseName)

%% Copyright 2008 by James Rath and the University of Texas at Austin
%% This code is available under a Creative Commons Attribution-ShareAlike license.  You
%% are free to copy and share this as you see fit; however, you must attribute any copy
%% to me.  You are also free to create derivative works; however, you must acknowledge
%% the use of this original, and you must share your work under the same terms you
%% received this copy.  For details, see http://creativecommons.org/licenses/by-sa/3.0/
%% For permission for any other use, please contact me at ratjamm@alum.mit.edu


% read in the data for the matrix
[MatIs,MatJs,MatEntries] = textread(strcat(fileBaseName,'.mm'),'%d %d %f');
% form the matrix, adding together duplicate entries
% (NB: that's they way it was intended)
theMat = sparse(MatIs,MatJs,MatEntries);
% free up memory (for big problems, it makes a big difference)
clear MatIs MatJs MatEntries;
% 'finished forming theMat.'


% read in data about the nodes; we'll need (x,y) info later to write things back
% out; we need the boundary markers to exclude zero/empty rows and columns from
% the matrix
%% first line is number of nodes, 2D indicator, number of attributes, boolean bdy
%% indicator (these three should be 2, 0, and 1 respectively)
%% FIXME: check that the number of rows of bdyInfo == numNodes from header
[nodeNums,nodeXs,nodeYs,bdyInfo] = ...
  textread(strcat(fileBaseName,'.node'),'%d %f %f %d','commentstyle','shell','headerlines',1);
if (~issorted(nodeNums))
  % just in case the nodes weren't given to us in order
  sortedInfo = sortrows([nodeNums,nodeXs,nodeYs,bdyInfo]);
  nodeXs = sortedInfo(:,2);
  nodeYs = sortedInfo(:,3);
  bdyInfo = sortedInfo(:,4);
  clear sortedInfo;
end
clear nodeNums;
numNodes = length(bdyInfo);


% trim the matrix so that we avoid the zero rows/columns associated with bdy nodes
% NB: ~bdy is where bdy==0; that is, we're not on the bdy -- we're an interior node
theMat = theMat(~bdyInfo,~bdyInfo);
% 'finished trimming theMat.'


% get the six smallest eigenvalues and their associated eigenvectors
if 1
  % use your supplied code
  [evecs,evals] = myEigProc(theMat);
else
  % use the built-in IRAP eigenroutine
  myOpts.issym = 1;
  myOpts.v0 = ones(numNodes-sum(bdyInfo),1);
  myOpts.disp = 0;
  [evecs,evals] = eigs(theMat,6,'sm',myOpts);
  evals = diag(evals);
end


% report the eigenvalues to the screen
sprintf('smallest six %s evals',fileBaseName)
evals


% silly matlab won't let you form items on the "save" command line nor print headers
for whichEval=1:6
  foo = zeros(numNodes,1);
  % fill the non-boundary entries of foo with the evec data
  foo(~bdyInfo) = evecs(:,whichEval);
  % write the eigenvector
  fooFile = fopen( sprintf('%s.%d.node',fileBaseName,whichEval), 'w' );
  %% header line is number of nodes, 2D indicator, number of attributes, boolean
  %% bdy indicator (these three should be 2, 1, and 1 respectively)
  fprintf(fooFile,'%d 2 1 1\n',numNodes);
  % print the data: node#, x,y, datum, boundaryMarker
  for i=1:numNodes
    fprintf(fooFile,'%d %.5g %.5g %.5g %d\n', i, nodeXs(i),nodeYs(i), foo(i), bdyInfo(i));
  end
  fclose(fooFile);
end
