UC Davis 8 node brick mesh generator in java - useful?

If you have a script you think might be useful to others post it
here. Hopefully we will be able to get the most useful of these incorporated in the manuals.

Moderators: silvia, selimgunay, Moderators

Post Reply
aneeman
Posts: 90
Joined: Thu Jan 12, 2006 1:13 pm
Contact:

UC Davis 8 node brick mesh generator in java - useful?

Post by aneeman » Thu Jan 29, 2009 9:03 pm

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;

/**
* Class to make tcl files: nodes, elements, and constraints in a
* rectangular volume for an OpenSees simulation.
* Makes UC Davis soil elements.
* To compile, type javac MeshBuilder.java
* To run with default size,type java MeshBuilder
* Or set your own mesh size:
* java MeshBuilder numXnodes numYnodes numZnodes distanceBetweenNodes
* e.g.
* java MeshBuilder 10 10 10 0.11
*/

/**
* @author alisa
* aneeman @ cse.ucsc.edu
* 29 Jan. 2009
*
*/
public class MeshBuilder {
double origin[]; // minX,minY,minZ
double incr; // space between 2 points, in x,y,or z
int numX,numY,numZ; // number of increments in each direction

/**
* allocate and set some defaults
*
*/
public MeshBuilder() {
origin = new double[3];
origin[0] = 0.0;
origin[1] = 0.0;
origin[2] = 0.0;
incr = 0.528680;
numX = 10;
numY = 10;
numZ = 10; // 1000 points, roughly same number of elements
}

/**
* Make an OpenSees tcl file with nodes for the given bounding box.
*
*/
public void makeNodes(){
int i,j,k;
double loc[] = new double[3];

FileWriter outputStream = null;
PrintWriter writer = null;

try {
outputStream = new FileWriter("InputSoilNodes.tcl");
writer = new PrintWriter(outputStream);

// some comments in the tcl file
writer.println("# Soil Simulation, List of nodes\n" +
"# Scripts for OpenSees generated by MeshBuilder.java\n" +
"# Written By Alisa Neeman\n" +
"# aneeman @ cse.ucsc.edu\n");

int ID = 1;
for ( k = 0; k < numZ; k++ ) {
for ( j = 0; j < numY; j++ ) {
for ( i = 0; i < numX; i++) {



loc[0] = origin[0] + (i*incr);
loc[1] = origin[1] + (j*incr);
loc[2] = origin[2] + (k*incr);
//
writer.println("node " + ID + " " + loc[0] +
" " + loc[1] + " " + loc[2]);
ID++;
}
}
}
writer.println("\n\nputs \"Finished creating soil nodes...\"");
} // end try
catch (Exception iox){
iox.printStackTrace();
}
finally {

if (writer !=null){
writer.close();
}
}

}

/**
* Create 8 node brick finite elemants using the nodes
* generated.
*
*/
public void makeElements() {
int nodeID = 1;
int elementID = 1;
int i,j,k;

FileWriter outputStream = null;
PrintWriter writer = null;

try {
outputStream = new FileWriter("InputSoilElements.tcl");
writer = new PrintWriter(outputStream);

// some comments in the tcl file
writer.println("# Soil Simulation, List of 8 node brick elements\n" +
"# Scripts for OpenSees generated by MeshBuilder.java\n" +
"# Written By Alisa Neeman\n" +
"# aneeman @ cse.ucsc.edu\n");

for ( i = 0; i < numX; i++) {
for ( j = 0; j < numY; j++ ) {
for ( k = 0; k < numZ; k++ ) {

if ( i < (numX-1) && j < (numY-1)
&& k < (numZ-1) ) {
// declare an eight node brick
// element Brick8N $ElementID
writer.print("element Brick8N " + elementID);


// 1 (maxX, maxY, maxZ)
writer.print(" " + (nodeID + 1 + numX*numY + numX ) );

// 2 (minX, maxY, maxZ)
writer.print(" " + (nodeID + numX*numY + numX) );

// 3 (minX, minY,maxZ)
writer.print(" " + (nodeID + (numX*numY)) );

// 4 (maxX,minY,maxZ)
writer.print(" " + (nodeID + 1 + numX*numY) );

//----------------------

// 5 (maxX, maxY,minZ)
writer.print(" " + (nodeID +1 + numX) );

// 6 (minX,maxY,minZ)
writer.print(" " + (nodeID + numX) );

// 7 (minX,minY,minZ)
writer.print(" " + (nodeID ) );

// 8 (maxX,minY,minZ )
writer.print(" " + (nodeID + 1) );

//$mat1 0.0 0.0 $g $rho1
writer.println(" $mat1 0.0 0.0 $g $rho1");
elementID++;

} // if create element
nodeID++;
}
}
}
writer.println("\n\nputs \"Finished creating soil elements...\"");
}
catch (Exception iox){
iox.printStackTrace();
}
finally {

if (writer !=null){
writer.close();
}
}
}

/**
* Create bounday conditions. bottom cannot move on X,Y,or Z
* Side cannot move on X or Y
*/
public void makeBC() {
int i,j,k;

FileWriter outputStream = null;
PrintWriter writer = null;

try {
outputStream = new FileWriter("Apply_SoilBC.tcl");
writer = new PrintWriter(outputStream);

// some comments in the tcl file
writer.println("# Soil Simulation, List of boundary constraints\n" +
"# Bottom cannot move on X,Y,or Z, Sides cannot move on X or Y\n"+
"# Can use for point forces but not quake (no equalDOF chaining)" +
"# Scripts for OpenSees generated by MeshBuilder.java\n" +
"# Written By Alisa Neeman\n" +
"# aneeman @ cse.ucsc.edu\n");

// fix the bottom so it won't move
for ( i = 0; i < numX; i++) {
for ( j = 0; j < numY; j++ ) {
//fix (i + j*numX + 1) 1 1 1 0 0 0
writer.println("fix " +
((int) (i + j*numX + 1)) +
" 1 1 1 0 0 0");
}
}
writer.println("\n");

for ( i = 0; i < numX; i++) {
for ( j = 0; j < numY; j++ ) {
for ( k = 1; k < numZ; k++ ) {
if ( i == 0 || i == (numX-1) ) {
// fix (i + j*numX + k*numX*numY + 1)
// 1 1 0 0 0 0
writer.println("fix " +
((int) (i + j*numX + k*numX*numY + 1))
+ " 1 1 0 0 0 0");
}

else if (j == 0 || j == (numY - 1) ) {
// fix (i + j*numX + k*numX*numY + 1)
// 1 1 0 0 0 0
writer.println("fix " +
((int) (i + j*numX + k*numX*numY + 1))
+ " 1 1 0 0 0 0");
}

}
}
}
}
catch (Exception iox){
iox.printStackTrace();
}
finally {

if (writer !=null){
writer.close();
}
}
}

/**
* Make point loads for the top of the mesh
* Pushing slightly down and more along + X
*/
public void makeLateralPointLoads() {
FileWriter outputStream = null;
PrintWriter writer = null;

int point1, point2;

try {
outputStream = new FileWriter("Apply_SoilBC.tcl");
writer = new PrintWriter(outputStream);

writer.println("# Soil Simulation, Some point loads on top surface\n" +
"# Scripts for OpenSees generated by MeshBuilder.java\n" +
"# Written By Alisa Neeman\n" +
"# aneeman @ cse.ucsc.edu\n");

writer.println("#10 cos(15) kilonewtons load");
writer.println("set downLoad [expr 9.659*$kN*0.1]\n");

writer.println("#10 cos(75) kilonewtons load ");
writer.println("set sideLoad [expr 2.588*$kN*500] \n");

// find nodes on top to apply to
point1 = (numX*numY*(numZ-2)) + (numX*(numY/2)) + (numX/3) ;

point2 = (numX*numY*(numZ-2)) + (numX*(numY/2)) + ((2*numX)/3);

writer.println("#x is the longest side");
writer.println("pattern Plain 4 \"Linear\" {");
writer.println("load " + point1 + " $sideLoad 0.0 -$downLoad 0.0 0.0 0.0");
writer.println("load " + point2 + " $sideLoad 0.0 -$downLoad 0.0 0.0 0.0");
writer.println("}\n");
writer.println("\nputs \"Finished applying vertical load to specified top nodes...\"");

}
catch (Exception iox){
iox.printStackTrace();
}
finally {
if (writer !=null){
writer.close();
}
}

}


/**
* Create the mesh!
* @param args
*/
public static void main(String[] args) {
if (args.length != 4 ) {
if (args.length != 0)
System.out.println("Usage: java MeshBuilder numXnodes numYnodes numZnodes distanceBetweenNodes");
}

MeshBuilder mb = new MeshBuilder();

if (args.length == 4 ) {
try {
mb.numX = Integer.parseInt(args[0]);
mb.numY = Integer.parseInt(args[1]);
mb.numZ = Integer.parseInt(args[2]);
mb.incr = Double.parseDouble(args[3]);
}
catch(Exception ex) {
System.out.println("Unable to parse input as numbers\n" + ex.toString());

}

}

mb.makeNodes();
mb.makeElements();
mb.makeBC();

}

}

Post Reply