void FieldBaseImpl::insert_restriction( const char * arg_method , EntityRank arg_entity_rank , const Selector & arg_selector , const unsigned * arg_stride, const void* arg_init_value ) { TraceIfWatching("stk::mesh::impl::FieldBaseImpl::insert_restriction", LOG_FIELD, m_ordinal); FieldRestriction tmp( arg_entity_rank , arg_selector ); { unsigned i = 0 ; if ( m_field_rank ) { for ( i = 0 ; i < m_field_rank ; ++i ) { tmp.stride(i) = arg_stride[i] ; } } else { // Scalar field is 0 == m_field_rank i = 1 ; tmp.stride(0) = 1 ; } // Remaining dimensions are 1, no change to stride for ( ; i < MaximumFieldDimension ; ++i ) { tmp.stride(i) = tmp.stride(i-1) ; } for ( i = 1 ; i < m_field_rank ; ++i ) { const bool bad_stride = 0 == tmp.stride(i) || 0 != tmp.stride(i) % tmp.stride(i-1); ThrowErrorMsgIf( bad_stride, arg_method << " FAILED for " << *this << " WITH BAD STRIDE!"); } } if (arg_init_value != NULL) { //insert_restriction can be called multiple times for the same field, giving //the field different lengths on different mesh-parts. //We will only store one initial-value array, we need to store the one with //maximum length for this field so that it can be used to initialize data //for all field-restrictions. For the parts on which the field is shorter, //a subset of the initial-value array will be used. // //We want to end up storing the longest arg_init_value array for this field. // //Thus, we call set_initial_value only if the current length is longer //than what's already been stored. //length in bytes is num-scalars X sizeof-scalar: size_t num_scalars = 1; //if rank > 0, then field is not a scalar field, so num-scalars is //obtained from the stride array: if (m_field_rank > 0) num_scalars = tmp.stride(m_field_rank-1); size_t sizeof_scalar = m_data_traits.size_of; size_t nbytes = sizeof_scalar * num_scalars; size_t old_nbytes = 0; if (get_initial_value() != NULL) { old_nbytes = get_initial_value_num_bytes(); } if (nbytes > old_nbytes) { set_initial_value(arg_init_value, num_scalars, nbytes); } } { FieldRestrictionVector & srvec = selector_restrictions(); bool restriction_already_exists = false; for(FieldRestrictionVector::const_iterator it=srvec.begin(), it_end=srvec.end(); it!=it_end; ++it) { if (tmp == *it) { restriction_already_exists = true; if (tmp.not_equal_stride(*it)) { ThrowErrorMsg("Incompatible selector field-restrictions!"); } } } if ( !restriction_already_exists ) { // New field restriction, verify we are not committed: ThrowRequireMsg(!m_meta_data->is_commit(), "mesh MetaData has been committed."); srvec.push_back( tmp ); } } }
int main(int argc, char *argv[]) { int n=get_num_ind(); int i,j; struct timeval tv1,tv2; adouble *xad; adouble fad; double f; double *x; x=new double[n]; xad=new adouble[n]; get_initial_value(x); printf("evaluating the function..."); trace_on(tag); for(i=0;i<n;i++) { xad[i] <<= x[i]; } fad=func_eval(xad); fad >>= f; trace_off(); printf("done!\n"); // printf("function value =<%10.20f>\n",f); // function(tag,1,n,x,&f); // printf("adolc func value=<%10.20f>\n",f); //tape_doc(tag,1,n,x,&f); #ifdef _compare_with_full double **H; H = myalloc2(n,n); printf("computing full hessain...."); gettimeofday(&tv1,NULL); hessian(tag,n,x,H); printf("done\n"); gettimeofday(&tv2,NULL); printf("Computing the full hessian cost %10.6f seconds\n",(tv2.tv_sec-tv1.tv_sec)+(double)(tv2.tv_usec-tv1.tv_usec)/1000000); #ifdef _PRINTOUT for(i=0;i<n;i++){ for(j=0;j<n;j++){ printf("H[%d][%d]=<%10.10f>",i,j,H[i][j]); } printf("\n"); } printf("\n"); #endif #endif #ifdef edge_pushing unsigned int *rind = NULL; unsigned int *cind = NULL; double *values = NULL; int nnz; int options[2]; options[0]=PRE_ACC; options[1]=COMPUT_GRAPH; gettimeofday(&tv1,NULL); // edge_hess(tag, 1, n, x, &nnz, &rind, &cind, &values, options); sparse_hess(tag,n,0,x, &nnz, &rind, &cind, &values, options); gettimeofday(&tv2,NULL); printf("Sparse Hessian: edge pushing cost %10.6f seconds\n",(tv2.tv_sec-tv1.tv_sec)+(double)(tv2.tv_usec-tv1.tv_usec)/1000000); #ifdef _PRINTOUT for(i=0;i<nnz;i++){ printf("<%d,%d>:<%10.10f>\n",cind[i],rind[i],values[i]); // printf("%d %d \n", rind[i], cind[i]); } #endif #endif #ifdef _compare_with_full #ifdef edge_pushing compare_matrix(n,H,nnz,cind,rind,values); #endif myfree2(H); #endif #ifdef edge_pushing printf("nnz=%d\n", nnz); free(rind); rind=NULL; free(cind); cind=NULL; free(values); values=NULL; #endif delete[] x; delete[] xad; return 0; }
void FieldBaseImpl::insert_restriction( const char * arg_method , EntityRank arg_entity_rank , const Part & arg_part , const unsigned * arg_stride, const void* arg_init_value ) { TraceIfWatching("stk::mesh::impl::FieldBaseImpl::insert_restriction", LOG_FIELD, m_ordinal); FieldRestriction tmp( arg_entity_rank , arg_part.mesh_meta_data_ordinal() ); { unsigned i = 0 ; if ( m_field_rank ) { for ( i = 0 ; i < m_field_rank ; ++i ) { tmp.stride(i) = arg_stride[i] ; } } else { // Scalar field is 0 == m_field_rank i = 1 ; tmp.stride(0) = 1 ; } // Remaining dimensions are 1, no change to stride for ( ; i < MaximumFieldDimension ; ++i ) { tmp.stride(i) = tmp.stride(i-1) ; } for ( i = 1 ; i < m_field_rank ; ++i ) { const bool bad_stride = 0 == tmp.stride(i) || 0 != tmp.stride(i) % tmp.stride(i-1); ThrowErrorMsgIf( bad_stride, arg_method << " FAILED for " << *this << " WITH BAD STRIDE " << print_restriction( tmp, arg_entity_rank, arg_part, m_field_rank ));; } } if (arg_init_value != NULL) { //insert_restriction can be called multiple times for the same field, giving //the field different lengths on different mesh-parts. //We will only store one initial-value array, we need to store the one with //maximum length for this field so that it can be used to initialize data //for all field-restrictions. For the parts on which the field is shorter, //a subset of the initial-value array will be used. // //We want to end up storing the longest arg_init_value array for this field. // //Thus, we call set_initial_value only if the current length is longer //than what's already been stored. //length in bytes is num-scalars X sizeof-scalar: size_t num_scalars = 1; //if rank > 0, then field is not a scalar field, so num-scalars is //obtained from the stride array: if (m_field_rank > 0) num_scalars = tmp.stride(m_field_rank-1); size_t sizeof_scalar = m_data_traits.size_of; size_t nbytes = sizeof_scalar * num_scalars; size_t old_nbytes = 0; if (get_initial_value() != NULL) { old_nbytes = get_initial_value_num_bytes(); } if (nbytes > old_nbytes) { set_initial_value(arg_init_value, num_scalars, nbytes); } } { FieldRestrictionVector & restrs = restrictions(); FieldRestrictionVector::iterator restr = restrs.begin(); FieldRestrictionVector::iterator last_restriction = restrs.end(); restr = std::lower_bound(restr,last_restriction,tmp); const bool new_restriction = ( ( restr == last_restriction ) || !(*restr == tmp) ); if ( new_restriction ) { // New field restriction, verify we are not committed: ThrowRequireMsg(!m_meta_data->is_commit(), "mesh MetaData has been committed."); unsigned num_subsets = 0; for(FieldRestrictionVector::iterator i=restrs.begin(), iend=restrs.end(); i!=iend; ++i) { if (i->entity_rank() != arg_entity_rank) continue; const Part& partI = *m_meta_data->get_parts()[i->part_ordinal()]; bool found_subset = contain(arg_part.subsets(), partI); if (found_subset) { ThrowErrorMsgIf( i->not_equal_stride(tmp), arg_method << " FAILED for " << *this << " " << print_restriction( *i, arg_entity_rank, arg_part, m_field_rank ) << " WITH INCOMPATIBLE REDECLARATION " << print_restriction( tmp, arg_entity_rank, arg_part, m_field_rank )); *i = tmp; ++num_subsets; } bool found_superset = contain(arg_part.supersets(), partI); if (found_superset) { ThrowErrorMsgIf( i->not_equal_stride(tmp), arg_method << " FAILED for " << *this << " " << print_restriction( *i, arg_entity_rank, arg_part, m_field_rank ) << " WITH INCOMPATIBLE REDECLARATION " << print_restriction( tmp, arg_entity_rank, arg_part, m_field_rank )); //if there's already a restriction for a superset of this part, then //there's nothing to do and we're out of here.. return; } } if (num_subsets == 0) { restrs.insert( restr , tmp ); } else { //if subsets were found, we replaced them with the new restriction. so now we need //to sort and unique the vector, and trim it to remove any duplicates: std::sort(restrs.begin(), restrs.end()); FieldRestrictionVector::iterator it = std::unique(restrs.begin(), restrs.end()); restrs.resize(it - restrs.begin()); } } else { ThrowErrorMsgIf( restr->not_equal_stride(tmp), arg_method << " FAILED for " << *this << " " << print_restriction( *restr, arg_entity_rank, arg_part, m_field_rank ) << " WITH INCOMPATIBLE REDECLARATION " << print_restriction( tmp, arg_entity_rank, arg_part, m_field_rank )); } } }
/*! * @brief OLED显示 * @since v1.0 * @note 没啥 * Sample usage: oled_display(); */ void oled_display() { //第一行显示拨码开关状态 OLED_P6x8Str(0, 0, "12345678"); OLED_P6x8Str(50, 0, "speed:"); Display_number(86, 0, vPID.Setpoint); OLED_P6x8Str(50, 1, "Error:"); Display_number(86, 1, Error); OLED_P6x8Str(0, 2, "slope:"); DisplayFloat(36, 2, slope.slope); OLED_P6x8Str(80, 2, "P:"); DisplayFloatpid(92, 2, actuator_P); OLED_P6x8Str(0, 3, "Distance:"); Display_number(54, 3, distance); OLED_P6x8Str(86, 3, "D:"); DisplayFloatpid(98, 3, actuator_D); OLED_P6x8Str(0, 4, "Bluetooth:"); if(bluetooth == 1) OLED_P6x8Str(60, 4, "connected "); else if(bluetooth == 0) OLED_P6x8Str(60, 4, "disconnect"); else OLED_P6x8Str(60, 4, "off "); OLED_P6x8Str(0, 5, "stop?"); if(stop_done) OLED_P6x8Str(36, 5, "yes"); else OLED_P6x8Str(36, 5, "no"); #if ( CAR_NUMBER == 1 ) OLED_P6x8Str(0, 7, "Interesting"); OLED_P6x8Str(91, 7, "Car:1A"); #endif #if ( CAR_NUMBER == 2 ) OLED_P6x8Str(0, 7, "Interesting"); OLED_P6x8Str(91, 7, "Car:2B"); #endif if(!gpio_get (PTE1)) OLED_P6x8Str(0, 1, "^"); else OLED_P6x8Str(0, 1, " "); if(!gpio_get (PTE2)) OLED_P6x8Str(6, 1, "^"); else OLED_P6x8Str(6, 1, " "); if(!gpio_get (PTE3)) OLED_P6x8Str(12, 1, "^"); else OLED_P6x8Str(12, 1, " "); if(!gpio_get (PTE4)) OLED_P6x8Str(18, 1, "^"); else OLED_P6x8Str(18, 1, " "); if(!gpio_get (PTE5)) OLED_P6x8Str(24, 1, "^"); else OLED_P6x8Str(24, 1, " "); if(!gpio_get (PTE6)) OLED_P6x8Str(30, 1, "^"); else OLED_P6x8Str(30, 1, " "); if(!gpio_get (PTE7)) OLED_P6x8Str(36, 1, "^"); else OLED_P6x8Str(36, 1, " "); if(!gpio_get (PTE8)) OLED_P6x8Str(42, 1, "^"); else OLED_P6x8Str(42, 1, " "); if(key_check(KEY_A) == KEY_DOWN) { OLED_Init(); //OLED初始化 防花屏 } if(key_check(KEY_U) == KEY_DOWN) //按K2重新获取两边线数组,串口接收 { get_initial_value(img, &slope); printf("const int left_initial[110] ={"); for(i = 0; i < 110; i++) { printf("%d, ", slope.left_initial_value[i]); //printf("[%d]=%d\n", i, slope.right_initial_value[i]); } printf("};\nconst int right_initial[110] ={"); for(i = 0; i < 110; i++) { printf("%d, ", slope.right_initial_value[i]); //printf("[%d]=%d\n", i, slope.right_initial_value[i]); } printf("};//Pay attention to delete the last \",\"\n"); //initial_value_get=1; } }