Changeset 6238


Ignore:
Timestamp:
10/11/10 12:28:01 (14 years ago)
Author:
Mathieu Morlighem
Message:

Fixed Gradient scaling (Added new module)

Location:
issm/trunk/src
Files:
6 added
13 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified issm/trunk/src/c/Makefile.am

    r6213 r6238  
    551551                                        ./modules/ControlInputSetGradientx/ControlInputSetGradientx.cpp\
    552552                                        ./modules/ControlInputSetGradientx/ControlInputSetGradientx.h\
     553                                        ./modules/ControlInputScaleGradientx/ControlInputScaleGradientx.cpp\
     554                                        ./modules/ControlInputScaleGradientx/ControlInputScaleGradientx.h\
    553555                                        ./modules/SystemMatricesx/SystemMatricesx.cpp\
    554556                                        ./modules/SystemMatricesx/SystemMatricesx.h\
     
    11151117                                        ./modules/ControlInputSetGradientx/ControlInputSetGradientx.cpp\
    11161118                                        ./modules/ControlInputSetGradientx/ControlInputSetGradientx.h\
     1119                                        ./modules/ControlInputScaleGradientx/ControlInputScaleGradientx.cpp\
     1120                                        ./modules/ControlInputScaleGradientx/ControlInputScaleGradientx.h\
    11171121                                        ./modules/SystemMatricesx/SystemMatricesx.cpp\
    11181122                                        ./modules/SystemMatricesx/SystemMatricesx.h\
  • TabularUnified issm/trunk/src/c/modules/Orthx/Orthx.cpp

    r5323 r6238  
    2525        }
    2626
    27         /*scale to 1: gradient=gradient/max(abs(gradient))*/
    28         VecNorm(newgradj,NORM_INFINITY,&norm_new);
    29         if (norm_new<=0){
    30                 ISSMERROR("||∂J/∂α||∞ = 0  gradient is zero");
    31         }
    32         if(isnan(norm_new)){
    33                 ISSMERROR("input vector norm is NaN\n");
    34         }
    35         VecScale(newgradj,1.0/norm_new);
    36 
    3727        /*Assign correct pointer*/
    3828        *pnewgradj=newgradj;
  • TabularUnified issm/trunk/src/c/modules/modules.h

    r6200 r6238  
    2121#include "./ControlInputGetGradientx/ControlInputGetGradientx.h"
    2222#include "./ControlInputSetGradientx/ControlInputSetGradientx.h"
     23#include "./ControlInputScaleGradientx/ControlInputScaleGradientx.h"
    2324#include "./CostFunctionx/CostFunctionx.h"
    2425#include "./CreateNodalConstraintsx/CreateNodalConstraintsx.h"
  • TabularUnified issm/trunk/src/c/objects/Elements/Element.h

    r6216 r6238  
    6060                virtual void   ControlInputGetGradient(Vec gradient,int enum_type)=0;
    6161                virtual void   ControlInputSetGradient(double* gradient,int enum_type)=0;
     62                virtual void   ControlInputScaleGradient(int enum_type, double scale)=0;
    6263                virtual void   ProcessResultsUnits(void)=0;
    6364                virtual void   MinVel(double* pminvel, bool process_units)=0;
  • TabularUnified issm/trunk/src/c/objects/Elements/Penta.cpp

    r6232 r6238  
    816816        ((ControlInput*)input)->GetGradient(gradient,&doflist1[0]);
    817817
     818}/*}}}*/
     819/*FUNCTION Penta::ControlInputScaleGradient{{{1*/
     820void Penta::ControlInputScaleGradient(int enum_type,double scale){
     821
     822        Input* input=NULL;
     823
     824        if(enum_type==RheologyBbarEnum){
     825                input=(Input*)matice->inputs->GetInput(enum_type);
     826        }
     827        else{
     828                input=inputs->GetInput(enum_type);
     829        }
     830        if (!input) ISSMERROR("Input %s not found",EnumToString(enum_type));
     831        if (input->Enum()!=ControlInputEnum) ISSMERROR("Input %s is not a ControlInput",EnumToString(enum_type));
     832
     833        ((ControlInput*)input)->ScaleGradient(scale);
    818834}/*}}}*/
    819835/*FUNCTION Penta::ControlInputSetGradient{{{1*/
  • TabularUnified issm/trunk/src/c/objects/Elements/Penta.h

    r6231 r6238  
    9595                void   InputScale(int enum_type,double scale_factor);
    9696                void   ControlInputGetGradient(Vec gradient,int enum_type);
     97                void   ControlInputScaleGradient(int enum_type,double scale);
    9798                void   ControlInputSetGradient(double* gradient,int enum_type);
    9899                void   InputToResult(int enum_type,int step,double time);
  • TabularUnified issm/trunk/src/c/objects/Elements/Tria.cpp

    r6233 r6238  
    793793
    794794}/*}}}*/
     795/*FUNCTION Tria::ControlInputScaleGradient{{{1*/
     796void Tria::ControlInputScaleGradient(int enum_type,double scale){
     797
     798        Input* input=NULL;
     799
     800        if(enum_type==RheologyBbarEnum){
     801                input=(Input*)matice->inputs->GetInput(enum_type);
     802        }
     803        else{
     804                input=inputs->GetInput(enum_type);
     805        }
     806        if (!input) ISSMERROR("Input %s not found",EnumToString(enum_type));
     807        if (input->Enum()!=ControlInputEnum) ISSMERROR("Input %s is not a ControlInput",EnumToString(enum_type));
     808
     809        ((ControlInput*)input)->ScaleGradient(scale);
     810}/*}}}*/
    795811/*FUNCTION Tria::ControlInputSetGradient{{{1*/
    796812void Tria::ControlInputSetGradient(double* gradient,int enum_type){
  • TabularUnified issm/trunk/src/c/objects/Elements/Tria.h

    r6231 r6238  
    9696                void   InputScale(int enum_type,double scale_factor);
    9797                void   ControlInputGetGradient(Vec gradient,int enum_type);
     98                void   ControlInputScaleGradient(int enum_type,double scale);
    9899                void   ControlInputSetGradient(double* gradient,int enum_type);
    99100                void   InputToResult(int enum_type,int step,double time);
  • TabularUnified issm/trunk/src/c/objects/Inputs/ControlInput.cpp

    r6200 r6238  
    266266        if(gradient) gradient->GetVectorFromInputs(gradient_vec,doflist);
    267267}/*}}}*/
     268/*FUNCTION ControlInput::ScaleGradient{{{1*/
     269void ControlInput::ScaleGradient(double scaling_factor){
     270        if(!gradient) ISSMERROR("Gradient of ControlInput %s not found",EnumToString(enum_type));
     271        gradient->Scale(scaling_factor);
     272}/*}}}*/
    268273/*FUNCTION ControlInput::SetGradient{{{1*/
    269274void ControlInput::SetGradient(Input* gradient_in){
  • TabularUnified issm/trunk/src/c/objects/Inputs/ControlInput.h

    r6200 r6238  
    7878                ElementResult* SpawnGradient(int step, double time);
    7979                void GetGradient(Vec gradient_vec,int* doflist);
     80                void ScaleGradient(double scale);
    8081                void SetGradient(Input* gradient_in);
    8182                void UpdateValue(double scalar);
  • TabularUnified issm/trunk/src/c/solutions/gradient_core.cpp

    r6230 r6238  
    2121        int    *control_type   = NULL;
    2222        double *optscal_list   = NULL;
     23        double  optscal,norm_grad;
    2324
    2425        /*Intermediaries*/
     
    3435        femmodel->parameters->FindParam(&verbose,VerboseEnum);
    3536
     37        /*Compute and norm gradient of all controls*/
    3638        for (int i=0;i<num_controls;i++){
    3739
     
    4446                        _printf_("%s","      orthogonalization...\n");
    4547                        ControlInputGetGradientx(&old_gradient,femmodel->elements,femmodel->nodes, femmodel->vertices,femmodel->loads, femmodel->materials,femmodel->parameters,control_type[i]);
    46                         Orthx(&new_gradient,gradient,old_gradient);
    47                         VecFree(&old_gradient);
     48                        Orthx(&new_gradient,gradient,old_gradient); VecFree(&old_gradient); VecFree(&gradient);
    4849                }
    4950                else{
    5051                        _printf_("%s","      normalizing directions...\n");
    51                         Orthx(&new_gradient,gradient,NULL);
     52                        Orthx(&new_gradient,gradient,NULL); VecFree(&gradient);
    5253                }
    53                 VecFree(&gradient);
    5454
    55                 /*Scale gradient for current step and current parameter*/
    56                 VecScale(new_gradient,optscal_list[num_controls*step+i]);
     55                /*Get scaling factor of current control:*/
     56                VecNorm(new_gradient,NORM_INFINITY,&norm_grad);
     57                if(norm_grad<=0)    ISSMERROR("||∂J/∂α||∞ = 0    gradient norm of J with respect to %s is zero",EnumToString(control_type[i]));
     58                if(isnan(norm_grad))ISSMERROR("||∂J/∂α||∞ = NaN  gradient norm of J with respect to %s is NaN" ,EnumToString(control_type[i]));
     59                if(i==0 || (optscal_list[num_controls*step+i]/norm_grad)<optscal) optscal=optscal_list[num_controls*step+i]/norm_grad;
    5760
    5861                /*plug back into inputs: */
    5962                ControlInputSetGradientx(femmodel-> elements,femmodel-> nodes, femmodel-> vertices,femmodel-> loads, femmodel-> materials,  femmodel->parameters,control_type[i],new_gradient);
    60 
    61                 /*Free ressources and return:*/
    6263                VecFree(&new_gradient);
    6364        }
     65        printf("norm grad = %g\n",norm_grad);
     66        printf("optscal = %g\n",optscal);
     67
     68        /*Scale Gradients*/
     69        for (int i=0;i<num_controls;i++) ControlInputScaleGradientx(femmodel-> elements,femmodel-> nodes, femmodel-> vertices,femmodel-> loads, femmodel-> materials,  femmodel->parameters,control_type[i],optscal);
    6470
    6571        /*Clean up and return*/
  • TabularUnified issm/trunk/src/m/solutions/gradient_core.m

    r6226 r6238  
    2525        control_type=femmodel.parameters.ControlType;
    2626        control_steady=femmodel.parameters.ControlSteady;
    27         optscal=femmodel.parameters.OptScal;
     27        optscal_list=femmodel.parameters.OptScal;
    2828
    2929        for i=1:num_controls,
     
    3232                grad=Gradj(femmodel.elements,femmodel.nodes,femmodel.vertices,femmodel.loads,femmodel.materials,femmodel.parameters);
    3333
    34 
    35                 if control_steady;
     34                if control_steady,
    3635                        femmodel=diagnostic_core(femmodel);
    3736                end
     
    4645                end
    4746
    48                 %Scale Gradient
    49                 new_gradient=optscal(step,i)*new_gradient;
     47                 %Get scaling factor of current control:
     48                 norm_grad=norm(new_gradient,inf);
     49                 if(norm_grad<=0),     error(['||∂J/∂α||∞ = 0   gradient norm of J with respect to ' EnumToString(control_type(i))  ' is zero']); end
     50                 if(isnan(norm_grad)), error(['||∂J/∂α||∞ = NaN gradient norm of J with respect to ' EnumToString(control_type(i))  ' is NaN' ]); end
     51                 if(i==1 | (optscal_list(step,i)/norm_grad)<optscal) optscal=optscal_list(step,i)/norm_grad; end
    5052
    5153                %plug back into inputs:
    5254                [femmodel.elements,femmodel.nodes,femmodel.vertices,femmodel.loads,femmodel.materials,femmodel.parameters]=ControlInputSetGradient(femmodel.elements,femmodel.nodes, femmodel.vertices,femmodel.loads, femmodel.materials,  femmodel.parameters,control_type(i),new_gradient);
    5355        end
     56
     57        %Scale all gradients
     58        for i=1:num_controls,
     59                [femmodel.elements,femmodel.nodes,femmodel.vertices,femmodel.loads,femmodel.materials,femmodel.parameters]=ControlInputScaleGradient(femmodel.elements,femmodel.nodes, femmodel.vertices,femmodel.loads, femmodel.materials,  femmodel.parameters,control_type(i),optscal);
     60        end
  • TabularUnified issm/trunk/src/mex/Makefile.am

    r6200 r6238  
    1515                                ControlOptimization\
    1616                                ControlInputGetGradient\
     17                                ControlInputScaleGradient\
    1718                                ControlInputSetGradient\
    1819                                ContourToMesh \
     
    133134                                                                                ControlInputGetGradient/ControlInputGetGradient.h
    134135
     136ControlInputScaleGradient_SOURCES = ControlInputScaleGradient/ControlInputScaleGradient.cpp\
     137                                                                                         ControlInputScaleGradient/ControlInputScaleGradient.h
     138
    135139ControlInputSetGradient_SOURCES = ControlInputSetGradient/ControlInputSetGradient.cpp\
    136140                          ControlInputSetGradient/ControlInputSetGradient.h
Note: See TracChangeset for help on using the changeset viewer.