/// <summary> /// This method calculates the vibration using finite element concepts and writes the results in a file. /// Each line in the file contains the result in an instant of time at an angular frequency. /// </summary> /// <param name="request"></param> /// <returns></returns> protected override async Task <FiniteElementResponse> ProcessOperation(TRequest request) { var response = new FiniteElementResponse { Data = new FiniteElementResponseData() }; response.SetSuccessCreated(); // Step 1 - Sets the numerical method to be used in analysis. base._numericalMethod = NumericalMethodFactory.CreateMethod(request.NumericalMethod); // Step 2 - Creates the input to numerical method. FiniteElementMethodInput input = await this.CreateInput(request).ConfigureAwait(false); // Step 3 - Generates the path to save the maximum values of analysis results and save the file URI. string maxValuesPath = await this.CreateMaxValuesPath(request, input).ConfigureAwait(false); ICollection <string> fileUris = new Collection <string>(); fileUris.Add(Path.GetDirectoryName(maxValuesPath)); while (input.AngularFrequency <= input.FinalAngularFrequency) { // Step 4 - Sets the value for time step and final time based on angular frequency and element mechanical properties. input.TimeStep = await this._time.CalculateTimeStep(input.AngularFrequency, request.PeriodDivision).ConfigureAwait(false); input.FinalTime = await this._time.CalculateFinalTime(input.AngularFrequency, request.PeriodCount).ConfigureAwait(false); // Step 5 - Generates the path to save the analysis results. // Each combination of damping ratio and angular frequency will have a specific path. string solutionPath = await this.CreateSolutionPath(request, input).ConfigureAwait(false); string solutionFolder = Path.GetDirectoryName(solutionPath); if (fileUris.Contains(solutionFolder) == false) { fileUris.Add(Path.GetDirectoryName(solutionPath)); } var previousResult = new FiniteElementResult { Displacement = new double[input.NumberOfTrueBoundaryConditions], Velocity = new double[input.NumberOfTrueBoundaryConditions], Acceleration = new double[input.NumberOfTrueBoundaryConditions], Force = input.OriginalForce }; var maxValuesResult = new FiniteElementResult { Displacement = new double[input.NumberOfTrueBoundaryConditions], Velocity = new double[input.NumberOfTrueBoundaryConditions], Acceleration = new double[input.NumberOfTrueBoundaryConditions], Force = new double[input.NumberOfTrueBoundaryConditions] }; try { using (StreamWriter streamWriter = new StreamWriter(solutionPath)) { // Step 6 - Calculates the initial results and writes it into a file. FiniteElementResult result = await this._numericalMethod.CalculateFiniteElementResultForInitialTime(input).ConfigureAwait(false); streamWriter.WriteResult(input.InitialTime, result.Displacement); // Step 7 - Sets the next time. double time = input.InitialTime + input.TimeStep; while (time <= input.FinalTime) { // Step 8 - Calculates the results and writes it into a file. result = await this._numericalMethod.CalculateFiniteElementResult(input, previousResult, time).ConfigureAwait(false); streamWriter.WriteResult(time, result.Displacement); previousResult = result; // Step 9 - Compares the previous results with the new calculated to catch the maximum values. await this.CompareValuesAndUpdateMaxValuesResult(result, maxValuesResult).ConfigureAwait(false); time += input.TimeStep; } } } catch (Exception ex) { response.AddError(OperationErrorCode.InternalServerError, $"Occurred error while calculating the analysis results and writing them in the file. {ex.Message}", HttpStatusCode.InternalServerError); response.SetInternalServerError(); return(response); } try { // Step 10 - Writes the maximum values of analysis result into a file. using (StreamWriter streamWriter = new StreamWriter(maxValuesPath, true)) { streamWriter.WriteResult(input.AngularFrequency, maxValuesResult.Displacement); } } catch (Exception ex) { response.AddError(OperationErrorCode.InternalServerError, $"Occurred error while writing the maximum values in the file. {ex.Message}", HttpStatusCode.InternalServerError); response.SetInternalServerError(); return(response); } input.AngularFrequency += input.AngularFrequencyStep; } // Step 11 - Calculates the structure natural frequencies and writes them into a file. //double[] naturalFrequencies = await this._naturalFrequency.CalculateByQRDecomposition(input.Mass, input.Stiffness, tolerance: 1e-3).ConfigureAwait(false); //this._file.Write("Natural Frequencies", naturalFrequencies, maxValuesPath); // Step 12 - Maps the response. response.Data.Author = request.Author; response.Data.AnalysisExplanation = "Not implemented."; response.Data.FileUris = fileUris; return(response); }
/// <summary> /// This method calculates the vibration using rigid body concepts and writes the results in a file. /// Each line in the file contains the result in an instant of time at an angular frequency and a damping ratio. /// </summary> /// <param name="request"></param> /// <returns></returns> protected override async Task <TResponse> ProcessOperation(TRequest request) { var response = new TResponse { Data = new TResponseData() }; response.SetSuccessCreated(); // Step 1 - Sets the numerical method to be used in analysis. base._numericalMethod = NumericalMethodFactory.CreateMethod(request.NumericalMethod); // Step 2 - Creates the input to numerical method. TInput input = await this.CreateInput(request).ConfigureAwait(false); // Step 3 - Creates the initial conditions for displacement and velocity. double[] previousResult = await this.BuildInitialConditions(request).ConfigureAwait(false); ICollection <string> fileUris = new Collection <string>(); foreach (double dampingRatio in request.DampingRatios) { // Step 4 - Sets the initial angular frequency. input.AngularFrequency = request.InitialAngularFrequency; // Step 5 - Sets the damping ratio to be used in analysis. input.DampingRatio = dampingRatio; // Step 6 - Generates the path to save the maximum values of analysis results and save the file URI. string maxValuesPath = await this.CreateMaxValuesPath(request, input).ConfigureAwait(false); if (fileUris.Contains(Path.GetDirectoryName(maxValuesPath)) == false) { fileUris.Add(Path.GetDirectoryName(maxValuesPath)); } while (input.AngularFrequency <= input.FinalAngularFrequency) { // Step 7 - Sets the value for time step and final time based on angular frequency and element mechanical properties. input.TimeStep = await this._time.CalculateTimeStep(input.Mass, input.Stiffness, input.AngularFrequency, request.PeriodDivision).ConfigureAwait(false); input.FinalTime = await this._time.CalculateFinalTime(input.AngularFrequency, request.PeriodCount).ConfigureAwait(false); double[] maxValues = new double[previousResult.Length]; // Step 8 - Generates the path to save the analysis results. // Each combination of damping ratio and angular frequency will have a specific path. string solutionPath = await this.CreateSolutionPath(request, input).ConfigureAwait(false); if (fileUris.Contains(Path.GetDirectoryName(solutionPath)) == false) { fileUris.Add(Path.GetDirectoryName(solutionPath)); } // Step 9 - Sets the initial conditions for time, displacement and velocity. double time = 0; double[] result = previousResult; try { // Step 10 - Calculates the results and writes it into a file. using (StreamWriter streamWriter = new StreamWriter(solutionPath)) { // Step 10.1 - Writes the initial values into a file. streamWriter.WriteResult(time, result); while (time <= input.FinalTime) { // Step 10.2 - Calculates the analysis results. result = await this.CalculateRigidBodyResult(input, time, result).ConfigureAwait(false); // Step 10.3 - Writes the analysis results into a file. streamWriter.WriteResult(time + input.TimeStep, result); time += input.TimeStep; // Step 11 - Compares the previous results with the new calculated to catch the maximum values. await this.CompareValuesAndUpdateMaxValuesResult(result, maxValues).ConfigureAwait(false); } } } catch (Exception ex) { response.AddError(OperationErrorCode.InternalServerError, $"Occurred error while calculating the analysis results and writing them in the file. {ex.Message}", HttpStatusCode.InternalServerError); response.SetInternalServerError(); return(response); } try { // Step 12 - Writes the maximum values of analysis result into a file. using (StreamWriter streamWriter = new StreamWriter(maxValuesPath, true)) { streamWriter.WriteResult(input.AngularFrequency, maxValues); } } catch (Exception ex) { response.AddError(OperationErrorCode.InternalServerError, $"Occurred error while writing the maximum values in the file. {ex.Message}", HttpStatusCode.InternalServerError); response.SetInternalServerError(); return(response); } input.AngularFrequency += input.AngularFrequencyStep; } } // Step 13 - Maps the response. response.Data.Author = request.Author; response.Data.AnalysisExplanation = "Not implemented."; response.Data.FileUris = fileUris; return(response); }