Changeset 22521


Ignore:
Timestamp:
03/11/18 09:46:49 (7 years ago)
Author:
Mathieu Morlighem
Message:

CHG: simplifying implementation of inputs, should call GetInput rather than reimplementing the same thing over and over again

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/classes/Inputs/Inputs.cpp

    r22519 r22521  
    3131int  Inputs::AddInput(Input* in_input){/*{{{*/
    3232
    33         /*First, go through dataset of inputs and check whether any input
    34          * with the same name is already in. If so, erase the corresponding
    35          * object before adding this new one: */
    36         vector<Object*>::iterator object;
    37         Input* input=NULL;
    38 
    39         /*In debugging mode, check that the input is not a NULL pointer*/
    40         _assert_(in_input);
    41 
    42         for ( object=objects.begin() ; object < objects.end(); object++ ){
    43 
    44                 input=xDynamicCast<Input*>(*object);
    45 
    46                 if (input->InstanceEnum()==in_input->InstanceEnum()){
    47                         this->DeleteObject(input);
    48                         break;
    49                 }
    50         }
     33        _assert_(in_input);
     34
     35        /*Delete input if it already exists*/
     36        this->DeleteInput(in_input->InstanceEnum());
     37
     38        /*Now add new input to the dataset*/
    5139        this->AddObject(in_input);
    5240
     
    5644void  Inputs::ChangeEnum(int oldenumtype,int newenumtype){/*{{{*/
    5745
    58         /*Go through dataset of inputs and look for input with
    59          * same enum as input enum, once found, just change its name */
    60         vector<Object*>::iterator object;
    61         Input* input=NULL;
    62 
    63         /*Delete existing input of newenumtype if it exists*/
    64         for ( object=objects.begin() ; object < objects.end(); object++ ){
    65                 input=xDynamicCast<Input*>(*object);
    66 
    67                 if (input->InstanceEnum()==newenumtype){
    68                         this->DeleteObject(input);
    69                         break;
    70                 }
    71         }
    72 
    73         /*Change enum_type of input of oldenumtype*/
    74         for ( object=objects.begin() ; object < objects.end(); object++ ){
    75 
    76                 input=xDynamicCast<Input*>(*object);
    77 
    78                 if (input->InstanceEnum()==oldenumtype){
    79                         input->ChangeEnum(newenumtype);
    80                         break;
    81                 }
    82         }
    83 }
    84 /*}}}*/
     46        /*Delete input if it already exists*/
     47        this->DeleteInput(newenumtype);
     48
     49        /*Now get old input and change its enum (do not error out if not found)*/
     50        Input* input=this->GetInput(oldenumtype);
     51        if(input) input->ChangeEnum(newenumtype);
     52}/*}}}*/
    8553void Inputs::Configure(Parameters* parameters){/*{{{*/
    8654
     
    9967int  Inputs::DeleteInput(int enum_type){/*{{{*/
    10068
    101         vector<Object*>::iterator object;
    102         Input* input=NULL;
    103 
    104         for ( object=objects.begin() ; object < objects.end(); object++ ){
    105 
    106                 input=xDynamicCast<Input*>(*object);
    107 
    108                 if (input->InstanceEnum()==enum_type){
    109                         this->DeleteObject(input);
    110                         break;
    111                 }
    112         }
    113 
     69        Input* input=this->GetInput(enum_type);
     70        if(input) this->DeleteObject(input);
    11471        return 1;
    11572
     
    11976
    12077        /*Make a copy of the original input: */
    121         Input* original=xDynamicCast<Input*>(this->GetInput(original_enum));
     78        Input* original=this->GetInput(original_enum);
    12279        if(!original)_error_("could not find input with enum: " << EnumToStringx(original_enum));
    12380        Input* copy=xDynamicCast<Input*>(original->copy());
     
    13693
    13794        for ( object=objects.begin() ; object < objects.end(); object++ ){
    138 
    13995                input=xDynamicCast<Input*>(*object);
    140 
    141                 if (input->InstanceEnum()==enum_name){
    142                         return input;
    143                 }
    144         }
     96                if (input->InstanceEnum()==enum_name) return input;
     97        }
     98
    14599        return NULL;
    146100}
     
    148102void Inputs::GetInputAverage(IssmDouble* pvalue,int enum_type){/*{{{*/
    149103
    150         vector<Object*>::iterator object;
    151         Input* input=NULL;
    152         bool   found=false;
    153 
    154         /*Go through inputs and check whether any input with the same name is already in: */
    155         for ( object=objects.begin() ; object < objects.end(); object++ ){
    156 
    157                 input=xDynamicCast<Input*>(*object);
    158                 if (input->InstanceEnum()==enum_type){
    159                         found=true;
    160                         break;
    161                 }
    162         }
    163 
    164         if (!found){
    165                 /*we could not find an input with the correct enum type. No defaults values were provided,
    166                  * error out: */
    167                 _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
    168         }
     104        /*Find input in current dataset*/
     105        Input* input=this->GetInput(enum_type);
     106        if (!input) _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
    169107
    170108        /*Ok, we have an input if we made it here, request the input to return the value: */
     
    175113void Inputs::GetInputValue(bool* pvalue,int enum_type){/*{{{*/
    176114
    177         vector<Object*>::iterator object;
    178         Input* input=NULL;
    179         bool   found=false;
    180 
    181         /*Go through inputs and check whether any input with the same name is already in: */
    182         for ( object=objects.begin() ; object < objects.end(); object++ ){
    183 
    184                 input=xDynamicCast<Input*>(*object);
    185                 if (input->InstanceEnum()==enum_type){
    186                         found=true;
    187                         break;
    188                 }
    189         }
    190 
    191         if (!found){
    192                 /*we could not find an input with the correct enum type. No defaults values were provided,
    193                  * error out: */
    194                 _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
    195         }
     115        /*Find input in current dataset*/
     116        Input* input=this->GetInput(enum_type);
     117        if (!input) _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
    196118
    197119        /*Ok, we have an input if we made it here, request the input to return the value: */
     
    202124void Inputs::GetInputValue(int* pvalue,int enum_type){/*{{{*/
    203125
    204         vector<Object*>::iterator object;
    205         Input* input=NULL;
    206         bool   found=false;
    207 
    208         /*Go through inputs and check whether any input with the same name is already in: */
    209         for ( object=objects.begin() ; object < objects.end(); object++ ){
    210 
    211                 input=xDynamicCast<Input*>(*object);
    212                 if (input->InstanceEnum()==enum_type){
    213                         found=true;
    214                         break;
    215                 }
    216         }
    217 
    218         if (!found){
    219                 /*we could not find an input with the correct enum type. No defaults values were provided,
    220                  * error out: */
    221                 _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
    222         }
     126        /*Find input in current dataset*/
     127        Input* input=this->GetInput(enum_type);
     128        if (!input) _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
    223129
    224130        /*Ok, we have an input if we made it here, request the input to return the value: */
    225131        input->GetInputValue(pvalue);
    226132
    227 }
    228 /*}}}*/
     133}/*}}}*/
    229134void Inputs::GetInputValue(IssmDouble* pvalue,int enum_type){/*{{{*/
    230135
    231         vector<Object*>::iterator object;
    232         Input* input=NULL;
    233         bool   found=false;
    234 
    235         /*Go through inputs and check whether any input with the same name is already in: */
    236         for ( object=objects.begin() ; object < objects.end(); object++ ){
    237 
    238                 input=xDynamicCast<Input*>(*object);
    239                 if (input->InstanceEnum()==enum_type){
    240                         found=true;
    241                         break;
    242                 }
    243         }
    244 
    245         if (!found){
    246                 /*we could not find an input with the correct enum type. No defaults values were provided,
    247                  * error out: */
    248                 _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
    249         }
     136        /*Find input in current dataset*/
     137        Input* input=this->GetInput(enum_type);
     138        if (!input) _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
    250139
    251140        /*Ok, we have an input if we made it here, request the input to return the value: */
    252141        input->GetInputValue(pvalue);
    253142
    254 }
    255 /*}}}*/
     143}/*}}}*/
    256144IssmDouble Inputs::Max(int enumtype){/*{{{*/
    257145
    258         /*Output*/
    259         IssmDouble max;
    260 
    261         /*Get input*/
    262         Input* input=xDynamicCast<Input*>(this->GetInput(enumtype));
    263 
    264         /*Apply ContrainMin: */
    265         if (input){
    266                 max=input->Max();
    267         }
    268         else{
    269                 _error_("Input " << EnumToStringx(enumtype) << " not found");
    270         }
    271 
    272         /*Return output*/
    273         return max;
    274 }
    275 /*}}}*/
     146        /*Find input in current dataset*/
     147        Input* input=this->GetInput(enumtype);
     148        if (!input) _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
     149
     150        /*Return output*/
     151        return input->Max();
     152
     153}/*}}}*/
    276154IssmDouble Inputs::MaxAbs(int enumtype){/*{{{*/
    277155
    278         /*Output*/
    279         IssmDouble max;
    280 
    281         /*Get input*/
    282         Input* input=xDynamicCast<Input*>(this->GetInput(enumtype));
    283 
    284         /*Apply ContrainMin: */
    285         if (input){
    286                 max=input->MaxAbs();
    287         }
    288         else{
    289                 _error_("Input " << EnumToStringx(enumtype) << " not found");
    290         }
    291 
    292         /*Return output*/
    293         return max;
    294 }
    295 /*}}}*/
     156        /*Find input in current dataset*/
     157        Input* input=this->GetInput(enumtype);
     158        if (!input) _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
     159
     160        /*Return output*/
     161        return input->MaxAbs();
     162
     163}/*}}}*/
    296164IssmDouble Inputs::Min(int enumtype){/*{{{*/
    297165
    298         /*Output*/
    299         IssmDouble min;
    300 
    301         /*Get input*/
    302         Input* input=xDynamicCast<Input*>(this->GetInput(enumtype));
    303 
    304         /*Apply ContrainMin: */
    305         if (input){
    306                 min=input->Min();
    307         }
    308         else{
    309                 _error_("Input " << EnumToStringx(enumtype) << " not found");
    310         }
    311 
    312         /*Return output*/
    313         return min;
    314 }
    315 /*}}}*/
     166        /*Find input in current dataset*/
     167        Input* input=this->GetInput(enumtype);
     168        if (!input) _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
     169
     170        /*Return output*/
     171        return input->Min();
     172
     173}/*}}}*/
    316174IssmDouble Inputs::MinAbs(int enumtype){/*{{{*/
    317175
    318         /*Output*/
    319         IssmDouble min;
    320 
    321         /*Get input*/
    322         Input* input=xDynamicCast<Input*>(this->GetInput(enumtype));
    323 
    324         /*Apply ContrainMin: */
    325         if (input){
    326                 min=input->MinAbs();
    327         }
    328         else{
    329                 _error_("Input " << EnumToStringx(enumtype) << " not found");
    330         }
    331 
    332         /*Return output*/
    333         return min;
    334 }
    335 /*}}}*/
     176        /*Find input in current dataset*/
     177        Input* input=this->GetInput(enumtype);
     178        if (!input) _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
     179
     180        /*Return output*/
     181        return input->MinAbs();
     182
     183}/*}}}*/
    336184Inputs* Inputs::SpawnSegInputs(int index1,int index2){/*{{{*/
    337185
Note: See TracChangeset for help on using the changeset viewer.