Changeset 26712


Ignore:
Timestamp:
12/06/21 17:57:34 (3 years ago)
Author:
Mathieu Morlighem
Message:

CHG: fixed interpolation method

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/jl/md/utils.jl

    r26703 r26712  
    5353        return output
    5454end# }}}
    55 function InterpFromMeshToMesh2d(index::Array,x::Vector,y::Vector,data::Vector,xout::Vector,yout::Vector) #{{{
     55function InterpFromMeshToMesh2d(index_data::Array,x_data::Vector,y_data::Vector,data::Vector,xout::Vector,yout::Vector,default::Float64=NaN) #{{{
     56
     57        #Allocate output
     58        nods_out = length(xout)
     59        data_out = default*ones(nods_out)
     60
     61        #Interpolation type
     62        data_length = size(data,1)
     63        nods_data   = length(x_data)
     64        nels_data   = size(index_data,1)
     65        if(data_length==nods_data)
     66                interpolation_type=1;
     67        elseif (data_length==nels_data)
     68                interpolation_type=2
     69        else
     70                error("length of vector data not supported yet. It should be of length (number of nodes) or (number of elements)!")
     71        end
     72        xmin = minimum(xout); xmax = maximum(xout)
     73        ymin = minimum(yout); ymax = maximum(yout)
     74
     75        for i in 1:nels_data
     76
     77                #skip element if no overlap
     78                if (minimum(x_data[index_data[i,:]]) > xmax) continue end
     79                if (minimum(y_data[index_data[i,:]]) > ymax) continue end
     80                if (maximum(x_data[index_data[i,:]]) < xmin) continue end
     81                if (maximum(y_data[index_data[i,:]]) < ymin) continue end
     82
     83                #get area of the current element (Jacobian = 2 * area)*/
     84                #area =x2 * y3 - y2*x3 + x1 * y2 - y1 * x2 + x3 * y1 - y3 * x1;
     85                area = (x_data[index_data[i,2]]*y_data[index_data[i,3]]-y_data[index_data[i,2]]*x_data[index_data[i,3]]
     86                                  +  x_data[index_data[i,1]]*y_data[index_data[i,2]]-y_data[index_data[i,1]]*x_data[index_data[i,2]]
     87                                  +  x_data[index_data[i,3]]*y_data[index_data[i,1]]-y_data[index_data[i,3]]*x_data[index_data[i,1]])
     88
     89                for j in 1:nods_out
     90                        #Get first area coordinate = det(x-x3  x2-x3 ; y-y3   y2-y3)/area
     91                        area_1=((xout[j]-x_data[index_data[i,3]])*(y_data[index_data[i,2]]-y_data[index_data[i,3]])
     92                                         -  (yout[j]-y_data[index_data[i,3]])*(x_data[index_data[i,2]]-x_data[index_data[i,3]]))/area
     93                        #Get second area coordinate =det(x1-x3  x-x3 ; y1-y3   y-y3)/area
     94                        area_2=((x_data[index_data[i,1]]-x_data[index_data[i,3]])*(yout[j]-y_data[index_data[i,3]])
     95                                          - (y_data[index_data[i,1]]-y_data[index_data[i,3]])*(xout[j]-x_data[index_data[i,3]]))/area
     96                        #Get third area coordinate = 1-area1-area2
     97                        area_3=1-area_1-area_2
     98
     99                        if (area_1>=0 && area_2>=0 && area_3>=0)
     100                                if (interpolation_type==1)
     101                                        #nodal interpolation
     102                                        data_out[j]=area_1*data[index_data[i,1]]+area_2*data[index_data[i,2]]+area_3*data[index_data[i,3]];
     103                                else
     104                                        #element interpolation
     105                                        data_out[j]=data[i];
     106                                end
     107                        end
     108                end
     109        end
     110        return data_out
     111
     112        #OLD STUFF!!! not working...
     113       
    56114        #prepare input arrays
    57115        nods = Cint(length(x))
     
    126184        end
    127185end #}}}
     186function meshgrid(x::Vector, y::Vector)
     187    X = [i for i in x, j in 1:length(y)]
     188    Y = [j for i in 1:length(x), j in y]
     189    return X, Y
     190end
Note: See TracChangeset for help on using the changeset viewer.