Changeset 18515


Ignore:
Timestamp:
09/12/14 15:39:55 (11 years ago)
Author:
bdef
Message:

BUG:debugging python in VTK export

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/contrib/paraview/exportVTK.py

    r18513 r18515  
    11import numpy
    2 
    32def exportVTK(filename,model,options):
    43    '''
     
    2726
    2827#get the element related variables
    29 points=[model.mesh.x model.mesh.y model.mesh.z]
     28if model.mesh.shape[0]==2:
     29    points=[model.mesh.x model.mesh.y zeros(model.mesh.numberofvertices,1)];
     30else:
     31    points=[model.mesh.x model.mesh.y model.mesh.z]
     32
    3033[num_of_points,dim]=numpy.size(points)
    3134[num_of_elt]=numpy.size(model.mesh.elements,1)
     
    4245#this is the result structure
    4346res_struct=model.results
    44 #Getting all the solutions of the model
    45 solnames=fields(res_struct)
    46 num_of_sols=numpy.length(solnames)
    47 num_of_timesteps=1
    48 #%building solutionstructure
    49 for solution in num_of_sols:
    50     sol_struct{i}=res_struct.(solnames{i});
    51     #looking for multiple time steps
    52     if(numpy.size(sol_struct{i},2)>num_of_timesteps):
    53         num_of_timesteps=numpy.size(sol_struct{i},2);
     47if (len(fields(res_struct))>0):
     48    #Getting all the solutions of the model
     49    solnames=fields(res_struct)
     50    num_of_sols=numpy.length(solnames)
     51    num_of_timesteps=1
     52    #%building solutionstructure
     53    for solution in num_of_sols:
     54        sol_struct{i}=res_struct.(solnames{i});
     55        #looking for multiple time steps
     56        if(numpy.size(sol_struct{i},2)>num_of_timesteps):
     57            num_of_timesteps=numpy.size(sol_struct{i},2);
     58
     59else:
     60    num_of_timesteps=1
    5461
    5562for step in num_of_timesteps:
     
    7380       
    7481    fid.write('CELLS %d %d\n' % num_of_elt % num_of_elt*(point_per_elt+1))
    75         s='%d'
    76         for j=1:point_per_elt:
    77             s=horzcat(s,{' %d'})
     82    s='%d'
     83    for j=1:point_per_elt:
     84        s=horzcat(s,{' %d'})
    7885       
    79         s=cell2mat(horzcat(s,{'\n'}))
    80         fid.write(fid,s,[(point_per_elt)*ones(num_of_elt,1) model.mesh.elements-1]transpose)
    81        
    82         fid.write(fid,'CELL_TYPES %d\n',num_of_elt)
    83         s='%d\n'
    84         fid.write(fid,s,celltype*ones(num_of_elt,1))
    85         fid.write(fid,'POINT_DATA %s \n',num2str(num_of_points))
    86 
    87         #loop over the different solution structures
    88         for j=1:num_of_sols:
     86    s=cell2mat(horzcat(s,{'\n'}))
     87    fid.write(fid,s,[(point_per_elt)*ones(num_of_elt,1) model.mesh.elements-1]transpose)
     88   
     89    fid.write(fid,'CELL_TYPES %d\n',num_of_elt)
     90    s='%d\n'
     91    fid.write(fid,s,celltype*ones(num_of_elt,1))
     92    fid.write(fid,'POINT_DATA %s \n',num2str(num_of_points))
     93   
     94    #loop over the different solution structures
     95    if 'num_of_sols' in locals():
     96        for j=1:num_of_sols:
    8997            #dealing with results on different timesteps
    9098            if(numpy.size(sol_struct{j},2)>timestep):
     
    92100            else:
    93101                timestep = numpy.size(sol_struct{j},2)
    94          
     102               
    95103            #getting the number of fields in the solution
    96104            fieldnames=fields(sol_struct{j}(timestep))
     
    111119                    fid.write(s % sol_struct{j}(timestep).(fieldnames{k}))
    112120                   
    113                     #loop on arguments, if something other than result is asked, do
    114                     #it now
    115                 for j= 1:nargin-2:
    116                     res_struct=model.(varargin{j})
    117                     fieldnames=fields(res_struct)
    118                     num_of_fields=numpy.length(fieldnames)
    119                     for k=1:num_of_fields:
    120                         if ((numel(res_struct.(fieldnames{k})))==num_of_points)
    121                                 #paraview does not like NaN, replacing
    122                                 nanval=find(isnan(res_struct.(fieldnames{k})))
    123                                 res_struct.(fieldnames{k})(nanval)=-9999
    124                                 #also checking for verry small value that mess up
    125                                 smallval=(abs(res_struct.(fieldnames{k}))<1.0e-20)
    126                                 res_struct.(fieldnames{k})(smallval)=0.0
    127                                 fid.write(fid,'SCALARS %s float 1 \n',fieldnames{k})
    128                                 fid.write(fid,'LOOKUP_TABLE default\n')
    129                                 s='%e\n'
    130                                 fid.write(fid,s,res_struct.(fieldnames{k}))
    131         fid.close();
     121    #loop on arguments, if something other than result is asked, do
     122    #it now
     123    for j= 1:nargin-2:
     124        res_struct=model.(varargin{j})
     125        fieldnames=fields(res_struct)
     126        num_of_fields=numpy.length(fieldnames)
     127        for k=1:num_of_fields:
     128            if ((numel(res_struct.(fieldnames{k})))==num_of_points):
     129                #paraview does not like NaN, replacing
     130                nanval=find(isnan(res_struct.(fieldnames{k})))
     131                res_struct.(fieldnames{k})(nanval)=-9999
     132                #also checking for verry small value that mess up
     133                smallval=(abs(res_struct.(fieldnames{k}))<1.0e-20)
     134                res_struct.(fieldnames{k})(smallval)=0.0
     135                fid.write(fid,'SCALARS %s float 1 \n',fieldnames{k})
     136                fid.write(fid,'LOOKUP_TABLE default\n')
     137                s='%e\n'
     138                fid.write(fid,s,res_struct.(fieldnames{k}))
     139    fid.close();
Note: See TracChangeset for help on using the changeset viewer.