int GradientCheckConn::calc_dW() {
   assert(plasticityFlag);
   int status;
   //for(int arborId=0;arborId<numberOfAxonalArborLists();arborId++) {
   //   status = initialize_dW(arborId);
   //   if (status==PV_BREAK) { break; }
   //   assert(status == PV_SUCCESS);
   //}
   for(int arborId=0;arborId<numberOfAxonalArborLists();arborId++) {
      status = update_dW(arborId);
      if (status==PV_BREAK) { break; }
      assert(status == PV_SUCCESS);
   }
   for(int arborId=0;arborId<numberOfAxonalArborLists();arborId++) {
      status = reduce_dW(arborId);
      if (status==PV_BREAK) { break; }
      assert(status == PV_SUCCESS);
   }
   for(int arborId=0;arborId<numberOfAxonalArborLists();arborId++) {
      status = normalize_dW(arborId);
      if (status==PV_BREAK) { break; }
      assert(status == PV_SUCCESS);
   }
   return PV_SUCCESS;
}
int MomentumConn::calc_dW() {
   assert(plasticityFlag);
   int status;
   if(timeBatchIdx >= timeBatchPeriod){
      timeBatchIdx = 0;
   }
   else{
      timeBatchIdx++;
   }

   //Clear at time 0, update at time timeBatchPeriod - 1
   bool need_update_w = false;
   bool need_clear_dw = false;
   if(timeBatchIdx == 0){
      need_clear_dw = true;
   }
   if(timeBatchIdx == timeBatchPeriod - 1){
      need_update_w = true;
   }

   for(int arborId=0;arborId<numberOfAxonalArborLists();arborId++) {
      //Clear every batch period
      if(need_clear_dw){
         status = initialize_dW(arborId);
         if (status==PV_BREAK) { break; }
         assert(status == PV_SUCCESS);
      }
   }

   for(int arborId=0;arborId<numberOfAxonalArborLists();arborId++) {
      //Sum up parts every timestep
      status = update_dW(arborId);
      if (status==PV_BREAK) { break; }
      assert(status == PV_SUCCESS);
   }

   for(int arborId=0;arborId<numberOfAxonalArborLists();arborId++) {
      //Reduce only when we need to update
      if(need_update_w){
         status = reduce_dW(arborId);
         if (status==PV_BREAK) { break; }
         assert(status == PV_SUCCESS);
      }
   }

   for(int arborId=0;arborId<numberOfAxonalArborLists();arborId++) {
      //Normalize only when reduced
      if(need_update_w){
         status = normalize_dW(arborId);
         if (status==PV_BREAK) { break; }
         assert(status == PV_SUCCESS);
      }
   }
   return PV_SUCCESS;
}