Beispiel #1
0
// this routine is called either by GLUT or directly. It manages time evolution
void idle () {
  if(sim_time>stop_time) shutdown_app(); // quit when time runs out
  if(is_step || !is_stepping) {
    is_step=false;
    double new_real = get_real_secs();
    if(is_sim_throttling) {
      // time math avoids incremental deltas to limit error accumulation
      double real_delta = new_real - last_inflection_real;
      double new_time = real_delta/time_ratio + last_inflection_sim;
      if(new_time-last_sim_time > step_size/2) {
        flo tfps = 1/(new_real-last_real); last_real = new_real;
        fps = (1-FPS_DECAY)*tfps + FPS_DECAY*fps;
        // step_size = min step
        sim_time = max(new_time, last_sim_time + step_size);
        evolution_lagging = (sim_time-last_sim_time > step_size*10);
        if(evolution_lagging) { // maximum step is 10x normal step
          sim_time = last_sim_time+(step_size*10);
        }
        advance_time(); last_sim_time=sim_time;
      }
    } else {
      flo tfps = 1/(new_real-last_real); last_real = new_real;
      fps = (1-FPS_DECAY)*tfps + FPS_DECAY*fps;
      sim_time+=step_size; evolution_lagging=false;
      advance_time(); last_sim_time=sim_time;
    }
    
  }
}
Beispiel #2
0
void qss2::dext(Event x, double t) {
//The input event is in the 'x' variable.
//where:
//     'x.value' is the value
//     'x.port' is the port number
double *derx;
double diffxq[10];
double dt1;

derx=(double*)x.value;
if (x.port==0) {
 X[0]=evaluate_poly(X,e,2);
 X[1]=derx[0];
 X[2]=derx[1]/2;
 if (sigma>0){
	advance_time(q,e,1);
	diffxq[1]=q[1]-X[1];
	diffxq[2]=-X[2];
	diffxq[0]=q[0]-X[0]-dQ;
	sigma=minposroot(diffxq,2);
	diffxq[0]=q[0]-X[0]+dQ;
	dt1=minposroot(diffxq,2);
	if (dt1<sigma) sigma=dt1;
	if (fabs(X[0]-q[0])>dQ) sigma=0;
 }	
} else {
	advance_time(X,e,2);
	X[0]=derx[0];
	sigma=0;
}
}
Beispiel #3
0
void qss2::dint(double t) {
  advance_time(X,sigma,2);
	q[0]=X[0];
	q[1]=X[1];
	dQ=dQrel*fabs(X[0]);
	if (dQ<dQmin)dQ=dQmin;
	if (X[2]==0){
		sigma=INF;
	} else {
		sigma=sqrt(fabs(dQ/X[2]));
	}
}
Beispiel #4
0
/* The following function is major function which makes scheduling. */
void scheduler_state::schedule_estimated_basic_block (void)
{
  dependence_graph_node_t current_node;

  while (last_result_list_node != graph_bottom)
    {
      while (data_ready_list->head () == NULL || scheduler::is_dead_lock ())
        advance_time ();
      for (current_node = data_ready_list->head ();
           current_node != NULL;
           current_node = current_node->next_list_node)
        if (current_node->corresponding_instruction == NULL
            || scheduler::transition (current_node->corresponding_instruction))
          {
            data_ready_list->remove (current_node);
            add_node_to_result_list (current_node);
            break;
          }
      if (current_node == NULL)
        advance_time ();
    }
}
Beispiel #5
0
Event qss2::lambda(double t) {
//This function return an event:
//     Event(%&Value%, %NroPort%)
//where:
//     %&Value% is a direction to the variable that contain the value.
//     %NroPort% is the port number (from 0 to n-1)


y[0]=X[0];
y[1]=X[1];
y[2]=X[2];     
advance_time(y,sigma,2);
y[2]=0;
return Event(&y,0);
}
Beispiel #6
0
int main()
{
	// Simulate the TTC!
	// To keep things consitent, we'll use a predefined seed of 1
	srand(1);

	// we'll be outputting data to a file called data.csv
	FILE *file;
	file = fopen("data.csv","w+");

	// set up our first station and first two trains 
	struct station *kipling = read_stations();
	struct train *first = make_train(5, 0);
	struct train *second = make_train(0, 5);
	first->next = second;

	if(DEBUGGING == 1)
	{
		printf("BEGINNING TEST\n");
		test_cases_for_students(kipling, first);
	}
	else
	{
		// if we're not debugging, then simulate!
		fprintf(file, "time, avg_wait, num_trains, avg_dist\n");
		int i = 0;
		for(i = 0; i < SIM_TIME; i++)
		{
			advance_time(kipling, &first);
			// print data every half hour
			if(i % 30 == 0)
			{
				fprintf(file, "%d, %lf, %d, %lf\n", i, average_wait_time(kipling), num_trains(first), avg_train_dist(first));
			}
		}

		print_track(kipling, &first, i, 1);
	}

	// cleanup
	clear_all_trains(&first);
	remove_all_stations(&kipling);
	fclose(file);
	return 0;
}
Beispiel #7
0
int main(int argc, char *argv[]) {
  int cycle;

  parse_args(argc, argv);
  init_system();
  
  for (cycle = 0; cycle < args.num_cycles; ++cycle) {
    advance_time();
  }

  for (int i = 0; i < args.num_procs; ++i) {
    caches[i]->print_stats();
    ius[i]->print_stats();
  }

  finish_test();
  
}
Beispiel #8
0
// Handle trigger requests from the sim module ("sim_exec.trigger")
static void trigger_cb (flux_t *h,
                        flux_msg_handler_t *w,
                        const flux_msg_t *msg,
                        void *arg)
{
    json_t *o = NULL;
    const char *json_str = NULL;
    double next_termination = -1;
    zhash_t *job_hash = NULL;
    ctx_t *ctx = (ctx_t *)arg;

    if (flux_msg_get_string (msg, &json_str) < 0 || json_str == NULL
        || !(o = Jfromstr (json_str))) {
        flux_log (h, LOG_ERR, "%s: bad message", __FUNCTION__);
        return;
    }

    // Logging
    flux_log (h,
              LOG_DEBUG,
              "received a trigger (sim_exec.trigger: %s",
              json_str);

    // Handle the trigger
    ctx->sim_state = json_to_sim_state (o);
    handle_queued_events (ctx);
#if SIMEXEC_IO
    job_hash = determine_all_min_bandwidth (ctx->rdl, ctx->running_jobs);
#endif
    advance_time (ctx, job_hash);
    handle_completed_jobs (ctx);
    next_termination =
        determine_next_termination (ctx, ctx->sim_state->sim_time, job_hash);
    set_event_timer (ctx, "sim_exec", next_termination);
    send_reply_request (h, module_name, ctx->sim_state);

    // Cleanup
    free_simstate (ctx->sim_state);
    ctx->sim_state = NULL;
    Jput (o);
    zhash_destroy (&job_hash);
}
Beispiel #9
0
void liqss3::dext(Event x, double t) {
//The input event is in the 'x' variable.
//where:
//     'x.value' is the value
//     'x.port' is the port number
double *derx;
double diffxq[10];
double dt1,dt2,dt3;

derx=(double*)x.value;

if (x.port==0) { 
	//linear model estimation
		if((e>0)||(t==0)){
			band=false;
			band3=false;
		}
		if (band&&fabs(q_old+dq_old-q[0]-dq)>1e-12){
			a=(dx_old-derx[0])/(q_old+dq_old-q[0]-dq);
		       	if ((a<-1e30)||(a>0)){a=0;}
	  	} else {
			advance_time(q,e,2);
		};
		//printLog("en t=%g estimamos a=%g\n",t,a);
		u[0]=derx[0]-a*(q[0]+dq);
		u[1]=derx[1]-a*q[1];
		u[2]=derx[2]-a*q[2];

		X[0]=X[0]+X[1]*e+X[2]*e*e+X[3]*e*e*e;
	  X[1]=derx[0];
		X[2]=derx[1]/2;
		X[3]=derx[2]/3;
		//printLog("t=%g: e=%g, X=[%g, %g, %g, %g], q= [%g, %g, %g], u=[%g ,%g, %g], a=%g \n",t,e,X[0],X[1],X[2],X[3],q[0],q[1],q[2],u[0],u[1],u[2],a);
		if (band4){
		 //printLog("t=%g: e=%g, X=[%g, %g, %g, %g], q= [%g, %g, %g], u=[%g ,%g, %g], a=%g \n",t,e,X[0],X[1],X[2],X[3],q[0],q[1],q[2],u[0],u[1],u[2],a);
		 sigma=0;
		}
		if (sigma>0){
		  	diffxq[1]=q[1]-X[1];
			  diffxq[2]=q[2]-X[2];
			  diffxq[3]=-X[3];
	   		diffxq[0]=q[0]-X[0]-dQ;
		   	sigma=minposroot(diffxq,3);
	   		diffxq[0]=q[0]-X[0]+dQ;
		   	dt1=minposroot(diffxq,3);
		   	if (dt1<sigma) sigma=dt1;
				if (dt1!=sigma) {diffxq[0]=q[0]-X[0]-dQ;}
		 	  if (a!=0&&(fabs(X[3])>1e-10)&&!band3&&!band2){
					double diff1[10];
					diff1[0]=a*a*a*(q[0]+dq)+a*a*u[0]+a*u[1]+2*u[2];
					diff1[1]=a*a*a*q[1]+a*a*u[1]+a*2*u[2];
					diff1[2]=a*a*a*q[2]+a*a*u[2];
					dt3=minposroot(diff1,2);
					if (dt3<sigma){
						band2=true;				
						sigma=dt3;
					} else {
						band2=false;
					}
				}
  		//if (sigma==0)printLog("t=%g: e=%g, X=[%g, %g, %g, %g], q= [%g, %g, %g], u=[%g ,%g, %g], a=%g \n",t,e,X[0],X[1],X[2],X[3],q[0],q[1],q[2],u[0],u[1],u[2],a);
				if (sigma>getFinalTime())sigma=getFinalTime();
		  	advance_time(diffxq,sigma/2,3);
		  	if (fabs(diffxq[0])>3*dQ) {
					sigma=1e-12;	
		 	}
	
		};
} else {
	advance_time(X,e,3);
	X[0]=derx[0];
	sigma=0;
	band2=false;
  band=false;
  band3=false;
  band4=false;
  a=0;
}
}
Beispiel #10
0
Event liqss3::lambda(double t) {
//This function return an event:
//     Event(%&Value%, %NroPort%)
//where:
//     %&Value% is a direction to the variable that contain the value.
//     %NroPort% is the port number (from 0 to n-1)
double ddx_est;
	band3=false;
 	advance_time(q,sigma,2);
	q_old=q[0];
	dq_old=dq;
	advance_time(u,sigma,2);
	advance_time(X,sigma,3);
	dx_old=X[1];
	//printLog("\n\n Lambda: t=%g: e=%g, X=[%g, %g, %g, %g], q= [%g, %g, %g], u=[%g ,%g, %g], a=%g \n",t,e,X[0],X[1],X[2],X[3],q[0],q[1],q[2],u[0],u[1],u[2],a);
	if (!band2) {
	  	q[0]=X[0];
		dQ=dQrel*fabs(X[0]);
		if (dQ<dQmin)dQ=dQmin;
	} else {
		band2=false;
	}
	if (a<-1e-30){        
	  if (X[3]>0) {
		//printLog("t=%g: Checking up \n",t);
 		//we try the upper value
		ddx_est=a*a*a*(q[0]+dQ)+a*a*u[0]+a*u[1]+2*u[2];
		if (ddx_est>=0) {
			dq=dQ; //OK
		} else {
			 dq=-2*u[2]/a/a/a-u[1]/a/a-u[0]/a-q[0];
			//printLog("Setting dq=%g\n",dq);
			band3=true;
		};
     		 if (fabs(dq)>dQ){dq=dQ;}
	   } else  {
		//printLog("t=%g: Checking down \n",t);
		//we try the lower value
		ddx_est=a*a*a*(q[0]-dQ)+a*a*u[0]+a*u[1]+2*u[2];
		if (ddx_est<=0) {
			dq=-dQ; //OK
		} else {
			dq=-2*u[2]/a/a/a-u[1]/a/a-u[0]/a-q[0];
			//printLog("Setting dq=%g\n",dq);
			band3=true;
		};
     		 if (fabs(dq)>dQ){dq=-dQ;}
	  };
	  //if (band4) printLog("band4=true\n");
    	  if (q[1]*X[1]<0&&!band4&&!band2&&!band3) {
  		if (q[1]<0) {
			dq=q_old-q[0]+dq_old-fabs(dq_old)*0.1;
  		} else {
			dq=q_old-q[0]+dq_old+fabs(dq_old)*0.1;
  		}
  		band4=true; //do it only once
		//  printLog("We detected a change\n");
	  } else if (band4) {
  		band4=false;
  		if (fabs(-2*u[2]/a/a/a-u[1]/a/a-u[0]/a-q[0])<3*dQ){ 
			dq=-2*u[2]/a/a/a-u[1]/a/a-u[0]/a-q[0];
			band3=true;
			//       printLog("We try provoke X[3]=0\n");

		}
	  }
 
	} else {
         	//not enough self feedback
		band4=false;
		if (X[3]>0) {dq=-dQ;} else {dq=dQ;}
	}
 	if (!band3) {
		q[1]=X[1];
		q[2]=X[2];
	} else {
		q[1]=a*(q[0]+dq)+u[0];
		q[2]=a*q[1]/2+u[1]/2;
	};

	y[0]=q[0]+dq;  
	y[1]=q[1];
	y[2]=q[2];
	band=true;
return Event(&y,0);
}
Beispiel #11
0
/**
 * Mouse motion callback
 *
 */
gint
scorearea_motion_notify (GtkWidget * widget, GdkEventButton * event)
{
  DenemoProject *gui = Denemo.project;
  if (gui == NULL || gui->movement == NULL)
    return FALSE;
  if (Denemo.scorearea == NULL)
    return FALSE;
  gint allocated_height = get_widget_height (Denemo.scorearea);
  gint line_height = allocated_height * gui->movement->system_height;
  if(dragging_outside)
    {
          gint incrx, incry;
          incrx=incry=0;
          if(((gint)((last_event_x - event->x_root)/gui->movement->zoom)) != 0)
            {
                incrx = -(last_event_x - event->x_root)/gui->movement->zoom;
                last_event_x = event->x_root;
            }
          if( ((gint)((last_event_y - event->y_root)/gui->movement->zoom)) != 0)
            {
                incry = -(last_event_y - event->y_root)/gui->movement->zoom;
                last_event_y = event->y_root;
            }
        if((dragging_outside==DRAG_DIRECTION_RIGHT) && (incrx > 1)
            || ((dragging_outside==DRAG_DIRECTION_LEFT) && (incrx < -1))
            || ((dragging_outside==DRAG_DIRECTION_UP) && (incry < 0))
            || ((dragging_outside==DRAG_DIRECTION_DOWN) && (incry > 0)))
            extend_selection(dragging_outside);
    return TRUE;
    }
  if (event->y < 0)
    event->y = 0.0;
  gint line_num = ((int) event->y) / line_height;


   if (last_directive && (GDK_SHIFT_MASK & event->state) && (GDK_CONTROL_MASK & event->state))
      {
          gint incrx, incry;
          incrx=incry=0;
          if(((gint)((last_event_x - event->x_root)/gui->movement->zoom)) != 0)
            {
                incrx = (last_event_x - event->x_root)/gui->movement->zoom;
                last_event_x = event->x_root;
            }
          if( ((gint)((last_event_y - event->y_root)/gui->movement->zoom)) != 0)
            {
                incry = (last_event_y - event->y_root)/gui->movement->zoom;
                last_event_y = event->y_root;
            }

        if(last_directive->graphic)
            {
                last_directive->gx -= incrx;
                last_directive->gy -= incry;
            }
        else
            {
                last_directive->tx -= incrx;
                last_directive->ty -= incry;
            }
        draw_score_area();

        return TRUE;
      }



  if(gui->movement->recording && dragging_audio)
    {
        if(gui->movement->recording->type == DENEMO_RECORDING_MIDI)
        {
            #if 0
            //This is moving only the NoteOn, so it could be moved later than the note off, and indeed later than a later note in the stream
            //- quite a bit more work needed to drag MIDI to correct the timing.
            smf_event_t *midievent;
            GList *marked_onset = gui->movement->marked_onset;
            if(marked_onset)
                {
                midievent = ((DenemoRecordedNote *)marked_onset->data)->event;
                gint shift =  2500*(event->x_root - last_event_x)/gui->movement->zoom;
                g_debug (" %f (%f %f)",shift/(double)gui->movement->recording->samplerate,
                    midievent->time_seconds,
                    ((DenemoRecordedNote *)marked_onset->data)->timing/(double)gui->movement->recording->samplerate) ;

                ((DenemoRecordedNote *)marked_onset->data)->timing += shift;

                midievent->time_seconds += shift/(double)gui->movement->recording->samplerate;
                }
            #endif
            g_warning("No drag for MIDI yet");
            return TRUE;
        }

        gui->movement->recording->leadin -= 500*(event->x_root - last_event_x)/gui->movement->zoom;//g_debug("%d %d => %d\n", (int)(10*last_event_x), (int)(10*event->x_root), (int)(10*last_event_x) - (int)(10*event->x_root));
        last_event_x = event->x_root;
        update_leadin_widget ( gui->movement->recording->leadin/(double)gui->movement->recording->samplerate);
        gtk_widget_queue_draw(Denemo.scorearea);
        return TRUE;
    }
  if(gui->movement->recording && dragging_tempo)
    {
        gdouble change = (event->x_root - last_event_x)/gui->movement->zoom;
        last_event_x = event->x_root;
        struct placement_info pi;
        get_placement_from_coordinates (&pi, event->x, 0, gui->lefts[line_num], gui->rights[line_num], gui->scales[line_num]);
        change /= pi.measure_number;
        update_tempo_widget ( change);
        set_tempo ();
        score_status (Denemo.project, TRUE);
        exportmidi (NULL, gui->movement);
        gtk_widget_queue_draw(Denemo.scorearea);
        return TRUE;
    }
#define DENEMO_MINIMUM_SYSTEM_HEIGHT (0.01)


  if (dragging_separator)
    {
      gui->movement->system_height = event->y / get_widget_height (Denemo.scorearea);
      if (gui->movement->system_height < DENEMO_MINIMUM_SYSTEM_HEIGHT)
        gui->movement->system_height = DENEMO_MINIMUM_SYSTEM_HEIGHT;
      if (gui->movement->system_height > 1.0)
        gui->movement->system_height = 1.0;
      scorearea_configure_event (Denemo.scorearea, NULL);
      draw_score_area();
      return TRUE;
    }

  if (line_height - ((int) event->y - 8) % line_height < 12)
    gdk_window_set_cursor (gtk_widget_get_window (Denemo.window), gdk_cursor_new (GDK_SB_V_DOUBLE_ARROW));
  else
    gdk_window_set_cursor (gtk_widget_get_window (Denemo.window), gdk_cursor_new (GDK_LEFT_PTR));       //FIXME? does this take time/hog memory

  transform_coords (&event->x, &event->y);
  //g_debug("Marked %d\n", gui->movement->markstaffnum);


  if (gui->lefts[line_num] == 0)
    return TRUE;




  if (lh_down || (selecting && gui->movement->markstaffnum))
    {
      struct placement_info pi;
      pi.the_staff = NULL;
      if (event->y < 0)
        get_placement_from_coordinates (&pi, event->x, 0, gui->lefts[line_num], gui->rights[line_num], gui->scales[line_num]);
      else
        get_placement_from_coordinates (&pi, event->x, event->y, gui->lefts[line_num], gui->rights[line_num], gui->scales[line_num]);
      if (pi.the_staff == NULL)
        return TRUE;            //could not place the cursor
      if (pi.the_measure != NULL)
        {                       /*don't place cursor in a place that is not there */
          change_staff (gui->movement, pi.staff_number, pi.the_staff);
          gui->movement->currentmeasurenum = pi.measure_number;
          gui->movement->currentmeasure = pi.the_measure;
          gui->movement->currentobject = pi.the_obj;
          gui->movement->cursor_x = pi.cursor_x;
          gui->movement->cursor_appending = (gui->movement->cursor_x == (gint) (g_list_length ((objnode *) ((DenemoMeasure*)gui->movement->currentmeasure->data)->objects)));

          set_cursor_y_from_click (gui, event->y);
          if (lh_down & !selecting)
            {
              if (gui->movement->markstaffnum)
                set_point (NULL, NULL);
              else
                set_mark (NULL, NULL);
              selecting = TRUE;
            }
          calcmarkboundaries (gui->movement);
          if (event->state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK))
            perform_command (event->state, GESTURE_MOVE, event->state & GDK_BUTTON1_MASK);

          /* redraw to show new cursor position  */
          draw_score_area();
        }
    }

  if (Denemo.project->midi_destination & MIDICONDUCT)
    {
      advance_time (0.01);
      return TRUE;
    }
  return TRUE;
}