Variable* Mesh_GenerateElGlobalIdVar( void* mesh ) { /* Returns a Variable that stores the global element indices. * Assumes the number of mesh elements never changes. */ Mesh* self = (Mesh*)mesh; char* name; unsigned dim; // if variable already exists return it if( self->eGlobalIdsVar ) return self->eGlobalIdsVar; dim = Mesh_GetDimSize(mesh); // Create the Variable data structure, int[local node count] self->lEls = Mesh_GetLocalSize( self, dim ); self->elgid = Memory_Alloc_Array( int, self->lEls, "Mesh::vertsgid" ); Stg_asprintf( &name, "%s-%s", self->name, "verticesGlobalIds" ); self->eGlobalIdsVar = Variable_NewScalar( name, NULL, Variable_DataType_Int, &self->lEls, NULL, (void**)&self->elgid, NULL ); Stg_Component_Build(self->eGlobalIdsVar, NULL, False); Stg_Component_Initialise(self->eGlobalIdsVar, NULL, False); free(name); // Evaluate the global indices for the local nodes int ii, gid; for( ii=0; ii<self->lEls; ii++ ) { gid = Mesh_DomainToGlobal( self, dim, ii ); Variable_SetValue( self->eGlobalIdsVar, ii, (void*)&gid ); } // return new variable return self->eGlobalIdsVar; }
//----------------------------------------------------------------------------- // Variable_InternalInit() // Internal method of initializing a new variable. //----------------------------------------------------------------------------- static int Variable_InternalInit( udt_Variable *self, // variable to initialize unsigned numElements, // number of elements to allocate udt_VariableType *type, // variable type SQLUINTEGER size, // size of variable SQLSMALLINT scale, // scale of variable PyObject *value, // value to set (optional) int input, // input variable? int output) // output variable? { unsigned PY_LONG_LONG dataLength; SQLUINTEGER i; // perform basic initialization self->position = -1; if (numElements < 1) self->numElements = 1; else self->numElements = numElements; self->size = size; if (type->getBufferSizeProc) self->bufferSize = (*type->getBufferSizeProc)(self, size); else self->bufferSize = type->bufferSize; self->scale = scale; self->type = type; self->input = input; self->output = output; self->lengthOrIndicator = NULL; self->data = NULL; self->inConverter = NULL; self->outConverter = NULL; // allocate the indicator and data arrays dataLength = (unsigned PY_LONG_LONG) numElements * (unsigned PY_LONG_LONG) self->bufferSize; if (dataLength > INT_MAX) { PyErr_SetString(PyExc_ValueError, "array size too large"); return -1; } self->lengthOrIndicator = PyMem_Malloc(numElements * sizeof(SQLLEN)); self->data = PyMem_Malloc((size_t) dataLength); if (!self->lengthOrIndicator || !self->data) { PyErr_NoMemory(); return -1; } // ensure that all variable values start out NULL for (i = 0; i < numElements; i++) self->lengthOrIndicator[i] = SQL_NULL_DATA; // set value, if applicable if (value && Variable_SetValue(self, 0, value) < 0) return -1; return 0; }
//----------------------------------------------------------------------------- // Variable_ExternalSetValue() // Set the value of the variable at the given position. //----------------------------------------------------------------------------- static PyObject *Variable_ExternalSetValue( udt_Variable *self, // variable to set value for PyObject *args) // arguments { PyObject *value; unsigned pos; if (!PyArg_ParseTuple(args, "iO", &pos, &value)) return NULL; if (Variable_SetValue(self, pos, value) < 0) return NULL; Py_INCREF(Py_None); return Py_None; }
void VariableCondition_ApplyToIndexVariable( void* variableCondition, Index localIndex, VariableCondition_VariableIndex varIndex, void* context ) { VariableCondition* self = (VariableCondition*)variableCondition; Variable_Index globalVarIndex; Variable* var; ConditionFunction* cf; Index index; /* * NOTE: This algorithm here is RIDICULOUSLY slow. I've added a mapping * to the class, that should help. for (index = 0; index < self->indexCount; index++) if (self->indexTbl[index] == localIndex) break; */ if(!UIntMap_Map( self->mapping, localIndex, &index )) return; globalVarIndex = self->vcTbl[index][varIndex].varIndex; var = self->variable_Register->_variable[globalVarIndex]; switch (self->valueTbl[self->vcTbl[index][varIndex].valIndex].type) { case VC_ValueType_Double: Variable_SetValueDouble( var, self->indexTbl[index], self->valueTbl[self->vcTbl[index][varIndex].valIndex].as.typeDouble ); break; case VC_ValueType_CFIndex: cf = self->conFunc_Register->_cf[self->valueTbl[self->vcTbl[index][varIndex].valIndex].as.typeCFIndex]; ConditionFunction_Apply( cf, localIndex, globalVarIndex, context, Variable_GetStructPtr( var, self->indexTbl[index]) ); break; case VC_ValueType_DoubleArray: Variable_SetValue( var, self->indexTbl[index], self->valueTbl[self->vcTbl[index][varIndex].valIndex].as.typeArray.array ); break; case VC_ValueType_Int: Variable_SetValueInt( var, self->indexTbl[index], self->valueTbl[self->vcTbl[index][varIndex].valIndex].as.typeInt ); break; case VC_ValueType_Short: Variable_SetValueShort( var, self->indexTbl[index], self->valueTbl[self->vcTbl[index][varIndex].valIndex].as.typeShort ); break; case VC_ValueType_Char: Variable_SetValueChar( var, self->indexTbl[index], self->valueTbl[self->vcTbl[index][varIndex].valIndex].as.typeChar ); break; case VC_ValueType_Ptr: Variable_SetValuePointer( var, self->indexTbl[index], self->valueTbl[self->vcTbl[index][varIndex].valIndex].as.typePtr ); break; default: assert(0); break; } }
void VariableCondition_ApplyToIndex( void* variableCondition, Index localIndex, void* context ) { VariableCondition* self = (VariableCondition*)variableCondition; Variable* var; Variable_Index varIndex; VariableCondition_ValueIndex val_I; ConditionFunction* cf; Index index, i; Stream* errorStr = Journal_Register( Error_Type, self->type ); /* Ensure that the index provided (localIndex) has a condition attached to it */ insist( UIntMap_Map( self->mapping, localIndex, &index ), == True ); /* For each variable that has a condition at this index */ for (i = 0; i < self->vcVarCountTbl[index]; i++) { varIndex = self->vcTbl[index][i].varIndex; assert( varIndex != (unsigned)-1 ); var = self->variable_Register->_variable[varIndex]; val_I = self->vcTbl[index][i].valIndex; assert( val_I != (unsigned)-1 ); switch (self->valueTbl[val_I].type) { case VC_ValueType_Double: Journal_Firewall( var->dataTypeCounts[0] == 1, errorStr, "Error - in %s: while applying values for variable condition " "\"%s\", to index %d - asked to apply a scalar %s to Variable \"%s\" " "which has %d components. Specify a scalar Variable instead.\n", __func__, self->name, self->indexTbl[index], "double", var->name, var->dataTypeCounts[0] ); Variable_SetValueDouble( var, self->indexTbl[index], self->valueTbl[val_I].as.typeDouble ); break; case VC_ValueType_DoubleArray: Variable_SetValue( var, self->indexTbl[index], self->valueTbl[val_I].as.typeArray.array ); break; case VC_ValueType_CFIndex: Journal_Firewall( self->valueTbl[val_I].as.typeCFIndex != (unsigned)-1, errorStr, "Error - in %s: trying to apply to index %d of variable \"%s\", which " "is supposed to be a condition function, but the cond. func. wasn't " "found in the c.f. register.\n", __func__, localIndex, var->name ); cf = self->conFunc_Register->_cf[self->valueTbl[val_I].as.typeCFIndex]; ConditionFunction_Apply( cf, localIndex, varIndex, context, Variable_GetStructPtr( var, self->indexTbl[index] ) ); break; case VC_ValueType_Int: Journal_Firewall( var->dataTypeCounts[0] == 1, errorStr, "Error - in %s: while applying values for variable condition " "\"%s\", to index %d - asked to apply a scalar %s to Variable \"%s\" " "which has %d components. Specify a scalar Variable instead.\n", __func__, self->name, self->indexTbl[index], "int", var->name, var->dataTypeCounts[0] ); Variable_SetValueInt( var, self->indexTbl[index], self->valueTbl[val_I].as.typeInt ); break; case VC_ValueType_Short: Journal_Firewall( var->dataTypeCounts[0] == 1, errorStr, "Error - in %s: while applying values for variable condition " "\"%s\", to index %d - asked to apply a scalar %s to Variable \"%s\" " "which has %d components. Specify a scalar Variable instead.\n", __func__, self->name, self->indexTbl[index], "short", var->name, var->dataTypeCounts[0] ); Variable_SetValueShort( var, self->indexTbl[index], self->valueTbl[val_I].as.typeShort ); break; case VC_ValueType_Char: Journal_Firewall( var->dataTypeCounts[0] == 1, errorStr, "Error - in %s: while applying values for variable condition " "\"%s\", to index %d - asked to apply a scalar %s to Variable \"%s\" " "which has %d components. Specify a scalar Variable instead.\n", __func__, self->name, self->indexTbl[index], "char", var->name, var->dataTypeCounts[0] ); Variable_SetValueChar( var, self->indexTbl[index], self->valueTbl[val_I].as.typeChar ); break; case VC_ValueType_Ptr: Variable_SetValuePointer( var, self->indexTbl[index], self->valueTbl[val_I].as.typePtr ); break; default: assert(0); break; } } }
Variable* Mesh_GenerateENMapVar( void* mesh ) { /* Returns a Variable that stores the mapping of * [local element] [global node indices] * Assumes the mapping never changes. */ Mesh* self = (Mesh*)mesh; char* name; int n_i, e_i, nNbr, localElements, localtotal; unsigned buffy_tvs; // buffer for global node indices unsigned dim, *nbr, temp; int *numberNodesPerEl = NULL; IArray* inc = NULL; Stream* error = Journal_Register( Error_Type, (Name)self->type ); // if variable already exists return it if( self->enMapVar ) return self->enMapVar; /* go over local elementNode map to get size of data */ inc = IArray_New( ); self->localtotalNodes=0; dim = Mesh_GetDimSize( self ); localElements = Mesh_GetLocalSize( self, dim ); numberNodesPerEl = Memory_Alloc_Array( int, localElements, "Mesh::numberNodesPerEl" ); for( e_i=0 ; e_i < localElements; e_i++ ) { Mesh_GetIncidence( self, dim, (unsigned)e_i, MT_VERTEX, inc ); nNbr = IArray_GetSize( inc ); nbr = IArray_GetPtr( inc ); numberNodesPerEl[e_i] = nNbr; self->localtotalNodes += nNbr; } /* Create the Variable data structure, int[nbrNodesPerEl*local element count] * Note: this is stored in a 1D array - so whatever read or writes to this variable * needs to know how to parse it. */ self->e_n = Memory_Alloc_Array( int, self->localtotalNodes, "Mesh::nodeConn" ); Stg_asprintf( &name, "%s-%s", self->name, "nodeConn" ); self->enMapVar = Variable_NewScalar( name, NULL, Variable_DataType_Int, &self->localtotalNodes, NULL, (void**)&self->e_n, NULL ); Stg_Component_Build(self->enMapVar, NULL, False); Stg_Component_Initialise(self->enMapVar, NULL, False); free(numberNodesPerEl); free(name); // Evaluate the global indices for the local nodes localtotal=0; for( e_i=0; e_i<localElements; e_i++ ) { Mesh_GetIncidence( self, dim, (unsigned)e_i, MT_VERTEX, inc ); nNbr = IArray_GetSize( inc ); nbr = IArray_GetPtr( inc ); for( n_i=0; n_i< nNbr; n_i++ ) { buffy_tvs = Mesh_DomainToGlobal( self, MT_VERTEX, nbr[n_i] ); Variable_SetValue( self->enMapVar, localtotal, (void*)&buffy_tvs ); localtotal++; } } Stg_Class_Delete( inc ); // return new variable return self->enMapVar; }