Code covered by the BSD License  

Highlights from
NanoVis: molecular visualizer

image thumbnail
from NanoVis: molecular visualizer by Giuseppe Romano
NanoVis: Molecular visualizer

NanoVis(filename,flag_bond)
function NanoVis(filename,flag_bond)
%NanoVis(filename,flag_bond) (beta version) displays an atomistic structure whose atom coordinates are
%reported in file called "filename". 
%The formats supported are "gen" and "xyz".
%flag_bond is related to display mode and can be:
%1 ->normal mode (default)
%0 ->spacefill mode (more fast)
%Giuseppe Romano, Rome, Please report bugs to giuseppe.romano@uniroma2.it 


%Default control
if nargin==2
    fbond=min(flag_bond,1);
else
    fbond=1;
end

%Graphics quality
n=10; %10 is raccomanded"

%Bonds color
colorbond=[1 1 0]; 


%%%Read the structure
fid = fopen(filename,'r');
if fid==-1
   disp(strcat('File "',filename,'" does not exist'));
   return
end



%Default properties
set(gcf,'DefaultSurfaceLineStyle','none')
set(gcf,'DefaultSurfaceFaceColor',colorbond)

if strcmpi(filename(end-2:end),'gen')
 
  %Read a "gen" file  
  natom = fscanf(fid,'%i');
  
  %%read the atom species%%%%%%%%%%%%%%%%%%%%%%
  a=fscanf(fid,'%s\n',1);
  kk=0;
  a=[];
  while (numel(a) == 0)
      
    kk=kk+1;
    sp(kk) = {fscanf(fid,'%s',1)};
    a=str2num(char(sp(kk)));
    
  end

  species=sp(1:end-1)';
  ltype=numel(species);
  

  %fill the properties matix reading from "DataBase.dat"   
  properties=zeros(ltype,6);    %properties matrix
  
  for k1=1:ltype
    properties(k1,1:6)=(pick_properties(species(k1)));
  end
  
  %Read atom coordinates
  pos=zeros(3,1);
  
  for k1=1:natom
    
    if ~(k1==1)
      fscanf(fid,'%i',1);
    end
    
    %Read coordinates
    data(k1,1)=fscanf(fid,'%i',1); 
    data(k1,2)=fscanf(fid,'%f',1);
    data(k1,3)=fscanf(fid,'%f',1);
    data(k1,4)=fscanf(fid,'%f\n',1); 
    
    %read valence
    valence(k1)=round(properties(data(k1,1),3));
   
  end

else
    
  %Read a "xyz" file
    
  natom = fscanf(fid,'%i\n');
 
  fscanf(fid,'%i\n');
  data=zeros(natom,4);
  properties=[];
  species=[];
 
  for k1=1:natom
     
    sp = fscanf(fid,'%c',2);
    I=strmatch(sp,species,'exact');
     
    if (numel(I)==0)
        newproperties=pick_properties(sp);         
        properties=[properties;newproperties];
        species=[species;sp];       
        data(k1,1)=size(properties,1); 
    else
        data(k1,1)=I;      
    end
      
    %Read coordinates
    data(k1,2)=fscanf(fid,'%f',1);
    data(k1,3)=fscanf(fid,'%f',1);
    data(k1,4)=fscanf(fid,'%f\n',1); 
    
    %read valence
    valence(k1)=round(properties(data(k1,1),3));
     
  end 
  
end

fclose(fid);    


%The matrix properties size is number_of_species X 6
%where the columns meaning is
%1 Ray
%2 Bonding lenght
%3 valence
%4 r-color
%5 g-color
%6 b-color


%%%draw the structure%%%%%%
if fbond==1
    a=1;
else
    a=2;
end


if fbond==1
    
   %%%Draw atoms%%%%%
   [Sx Sy Sz]=sphere(n); 
   for k1=1:natom
     
       color=properties(data(k1,1),4:6);
       Ray=a*properties(data(k1,1),1);
       surface(Ray*Sx + data(k1,2),Ray*Sy + data(k1,3),Ray*Sz + data(k1,4),'FaceColor',color)
  
   end
   
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%


    
%draw bond as cylinders
[xcyl,ycyl,zcyl]=cylinder(1,n); %z-oriented
dirc=[1e-10 1e-10 10000']; %Create a vector with a z-direction

nv=0;
bn=zeros(1,natom);

for k1=1:natom-1
    
    %valence check
    if bn(k1)<valence(k1)
        
        for k2=k1+1:natom
            
            %valence check
            if bn(k2)<valence(k2)
                
              %Calculate the k1-k2 atoms distance 
              r=((data(k1,2)-data(k2,2))^2+(data(k1,3)-data(k2,3))^2+(data(k1,4)-data(k2,4))^2)^(1/2);
              
              %The atoms k1-k2 are bonded if the sum of their bonding
              %lenght are less that r
              if (r <= properties(data(k1,1),2)+properties(data(k2,1),2))
                 
                 %The valences maust be increased 
                 bn(k1)=bn(k1)+1;
                 bn(k2)=bn(k2)+1;
                   
                 %Set the bond ray
                 Ray=min(properties(data(k1,1),1),properties(data(k2,1),1))/3;
                 
                 if fbond==0
                     
                    line([data(k1,2),data(k2,2)],[data(k1,3),data(k2,3)],[data(k1,4),data(k2,4)],'Color',colorbond,'LineWidth',2*Ray);
                    
                 else
                    %XData 
                    a=xcyl*Ray;
                    %YData
                    b=ycyl*Ray;
                    %ZData
                    c=zcyl;
                    %first atom position
                    pc(1,1:3)=data(k1,2:4);
                    %second atom position
                    pc(2,1:3)=data(k2,2:4);
                    %Bond creation
                    [m,I]=sort(pc(:,3));
                    a1=I(1);
                    b1=I(2);
                    h=surface(a+pc(a1,1),b+pc(a1,2),r*c+pc(a1,3),'FaceColor',colorbond);
                    a=pc(b1,:)-pc(a1,:);
                    b=dirc;
                    cr(1) = b(3)*a(2) - b(2)*a(3);
                    cr(2) = b(1)*a(3) - b(3)*a(1);
                    cr(3) = b(2)*a(1) - b(1)*a(2);
                    dir=pc(a1,:)+cr;
                    alfa=(180/pi)*(atan2(pc(b1,3)-pc(a1,3),((pc(b1,1)-pc(a1,1))^2+(pc(b1,2)-pc(a1,2))^2)^(1/2))-pi/2);                               
                    rotate(h,dir,alfa,pc(a1,:)+1e-6)

                  end
                            
                end
            end  
         end
      end  
end 



 
%Axis and figure properties
lighting gouraud
camlight
rotate3D on
set(gca,'DataAspectRatio',[ 1 1 1 ])
set(gca,'PlotBoxAspectRatio',[ 1 1 1 ])
set(gca,'CameraViewAngleMode','manual')
set(gcf,'Color',[0.3 0.6 0.7])
set(gcf,'Name','Atomistic Visualizer: NanoVis')
camproj('perspective')
set(gca,'XTick',[])
set(gca,'YTick',[])
set(gca,'ZTick',[])
axis off
hold off



function[data]=pick_properties(Str)
%This function gives the data properties of a given species (Str) by
%reading from file "DataBase.dat"
fid2=fopen('DataBase.dat');
fgets(fid2);
a=1;
while (a==1)
      fscanf(fid2,'%i',1);
      Sp=fscanf(fid2,'%s',1);
      if strcmpi(strtok(Sp),strtok(Str))  
         for k1=1:6 
             data(k1)=fscanf(fid2,'%f',1);
         end
         a=0; 
      end
      fscanf(fid2,'%*[^\n]',1);
end
fclose(fid2);


Contact us at files@mathworks.com