void CheckResults(const std::string& rModelName,
                      std::vector<double>& rVoltages,
                      std::vector<double>& rTimes,
                      unsigned outputFreq,
                      double tolerance=1.0)
    {
        // Read data entries for the reference file
        ColumnDataReader data_reader("heart/test/data/cellml", rModelName, false);
        std::vector<double> valid_times = data_reader.GetValues("Time");
        std::vector<double> valid_voltages = GetVoltages(data_reader);

        TS_ASSERT_EQUALS(rTimes.size(), (valid_times.size()-1)*outputFreq+1);
        for (unsigned i=0; i<valid_times.size(); i++)
        {
            TS_ASSERT_DELTA(rTimes[i*outputFreq], valid_times[i], 1e-12);
            TS_ASSERT_DELTA(rVoltages[i*outputFreq], valid_voltages[i], tolerance);
        }
    }
Example #2
0
File: hockey.c Project: VoltBit/CP
int main()
{
    punct teren[4]; punct poarta[4]; punct M;
    double alfa,x,y;
    data_reader(teren,poarta,&x,&y,&alfa);
    M.x = x; M.y = y;
    /*
    + in functie de unghiul cu care pucul se deplaseaza, exista sansa ca el sa loveasca
      doua ziduri, algoritmul determina care dintre cele doua ziduri este lovit
    + colturile terenului sunt numerotate de la 0 la 3 iar zidurile sunt identificate
      in functie de colturi: 30 10 12 23
      (3)----------------------------(2)
      |                90              |
      |                                |
      |      180                0      |
      |                                |
      |               270              |
      (0)----------------------------(1)
    */
while(1)//programul ruleaza pana cand energia cinetica devine 0
{
    if(alfa == 0 || alfa == 90 || alfa == 180 || alfa == 270)
    {
        if(alfa == 180 && poarta[0].y <= M.y && poarta[1].y >= M.y)
        {
            printf("%3.3f %3.3f\n",x,y);
            printf("%3.3f %3.3f\n",poarta[0].x,M.y);
            printf("%i\n",-1);
            return 0;
        }else if(alfa == 0 && poarta[2].y <= M.y && poarta[3].y >= M.y)
        {
            printf("%3.3f %3.3f\n",x,y);
            printf("%3.3f %3.3f\n",poarta[2].x,M.y);
            printf("%i\n",1);
            return 0;
        }
        printf("%i\n",0);
        return 0;
    }
    printf("%3.3f %3.3f\n",M.x,M.y);
    if(alfa > 0 && alfa < 90) {//poate lovi zidul 12 sau zidul 23
            /*se intersecteaza traiectoria pucului cu zidul 12,
              apoi verifica daca aceasta se afla intradevar in interiorul terenului
              conditia implica si egalitate pentru cazul in care pucul loveste chiar
              coltul terenului, in aceasta situatie mantinela se considera ca fiind atinsa
              si este verificata si conditia pentru gol*/
        intersectie(M,alfa,teren,12,&x,&y);
        if(y <= teren[2].y)
        {
            printf("%3.3f %3.3f\n",x,y);
            goal_check(poarta,2,3,x,y);
            return 0;
        }
        /*in cazul in care pucul nu a lovit mantinela
          se intersecteaza traiectoria cu al doilea zid posibil
          si se retine locul de unde ricoseaza si se modifica unghiul alfa
         */
        intersectie(M,alfa,teren,23,&x,&y);
        if(x < teren[2].x)//pucul loveste zidul 23
        {
            M.x = x;
            M.y = y;
            alfa = 360 - alfa;
        }
    }
    else if(alfa > 90 && alfa < 180)
    {
        intersectie(M,alfa,teren,30,&x,&y);
        if(y <= teren[3].y)
        {
            printf("%3.3f %3.3f\n",x,y);
            goal_check(poarta,0,1,x,y);
            return 0;
        }
        intersectie(M,alfa,teren,23,&x,&y);
        if(x > teren[3].x)
        {
            M.x = x;
            M.y = y;
            alfa = 180 + (180 - alfa);
        }
    }
    else if(alfa > 180 && alfa < 270)
    {
        intersectie(M,alfa,teren,30,&x,&y);
        if(y >= teren[0].y)
        {
            printf("%3.3f %3.3f\n",x,y);
            goal_check(poarta,0,1,x,y);
            return 0;
        }
        intersectie(M,alfa,teren,10,&x,&y);
        if(x > teren[0].x)
        {
            M.x = x;
            M.y = y;
            alfa = 180 + (180 - alfa);
        }
    }
    else if(alfa > 270 && alfa < 360)
    {
        intersectie(M,alfa,teren,12,&x,&y);
        if(y >= teren[1].y)
        {
            printf("%3.3f %3.3f\n",x,y);
            goal_check(poarta,2,3,x,y);
            return 0;
        }
        intersectie(M,alfa,teren,10,&x,&y);
        if(x < teren[1].x)
        {
            M.x = x;
            M.y = y;
            alfa = 360 - alfa;
        }
    }
}
    return 0;
}
    /* HOW_TO_TAG Cardiac/Electro-mechanics
     * Run electro-mechanical simulations using bidomain instead of monodomain
     *
     * This test is the same as above but with bidomain instead of monodomain.
     * Extracellular conductivities are set very high so the results should be the same.
     */
    void TestWithHomogeneousEverythingCompressibleBidomain() throw(Exception)
    {
        EntirelyStimulatedTissueCellFactory cell_factory;

        TetrahedralMesh<2,2> electrics_mesh;
        electrics_mesh.ConstructRegularSlabMesh(0.01, 0.05, 0.05);

        QuadraticMesh<2> mechanics_mesh;
        mechanics_mesh.ConstructRegularSlabMesh(0.025, 0.05, 0.05);

        std::vector<unsigned> fixed_nodes;
        std::vector<c_vector<double,2> > fixed_node_locations;

        // fix the node at the origin so that the solution is well-defined (ie unique)
        fixed_nodes.push_back(0);
        fixed_node_locations.push_back(zero_vector<double>(2));

        // for the rest of the nodes, if they lie on X=0, fix x=0 but leave y free.
        for(unsigned i=1 /*not 0*/; i<mechanics_mesh.GetNumNodes(); i++)
        {
            if(fabs(mechanics_mesh.GetNode(i)->rGetLocation()[0])<1e-6)
            {
                c_vector<double,2> new_position;
                new_position(0) = 0.0;
                new_position(1) = SolidMechanicsProblemDefinition<2>::FREE;
                fixed_nodes.push_back(i);
                fixed_node_locations.push_back(new_position);
            }
        }

        ElectroMechanicsProblemDefinition<2> problem_defn(mechanics_mesh);
        problem_defn.SetContractionModel(KERCHOFFS2003,1.0);
        problem_defn.SetUseDefaultCardiacMaterialLaw(COMPRESSIBLE);
        problem_defn.SetFixedNodes(fixed_nodes, fixed_node_locations);
        problem_defn.SetMechanicsSolveTimestep(1.0);

        // the following is just for coverage - applying a zero pressure so has no effect on deformation
        std::vector<BoundaryElement<1,2>*> boundary_elems;
        boundary_elems.push_back(* (mechanics_mesh.GetBoundaryElementIteratorBegin()));
        problem_defn.SetApplyNormalPressureOnDeformedSurface(boundary_elems, 0.0);

        HeartConfig::Instance()->SetSimulationDuration(10.0);
        HeartConfig::Instance()->SetExtracellularConductivities(Create_c_vector(1500,1500,1500));
        //creates the EM problem with ELEC_PROB_DIM=2
        CardiacElectroMechanicsProblem<2,2> problem(COMPRESSIBLE,
                                                    BIDOMAIN,
                                                    &electrics_mesh,
                                                    &mechanics_mesh,
                                                    &cell_factory,
                                                    &problem_defn,
                                                    "TestCardiacEmHomogeneousEverythingCompressibleBidomain");

        problem.Solve();
        std::vector<c_vector<double,2> >& r_deformed_position = problem.rGetDeformedPosition();

        // not sure how easy is would be determine what the deformation should be
        // exactly, but it certainly should be constant squash in X direction, constant
        // stretch in Y.

        // first, check node 8 starts is the far corner
        assert(fabs(mechanics_mesh.GetNode(8)->rGetLocation()[0] - 0.05)<1e-8);
        assert(fabs(mechanics_mesh.GetNode(8)->rGetLocation()[1] - 0.05)<1e-8);

        double X_scale_factor = r_deformed_position[8](0)/0.05;
        double Y_scale_factor = r_deformed_position[8](1)/0.05;

        std::cout << "Scale_factors = " << X_scale_factor << " " << Y_scale_factor << ", product = " << X_scale_factor*Y_scale_factor<<"\n";

        for(unsigned i=0; i<mechanics_mesh.GetNumNodes(); i++)
        {
            double X = mechanics_mesh.GetNode(i)->rGetLocation()[0];
            double Y = mechanics_mesh.GetNode(i)->rGetLocation()[1];

            TS_ASSERT_DELTA( r_deformed_position[i](0), X * X_scale_factor, 1e-6);
            TS_ASSERT_DELTA( r_deformed_position[i](1), Y * Y_scale_factor, 1e-6);
        }

        //check interpolated voltages and calcium

        unsigned quad_points = problem.mpCardiacMechSolver->GetTotalNumQuadPoints();
        TS_ASSERT_EQUALS(problem.mInterpolatedVoltages.size(), quad_points);
        TS_ASSERT_EQUALS(problem.mInterpolatedCalciumConcs.size(), quad_points);

        //two hardcoded values
        TS_ASSERT_DELTA(problem.mInterpolatedVoltages[0],9.267,1e-3);
        TS_ASSERT_DELTA(problem.mInterpolatedCalciumConcs[0],0.001464,1e-6);

        //for the rest, we check that, at the end of this simulation, all quad nodes have V and Ca above a certain threshold
        for(unsigned i = 0; i < quad_points; i++)
        {
            TS_ASSERT_LESS_THAN(9.2,problem.mInterpolatedVoltages[i]);
            TS_ASSERT_LESS_THAN(0.0014,problem.mInterpolatedCalciumConcs[i]);
        }

        //check default value of whether there is a bath or not
        TS_ASSERT_EQUALS(problem.mpElectricsProblem->GetHasBath(), false);

        //test the functionality of having phi_e on the mechanics mesh (values are tested somewhere else)
        Hdf5DataReader data_reader("TestCardiacEmHomogeneousEverythingCompressibleBidomain/electrics","voltage_mechanics_mesh");
        TS_ASSERT_THROWS_NOTHING(data_reader.GetVariableOverTime("Phi_e",0u));
    }
Example #4
0
void RequestHandler::handling_thread(g_message* _request) {

	// wrap in local for auto-delete
	g_local < g_message > request(_request);

	// read parameters
	g_pid requester_pid = g_get_pid_for_tid(request()->sender);
	g_fd requesters_output = request()->parameterA;
	g_fd requesters_input = request()->parameterB;

	g_pid my_pid = g_get_pid();

	// register a name
	std::stringstream namestr;
	namestr << "windowserver:handler@";
	namestr << requester_pid;
	g_task_register_id(namestr.str().c_str());

	// clone pipe ends
	g_fs_clonefd_status clone_input_status;
	g_fd requester_out = g_clone_fd_s(requesters_input, requester_pid, my_pid, &clone_input_status);
	if (clone_input_status != G_FS_CLONEFD_SUCCESSFUL) {
		g_logger::log("unable to clone input file descriptor (%i in process %i) on open request (status: %i)", requesters_input, requester_pid,
				clone_input_status);
		return;
	}

	g_fs_clonefd_status clone_output_status;
	g_fd requester_in = g_clone_fd_s(requesters_output, requester_pid, my_pid, &clone_output_status);
	if (clone_output_status != G_FS_CLONEFD_SUCCESSFUL) {
		g_logger::log("unable to clone output file descriptor (%i in process %i) on open request (status: %i)", requesters_input, requester_pid,
				clone_output_status);
		return;
	}

	// send response
	g_message_empty (response);
	response.type = G_UI_COMMAND_OPEN_RESPONSE;
	response.topic = request()->topic;
	g_send_msg(request()->sender, &response);

	// add process
	add_process(requester_pid, requester_out, requester_in);

	// start event dispatch thread
	g_create_thread((void*) &event_dispatch_thread);

	while (true) {
		// read transaction id
		uint32_t idlen = sizeof(g_ui_transaction_id);
		uint8_t id[idlen];
		g_read(requester_in, id, idlen);
		g_ui_transaction_id transaction = *((g_ui_transaction_id*) id);

		// read length
		uint32_t lenlen = sizeof(uint32_t);
		uint8_t len[lenlen];
		g_read(requester_in, len, lenlen);
		uint32_t length = *((uint32_t*) len);

		// read data
		// TODO limit data
		uint8_t* data = new uint8_t[length];
		int32_t rd = 0;
		while (rd < length) {
			rd += g_read(requester_in, &data[rd], length - rd);
		}
		g_value_placer data_reader(data);

		// handle command
		g_ui_protocol_command_id command = data_reader.get<g_ui_protocol_command_id>();

		if (command == G_UI_PROTOCOL_CREATE_WINDOW) {
			uint32_t window_id;
			g_ui_protocol_status status = createWindow(&window_id);

			// write response
			uint32_t response_len = G_UI_PROTOCOL_HEADER_LENGTH + G_UI_PROTOCOL_CREATE_WINDOW_RESPONSE_LENGTH;
			g_local < uint8_t > response(new uint8_t[response_len]);

			g_value_placer response_writer(response());
			response_writer.put(G_UI_PROTOCOL_CREATE_WINDOW);
			response_writer.put(status);
			response_writer.put(window_id);
			send(requester_out, transaction, response(), response_len);

		} else if (command == G_UI_PROTOCOL_SET_VISIBLE) {
			uint32_t component_id = data_reader.get<uint32_t>();
			bool visible = data_reader.get<uint8_t>();

			// handle command
			g_ui_protocol_status status = setVisible(component_id, visible);

			// write response
			uint32_t response_len = G_UI_PROTOCOL_HEADER_LENGTH + G_UI_PROTOCOL_SET_VISIBLE_RESPONSE_LENGTH;
			g_local < uint8_t > response(new uint8_t[response_len]);
			g_value_placer response_writer(response());
			response_writer.put(G_UI_PROTOCOL_SET_VISIBLE);
			response_writer.put(status);
			send(requester_out, transaction, response(), response_len);

		} else if (command == G_UI_PROTOCOL_CREATE_COMPONENT) {
			uint32_t component_type = data_reader.get<uint32_t>();

			// handle command
			uint32_t component_id;
			g_ui_protocol_status status = createComponent(component_type, &component_id);

			// write response
			uint32_t response_length = G_UI_PROTOCOL_HEADER_LENGTH + G_UI_PROTOCOL_CREATE_COMPONENT_RESPONSE_LENGTH;
			g_local < uint8_t > response(new uint8_t[response_length]);
			g_value_placer response_writer(response());
			response_writer.put(G_UI_PROTOCOL_CREATE_COMPONENT);
			response_writer.put(status);
			response_writer.put(component_id);
			send(requester_out, transaction, response(), response_length);

		} else if (command == G_UI_PROTOCOL_ADD_COMPONENT) {
			uint32_t parent_id = data_reader.get<uint32_t>();
			uint32_t child_id = data_reader.get<uint32_t>();

			// handle command
			g_ui_protocol_status status = addComponent(parent_id, child_id);

			// write response
			uint32_t response_length = G_UI_PROTOCOL_HEADER_LENGTH + G_UI_PROTOCOL_ADD_COMPONENT_RESPONSE_LENGTH;
			g_local < uint8_t > response(new uint8_t[response_length]);
			g_value_placer response_writer(response());
			response_writer.put(G_UI_PROTOCOL_ADD_COMPONENT);
			response_writer.put(status);
			send(requester_out, transaction, response(), response_length);

		} else if (command == G_UI_PROTOCOL_SET_TITLE) {
			uint32_t component_id = data_reader.get<uint32_t>();
			uint32_t title_length = data_reader.get<uint32_t>();
			g_local<char> title(new char[title_length]);
			data_reader.get((uint8_t*) title(), title_length);

			// handle command
			g_ui_protocol_status status = setTitle(component_id, title());

			// write response
			uint32_t response_length = G_UI_PROTOCOL_HEADER_LENGTH + G_UI_PROTOCOL_SET_TITLE_RESPONSE_LENGTH;
			g_local < uint8_t > response(new uint8_t[response_length]);
			g_value_placer response_writer(response());
			response_writer.put(G_UI_PROTOCOL_SET_TITLE);
			response_writer.put(status);
			send(requester_out, transaction, response(), response_length);

		} else if (command == G_UI_PROTOCOL_GET_TITLE) {
			uint32_t component_id = data_reader.get<uint32_t>();

			// handle command
			std::string title;
			g_ui_protocol_status status = getTitle(component_id, title);

			int title_length = title.length() + 1;

			// write response
			uint32_t response_length = G_UI_PROTOCOL_HEADER_LENGTH + G_UI_PROTOCOL_GET_TITLE_RESPONSE_LENGTH + title_length;
			g_local < uint8_t > response(new uint8_t[response_length]);
			g_value_placer response_writer(response());
			response_writer.put(G_UI_PROTOCOL_GET_TITLE);
			response_writer.put(status);
			response_writer.put(title_length);
			response_writer.put((uint8_t*) title.c_str(), title_length);
			send(requester_out, transaction, response(), response_length);

		} else if (command == G_UI_PROTOCOL_SET_BOUNDS) {
			uint32_t component_id = data_reader.get<uint32_t>();
			int32_t x = data_reader.get<int32_t>();
			int32_t y = data_reader.get<int32_t>();
			int32_t width = data_reader.get<int32_t>();
			int32_t height = data_reader.get<int32_t>();

			// handle command
			g_ui_protocol_status status = setBounds(component_id, x, y, width, height);

			// write response
			uint32_t response_length = G_UI_PROTOCOL_HEADER_LENGTH + G_UI_PROTOCOL_SET_BOUNDS_RESPONSE_LENGTH;
			g_local < uint8_t > response(new uint8_t[response_length]);
			g_value_placer response_writer(response());
			response_writer.put(G_UI_PROTOCOL_SET_BOUNDS);
			response_writer.put(status);
			send(requester_out, transaction, response(), response_length);

		} else if (command == G_UI_PROTOCOL_SET_ACTION_LISTENER) {
			uint32_t component_id = data_reader.get<uint32_t>();

			// handle command
			uint32_t listener_id;
			g_ui_protocol_status status = setActionListener(requester_pid, component_id, &listener_id);

			// write response
			uint32_t response_length = G_UI_PROTOCOL_HEADER_LENGTH + G_UI_PROTOCOL_SET_ACTION_LISTENER;
			g_local < uint8_t > response(new uint8_t[response_length]);
			g_value_placer response_writer(response());
			response_writer.put<g_ui_protocol_command_id>(G_UI_PROTOCOL_SET_ACTION_LISTENER);
			response_writer.put<g_ui_protocol_status>(status);
			response_writer.put<uint32_t>(listener_id);
			send(requester_out, transaction, response(), response_length);
		}
	}

	// TODO close all windows
	// TODO remove listeners
	remove_process(requester_pid);
}
Example #5
0
/**
 * Waits for responses from the window manager and stores them
 * in the transaction message map.
 */
void g_ui::asynchronous_receiver_thread() {

	while (true) {

		// TODO properly check if each read/write was successful & validate the data length

		// read the id
		uint32_t idlen = sizeof(g_ui_transaction_id);
		uint8_t id[idlen];
		g_read(g_ui_channel_in, &id, idlen);
		g_ui_transaction_id transaction = *((g_ui_transaction_id*) id);

		// read the length
		uint32_t lenlen = sizeof(uint32_t);
		uint8_t len[lenlen];
		g_read(g_ui_channel_in, &len, lenlen);
		uint32_t length = *((uint32_t*) len);

		// read the data
		uint8_t* data = new uint8_t[length];
		int32_t data_read = 0;
		while (data_read < length) {
			data_read += g_read(g_ui_channel_in, &data[data_read], length - data_read);
		}

		// no transaction? -> event
		if (transaction == 0) {

			// get id from data
			g_value_placer data_reader(data);
			uint32_t listener_id = data_reader.get<uint32_t>();

			// notify listener
			if (listeners.count(listener_id) > 0) {
				g_listener* listener = listeners.at(listener_id);

				// add event to dispatch queue
				g_ui_event_dispatch_data ldata;
				ldata.listener = listener;
				ldata.data = data;
				ldata.length = length;
				event_dispatch_queue_add(ldata);
			}

		} else {
			// does map even exist?
			if (transaction_map == 0) {
				g_logger::log("transaction map did not exist when receiving request");
				break;
			}

			// check if data exists
			if (transaction_map->count(transaction) < 1) {
				g_logger::log("transaction map did not contain data for a request that was received");
				break;
			}

			// update the data
			g_ui_transaction_data* transaction_data = transaction_map->at(transaction);
			transaction_data->data = data;
			transaction_data->length = length;
			transaction_data->waiting = false;
		}
	}
}