| 1 | /*
|
|---|
| 2 | * \file Inputs.c
|
|---|
| 3 | * \brief: implementation of the Inputs class, derived from DataSet class
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*Headers: {{{*/
|
|---|
| 7 | #ifdef HAVE_CONFIG_H
|
|---|
| 8 | #include <config.h>
|
|---|
| 9 | #else
|
|---|
| 10 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
|---|
| 11 | #endif
|
|---|
| 12 |
|
|---|
| 13 | #include <vector>
|
|---|
| 14 | #include <functional>
|
|---|
| 15 | #include <algorithm>
|
|---|
| 16 | #include <iostream>
|
|---|
| 17 |
|
|---|
| 18 | #include "./DataSet.h"
|
|---|
| 19 | #include "../shared/shared.h"
|
|---|
| 20 | #include "../include/include.h"
|
|---|
| 21 | #include "../EnumDefinitions/EnumDefinitions.h"
|
|---|
| 22 |
|
|---|
| 23 | using namespace std;
|
|---|
| 24 | /*}}}*/
|
|---|
| 25 |
|
|---|
| 26 | /*Object constructors and destructor*/
|
|---|
| 27 | /*FUNCTION Inputs::Inputs(){{{*/
|
|---|
| 28 | Inputs::Inputs(){
|
|---|
| 29 | return;
|
|---|
| 30 | }
|
|---|
| 31 | /*}}}*/
|
|---|
| 32 | /*FUNCTION Inputs::~Inputs(){{{*/
|
|---|
| 33 | Inputs::~Inputs(){
|
|---|
| 34 | return;
|
|---|
| 35 | }
|
|---|
| 36 | /*}}}*/
|
|---|
| 37 |
|
|---|
| 38 | /*Object management*/
|
|---|
| 39 | /*FUNCTION Inputs::GetInputValue(bool* pvalue,int enum-type){{{*/
|
|---|
| 40 | void Inputs::GetInputValue(bool* pvalue,int enum_type){
|
|---|
| 41 |
|
|---|
| 42 | vector<Object*>::iterator object;
|
|---|
| 43 | Input* input=NULL;
|
|---|
| 44 | bool found=false;
|
|---|
| 45 |
|
|---|
| 46 | /*Go through inputs and check whether any input with the same name is already in: */
|
|---|
| 47 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
|---|
| 48 |
|
|---|
| 49 | input=dynamic_cast<Input*>(*object);
|
|---|
| 50 | if (input->InstanceEnum()==enum_type){
|
|---|
| 51 | found=true;
|
|---|
| 52 | break;
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | if (!found){
|
|---|
| 57 | /*we could not find an input with the correct enum type. No defaults values were provided,
|
|---|
| 58 | * error out: */
|
|---|
| 59 | _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /*Ok, we have an input if we made it here, request the input to return the value: */
|
|---|
| 63 | input->GetInputValue(pvalue);
|
|---|
| 64 |
|
|---|
| 65 | }
|
|---|
| 66 | /*}}}*/
|
|---|
| 67 | /*FUNCTION Inputs::GetInputValue(int* pvalue,int enum-type){{{*/
|
|---|
| 68 | void Inputs::GetInputValue(int* pvalue,int enum_type){
|
|---|
| 69 |
|
|---|
| 70 | vector<Object*>::iterator object;
|
|---|
| 71 | Input* input=NULL;
|
|---|
| 72 | bool found=false;
|
|---|
| 73 |
|
|---|
| 74 | /*Go through inputs and check whether any input with the same name is already in: */
|
|---|
| 75 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
|---|
| 76 |
|
|---|
| 77 | input=dynamic_cast<Input*>(*object);
|
|---|
| 78 | if (input->InstanceEnum()==enum_type){
|
|---|
| 79 | found=true;
|
|---|
| 80 | break;
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | if (!found){
|
|---|
| 85 | /*we could not find an input with the correct enum type. No defaults values were provided,
|
|---|
| 86 | * error out: */
|
|---|
| 87 | _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | /*Ok, we have an input if we made it here, request the input to return the value: */
|
|---|
| 91 | input->GetInputValue(pvalue);
|
|---|
| 92 |
|
|---|
| 93 | }
|
|---|
| 94 | /*}}}*/
|
|---|
| 95 | /*FUNCTION Inputs::GetInputValue(IssmDouble* pvalue,int enum-type){{{*/
|
|---|
| 96 | void Inputs::GetInputValue(IssmDouble* pvalue,int enum_type){
|
|---|
| 97 |
|
|---|
| 98 | vector<Object*>::iterator object;
|
|---|
| 99 | Input* input=NULL;
|
|---|
| 100 | bool found=false;
|
|---|
| 101 |
|
|---|
| 102 | /*Go through inputs and check whether any input with the same name is already in: */
|
|---|
| 103 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
|---|
| 104 |
|
|---|
| 105 | input=dynamic_cast<Input*>(*object);
|
|---|
| 106 | if (input->InstanceEnum()==enum_type){
|
|---|
| 107 | found=true;
|
|---|
| 108 | break;
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | if (!found){
|
|---|
| 113 | /*we could not find an input with the correct enum type. No defaults values were provided,
|
|---|
| 114 | * error out: */
|
|---|
| 115 | _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /*Ok, we have an input if we made it here, request the input to return the value: */
|
|---|
| 119 | input->GetInputValue(pvalue);
|
|---|
| 120 |
|
|---|
| 121 | }
|
|---|
| 122 | /*}}}*/
|
|---|
| 123 | /*FUNCTION Inputs::GetInputAverage{{{*/
|
|---|
| 124 | void Inputs::GetInputAverage(IssmDouble* pvalue,int enum_type){
|
|---|
| 125 |
|
|---|
| 126 | vector<Object*>::iterator object;
|
|---|
| 127 | Input* input=NULL;
|
|---|
| 128 | bool found=false;
|
|---|
| 129 |
|
|---|
| 130 | /*Go through inputs and check whether any input with the same name is already in: */
|
|---|
| 131 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
|---|
| 132 |
|
|---|
| 133 | input=dynamic_cast<Input*>(*object);
|
|---|
| 134 | if (input->InstanceEnum()==enum_type){
|
|---|
| 135 | found=true;
|
|---|
| 136 | break;
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | if (!found){
|
|---|
| 141 | /*we could not find an input with the correct enum type. No defaults values were provided,
|
|---|
| 142 | * error out: */
|
|---|
| 143 | _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /*Ok, we have an input if we made it here, request the input to return the value: */
|
|---|
| 147 | input->GetInputAverage(pvalue);
|
|---|
| 148 |
|
|---|
| 149 | }
|
|---|
| 150 | /*}}}*/
|
|---|
| 151 | /*FUNCTION Inputs::AddInput{{{*/
|
|---|
| 152 | int Inputs::AddInput(Input* in_input){
|
|---|
| 153 |
|
|---|
| 154 | /*First, go through dataset of inputs and check whether any input
|
|---|
| 155 | * with the same name is already in. If so, erase the corresponding
|
|---|
| 156 | * object before adding this new one: */
|
|---|
| 157 | vector<Object*>::iterator object;
|
|---|
| 158 | Input* input=NULL;
|
|---|
| 159 |
|
|---|
| 160 | /*In debugging mode, check that the input is not a NULL pointer*/
|
|---|
| 161 | _assert_(in_input);
|
|---|
| 162 |
|
|---|
| 163 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
|---|
| 164 |
|
|---|
| 165 | input=dynamic_cast<Input*>(*object);
|
|---|
| 166 |
|
|---|
| 167 | if (input->InstanceEnum()==in_input->InstanceEnum()){
|
|---|
| 168 | this->DeleteObject(input);
|
|---|
| 169 | break;
|
|---|
| 170 | }
|
|---|
| 171 | }
|
|---|
| 172 | this->AddObject(in_input);
|
|---|
| 173 |
|
|---|
| 174 | return 1;
|
|---|
| 175 | }
|
|---|
| 176 | /*}}}*/
|
|---|
| 177 | /*FUNCTION Inputs::ChangeEnum{{{*/
|
|---|
| 178 | void Inputs::ChangeEnum(int oldenumtype,int newenumtype){
|
|---|
| 179 |
|
|---|
| 180 | /*Go through dataset of inputs and look for input with
|
|---|
| 181 | * same enum as input enum, once found, just change its name */
|
|---|
| 182 | vector<Object*>::iterator object;
|
|---|
| 183 | Input* input=NULL;
|
|---|
| 184 |
|
|---|
| 185 | /*Delete existing input of newenumtype if it exists*/
|
|---|
| 186 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
|---|
| 187 | input=dynamic_cast<Input*>(*object);
|
|---|
| 188 |
|
|---|
| 189 | if (input->InstanceEnum()==newenumtype){
|
|---|
| 190 | this->DeleteObject(input);
|
|---|
| 191 | break;
|
|---|
| 192 | }
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /*Change enum_type of input of oldenumtype*/
|
|---|
| 196 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
|---|
| 197 |
|
|---|
| 198 | input=dynamic_cast<Input*>(*object);
|
|---|
| 199 |
|
|---|
| 200 | if (input->InstanceEnum()==oldenumtype){
|
|---|
| 201 | input->ChangeEnum(newenumtype);
|
|---|
| 202 | break;
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 | /*}}}*/
|
|---|
| 207 | /*FUNCTION Inputs::ConstrainMin{{{*/
|
|---|
| 208 | void Inputs::ConstrainMin(int constrain_enum, IssmDouble minimum){
|
|---|
| 209 |
|
|---|
| 210 | /*Find x and y inputs: */
|
|---|
| 211 | Input* constrain_input=dynamic_cast<Input*>(this->GetInput(constrain_enum));
|
|---|
| 212 |
|
|---|
| 213 | /*some checks: */
|
|---|
| 214 | if(!constrain_input) _error_("input " << EnumToStringx(constrain_enum) << " could not be found!");
|
|---|
| 215 |
|
|---|
| 216 | /*Apply ContrainMin: */
|
|---|
| 217 | constrain_input->ConstrainMin(minimum);
|
|---|
| 218 | }
|
|---|
| 219 | /*}}}*/
|
|---|
| 220 | /*FUNCTION Inputs::InfinityNorm{{{*/
|
|---|
| 221 | IssmDouble Inputs::InfinityNorm(int enumtype){
|
|---|
| 222 |
|
|---|
| 223 | /*Output*/
|
|---|
| 224 | IssmDouble norm;
|
|---|
| 225 |
|
|---|
| 226 | /*Get input*/
|
|---|
| 227 | Input* input=dynamic_cast<Input*>(this->GetInput(enumtype));
|
|---|
| 228 |
|
|---|
| 229 | /*Apply ContrainMin: */
|
|---|
| 230 | if (input){
|
|---|
| 231 | norm=input->InfinityNorm();
|
|---|
| 232 | }
|
|---|
| 233 | else{
|
|---|
| 234 | norm=0;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | /*Return output*/
|
|---|
| 238 | return norm;
|
|---|
| 239 | }
|
|---|
| 240 | /*}}}*/
|
|---|
| 241 | /*FUNCTION Inputs::Max{{{*/
|
|---|
| 242 | IssmDouble Inputs::Max(int enumtype){
|
|---|
| 243 |
|
|---|
| 244 | /*Output*/
|
|---|
| 245 | IssmDouble max;
|
|---|
| 246 |
|
|---|
| 247 | /*Get input*/
|
|---|
| 248 | Input* input=dynamic_cast<Input*>(this->GetInput(enumtype));
|
|---|
| 249 |
|
|---|
| 250 | /*Apply ContrainMin: */
|
|---|
| 251 | if (input){
|
|---|
| 252 | max=input->Max();
|
|---|
| 253 | }
|
|---|
| 254 | else{
|
|---|
| 255 | _error_("Input " << EnumToStringx(enumtype) << " not found");
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | /*Return output*/
|
|---|
| 259 | return max;
|
|---|
| 260 | }
|
|---|
| 261 | /*}}}*/
|
|---|
| 262 | /*FUNCTION Inputs::MaxAbs{{{*/
|
|---|
| 263 | IssmDouble Inputs::MaxAbs(int enumtype){
|
|---|
| 264 |
|
|---|
| 265 | /*Output*/
|
|---|
| 266 | IssmDouble max;
|
|---|
| 267 |
|
|---|
| 268 | /*Get input*/
|
|---|
| 269 | Input* input=dynamic_cast<Input*>(this->GetInput(enumtype));
|
|---|
| 270 |
|
|---|
| 271 | /*Apply ContrainMin: */
|
|---|
| 272 | if (input){
|
|---|
| 273 | max=input->MaxAbs();
|
|---|
| 274 | }
|
|---|
| 275 | else{
|
|---|
| 276 | _error_("Input " << EnumToStringx(enumtype) << " not found");
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | /*Return output*/
|
|---|
| 280 | return max;
|
|---|
| 281 | }
|
|---|
| 282 | /*}}}*/
|
|---|
| 283 | /*FUNCTION Inputs::Min{{{*/
|
|---|
| 284 | IssmDouble Inputs::Min(int enumtype){
|
|---|
| 285 |
|
|---|
| 286 | /*Output*/
|
|---|
| 287 | IssmDouble min;
|
|---|
| 288 |
|
|---|
| 289 | /*Get input*/
|
|---|
| 290 | Input* input=dynamic_cast<Input*>(this->GetInput(enumtype));
|
|---|
| 291 |
|
|---|
| 292 | /*Apply ContrainMin: */
|
|---|
| 293 | if (input){
|
|---|
| 294 | min=input->Min();
|
|---|
| 295 | }
|
|---|
| 296 | else{
|
|---|
| 297 | _error_("Input " << EnumToStringx(enumtype) << " not found");
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | /*Return output*/
|
|---|
| 301 | return min;
|
|---|
| 302 | }
|
|---|
| 303 | /*}}}*/
|
|---|
| 304 | /*FUNCTION Inputs::MinAbs{{{*/
|
|---|
| 305 | IssmDouble Inputs::MinAbs(int enumtype){
|
|---|
| 306 |
|
|---|
| 307 | /*Output*/
|
|---|
| 308 | IssmDouble min;
|
|---|
| 309 |
|
|---|
| 310 | /*Get input*/
|
|---|
| 311 | Input* input=dynamic_cast<Input*>(this->GetInput(enumtype));
|
|---|
| 312 |
|
|---|
| 313 | /*Apply ContrainMin: */
|
|---|
| 314 | if (input){
|
|---|
| 315 | min=input->MinAbs();
|
|---|
| 316 | }
|
|---|
| 317 | else{
|
|---|
| 318 | _error_("Input " << EnumToStringx(enumtype) << " not found");
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | /*Return output*/
|
|---|
| 322 | return min;
|
|---|
| 323 | }
|
|---|
| 324 | /*}}}*/
|
|---|
| 325 | /*FUNCTION Inputs::GetInput{{{*/
|
|---|
| 326 | Input* Inputs::GetInput(int enum_name){
|
|---|
| 327 |
|
|---|
| 328 | vector<Object*>::iterator object;
|
|---|
| 329 | Input* input=NULL;
|
|---|
| 330 |
|
|---|
| 331 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
|---|
| 332 |
|
|---|
| 333 | input=dynamic_cast<Input*>(*object);
|
|---|
| 334 |
|
|---|
| 335 | if (input->InstanceEnum()==enum_name){
|
|---|
| 336 | return input;
|
|---|
| 337 | }
|
|---|
| 338 | }
|
|---|
| 339 | return NULL;
|
|---|
| 340 | }
|
|---|
| 341 | /*}}}*/
|
|---|
| 342 | /*FUNCTION Inputs::DeleteInput{{{*/
|
|---|
| 343 | int Inputs::DeleteInput(int enum_type){
|
|---|
| 344 |
|
|---|
| 345 | vector<Object*>::iterator object;
|
|---|
| 346 | Input* input=NULL;
|
|---|
| 347 |
|
|---|
| 348 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
|---|
| 349 |
|
|---|
| 350 | input=dynamic_cast<Input*>(*object);
|
|---|
| 351 |
|
|---|
| 352 | if (input->InstanceEnum()==enum_type){
|
|---|
| 353 | this->DeleteObject(input);
|
|---|
| 354 | break;
|
|---|
| 355 | }
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | return 1;
|
|---|
| 359 |
|
|---|
| 360 | }
|
|---|
| 361 | /*}}}*/
|
|---|
| 362 | /*FUNCTION Inputs::DuplicateInput{{{*/
|
|---|
| 363 | void Inputs::DuplicateInput(int original_enum,int new_enum){
|
|---|
| 364 |
|
|---|
| 365 | Input* original=NULL;
|
|---|
| 366 | Input* copy=NULL;
|
|---|
| 367 |
|
|---|
| 368 | /*Make a copy of the original input: */
|
|---|
| 369 | original=dynamic_cast<Input*>(this->GetInput(original_enum));
|
|---|
| 370 | if(!original)_error_("could not find input with enum: " << EnumToStringx(original_enum));
|
|---|
| 371 | copy=dynamic_cast<Input*>(original->copy());
|
|---|
| 372 |
|
|---|
| 373 | /*Change copy enum to reinitialized_enum: */
|
|---|
| 374 | copy->ChangeEnum(new_enum);
|
|---|
| 375 |
|
|---|
| 376 | /*Add copy into inputs, it will wipe off the one already there: */
|
|---|
| 377 | this->AddInput(dynamic_cast<Input*>(copy));
|
|---|
| 378 | }
|
|---|
| 379 | /*}}}*/
|
|---|
| 380 | /*FUNCTION Inputs::SpawnTriaInputs{{{*/
|
|---|
| 381 | Inputs* Inputs::SpawnTriaInputs(int* indices){
|
|---|
| 382 |
|
|---|
| 383 | /*Intermediary*/
|
|---|
| 384 | vector<Object*>::iterator object;
|
|---|
| 385 | Input* inputin=NULL;
|
|---|
| 386 | Input* inputout=NULL;
|
|---|
| 387 |
|
|---|
| 388 | /*Output*/
|
|---|
| 389 | Inputs* newinputs=new Inputs();
|
|---|
| 390 |
|
|---|
| 391 | /*Go through inputs and call Spawn function*/
|
|---|
| 392 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
|---|
| 393 |
|
|---|
| 394 | /*Create new input*/
|
|---|
| 395 | inputin=dynamic_cast<Input*>(*object);
|
|---|
| 396 | inputout=inputin->SpawnTriaInput(indices);
|
|---|
| 397 |
|
|---|
| 398 | /*Add input to new inputs*/
|
|---|
| 399 | newinputs->AddObject(inputout);
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | /*Assign output pointer*/
|
|---|
| 403 | return newinputs;
|
|---|
| 404 | }
|
|---|
| 405 | /*}}}*/
|
|---|
| 406 | /*FUNCTION Inputs::AXPY{{{*/
|
|---|
| 407 | void Inputs::AXPY(int MeshYEnum, IssmDouble scalar, int MeshXEnum){
|
|---|
| 408 |
|
|---|
| 409 | Input* xinput=NULL;
|
|---|
| 410 | Input* yinput=NULL;
|
|---|
| 411 |
|
|---|
| 412 | /*Find x and y inputs: */
|
|---|
| 413 | xinput=dynamic_cast<Input*>(this->GetInput(MeshXEnum));
|
|---|
| 414 | yinput=dynamic_cast<Input*>(this->GetInput(MeshYEnum));
|
|---|
| 415 |
|
|---|
| 416 | /*some checks: */
|
|---|
| 417 | if(!xinput) _error_("input " << EnumToStringx(MeshXEnum) << " could not be found!");
|
|---|
| 418 | if(!yinput) _error_("input " << EnumToStringx(MeshYEnum) << " could not be found!");
|
|---|
| 419 |
|
|---|
| 420 | /*Apply AXPY: */
|
|---|
| 421 | yinput->AXPY(xinput,scalar);
|
|---|
| 422 | }
|
|---|
| 423 | /*}}}*/
|
|---|
| 424 | /*FUNCTION Inputs::Configure{{{*/
|
|---|
| 425 | void Inputs::Configure(Parameters* parameters){
|
|---|
| 426 |
|
|---|
| 427 | vector<Object*>::iterator object;
|
|---|
| 428 | Input* input=NULL;
|
|---|
| 429 |
|
|---|
| 430 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
|---|
| 431 |
|
|---|
| 432 | input=dynamic_cast<Input*>(*object);
|
|---|
| 433 | input->Configure(parameters);
|
|---|
| 434 |
|
|---|
| 435 | }
|
|---|
| 436 |
|
|---|
| 437 | }
|
|---|
| 438 | /*}}}*/
|
|---|