Example #1
0
 /**
  * Main program.
  *
  * @param array $args Command-line arguments.
  * @return integer Zero on success; non-zero on failure.
  */
 public static function main($args)
 {
     printf("Application program number 8.\n");
     $status = 0;
     $simulation = new Simulation();
     $simulation->run(10000);
     return $status;
 }
 public function getOverview($id)
 {
     /*
      * $id= id from simulation required
      * to get all grids (days) from one simulation 
      * returns array of arrays
      */
     //to verify with actual classes:
     $overview = new Simulation();
     $days = $overview->getDay();
     $dimension = $overview->getDimension;
     $dayNumber = 0;
     foreach ($days as $day) {
         for ($i = 0; $i < $dimension; $i++) {
             for ($j = 0; $j < $dimension; $j++) {
                 switch (get_class($day[$i][$j])) {
                     case "Plant":
                         $this->simMatrix[$i][$j] = "plant";
                         break;
                     case "Carnivore":
                         $this->simMatrix[$i][$j] = "carni";
                         break;
                     case "Herbivore":
                         $this->simMatrix[$i][$j] = "herbi";
                         break;
                     default:
                         $this->simMatrix[$i][$j] = null;
                         break;
                 }
             }
         }
         array_push($this->overviewMatrix[$dayNumber], $this->simMatrix);
         $daynumber++;
     }
     return $this->overviewMatrix;
 }
Example #3
0
 public function describeCombatants(Simulation $simulation)
 {
     $this->describeCombatant($simulation->getCombatant(0));
     echo "\n";
     $this->describeCombatant($simulation->getCombatant(1));
 }
 public function makeSimulation($caption, $patient, $organ, $model, $protocol, $parameterData, $regionData, $needles)
 {
     $numerical_model = NumericalModel::whereName($model)->first();
     $protocol = Protocol::whereName($protocol)->whereModalityId($numerical_model->Modality_Id)->first();
     $context = Context::byNameFamily($organ, 'organ');
     $combinations = $numerical_model->Combinations()->whereProtocolId($protocol->Id)->where(Context::$idField, "=", $context->Id);
     $combination = $combinations->first();
     $simulation = Simulation::create(['Combination_Id' => $combination->Combination_Id, 'Patient_Id' => $patient->Id ?: '00000000-0000-0000-0000-000000000000', 'Caption' => 'Sample Simulation for ' . $caption, 'SegmentationType' => 0, 'Progress' => '0', 'State' => 0, 'Color' => 0, 'Active' => 0]);
     /*
         foreach ($regionData as $name => $locations)
         {
      $region = Region::whereName($name)->first();
      foreach ($locations as $location)
        $simulation->regions()->attach($region, ['Location' => $location]);
         }
     */
     $simulation->save();
     $simulationNeedles = [];
     $needleData = [];
     $needleUserParameters = new Collection();
     $n = 0;
     foreach ($needles as $needleConfig) {
         $n++;
         $needle = Needle::whereManufacturer($needleConfig["Manufacturer"])->whereName($needleConfig["Name"])->first();
         $needleUserParameters[$needle->Id] = new Collection();
         $simulationNeedle = SimulationNeedle::create(['Needle_Id' => $needle->Id, 'Simulation_Id' => $simulation->Id, 'Target_Id' => $this->makePointSet($needleConfig["Parameters"]["NEEDLE_TIP_LOCATION"])->Id, 'Entry_Id' => $this->makePointSet($needleConfig["Parameters"]["NEEDLE_ENTRY_LOCATION"])->Id]);
         $simulationNeedleId = $simulationNeedle->Id;
         foreach ($needleConfig["Parameters"] as $paramName => $paramValue) {
             $parameter = Parameter::whereName($paramName)->first();
             $parameter->Value = $paramValue;
             $needleUserParameters[$needle->Id][$paramName] = $parameter;
         }
         $simulationNeedles[] = $needle;
     }
     $parameters = new Collection();
     foreach ($parameterData as $parameterName => $value) {
         $parameter = Parameter::whereName($parameterName)->first();
         $parameter->Value = $value;
         $parameters[$parameter->Name] = $parameter;
     }
     $incompatibilities = [];
     $userRequiredParameters = [];
     list($parameters, $needleParameters) = $combination->compileParameters($parameters, $simulationNeedles, $needleUserParameters, $incompatibilities, $userRequiredParameters);
     if (count($incompatibilities)) {
         var_dump($incompatibilities);
         var_dump($userRequiredParameters);
     }
     foreach ($parameters as $parameterName => $parameter) {
         $simulation->Parameters()->attach($parameter, ['ValueSet' => $parameter->Value]);
     }
     $simulation->SimulationNeedles->each(function ($simulationNeedle) use($needleParameters) {
         if (array_key_exists($simulationNeedle->Needle_Id, $needleParameters)) {
             $needleParameters[$simulationNeedle->Needle_Id]->each(function ($p) use($simulationNeedle) {
                 $simulationNeedle->Parameters()->attach($p);
             });
         }
     });
     $this->r++;
     print "Simulation #{$this->r}: " . $simulation->Combination->asString . " [ " . strtoupper($simulation->Id) . " ]\n";
 }
 public function table($id)
 {
     $simulation = Simulation::find($id);
     return ['patient' => $simulation->patient, 'context' => $simulation->contextName, 'id' => $simulation->Id, 'timestamp' => $simulation->creationDate];
 }
 protected function process_simulations($session, $connection)
 {
     if (!file_exists('/tmp/lastcheck')) {
         $this->error('Create /tmp/lastcheck');
         return;
     }
     $lastRun = \Carbon\Carbon::createFromTimestamp(filemtime('/tmp/lastcheck'), 'Europe/London');
     $this->info('Time of previous check:');
     $this->info($lastRun);
     $lastRun->setTimezone('Europe/Berlin');
     //$lastRun->subMinutes(30);
     $lastRun->subSeconds(9);
     //RMV accounts for time offset at FIT
     $this->info('In Germany: ' . $lastRun);
     touch('/tmp/lastcheck');
     $simulations = Simulation::join('ItemSet as I', 'I.Id', '=', 'Simulation.Id')->where('I.CreationDate', '>', $lastRun)->select('Simulation.*', 'I.CreationDate')->get();
     $counter = new SessionCounter($simulations->lists('Id'), $connection, function ($i) {
         return $this->info($i);
     });
     $simulations->each(function ($s) {
         $this->info($s->CreationDate . ' :: ' . $s->Caption);
     });
     if ($simulations->isEmpty()) {
         $this->info('No new simulations');
         $connection->close();
         return;
     }
     $simulations->each(function ($s) use($session, $counter) {
         $this->info('SIMULATION ' . $s->Id . ' :: ' . $s->Caption);
         $runHere = $s->State != 2 || !$s->isDevelopment();
         $dev = false;
         if ($s->isDevelopment() && $s->State != 2) {
             $this->info('--development run -- we are responsible for this--');
             $s->State = 2;
             $s->Progress = 0;
             $s->save();
             $dev = true;
         }
         if ($runHere) {
             $xml = $s->buildXml($this->httpTransferrerDownloadBase);
             $this->info('Built XML');
             $finished = function () use($s, $counter) {
                 $counter->leave($s->Id);
             };
             $session->call($this->prefix . '.init', [$s->Id])->then(function ($res) use($session, $s, $xml, $finished) {
                 $session->call($this->prefix . '.update_settings_xml', [$s->Id, $xml->saveXML()])->then(function ($res) use($session, $s, $finished) {
                     $session->call($this->prefix . '.finalize', [$s->Id, '.'])->then(function ($res) use($session, $s, $finished) {
                         $session->call($this->prefix . '.start', [$s->Id])->then(function ($res) use($session, $s, $finished) {
                             $this->info('Started ' . $s->Id);
                             $finished();
                         }, function ($err) use($s, $finished) {
                             $this->error($err);
                             $finished();
                         });
                     }, function ($err) use($s, $finished) {
                         $this->error($err);
                         $finished();
                     });
                 }, function ($err) use($s, $finished) {
                     $this->error($err);
                     $finished();
                 });
             }, function ($err) use($s, $finished) {
                 $this->error($err);
                 $finished();
             });
             $this->info('Sent init call');
         }
         if ($dev) {
             Mail::send('emails.started_sim', ['guid' => $s->Id], function ($message) {
                 $message->to('*****@*****.**', 'Phil Weir')->subject('Development simulation started');
             });
         }
     });
 }
 protected function completed_simulation($args, $session)
 {
     $s = Simulation::find($args[0]);
     if (!$s || $s->State == 0) {
         $this->info("Irrelevant completion : " . $s->Id);
         return;
     }
     $this->info('Simulation ' . $args[0] . ' completed');
     if ($s->isDevelopment()) {
         $this->info('Simulation is marked as a development run, so updating DB and pushing our result');
         $guid = strtoupper($this->gen_uuid());
         $this->info('New file: ' . $guid);
         DB::table('ItemSet')->insert(['Id' => $guid, 'CreationDate' => date('Y-m-d H:i:s'), 'IsDeleted' => 0, 'Version' => 1]);
         DB::table('ItemSet_File')->insert(['Id' => $guid, 'State' => 0, 'FileName' => 'NUMA_External_Simulation', 'Extension' => 'vtp']);
         $target = $this->httpTransferrerUploadBase . '/' . strtolower($guid) . '/name/NUMA_External_Simulation';
         $session->call($this->prefix . '.request_files', [$s->Id, ['output/lesion_surface.vtp' => $target]])->then(function ($res) use($guid, $s) {
             $this->info('Uploaded result ' . $guid . ' for simulation ' . $s->Id);
             DB::table('ItemSet_VtkFile')->insert(['Id' => $guid, 'Simulation_Id' => strtoupper($s->Id)]);
             $s->Progress = 100;
             $s->State = 3;
             $s->save();
             $this->info('Processing complete, reported and uploaded');
             //$this->upload_signal($s, 'last_message');
         }, function ($err) {
             $this->error($err);
         });
     }
     Mail::send('emails.completed_sim', ['guid' => $args[0]], function ($message) {
         $message->to('*****@*****.**', 'Phil Weir')->subject('Simulation succeeded');
     });
 }