Changeset 8952 for issm/trunk


Ignore:
Timestamp:
07/13/11 13:25:13 (14 years ago)
Author:
Mathieu Morlighem
Message:

added error message to loadobj in model

Location:
issm/trunk/src/m
Files:
1 added
1 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk/src/m/classes/model.m

    r8936 r8952  
    405405         methods (Static)
    406406                 function md = loadobj(md) % {{{
    407                          % This function is directly called by matlab when
    408                          % a model object is loaded. If the input is a struct
    409                          % it is an old version of model and old fields must be
    410                          % recovered (make sure they are in the deprecated
     407                         % This function is directly called by matlab when a model object is
     408                         % loaded. If the input is a struct it is an old version of model and
     409                         % old fields must be recovered (make sure they are in the deprecated
    411410                         % model properties)
     411
     412                         if verLessThan('matlab','7.9'),
     413                                 disp('Warning: your matlab version is old and there is a risk that load does not work correctly');
     414                                 disp('         if the model is not loaded correctly, rename temporarily loadobj so that matlab does not use it');
     415
     416                                 % This is a Matlab bug: all the fields of md have their default value
     417                                 % md.name = {''    [1]    'String'}
     418                                 % instead of recovering the saved fields
     419                                 %
     420                                 % Example of error message:
     421                                 % Warning: Error loading an object of class 'model':
     422                                 % Undefined function or method 'exist' for input arguments of type 'cell'
     423                                 %
     424                                 % This has been fixed in MATLAB 7.9 (R2009b) and later versions
     425                         end
    412426
    413427                         if isstruct(md)
    414428                                 disp('Recovering model object from a previous version');
    415 
    416                                  %Ok the loaded model is a struct, initialize output and recover all fields
    417                                  structmd=md;
    418                                  md=model;
    419 
    420                                  structfields=fields(structmd);
    421                                  modelprops  =properties('model');
    422 
    423                                  for i=1:length(structfields),
    424                                          fieldname =structfields{i};
    425                                          fieldvalue=getfield(structmd,fieldname);
    426                                          if ismember(fieldname,modelprops),
    427                                                  md=setfield(md,fieldname,fieldvalue);
    428                                          end
    429                                  end
    430 
    431                                  %Field name change
    432                                  if isfield(structmd,'drag'), md.drag_coefficient=structmd.drag; end
    433                                  if isfield(structmd,'p'), md.drag_p=structmd.p; end
    434                                  if isfield(structmd,'q'), md.drag_q=structmd.p; end
    435                                  if isfield(structmd,'B'), md.rheology_B=structmd.B; end
    436                                  if isfield(structmd,'n'), md.rheology_n=structmd.n; end
    437                                  if isfield(structmd,'melting'), md.basal_melting_rate=structmd.melting; end
    438                                  if isfield(structmd,'melting_rate'), md.basal_melting_rate=structmd.melting_rate; end
    439                                  if isfield(structmd,'accumulation'), md.surface_mass_balance=structmd.accumulation; end
    440                                  if isfield(structmd,'accumulation_rate'), md.surface_mass_balance=structmd.accumulation_rate; end
    441                                  if isfield(structmd,'numberofgrids'), md.numberofnodes=structmd.numberofgrids; end
    442                                  if isfield(structmd,'numberofgrids2d'), md.numberofnodes2d=structmd.numberofgrids2d; end
    443                                  if isfield(structmd,'gridonhutter'), md.nodeonhutter=structmd.gridonhutter; end
    444                                  if isfield(structmd,'gridonmacayeal'), md.nodeonmacayeal=structmd.gridonmacayeal; end
    445                                  if isfield(structmd,'gridonpattyn'), md.nodeonpattyn=structmd.gridonpattyn; end
    446                                  if isfield(structmd,'gridonstokes'), md.nodeonstokes=structmd.gridonstokes; end
    447                                  if isfield(structmd,'uppergrids'), md.uppernodes=structmd.uppergrids; end
    448                                  if isfield(structmd,'lowergrids'), md.lowernodes=structmd.lowergrids; end
    449                                  if isfield(structmd,'gridonbed'), md.nodeonbed=structmd.gridonbed; end
    450                                  if isfield(structmd,'gridonsurface'), md.nodeonsurface=structmd.gridonsurface; end
    451                                  if isfield(structmd,'extractedgrids'), md.extractednodes=structmd.extractedgrids; end
    452                                  if isfield(structmd,'gridoniceshelf'), md.nodeoniceshelf=structmd.gridoniceshelf; end
    453                                  if isfield(structmd,'gridonicesheet'), md.nodeonicesheet=structmd.gridonicesheet; end
    454                                  if isfield(structmd,'gridonwater'), md.nodeonwater=structmd.gridonwater; end
    455                                  if isfield(structmd,'gridonnuna'), md.nodeonnuna=structmd.gridonnuna; end
    456                                  if isfield(structmd,'gridonboundary'), md.nodeonboundary=structmd.gridonboundary; end
    457                                  if isfield(structmd,'spcvelocity'),
    458                                          md.spcvx=NaN*ones(md.numberofnodes,1);
    459                                          md.spcvy=NaN*ones(md.numberofnodes,1);
    460                                          md.spcvz=NaN*ones(md.numberofnodes,1);
    461                                          pos=find(structmd.spcvelocity(:,1)); md.spcvx(pos)=structmd.spcvelocity(pos,4);
    462                                          pos=find(structmd.spcvelocity(:,2)); md.spcvy(pos)=structmd.spcvelocity(pos,5);
    463                                          pos=find(structmd.spcvelocity(:,3)); md.spcvz(pos)=structmd.spcvelocity(pos,6);
    464                                  end
    465 
    466                                  %Field class change
    467                                  if (isfield(structmd,'type') & ischar(structmd.type)),
    468                                          if strcmpi(structmd.type,'2d'), md.dim=2; end
    469                                          if strcmpi(structmd.type,'3d'), md.dim=3; end
    470                                  end
    471                                  if isnumeric(md.verbose), md.verbose=verbose; end
    472 
    473                                  %New fields
    474                                  if ~isfield(structmd,'upperelements');
    475                                          md.upperelements=transpose(1:md.numberofelements)+md.numberofelements2d;
    476                                          md.upperelements(end-md.numberofelements2d+1:end)=NaN;
    477                                  end
    478                                  if ~isfield(structmd,'lowerelements');
    479                                          md.lowerelements=transpose(1:md.numberofelements)-md.numberofelements2d;
    480                                          md.lowerelements(1:md.numberofelements2d)=NaN;
    481                                  end
     429                                 md = structtomodel(model,md);
    482430                         end
    483431                 end% }}}
     
    614562                 end
    615563                 %}}}
     564                 function md = structtomodel(md,structmd) % {{{
     565
     566                         if ~isstruct(structmd) error('input model is not a structure'); end
     567
     568                         %loaded model is a struct, initialize output and recover all fields
     569                         md = structtoobj(model,structmd);
     570
     571                         %Field name change
     572                         if isfield(structmd,'drag'), md.drag_coefficient=structmd.drag; end
     573                         if isfield(structmd,'p'), md.drag_p=structmd.p; end
     574                         if isfield(structmd,'q'), md.drag_q=structmd.p; end
     575                         if isfield(structmd,'B'), md.rheology_B=structmd.B; end
     576                         if isfield(structmd,'n'), md.rheology_n=structmd.n; end
     577                         if isfield(structmd,'melting'), md.basal_melting_rate=structmd.melting; end
     578                         if isfield(structmd,'melting_rate'), md.basal_melting_rate=structmd.melting_rate; end
     579                         if isfield(structmd,'accumulation'), md.surface_mass_balance=structmd.accumulation; end
     580                         if isfield(structmd,'accumulation_rate'), md.surface_mass_balance=structmd.accumulation_rate; end
     581                         if isfield(structmd,'numberofgrids'), md.numberofnodes=structmd.numberofgrids; end
     582                         if isfield(structmd,'numberofgrids2d'), md.numberofnodes2d=structmd.numberofgrids2d; end
     583                         if isfield(structmd,'gridonhutter'), md.nodeonhutter=structmd.gridonhutter; end
     584                         if isfield(structmd,'gridonmacayeal'), md.nodeonmacayeal=structmd.gridonmacayeal; end
     585                         if isfield(structmd,'gridonpattyn'), md.nodeonpattyn=structmd.gridonpattyn; end
     586                         if isfield(structmd,'gridonstokes'), md.nodeonstokes=structmd.gridonstokes; end
     587                         if isfield(structmd,'uppergrids'), md.uppernodes=structmd.uppergrids; end
     588                         if isfield(structmd,'lowergrids'), md.lowernodes=structmd.lowergrids; end
     589                         if isfield(structmd,'gridonbed'), md.nodeonbed=structmd.gridonbed; end
     590                         if isfield(structmd,'gridonsurface'), md.nodeonsurface=structmd.gridonsurface; end
     591                         if isfield(structmd,'extractedgrids'), md.extractednodes=structmd.extractedgrids; end
     592                         if isfield(structmd,'gridoniceshelf'), md.nodeoniceshelf=structmd.gridoniceshelf; end
     593                         if isfield(structmd,'gridonicesheet'), md.nodeonicesheet=structmd.gridonicesheet; end
     594                         if isfield(structmd,'gridonwater'), md.nodeonwater=structmd.gridonwater; end
     595                         if isfield(structmd,'gridonnuna'), md.nodeonnuna=structmd.gridonnuna; end
     596                         if isfield(structmd,'gridonboundary'), md.nodeonboundary=structmd.gridonboundary; end
     597                         if isfield(structmd,'TEST'), md.dummy=structmd.TEST; end
     598
     599                         %Field class change
     600                         if (size(structmd.pressureload,2)==3 | size(structmd.pressureload,2)==5),
     601                                 md.pressureload=[structmd.pressureload...
     602                                         WaterEnum*structmd.elementoniceshelf(structmd.pressureload(:,end))+AirEnum*structmd.elementonicesheet(structmd.pressureload(:,end))];
     603                         end
     604                         if (isfield(structmd,'type') & ischar(structmd.type)),
     605                                 if strcmpi(structmd.type,'2d'), md.dim=2; end
     606                                 if strcmpi(structmd.type,'3d'), md.dim=3; end
     607                         end
     608                         if isnumeric(md.verbose),
     609                                 md.verbose=verbose;
     610                         end
     611                         if isfield(structmd,'spcvelocity'),
     612                                 md.spcvx=NaN*ones(md.numberofnodes,1);
     613                                 md.spcvy=NaN*ones(md.numberofnodes,1);
     614                                 md.spcvz=NaN*ones(md.numberofnodes,1);
     615                                 pos=find(structmd.spcvelocity(:,1)); md.spcvx(pos)=structmd.spcvelocity(pos,4);
     616                                 pos=find(structmd.spcvelocity(:,2)); md.spcvy(pos)=structmd.spcvelocity(pos,5);
     617                                 pos=find(structmd.spcvelocity(:,3)); md.spcvz(pos)=structmd.spcvelocity(pos,6);
     618                         end
     619                         if isfield(structmd,'spcvelocity'),
     620                                 md.spcvx=NaN*ones(md.numberofnodes,1);
     621                                 md.spcvy=NaN*ones(md.numberofnodes,1);
     622                                 md.spcvz=NaN*ones(md.numberofnodes,1);
     623                                 pos=find(structmd.spcvelocity(:,1)); md.spcvx(pos)=structmd.spcvelocity(pos,4);
     624                                 pos=find(structmd.spcvelocity(:,2)); md.spcvy(pos)=structmd.spcvelocity(pos,5);
     625                                 pos=find(structmd.spcvelocity(:,3)); md.spcvz(pos)=structmd.spcvelocity(pos,6);
     626                         end
     627
     628                         %New fields
     629                         if ~isfield(structmd,'upperelements');
     630                                 md.upperelements=transpose(1:md.numberofelements)+md.numberofelements2d;
     631                                 md.upperelements(end-md.numberofelements2d+1:end)=NaN;
     632                         end
     633                         if ~isfield(structmd,'lowerelements');
     634                                 md.lowerelements=transpose(1:md.numberofelements)-md.numberofelements2d;
     635                                 md.lowerelements(1:md.numberofelements2d)=NaN;
     636                         end
     637                         if ~isfield(structmd,'diagnostic_ref');
     638                                 md.diagnostic_ref=NaN*ones(md.numberofnodes2d,6);
     639                         end
     640                 end% }}}
    616641                 function mdstruct=struct(md) % {{{1
     642                         %Cast operator needed because of Dependent Hidden properties
    617643                         mdstruct=struct();
    618644                         modelprops=properties('model');
  • issm/trunk/src/m/utils/Model/loadmodel.m

    r8726 r8952  
    2121        warning off MATLAB:unknownElementsNowStruc;
    2222        struc=load(path,'-mat');
    23 
    2423        warning on MATLAB:unknownElementsNowStruc;
    2524
    26         %Check that there is only one variable
    2725        name=char(fieldnames(struc));
    2826        if size(name,1)>1,
    2927                error(['loadmodel error message: file ' path ' contains several variables. Only one model should be present.']);
    3028        end
    31         md=eval(['struc.' name]);
     29        md=struc.(name);
    3230       
    3331        %now, transform md into a new model
    3432        if ~strcmpi(class(md),'model') & ~strcmpi(class(md),'planet'),
    35                 md2=structtomodel(model,md);
    36                 md=md2;
    37                 clear md2;
     33                md=structtomodel(model,md);
    3834        end
    3935        if nargout,
Note: See TracChangeset for help on using the changeset viewer.