<commit_before>using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using PersistentThrust;

namespace SolarSailNavigator {

    public class PreviewSegment {

	// Fields
	
	public Orbit orbit0; // Initial orbit of segment
	public Orbit orbitf; // Final orbit of segment
	public List<Orbit> orbits; // Intermediate segment orbits
	public Vector3d[] relativePoints; // Relative points along orbit
	public LineRenderer line; // Line drawing sail trajectory
	public double UT0; // Initial time of segment
	public double UTf; // Final time of segment
	double dT; // Step size
	GameObject obj; // Game object of line
	public double m0; // Initial mass
	public double m1; // Final mass
	
	// Constructor & calculate

	public void Propagate(Navigator navigator, Orbit orbit0, double UT0, double UTf, double dT, Control control, double m0in) {

	    // Control parameters
	    var throttle = control.throttle;
	    var sailon = control.sailon;
	    var frame = control.frame;
	    
	    // Update segment initial mass
	    m0 = m0in;
	    
	    // Working orbit at each time step
	    Orbit orbit = orbit0.Clone();

	    // Number of time steps
	    int nsteps = Convert.ToInt32(Math.Ceiling((UTf - UT0) / dT));

	    // Last time step size
	    double dTlast = (UTf - UT0) % dT;

	    // Current universal time
	    double UT;

	    // Current mass
	    double m0i = m0;

	    // Reseting time step to sample orbits for saving
	    double dTchoose = 0.0;

	    // List of orbits to preview
	    orbits = new List<Orbit>();

	    // Add initial orbit
	    orbits.Add(orbit0.Clone());

	    // Iterate for nsteps
	    for (int i = 0; i < nsteps; i++) {
		
		// Last step goes to UTf
		if (i == nsteps - 1) {
		    dT = dTlast;
		    UT = UTf;
		} else {
		    UT = UT0 + i * dT;
		}
		
		// Spacecraft reference frame
		Quaternion sailFrame = frame.qfn(orbit, UT, control.angles);

		// Total deltaV vector
		Vector3d deltaVV = new Vector3d(0.0, 0.0, 0.0);

		// Accumulated mass change for all engines
		double dms = 0.0;
		
		// Iterate over engines
		foreach (var e in navigator.engines) {

		    // Only count thrust of engines that are not shut down in preview
		    if (e.getIgnitionState) {

			// Local orientation of engine
			//Quaternion Qel = e.transform.localRotation;
			
			// World orientation of engine
			//Quaternion Qew = sailFrame * Qel;
			
			// Up vector for thrust
			//Vector3d newup = Qew * new Vector3d(0.0, 1.0, 0.0);
			//Debug.Log("new up vector: " + newup.ToString());
			Vector3d up = sailFrame * new Vector3d(0.0, 1.0, 0.0);
			
			// Isp: Currently vacuum. TODO: calculate at current air pressure
			float isp = e.atmosphereCurve.Evaluate(0);
			
			// Thrust vector
			float thrust = throttle * e.maxThrust;
			
			// DeltaV vector
			double mdot = thrust / (isp * 9.81); // Mass flow rate
			double dm = mdot * dT; // Change in mass
			dms += dm;
			double deltaV = isp * 9.81 * Math.Log(m0i / (m0i - dm)); // deltaV
			deltaVV += deltaV * up; // Increment deltaV vector
		    }
		}
		
		// Iterate over sails
		if (sailon) {
		    foreach (var s in navigator.sails) {
			
			// Check if sail in sun
			double sunlightFactor = 1.0;
			if (!SolarSailPart.inSun(orbit, UT)) {
			    sunlightFactor = 0.0;
			}
			
			// Normal vector
			Vector3d n = sailFrame * new Vector3d(0.0, 1.0, 0.0);
			
			// Force on sail
			Vector3d solarForce = SolarSailPart.CalculateSolarForce(s, orbit, n, UT) * sunlightFactor;
			
			// Sail acceleration
			Vector3d solarAccel = solarForce / m0i / 1000.0;
			
			// Update deltaVV
			deltaVV += solarAccel * dT;
		    }
		}

		// Update starting mass for next time step
		m0i -= dms;<commit_msg>Use methods from PersistentThrust to calculate deltaV and mass use<commit_after>using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using PersistentThrust;

namespace SolarSailNavigator {

    public class PreviewSegment {

	// Fields
	
	public Orbit orbit0; // Initial orbit of segment
	public Orbit orbitf; // Final orbit of segment
	public List<Orbit> orbits; // Intermediate segment orbits
	public Vector3d[] relativePoints; // Relative points along orbit
	public LineRenderer line; // Line drawing sail trajectory
	public double UT0; // Initial time of segment
	public double UTf; // Final time of segment
	double dT; // Step size
	GameObject obj; // Game object of line
	public double m0; // Initial mass
	public double m1; // Final mass
	
	// Constructor & calculate

	public void Propagate(Navigator navigator, Orbit orbit0, double UT0, double UTf, double dT, Control control, double m0in) {

	    // Control parameters
	    var throttle = control.throttle;
	    var sailon = control.sailon;
	    var frame = control.frame;
	    
	    // Update segment initial mass
	    m0 = m0in;
	    
	    // Working orbit at each time step
	    Orbit orbit = orbit0.Clone();

	    // Number of time steps
	    int nsteps = Convert.ToInt32(Math.Ceiling((UTf - UT0) / dT));

	    // Last time step size
	    double dTlast = (UTf - UT0) % dT;

	    // Current universal time
	    double UT;

	    // Current mass
	    double m0i = m0;

	    // Reseting time step to sample orbits for saving
	    double dTchoose = 0.0;

	    // List of orbits to preview
	    orbits = new List<Orbit>();

	    // Add initial orbit
	    orbits.Add(orbit0.Clone());

	    // Iterate for nsteps
	    for (int i = 0; i < nsteps; i++) {
		
		// Last step goes to UTf
		if (i == nsteps - 1) {
		    dT = dTlast;
		    UT = UTf;
		} else {
		    UT = UT0 + i * dT;
		}
		
		// Spacecraft reference frame
		Quaternion sailFrame = frame.qfn(orbit, UT, control.angles);

		// Total deltaV vector
		Vector3d deltaVV = new Vector3d(0.0, 0.0, 0.0);

		// Accumulated mass change for all engines
		double dms = 0.0;
		
		// Iterate over engines
		foreach (var e in navigator.engines) {

		    // Only count thrust of engines that are not shut down in preview
		    if (e.getIgnitionState) {

			// Thrust unit vector
			Vector3d thrustUV = sailFrame * new Vector3d(0.0, 1.0, 0.0);
			
			// Isp: Currently vacuum. TODO: calculate at current air pressure
			float isp = e.atmosphereCurve.Evaluate(0);
			
			// Thrust vector
			float thrust = throttle * e.maxThrust;

			// Calculate deltaV vector
			double demandMass;
			deltaVV += e.CalculateDeltaVV(m0i, dT, thrust, isp, thrustUV, out demandMass);

			// Update mass usage
			dms += demandMass * e.densityAverage;
		    }
		}
		
		// Iterate over sails
		if (sailon) {
		    foreach (var s in navigator.sails) {
			
			// Check if sail in sun
			double sunlightFactor = 1.0;
			if (!SolarSailPart.inSun(orbit, UT)) {
			    sunlightFactor = 0.0;
			}
			
			// Normal vector
			Vector3d n = sailFrame * new Vector3d(0.0, 1.0, 0.0);
			
			// Force on sail
			Vector3d solarForce = SolarSailPart.CalculateSolarForce(s, orbit, n, UT) * sunlightFactor;
			
			// Sail acceleration
			Vector3d solarAccel = solarForce / m0i / 1000.0;
			
			// Update deltaVV
			deltaVV += solarAccel * dT;
		    }
		}

		// Update starting mass for next time step
		m0i -= dms;
{'fraction_non_alphanumeric': 0.09993002099370189, 'fraction_numerical': 0.02001399580125962, 'mean_word_length': 2.424053665548634, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3571899', 'n_tokens_mistral': 2718, 'n_tokens_neox': 2252, 'n_words': 783}