[PYTHON] Run NASA CEA on Mac OS X

Operations required to execute NASA CEA with commands. Windows is relatively easy, but Mac is a bit annoying. I tried Mac book air, OSX yosemite For those who know what CEA is.

If you can read English, please refer to the PDF and youtube below. It's all written. http://spase.stanford.edu/CEA_files/cea_how_to_gfortran_snowleo.pdf https://www.youtube.com/watch?v=CGpxfxmqA-c

gfortran Install XCode and Command Line Tools to use GCC. Now you can compile Fortran with the command gfortran. http://qiita.com/ShinichiOkayama/items/962e55ac7f8920a5f007

CEA download and path

Go to this site at NASA Glenn Research Center. http://www.grc.nasa.gov/WWW/CEAWeb/ceaguiDownload-unix.htm Download the following file from here and move it to the folder you want to install.

I created a folder called ~ / Applications / NASAcea. スクリーンショット 2015-02-27 00.41.56.png

Now start the terminal. Go to ~ / Applications / NASAcea on the terminal and

zcat CEA+Fortran.tar.Z | tar xvf -
zcat CEAexec-mac.tar.Z | tar xvf -
zcat CEAgui-jar.tar.Z | tar xvf -

And defrost. In addition, enter the following as file permission settings.

chmod a+x b1b2b3
chmod a+x syntax
chmod a+x FCEA2
chmod a+x runCEA.sh

Now, pass the path to the folder so that it can be executed.

echo 'export PATH=$HOME/Applications/NASAcea:$PATH' >> ~/.bash_profile
bash -login

Also used by other users

To make it available to other users, do the following. Not required for personal use.

sudo vi /etc/paths

So add the following to the last line of this file:

~/Applications/NASAcea

CEA file update

Update the executable file. Enter the following at the terminal.

gfortran cea2.f
mv a.out FCEA2
gfortran b1b2b3.f
mv a.out b1b2b3
gfortran syntax.f
mv a.out syntax

After that, enter the commands in order to update the library. (Instead of copying and pasting continuously, insert line by line). [Addition] FCEA2 is in the path, and when executing in another directory, it is necessary to put it in the directory where thrmo.inp, trans.inp, cea2.inp is executed in advance. After executing the following command, it is normal if the trans.lib, thermo.lib, (cea2.lib) file contains unreadable characters in the binary file. In the case of files from, I haven't said it well.

FCEA2
trans
FCEA2
thermo
FCEA2
cea2

That's it.

sample

As a test, let's show the performance of the Kerosin / LOX rocket engine. Create a text file called sample.inp. The extension .inp is important. The contents are as follows.

sample.inp


problem    o/f=2.5,
    rocket  equilibrium  frozen  nfz=2 
  p,bar=100,
  pi/p=100,
react  
  fuel=RP-1 wt=100  t,k=298.15  
  oxid=O2(L) wt=100  t,k=90.17  
output  
    plot o/f isp aeat cf t 
end

Now go to the folder with sample.inp on the terminal and

FCEA2
sample

This second line contains the name part of the inp file. This is the result because a file called sample.out appears in the same folder.

If you don't know how to create a .inp file for CEA, write what you want to do with the online version of CEA and click on it. Finally, the .inp file will appear along with the result file, so refer to that. https://cearun.grc.nasa.gov/

reference

There is an example near the end of the paper NASA-RP1311. http://www.frad.t.u-tokyo.ac.jp/public/cea/doc/xRP-1311P2.pdf For example, rockets are in EXAMPLE 8 ~ 14. The part from problem to end after the comment # should be an .inp file.

Scripting

The results produced by CEA are old-fashioned and difficult to summarize, so it is necessary to summarize them. Try it with a python script. Some people are making CEA interfaces in Matlab https://github.com/PurdueH2Lab/MatlabCEA Rather than creating a decent interface, hitting it directly with a script would be a straightforward approach. For example, let's calculate the performance of a liquid oxygen / liquid hydrogen rocket engine. You can use it by changing the file name or .inp file part as appropriate. Put the following .py file in the same folder as FCEA2 and execute .py from the terminal. I moved to the FCEA2 folder (~ / Applications / NASAcea) with ipython

run LH2LO2.py

Is running.

LH2LO2.py


# -*- coding: utf-8 -*-
import sys
reload(sys)
#Change the default character code.
sys.setdefaultencoding('utf-8')

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from matplotlib.font_manager import FontProperties
from scipy import optimize
import csv
import os
import subprocess

# for Mac
font_path = '/Library/Fonts/Osaka.ttf'
font_prop = matplotlib.font_manager.FontProperties(fname=font_path)
matplotlib.rcParams['font.family'] = font_prop.get_name()

def cea (set_of, set_chamber):
	file_name = '00temp' #Temporarily created file
	
	# ---- .inp file creation----
	input_file = file_name + '.inp'
	str = """problem    o/f=%.1f,
	    rocket  equilibrium  frozen  nfz=2 
	  p,bar = %d
	  pi/p= %d
	react  
	  fuel=H2(L) wt=100  t,k=20.27  
	  oxid=O2(L) wt=100  t,k=90.17  
	output  
	    plot p pi/p o/f isp ivac aeat cf t 
	end
	""" % (set_of, set_chamber, set_chamber)
	 
	f = open(input_file, 'w') #Open in write mode
	f.write(str) #Write the argument string to a file
	f.close() #Close file

	# ----FCEA2 command----
	#The location of FCEA2~/Applications/NASAcea/FCEA2 is used, but rewrite as appropriate
	cmd = '~/Applications/NASAcea/FCEA2'
	p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
	p.communicate(file_name)

	# ---- .plt file read----
	output_file = file_name + '.plt'
	with open(output_file, 'r') as f:
	    reader = csv.reader(f, delimiter=' ')
	    header = next(reader)  #Skip header

	    index = 0
	    for row in reader:
	    	while row.count("") > 0:
	    		row.remove("")
	        # print row          #List display line by line
	        if index == 0:
	        	chamber = float(row[0])
	        elif index == 2:
	        	of = float(row[2])
	        	isp = float(row[3])
	        	ivac = float(row[4])
	        	aeat = float(row[5])
	        	cf = float(row[6])
	        index += 1
	    data = [chamber, of, isp, ivac, aeat, cf]

	#File deletion
	os.remove(input_file)
	os.remove(output_file)

	return data

if __name__ == '__main__': #main (starts here)
	data_file = '00output.csv'

	plt.ion()
	plt.figure()

	# ----Parameter specification----
	of_array = np.linspace(3.0, 6.0, 7)
	# chamber_array = np.linspace(5, 40, 30)
	chamber_array = np.linspace(10, 200, 30)
	data_array = np.zeros((len(chamber_array), 6))
	save_array = np.zeros([1,6]) #Stock location for data to write to CSV
	# ----Loop the command----
	#In this example, the combustion chamber pressure is on the horizontal axis, O/I want to PLOT with F on the vertical axis, so O/Loop in the order of F → pressure
	for j in of_array:
		k = 0
		for i in chamber_array:
			data = cea(j, i)
			print data
			data_array[k] = np.array(data)
			k+=1
		g = 9.80665
		press = data_array[:,0] / g #Combustion chamber pressure MPa
		of = data_array[:,1] # O/F
		Isp = data_array[:,2] / g #Maritime specific impulse sec
		Ivac = data_array[:,3] / g #Specific impulse in vacuum sec
		Cf = data_array[:,4] #Thrust coefficient Cf
		# ---- PLOT ----For example, PLOT the combustion chamber pressure and the specific impulse at sea.
		plt.plot(press, Isp, label='O/F=%.1f' % (j))
		# ----      ----
		save_array = np.vstack([save_array, data_array])

	# ----Save results in CSV----
	header = u'chamber[MPa]\tof[-]\tisp[sec]\tivac[sec]\taeat[-]\tcf[-]'
	np.savetxt(data_file, save_array, fmt='%.3f', delimiter='\t', header = header)

	# ----PLOT settings----
	plt.xlim(0,20)
	plt.xlabel('Pressure (MPa)')
	plt.ylabel('Isp (sec)')
	plt.grid()
	plt.title('LOX/LH2 100%  equilibrium  frozen  nfz=2')
	plt.legend(loc='best', fontsize=12)

I get this result. figure_1.png

Recommended Posts

Run NASA CEA on Mac OS X
Memo on Mac OS X
Install Sphinx on Mac OS X
Install mitmproxy on Mac OS X
Install pgmagick on Mac OS X 10.9
Installed aws-cli On Mac OS X Lion
Preparing to run ImageMagick + im4java on Mac OS
Shpinx (Python documentation builder) on Mac OS X
Preparing to use aws cli on Mac OS X
Building an environment for "Tello_Video" on Mac OS X
Try using E-Cell 4 on Windows 7 or Mac OS X
Run Tensorflow 2.x on Python 3.7
Build a Python development environment on Mac OS X
Using multiple versions of Python on Mac OS X (2) Usage
Using NAOqi 2.4.2 Python SDK on Mac OS X El Capitan
How to install Theano on Mac OS X with homebrew
Install Scipy on Mac OS Sierra
Using multiple versions of Python on Mac OS X (1) Multiple Ver installation
Continuation ・ Notes on preparing the Python development environment on Mac OS X
Install lp_solve on Mac OS X and call it with python.
Sphinx-autobuild (0.5.2) on Windows7, Python 3.5.1, Sphinx 1.3.5
Install Sphinx on Mac OS X
How to install Theano on Mac OS X 10.10 (using pyenv, anaconda)
Mac OS X Mavericks 10.9.5 Development environment construction
Install matplotlib on OS X El Capitan
How to erase Python 2.x on Mac.
GeoDjango + SQLite environment construction on OS X
Mac OS X Yosemite 10.10 Development environment construction
Mac OS X development environment construction memo
[Just a note] Until Keras + TensorFlow works on Mac OS X Sierra
python on mac
Put Python 2.7.x on Mac OSX 10.15.5 with pyenv
Mac OS X Mountain Lion 10.8.5 Development environment construction
Installing TensorFlow 0.11.0rc2 on OS X El Capitan (10.11.6)
Run Qiita API v2 Python wrapper in Python3 environment (Mac OS X 10.11 (El Capitan))
When import tkinter is not possible on Mac OS X 10.11.3 (El Capitan) + pyenv + Python 3.5.1.
Steps to use the AWS command line interface (Python / awscli) on Mac OS X
Build a machine learning Python environment on Mac OS
mac OS X 10.15.x pyenv Python If you can't install
Create a Python development environment on OS X Lion
pangolin x python x mac os build failed memorandum unsolved
Install pyenv on mac
Pyenv + virtualenv on Mac
Install Ansible on Mac
Run Django on PythonAnywhere
Install Python on Mac
Install Python 3 on Mac
Run mysqlclient on Lambda
numba installation on mac
I installed Caffe so that I can do deep learning on MAC OS X El Capitan
Install Python 3.4 on Mac
Install mecab on mac
Install mecab-python on Mac
Try deepdream on Mac
Note: When Python is run on ImageJ Fiji, java.lang.IllegalArgumentException appears and its solution (mac OS)
Word Count with Apache Spark and python (Mac OS X)
Introducing delocate, a wheel package utility for Mac OS X
Test Python with Miniconda on OS X and Linux with travis-ci
Environment construction procedure for the gym "Open AI Gym" that trains AI in games on Mac OS X