[4039] | 1 | /*!\file Patch.c
|
---|
| 2 | * \brief: implementation of the Patch object
|
---|
| 3 | */
|
---|
| 4 |
|
---|
[13975] | 5 | /*Include files */
|
---|
[4039] | 6 | #ifdef HAVE_CONFIG_H
|
---|
[9320] | 7 | #include <config.h>
|
---|
[4039] | 8 | #else
|
---|
| 9 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
---|
| 10 | #endif
|
---|
| 11 |
|
---|
[9320] | 12 | #include <stdio.h>
|
---|
[4039] | 13 | #include <string.h>
|
---|
[11399] | 14 | #include <math.h>
|
---|
[12832] | 15 | #include "./classes.h"
|
---|
[4236] | 16 | #include "../Container/Container.h"
|
---|
[4057] | 17 | #include "../io/io.h"
|
---|
[4039] | 18 | #include "../EnumDefinitions/EnumDefinitions.h"
|
---|
| 19 | #include "../shared/shared.h"
|
---|
| 20 | #include "../include/include.h"
|
---|
| 21 |
|
---|
| 22 | /*Object constructors and destructors:*/
|
---|
[12365] | 23 | /*FUNCTION Patch::Patch() default constructor {{{*/
|
---|
[4039] | 24 | Patch::Patch(){
|
---|
[13975] | 25 | this->numrows = 0;
|
---|
| 26 | this->numcols = 0;
|
---|
| 27 | this->maxvertices = 0;
|
---|
| 28 | this->maxnodes = 0;
|
---|
| 29 | this->values = NULL;
|
---|
[4039] | 30 | }
|
---|
| 31 | /*}}}*/
|
---|
[12365] | 32 | /*FUNCTION Patch::Patch(int numrows, int maxvertices, int maxnodes){{{*/
|
---|
[4039] | 33 | Patch::Patch(int in_numrows, int in_maxvertices, int in_maxnodes){
|
---|
| 34 |
|
---|
| 35 | this->numrows=in_numrows;
|
---|
| 36 | this->maxvertices=in_maxvertices;
|
---|
| 37 | this->maxnodes=in_maxnodes;
|
---|
| 38 | this->numcols=1 //enum_type
|
---|
| 39 | +1 //step
|
---|
| 40 | +1 //time
|
---|
| 41 | +1 //element id
|
---|
| 42 | +1 //interpolation type
|
---|
| 43 | +maxvertices //vertices
|
---|
| 44 | +maxnodes; //nodes
|
---|
| 45 |
|
---|
[4508] | 46 | //Allocate values and fill with NaN:
|
---|
| 47 | if (this->numcols*this->numrows==0){
|
---|
| 48 | this->values=NULL;
|
---|
| 49 | }
|
---|
| 50 | else{
|
---|
[13975] | 51 | this->values=xNew<IssmDouble>(this->numcols*this->numrows);
|
---|
| 52 | for(int i=0;i<this->numrows;i++){
|
---|
| 53 | for(int j=0;j<this->numcols;j++){
|
---|
[4508] | 54 | this->values[i*this->numcols+j]=NAN;
|
---|
| 55 | }
|
---|
[4039] | 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | }
|
---|
| 60 | /*}}}*/
|
---|
[12365] | 61 | /*FUNCTION Patch::~Patch(){{{*/
|
---|
[4039] | 62 | Patch::~Patch(){
|
---|
[12366] | 63 | xDelete<IssmDouble>(values);
|
---|
[4039] | 64 | }
|
---|
| 65 | /*}}}*/
|
---|
[4312] | 66 |
|
---|
| 67 | /*Object methods*/
|
---|
[12365] | 68 | /*FUNCTION Patch::fillelementinfo{{{*/
|
---|
[4042] | 69 | void Patch::fillelementinfo(int count, int element_id, int* vertices_ids, int num_vertices){
|
---|
[4039] | 70 |
|
---|
| 71 | int i;
|
---|
[12366] | 72 | IssmDouble* row=NULL;
|
---|
[4039] | 73 |
|
---|
| 74 | /*point to the start of the row: */
|
---|
| 75 | row=this->values+count*this->numcols;
|
---|
| 76 |
|
---|
| 77 | /*Let's remember what is on a row:
|
---|
| 78 | enum_type step time element_id interpolation vertices_ids nodal_values
|
---|
| 79 | */
|
---|
| 80 | row[3]=element_id;
|
---|
| 81 | for(i=0;i<num_vertices;i++){
|
---|
| 82 | row[5+i]=vertices_ids[i];
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | }
|
---|
| 86 | /*}}}*/
|
---|
[12365] | 87 | /*FUNCTION Patch::fillresultinfo{{{*/
|
---|
[12366] | 88 | void Patch::fillresultinfo(int count,int enum_type,int step, IssmDouble time, int interpolation, IssmDouble* nodal_values, int num_nodes){
|
---|
[4039] | 89 |
|
---|
| 90 | int i;
|
---|
[12366] | 91 | IssmDouble* row=NULL;
|
---|
[4039] | 92 |
|
---|
| 93 | /*point to the start of the row: */
|
---|
| 94 | row=this->values+count*this->numcols;
|
---|
| 95 |
|
---|
| 96 | /*Let's remember what is on a row:
|
---|
[13975] | 97 | enum_type step time element_id interpolation vertices_ids nodal_values */
|
---|
[4039] | 98 | row[0]=enum_type;
|
---|
[12366] | 99 | row[1]=(IssmDouble)step;
|
---|
[4039] | 100 | row[2]=time;
|
---|
| 101 | row[4]=interpolation;
|
---|
| 102 | for(i=0;i<num_nodes;i++){
|
---|
| 103 | row[5+this->maxvertices+i]=nodal_values[i];
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | }
|
---|
| 107 | /*}}}*/
|
---|
[12365] | 108 | /*FUNCTION Patch::Gather{{{*/
|
---|
[6851] | 109 | void Patch::Gather(void){
|
---|
[4039] | 110 |
|
---|
[4057] | 111 | int count;
|
---|
[13975] | 112 | int my_rank;
|
---|
| 113 | int num_procs;
|
---|
[4039] | 114 | int total_numrows;
|
---|
| 115 | int node_numrows;
|
---|
[12366] | 116 | IssmDouble *total_values = NULL;
|
---|
[12102] | 117 | #ifdef _HAVE_MPI_
|
---|
[4057] | 118 | MPI_Status status;
|
---|
[12102] | 119 | #endif
|
---|
[4039] | 120 |
|
---|
[13975] | 121 | /*recover my_rank:*/
|
---|
| 122 | my_rank=IssmComm::GetRank();
|
---|
| 123 | num_procs=IssmComm::GetSize();
|
---|
| 124 |
|
---|
[4039] | 125 | /*First, figure out total number of rows combining all the cpus: */
|
---|
[12102] | 126 | #ifdef _HAVE_MPI_
|
---|
[13975] | 127 | MPI_Reduce(&this->numrows,&total_numrows,1,MPI_INT,MPI_SUM,0,IssmComm::GetComm() );
|
---|
| 128 | MPI_Bcast(&total_numrows,1,MPI_INT,0,IssmComm::GetComm());
|
---|
[12102] | 129 | #else
|
---|
| 130 | total_numrows=this->numrows;
|
---|
| 131 | #endif
|
---|
[4039] | 132 |
|
---|
[4873] | 133 | /*return if patch empty*/
|
---|
| 134 | if(total_numrows==0) return;
|
---|
| 135 |
|
---|
[4039] | 136 | /*Now, allocate buffer to holds all the values, on node 0: */
|
---|
[12366] | 137 | if(my_rank==0)total_values=xNew<IssmDouble>(this->numcols*total_numrows);
|
---|
[4039] | 138 |
|
---|
| 139 | /*Start by copying node 0 values onto total_values: */
|
---|
| 140 | if(my_rank==0){
|
---|
| 141 | count=0;
|
---|
[12366] | 142 | xMemCpy<IssmDouble>(total_values+count,this->values,this->numcols*this->numrows);
|
---|
[4039] | 143 | count+=this->numrows*this->numcols;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | /*Now, ask other nodes to send their values: */
|
---|
[12102] | 147 | #ifdef _HAVE_MPI_
|
---|
[13975] | 148 | for(int i=1;i<num_procs;i++){
|
---|
[4039] | 149 | if (my_rank==i){
|
---|
[13975] | 150 | MPI_Send(&this->numrows,1,MPI_INT,0,1,IssmComm::GetComm());
|
---|
| 151 | if (this->numrows)MPI_Send(this->values,this->numrows*this->numcols,MPI_DOUBLE,0,1,IssmComm::GetComm());
|
---|
[4039] | 152 | }
|
---|
| 153 | if (my_rank==0){
|
---|
[13975] | 154 | MPI_Recv(&node_numrows,1,MPI_INT,i,1,IssmComm::GetComm(),&status);
|
---|
| 155 | if (node_numrows)MPI_Recv(total_values+count,node_numrows*this->numcols,MPI_DOUBLE,i,1,IssmComm::GetComm(),&status);
|
---|
[4039] | 156 | count+=node_numrows*this->numcols;
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
[12102] | 159 | #endif
|
---|
[4039] | 160 |
|
---|
| 161 | /*Now, node 0 has total_values, of size total_numrows*this->numcols. Update the fields in the patch, to reflect this new
|
---|
| 162 | * reality. For other cpus, no point in keeping their data anymore: */
|
---|
| 163 | if(my_rank==0){
|
---|
| 164 | this->numrows=total_numrows;
|
---|
[12366] | 165 | xDelete<IssmDouble>(this->values);
|
---|
[4039] | 166 | this->values=total_values;
|
---|
| 167 | }
|
---|
[12102] | 168 | #ifdef _HAVE_MPI_
|
---|
[4039] | 169 | else{
|
---|
| 170 | this->numrows=0;
|
---|
[12366] | 171 | xDelete<IssmDouble>(this->values);
|
---|
[4039] | 172 | }
|
---|
[12102] | 173 | #endif
|
---|
[4546] | 174 | }/*}}}*/
|
---|